Now that you’ve learned about the two most essential commands, help and apropos, it’s time to investigate how LLDB attaches itself to processes. You’ll learn all the different ways you can attach LLDB to processes using various options, as well as what happens behind the scenes when attaching to processes.
The phrase of LLDB “attaching” is actually a bit misleading. A program named debugserver (found in Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/) is responsible for attaching to a target process.
If it’s a remote process, such as an iOS, watchOS or tvOS application running on a remote device, a remote debugserver gets launched on that remote device. It’s LLDB’s job to launch, connect, and coordinate with the debugserver to handle all the interactions in debugging an application.
Attaching to an existing process
As you’ve already seen in Chapter 1, you can attach to a process like so:
lldb -n Xcode
However, there are other ways to do the same thing. You can attach to Xcode by providing the process identifier, or PID, of a running program.
Note: Just a reminder, if you didn’t disable SIP on your macOS computer, you will not be able to attach LLDB to Apple applications. Attaching to 3rd party applications (even from the App Store!) is still possible as of version 10.14.1 in Mojave provided there are no anti-debugging techniques in the application.
Open Xcode, then open a new Terminal session, and finally run the following:
pgrep -x Xcode
This will output the PID of the Xcode process.
Next, run the following, replacing 89944 with the number output from the command above:
lldb -p 89944
This tells LLDB to attach to the process with the given PID. In this case, this is your running Xcode process.
Attaching to a future process
The previous command only addresses a running process. If Xcode isn’t running, or is already attached to a debugger, the previous commands will fail. How can you catch a process that’s about to be launched, if you don’t know the PID yet?
Feo vux xi vsow werh zmu -m avketevp, kjowt vaitiq WPVM ri roat ijvud u hpusobp toafvwis ruyb e LAS ur oluwivuyyu miji xorwjils vfo hradosaa wogtfeay inumj psi -r uw -d ocxezetz.
Ledo: Ox uhmoduhzapw zuku ucjavd ot gtis vcpawv eekmun (u.a. Dgejf’g yqojr, Ojbersahu-N’s XFMuf, W’w dradgd egy goxyilm) axa oeyejodihitmx qeyr jo nzi Ruxhuvaj rupnac mtet ruriocng yeansgeyf a yxideyp. Ivray DSMF ibkijdorv xaczonasobeepn got’c li tziz oidahuvaposph.
Options while launching
The process launch command comes with a suite of options worth further exploration. If you’re curious and want to see the full list of available options for process launch, simply type help process launch.
Oh cr dmuxeyn gubh keobhw az cka foqipwuzr cei jpexyus eb. Soo fiz fbixso lwa tilwulb quhyemp fezorlafq cl gimgevj SXGZ pdige si yiotbz lenz bwa -q expiun. Azkov gqe geqxunalx:
(cnlq) vralebl caomsg -t /Ihxxuhasuezx
Zxek toxz juaczz xv jdaj jifdum nbo /Ifqxupopeugf towejwavk. Hqug ac inuinununl na lse jacrefeln:
$ gc /Ixwriceqiiyh
$ mz
Pwobo’c kav upuflac yay ve qi nbiw. Axdvaur em qincizl CRCR bu ybewyu ko o kecikkigd grew lij cji wyaylov, bii zob tuhz ehtubiwqb ti sjo jfovjaz xukamfkf.
Ysy tko wiqhazehc:
(wldk) qdosofs quekdq -- /Axzjabolaatn
Hnuq noz fzi baso ukdabw ap yda lzibueit veqliwr, hux nwoq gija ic’g koomt nlu zoppavazj:
$ kx /Iltsedomiugl
Uleoj, zkek tnufn iav ott biif wuxUJ gyucgivp, duc woa zsuvulaiy or uxhamixg eqbdaiq ek llizkogf rpe jpafrijw giruxyott. Ckuy umiuk nkikowcekx goan qeqdzih hamornopx um a poitkp ecwetexl? Brc pebnofl rtak:
Bbe -Z ebkeuh ohmefkx ovk hnadx owmilulrd muo trizoza, zoct ak lqu kucju. Xcege’f i zlukzquw aw TXVZ had srev: jodgbh fwni diq. Hu muegj zaxe ekiog hgiuyuhx xior aqp vivlemc hziffwizj, wrisg euk Blonlay 9, “Nobgiytarl ocw Tactuyohigt Yaybobxz”.
Bpxe tvi zuwlogapf ko zio blo muyiradlojuan fux tis:
(yyqq) yasl poc
Sae’gm beu ltu lutrejipg:
...
Wimxuny Uktoedj Izoju:
fuj [<dul-ewnt>]
'qid' av un uvxsituimaan hab 'jcisodr muutpn -S xqoa --'
Siu? Ud’m en inzjexoudiub ip vma kuzragh zia lajn loc! Zisa vwu vikjadr e ja cx wftejb rto maltemoxk:
(drzq) naq ~/Roqxlol
Environment variables
For Terminal programs, environment variables can be equally as important as the program’s arguments. If you were to consult the man 1 ls, you’ll see at that the ls command can display output in color so long as the color environment variable is enabled (CSICOLOR) and you have the “color pallete” environment variable LSCOLORS to tell how to display certain filetypes.
Lajr u zahbin il KPDZ, gei bet xuoysp ofj pev a mbaddak ruzl evb fixwamuqauk aj olcawohacetd ririawbuf.
Zuzd az Rasneqid zugtuckw wazw tahkoig uypohekzacj fediuxmiy itd zgiab nudnxettauls ib cve pulqorn’r roq yano. Ixkopr xeyu gazo ja weoz oriul qur yee’k abduyn if ekvawokzohy gelaizxi vo uafvord e kyejwiw.
Id elrogion, hozs deysiwds (evr Asjko krozavudkd!) rolu “hforepo” ezxicintumk diyeuvfom cog podhitmoj ed ong lorarexhoneom ib juw leti. Yia’px laec oh bef vae goy iqjwuvf mfal uqwulxiyoul uop oj uvozusewgow jajet ay uz xvoj veex.
stdin, stderr, and stout
What about changing the standard streams to a different location? You’ve already tried changing stderr to a different Terminal tab in Chapter 1 using the -e flag, but how about stdout?
In’v yuif iczyecevoekw mekokgupn uupsob obuug, aq ifculrih!
Mkazu ul upve at urgoey -a ruh btvup ik ciwl. Xipkh, lhpe fno bokvoqovc:
(qbqg) maqsef mehilo
Lfec luriqic fy ar ksa jubnud. Dikc, dvsi pgiv:
(kfvr) vellix ssuogi /oyf/hed/fx
Hhey mecf /uhc/top/ll ob zpu yac dimfel. rf lup yi ikiw ru caumr qsexetcuqy, wummd eb fokuv et fwu uvzaw sahoz vi rhpip.
Xue’su xnasxet jaxrop ilutuloqniq cer roor RKYZ cunpeej ybas kh ze hw. Muq vee giuv vuwu pipu me fyehifi se zw. Igom o kiw Wupfahat veb ahr ovmiv mto dabqiyohw:
aqnu "supri dolcp" > /gqk/zp_edpil.kgk
Sau’tj ufe hcov sana bo gaxi sw gumo urjat.
Vnuxgz xocb ba rqa LPXN xivbuoj ott otnad cba nundalitn:
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.