<span id="Label3"></p>Now You've learned two basic commands apropos and help. it's time to look at how LLDB attaches itself to the process. you will learn all the different ways to attach lldb to a process with different options and what happens when you attach to a process.<br>Additional attaching this word is somewhat misleading. a program called Debugserver (can be found in xcode.app/contents/sharedframeworks/lldb.framework/resources/), Responsible for attaching to the target process.<br>If you are attaching a remote process, such as an application running on an ios, watchos, or tvOS device, a remote debugserver will start on that remote Device. start, It is lldb to connect and use Debugserver positioning to handle all the interactions in the process of debugging an Application.<p><p></p></p><p><p>Attach to a process that already exists</p></p><p><p>As you see in the first chapter, you can attach to a process with the following command:</p></p><p><p>Lldb-n Xcode<br>however, we have another way to do it! You can attach a lldb to a running process by using the process identifier or PID.<br>Open xcode, and then open a new terminal window, and then run the following command:</p></p><p><p>Pgrep-x Xcode<br>This command outputs the PID of the Xcode process.<br>then, run the following command, and replace the 89944 with the PID obtained:</p></p><p><p>Lldb-p 89944<br>This instruction tells Lldb to use the specified PID to attach to the process. here is the process of Xcode.</p></p><p><p>Attach to a future process</p></p><p><p>The above command can only be attached to a running process. if Xcode is not running, the above command will fail. how do I capture a process that is about to start without knowing the process pid?<br>You can do this with the-w parameter, which causes the LLDB to enter the wait state until a specific PID process starts or the Executable's name is consistent with The-w Parameter.<br>For example, in the terminal, press CTRL + D to kill the LLDB process that already exists, and then enter the following command:</p></p><p><p>Lldb-n finder-w<br>This command will allow the lldb to attach to a process that is called the Finder the next time it is Launched. Then open a new terminal window and type in the Command:</p></p><p><p>Pkill Finder<br>This command kills the Finder process and forces the Finder process to Restart.<br>MacOS Automatically restarts the finder when the Finder is killed. switch to the terminal window you created for the first time, and you'll notice that LLDB has been attached to the recently created finder process.<br>Another way to attach to a specified process is to specify the path to the executable file and manually start the process at your convenience.</p></p><p><p>Lldb-f/system/library/coreservices/finder.app/contents/macos/finder<br>This will set the Finder to the executable file Startup. once you're ready to start debugging, simply enter the following command in the Lldb session:</p></p><p><p>(lldb) Process Launch<br>Note: An interesting phenomenon is that when you manually start a process, stderr is automatically sent to the terminal window. This is not done automatically when you attach lldb in other ways.<br>Options at startup</p></p><p><p>Process Start commands There are some options that deserve further exploration. if you're curious and want to know the complete list of options available when you start the process, simply type help process Launch.<br>Close the previous Lldb session, open a new terminal window and enter the following command:</p></p><p><p>Lldb-f/bin/ls<br>This command tells Lldb to Use/bin/ls (file list Command) as an executable file.<br>You will see the following output:</p></p><p><p>(lldb) Target Create "/bin/ls"<br>Current executable set to '/bin/ls ' (x86_64).<br>Given that LS is a very fast program (it will start, do its job and then exit), you will run this program multiple times with different parameters to see what has been done each time.<br>First try to start LS without parameters. Enter the following command:</p></p><p><p>(lldb) Process Launch<br>You will see the following output:</p></p><p><p>Process 7681 launched: '/bin/ls ' (x86_64)<br>... # omitted directory listing output<br>Process 7681 exited with status = 0 (0x00000000)<br>An LS process will be launched in your current working directory. You can also use the-w option to tell Lldb to change the current working directory.<br>Enter the following command:</p></p><p><p>(lldb) Process Launch-w/applications<br>This command will start the LS program in The/applications directory.<br>This command is equivalent to this one:</p></p><p><p>$ cd/applications<br>$ ls<br>We have another way to do the same thing. instead, you can tell lldb the directory of the itinerary, and you can pass a value to the program directory Parameter.<br>Enter the following command:</p></p><p><p>(lldb) Process Launch--/applications<br>This command has the same effect as the previous command, but this time he did the following things:</p></p><p><p>$ ls/applications<br>This time, you will list all of your MacOS applications, but you have specified a parameter that changes the LS startup directory. How do you use your desktop directory as a startup parameter? type the following command:</p></p><p><p>(lldb) Process Launch--~/desktop<br>You will see the following output:</p></p><p><p>Process 8103 launched: '/bin/ls ' (x86_64)<br>Ls: ~/desktop:no such file or directory<br>Process 8103 exited with status = 1 (0x00000001)<br>uh, It doesn't work? you need to expand the shell Parameters. try again with the following command:</p></p><p><p>(lldb) Process Launch-x True--~/desktop<br>The-x parameter expands any shell parameters you provide, such as ~.<br>Here's a shortcut to the Lldb command: Run. to learn more about the shortcut keys for creating your own commands, refer to Chapter eighth, "persisting and Customizing Commands".<br>Enter the following command to view the documentation for the run command:</p></p><p><p>(lldb) Help Run<br>You will see the following output:</p></p><p><p>...<br>Command Options Usage:<br>Run [<run-args>]<br>' Run ' is a abbreviation for ' process launch-x true--'<br>This is the abbreviation for the command you just ran. just type the following command to Run:</p></p><p><p>(lldb) Run ~/desktop<br>How do I change the position of the output? in the first chapter you have tried to output stderr to a different terminal window using THE-E parameter, But how do you change the position of stdout?<br>Try typing the following command:</p></p><p><p>(lldb) Process Launch-o/tmp/ls_output.txt--/applications<br>The-o option tells Lldb to output stdout to the specified file.<br>You will see the following output:</p></p><p><p>Process 15194 launched: '/bin/ls ' (x86_64)<br>Process 15194 exited with status = 0 (0x00000000)<br>Note that there is no direct output from Ls.<br>Open the following command and run the following command:</p></p><p><p>Cat/tmp/ls_output.txt<br>As expected, this is the next directory of your application output!<br>There is also a-i option for Stdin. first, type the following options:</p></p><p><p>(lldb) Target Delete<br>This command removes the LS as the Target. next, enter the following command:</p></p><p><p>(lldb) Target CREATE/USR/BIN/WC<br>This command TAKES/USR/BIN/WC as a new target. the WC can be used to count the characters, words, or lines in the stdin Input.<br>You have switched the LLDB target executable file from LS to wc. now you need to give the WC some parameters. open a new terminal window and enter the following command:</p></p><p><p>echo "hello world" >/tmp/wc_input.txt<br>You will see this file providing some input to the WC.<br>Switch to the Lldb session, and enter the following command:</p></p><p><p>(lldb) Process Launch-i/tmp/wc_input.txt<br>You will see the following output:</p></p><p><p>Process 24511 launched: '/USR/BIN/WC ' (x86_64)<br>1 2 12<br>Process 24511 exited with status = 0 (0x00000000)<br>This is equivalent to the following command:</p></p><p><p>$ WC </tmp/wc_input.txt<br>Sometimes you don't want to use stdin (standard input). This is useful for GUI programs like xcode, but there is no real help for terminal commands like LS and wc.<br>For example, If you run the WC program without any parameters, type the following:</p></p><p><p>(lldb) Run<br>This app will just hang there because it wants to read some input from Stdin.<br>Type Hello world to give it some input, at the end of the character, press return, and then press CONTROL + D.WC to parse the input and exit. you will see the same output as when you used the file as input in front of YOU.<br>now, start the process with the following command:</p></p><p><p>(lldb) Process Launch-n<br>You will see the WC exit immediately and see the following output:</p></p><p><p>Process 28849 launched: '/USR/BIN/WC ' (x86_64)<br>Process 28849 exited with status = 0 (0x00000000)<br>The-n option tells Lldb not to create a stdin. once the WC has no data to process and exits immediately.<br>Why do we have to learn these things?<br>Some of the more interesting options are available, and you'll see them in later Chapters. in later chapters, you will learn how lldb attaches itself to the remote debugsever of an iOS device.<br>Trying to attach to a Non-gui program from now on is like attaching to a GUI Program. try running your desired stdin or parameters in the terminal and see what you found?</p></p><p><p>Advanced+apple+debugging (3)</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service