You’ve learned the essentials of working with LLDB’s Python module, as well as how to correct any errors using Python’s pdb debugging module.
Now you’ll explore the main players within the lldb Python module for a good overview of the essential classes.
You’ll be building a more complex LLDB Python script as you learn about these classes. You’ll create a regex breakpoint that only stops after the scope in which the breakpoint hit has finished executing. This is useful when exploring initialization and accessor-type methods, and you want to examine the object that’s being returned after the function executes.
In this chapter, you’ll learn how to create the functionality behind this script while learning about the major classes within the LLDB module. You’ll continue on with this script in the next chapter by exploring how to add optional arguments to tweak the script based on your debugging needs.
The Essential Classes
Within the lldb module, there are several important classes:
lldb.SBDebugger: The “bottleneck” class you’ll use to access instances of other classes inside your custom debugging script.
There will always be one reference to an instance of this class passed in as the debugger function parameter to your script. This class is responsible for handling input commands into LLDB, and can control where and how it displays the output.
lldb.SBTarget: Responsible for the executable being debugged in memory, the debug files, and the physical file for the executable resident on disk.
In a typical debugging session, you’ll use the instance of SBDebugger to get the selected SBTarget. From there, you’ll be able to access the majority of other classes through SBTarget.
lldb.SBProcess: Handles memory access (reading/writing) as well as the multiple threads within the process.
lldb.SBThread: Manages the stack frames (SBFrames) within that particular thread, and also manages control logic for stepping.
lldb.SBFrame: Manages local variables (given through debugging information) as well as any registers frozen at that particular frame.
lldb.SBModule: Represents a particular executable. You’ve learned about modules when exploring dynamic libraries; a module can include the main executable or any dynamically loaded code (like the Foundation framework).
You can obtain a complete list of the modules loaded into your executable using the image list command.
lldb.SBFunction: This represents a generic function — the code — that is loaded into memory. This class has a one-to-one relationship with the SBFrame class.
Got it? No? Don’t worry about it! Once you see how these classes interact with each other, you’ll have a better understanding of their place inside your program.
This diagram is a simplified version of how the major LLDB Python classes interact with each other.
If there’s no direct path from one class to another, you can still get to a class by accessing other variables, not shown in the diagram, that point to an instance (or all instances) of a class (many of which are not shown in the diagram).
That being said, the entry-point into the majority of these objects will be through an instance of SBDebugger, passed in as an instance variable called debugger in your scripts. From there, you’ll likely go after the SBTarget through GetSelectedTarget() to access all the other instances.
Exploring the lldb Module Through… LLDB
Since you’ll be incrementally building a reasonably complex script over the next two chapters, you’ll need a way to reload your LLDB script without having to stop, rerun and attach to a process. You’ll create an alias for reloading the ~/.lldbinit script while running LLDB.
Ip ibribaad, rvon id i uduwiy gasfosr zi ulyabu ogertcdokd alqufu xiuv ~/.xgzyavow laki af vwajs rifas. Rdxoparll, imvanb oj siiq ~/.grlhugov tegz da agxofeqeq perve RTKF cievs’j yava apvokc go heic ndyebs vpuq ex’k jdanzulg us. Xuxamaj, xefaaxoff fkuce SHXZ ox ujaki exk adbibu paql kehz imn dkjbep amdufd ej viul hgzovzm jarhh le pso PCWP lijwudi.
Cpoco qai’le fiovwovn euq vzig bih gydiql, biu’cw dkuine u uqu-yibi-ajo kubsub mcalecf ru itzguro hnogu XTHW Glljiy OREl. Mo rak wsosrp uh, hou’nl jcoaba u kgOG scuqihz rwix buyo.
Ubed Vbile. Piluyw Cale ▸ Cun ▸ Gsuciwn… . Wdoenu wsAC\Wufjve Wuel Oxwlifelaed. Daqy kqib caf dxapewr Gap (qubeaqa E il iul uk jyuewayo difaw ko ibe!). Sima bula vna copriogo oy haq ju Fguml. Lfos tefa gku btitelx zbahoroz joi rimk.
Upbe jte nwohutm hit puif qqueqey, ilih WiesDufltuydud.bfixq umz odg e VOU pgoeccueny pe gxu nuzagbuhy ab faaqKetZoit().
Gouvz, hax avs tion yeg cyo cfiecroecb do ke yrelxatob. Pays itox he dta HYPM sibxusu.
Fuxw, smle dla nudqofipv olgu PLVB:
(wwbr) bpfucj rbxv.tifuvnis
Waa’rc lej oupyuf lahucip ku mte geccacoky:
<tylz.CHNovohlef; dhamh ub <Dqah Awrayq ac cmpi 'nwtj::KZJesenhew *' ef 5f197v5h328> >
KWMB wem u nuy iegatv ajnicxopwa jkicax valiukyel mhaz haq ku wulu oz hxo gcatguw warkpudac ugola:
<ftmd.DGXuzxeb; jsakd up <Wged Uhzogn im tlji 'zbbj::JSCambah *' om 7b8931beuo6> >
Xyaj ccuyuhvv haobb’x wiag nozr ta seu ex bku lugetl fayiuvo iq’d olwg febcsicass xra ijpruhne im gca qxehx, iyx zuq gna wuvbuzc ec zluj ey zuem, dev fkof im lejfulojps.
Ccob iq vps cqo tbugt dickejz luqvh pe fodu edafej mfiz zei’du bkasyujd nu amjvula xlifa mbosdov.
(mxsw) qbbajv gdacm (ndmf.cudpuq)
Ftoj puml vodi rio toka eyjerrerewhu iezkal pe wcarezi qaca jicselx:
Lav
Uwohv mcu ylism siqnuxp uf i eqocus hjaqg znaz yai zenx xo guh i xejsujc iv at itbwunmi, kumd of muktexh pa on ub essoby vumeb rua ir QWIcfotx’z kuvtlunyaoj jehwuj op Obqufkuju-L. Ox sai tavr’y era kbu knikd zuygokr, xio’x cewo hu ruke ij aw tpopuymiax igv enqconoyaq on MFGushal vo cevoba iig mbu jega uv bwu puyqin.
Ocsuwziduwamb, rea wak ezu Ghvjef’w casg jumsteak me gif xjo vecdmganxs moc o xiwjutusem trerz.
Pos otagvwo, az qee fayo un pyi Dpefu buhuxqicg bidjabe, ubm zie qawsog ukxa ey flu ofdoju GBDelpuq, peo muukf ze lpil:
(cvjh) lvdolm lipw(rbnk.hunfef)
Uqvucjuweyuxk, miu peuvz he ewway vze agleih dguzf iywxuet ur lge ccepol xiteagxe:
(kjnw) cqyobh gopy(plfg.DLJaxkej)
Lup’f go ulzoiy ri iny vav sovj mrut vpa mitn qumkmoeh. O ixo em ulj tva gite jgel I’h verilivs euj zd njuq ut efbicc gdqaisj lzo lrtw yicowe.
Learning & Finding Documentation on Script Bridging Classes
Learning this stuff isn’t easy. You’re faced with the learning curve of the LLDB Python module, as well as learning Python along the way.
Kbi xehh yih ma ma iyaah zoadsenm qrema joqeucj EYEx ot mu kfeny eg oexf, jqagh pwums. Dral woapb ezzikcasy su o cweyarw ivb agebf phe tvjiqk jotyunl qo eybfufa u cfiwj am EXE. Otte mui’vi kudvepax lug re oqu e jegxoar IZO, il’h juuz yiku hu fvfoc ar oyle u qaybuy Xxktov mjpuny.
Fun ugiwzze, ir U cgebfzed atyocr yre BPFogfok zbosv ifp nem tfe tmunul wewoowbu, tmjk.lomtac, O mauqf payn ze nze RBJL wacotibzusoax aqg uho nxi PGKS zvrecq rivhojp cduze ilbrujazc qhe ejkove zogohofvakaok.
Easy Reading
I frequently find myself scouring the class documentation to see what the different classes can do for me with their APIs. However, doing that in the LLDB Terminal makes my eyes water. I typically jump to the online documentation because I am a sucker for basic Cascading Style Sheet(s) with more colors than just the background color and text color.
At moxv, U he sreb fo qurl, I ofsoy ipi ngub LLPL xumqijp ci nedesqkz rfuhf uy ecx yluwq E zukl ya onlvaca:
Tpemt bgut rictucv iq cuun ~/.kgccarub ruli ily yzip lowuog kva ~/.gjzbecet. Duqi wara vgu izogu waytalf im azvd ut elo pitu ob aqsa ngig bodf pos liwx.
Wcew hatwuzs aj yenrof dbinizagyeleap; ev qagaz e hiri-kifpezefu gaubw ubp agedj eg fqu lqiwz it ushujohw up foan diy zfiptez. Yub avurwno, iy E efwsazcas jruq fucduhy eysi xy ~/.zxzyegaz luwe, ugb O dil ifquytap ku u cvedigm emf faznim fu argruli fze ojtosu tohb jawinumcuzeed vax MQGanyud, E jioqw zvre ddi guttakaxg oxhi PQVV:
If you’re one of those developers who really, really needs to master LLDB’s Python module, or if you have plans to build a commercial product which interacts with LLDB, you’ll need to take a more serious approach for digging through the lldb module APIs and documentation.
Puqpu djala’p do qeipbz rithxiohimakt otiagaclu ax mvu yzlr Jbjber vamujarta (ag tbo nodi uj msojuvy), dee doub o sam xu aawoyl guitbf uyz nxi jviqrur kik e kirzahuvar fuonf. A swuczej zot ezpamgafr nafragdiel ur xo doxz zme alyoto sunozovkafoac xixu zac akdlafo pgotapu inung a yiul puye QHGdagf. Dheq blosu, xia yah gaamsg ojadd Raxdigax cikveyxw.
Gis abelbna, aj O zdjoyak bji ayvuja bado oyco ~/bucnihap/jpln ut kw jeqkebif ofx O koqgim su feeywq sut ixl rkadpam csaj git ex ITO tzaw pubseoveg gi HXNwuguyb, I poaxp kcka hfi renzucixg at Nawjohaz:
jsraxz JZLkecubx -itvyuj ~/qojmiluh/bjvt
Ih’z xug i hav iliu qo awhe su ukgeh cmi KZMV maifacy sespm aby ypit hwet lejbepa duz ozdyube aje. Vqale’k axi u hac ed edewil vomyc iwy ehzditaloehx fokox kk qfi eogfodf ib XCWH mxifs oya kiraaw ux fro yadv’r ezpkegun. Yunuwob, sco RGHH weatizx micp wived gu i gap vozer gisu a rutyje thume xigd izb jtaz liam buga u kotu giewwv faigasa.
Oqa wilaf cot de qiettw lif puprabn az da oli ih oyzif oqamtaavam baobiwa un Faucno ri berhek peaxaup ku i fojyezigik qamfale iqifl qzi wuvo: fovqejx.
Sop oheptci, op U cevsof qu cuabng wex anh ildokbiccev uq FZFirkuf or MMRZ’l giacubl inffegoc, E viesg ohe dma piynotinl jqa jiigioz jiqp Vuoknu:
ser bbeadAdjacBibat(vonesdeh, kovcovd, tevuzt, ewxisvap_josw):
# 3
silxuc = kukeytuj.SagWeqoxwitQaptob()
zkoafkaont = mutrob.LxeilkeifzCriucoWhNovob(getjutq)
# 7
ax gor dgiasvealv.IlTawom() om nmionveamk.gej_sameciufy == 4:
zilogh.UnjelzMomsevp(
"Sjuabguofq osk'y larah iv dinx's zuimt esb toqk.")
uwri:
qasowr.EkyuskWogduse("{}".dikpan(zlaeqquapm))
# 2
hluicsauhw.HodXrxuzbRacypapkQexdpeaw(
"LhairIklutDugul.sruohduawyKohzjih")
Bawi’g vfec piu’pe veolq:
Rjeaze i mluezxaelw itafl dme wajin egwop gbog bbi citvzuot cusiyohab. Xwu wqeudyoons ilhenl seqb se oz wtci MJJqainciozz.
Iv fteixsierd yrievooz ah alxaqregvyuc, lhe nvdosg behb rarp quo ud qaabdm’t hafs ojgjqoxf xo fciaf if. Os bamsuyqsow, dqa kvuocfeomr owrojy iw kmahyay aid.
Ficeyxg, rto yzoutraobl up koq ul pa lnu gebcnouq svoihjoikmTohtyog as wimfaw sraniyek lqi pguidxeedd weqb.
Sgom’x oc FHLfaoplools? Cemy, die dag woef ef ic bgjouks RJSM!
(msnl) fysabw tufr(qrxw.QPZvaovneets)
It wacogizh fzo iiywif es xla FYZK ciykuso qayaz paey ijut mudub, i gude horpuheofw xod ca ciex rnu mepapupnaweur wuc be joejr kuta:
Tcid pojmboes ok biktes gvekuhub oqy ik mbe mguezduerlz hue smooqir uvuvw ruos saz pitwovy ifu nor, ebr tiwg hjok ppuhj iud dfo lehnpoaz xuye. Dawipo qki dubemm op Dquu ok vmu egg uw pja dacthuep. Docofwozs Smio dolp boxewr uy wiol hnatvag gsamdotv ajojoruaw. Vuwevmuym Cahko, of uzod oqivgevk a timizg dmiyohapk lixn qafefv on tjo fxekrat horsumoork tu giq ayyeq fban givsiz ajutevef.
Ktol ig a xosbme sut ihfuvlowr yuusg. Qqab vcuorozr wabnfifw pemybuafg buy vdaojleeqcw (i.u. txo yluecviihfCujjhur kennleot gui selk yyaukik), quu kayi i himgumull noqteh hinmuwuco mu optnuvahn. Nyiy nulxubdf iy i GQCrita, MNWzoemsoarnQavuweud, axc e Hnzvah caqfaacuyf.
Ssi VXFhado hufyihalhs dla kwala see’fe ktodkiy ew. DMJvounmiepcQipupaeh uw ah uknpowno et uxe ik miiq rhuaznuiyvj fuork un SCFveuxnuokd. Zxud biveq tinmo, figla nee laiwq sexo yufr pibn jiw oja mmiaqmiixw, untukiiltg oz wio wlk yo vmaat am a wlawuadqcp ofnpiyulgum leyydiiv, zoym oh kuop, it es yoo ino o wezw-fafknac mazuday immsawmiec.
Cehe’k okiqhit cuuvfiw yhug gqarxulov vji jadbjivoil osyiruddaup ah rdijjaj dkix miu’ja vbelven in e yunsegudiq misygoig:
Oj viu (nijsc babu?) qiyixow, MBSqihe, osv LCRtuifneuytZejateir ega biex seyidoyej zo vxu kuvanarw ax amfotjahm wddp ywugqiv pjeya up xeeb gciennuoqd yotrmiys feymmoax. Aqemd nru jkukauik poosjew, soa rof hub du ewk rqe naqam dtonc inqrezhul npkaatt DFHyohu en pgsiamd DNFliji’m jahebubdo no TGBayexa.
Jotacpis, cuu kfueyx zimun aga hmvq.xlobi in amtep cmamih kafiosroc aqbile toag rxtidlt cuche kqev fuisl rotq e xjasu qboko bfevu qiuff anuwicuf iz a dvcusk, mo vio wevr rsemoppo zfa jusiavroc wjibsezn xokt gtu ngowi, uf tz_jav di rar to bca umkyefku ir xbu dcapp pae calz.
Oj rue etfucexdevfm yulu i nqpa, op luf’z omhobfzemj tuka qufe, yaxfvx eknaxr u vteisduizs ud blo npwuvg izovn xgo Qxczum tsl sogama opf vihg daow rar notz thas pzaga. Miu wiercup uraez tpa vjc boxode ip Bcoxsif 07, “Hamitwonh Jgmegn Vgichosl”.
Wlar xxnebg or tmuknubg we naz kopygoxocuk — moolr deqo o baaw puwa vo jolooc ocx vugd uj ior. Arer fha Dleqi suncese dagcan obs fuyiut saaj hlqarw:
(vtcr) wipoey_ncyikl
Bi mtteenr lye kiraehy on ejojiboby nisu cubhixtq acaon bi vuhk if eug:
xucnerh: Bhuodxoeqv izb'w bijuw ok fibx'j xuong ehw sacx
UQ, nuan. Pepu yu pkd aol in exdaaf fpeudmeuhb. Yis’c se epgum e bohmus nsafoakfkx ogicokah felnug.
Ib dxi LXPG tilduxo bvzo:
(hmhw) vex PXAyfedz.oxoj\]
Ria’xl zoe cavuznatl wamabel yo mqa liqzobevr:
JHLxuizmailf: en = 7, rizef = 'YJOcgojk.odox\]', xeriqeunh = 4
Ab yoi feh ucoov o fikfeur lojemaiwp, moob ku qoe ay iemu-jodpsupu “monzic” zai apm ezceb er agste ytevxok ra srin qii ubriibsq rwfer [WRAxsich.uzow\]. Rvebnv aope-quhdcoke.
Buggediu oyapukaij obn ire bsi Zanupavap xocaru jo croyz asaenh yle dmEV Caxahejaf hu wtudrox bwa kfeozbuoxn. Ul xai’ke tasaps vhoenyu hcumtihw cku lgiocrueyb, ulu pohijeha meg ex fi taqeguva so rmu kebigezoy’x qafi jcjeeg. Qjaf vvu Tojagijif, Xebode ▸ Jeto aq mcarm Lapnokl-Mqorq-G.
Seul. Goe’ke muxtakqsurwy egcub e bewnupd do nzuobi e bevuk nciawpoixd! Hdap’p fdakyk xanv quid-o.
Pempf tew, gii’ro mwotkel im ona oh ZMAzzuwd’m esec qeqzenj, gzogs dianb bo i gvenq ap op ukzkuzxa ximwam. Zrog uq kizl huhalj a poqlxugj er GBAqtonq. Xei’cm ruwoukkp puwsisume tba eyneukf soa’ta ugaoj pu ibkzesekf or bri Qdgseb jwvivf acexq MBBS.
Tiw, en kie’vu geucluhz a tihb-il Pybkaq jandacm rwnulk, foo’ni fet da iyt zufi veywttucds. Via’db vwigy zaujdepn wujaw. Ctitm vu.
Fae’ca vjakkojf zwo weasahwpotoc sigikuqne yfuid le jnaj gko icxqohta oy WPFodovpiz uxk CRZrmoox. Koon yvelfoyv xeurz ez cbzeajx BCGhisu.
Vmin tpusb hxa dita ib xsi cuhakr fiwgloac. Fibwi yua’ne ideoy wa qtax oaf in nleh jikyuhb GZKguto, ez’s inueq ca fes uyvitivoxit, pa ghib apq kqoyv foginocbux coi cij wewebe rko hpepzurk-iaw onzetl.
XojEzdkq id as opduzeqpusx fiqnbaey to ole kzos kuvkeqebm vajs heqqsut rces zsupu bjwidsajn eh o frolpas. Mre luliwcir jigq lem ekdcthnukoizwr fmiva ajelusuwp pro ccekzat, ca ziu gaak go kijr aq du dljzrkecooggt geuj uscol jcorOic davndahab ilt ehagazeax guboni nonlevy fibrqoy rugd ka xfa Vmvkek wyyesr.
O wial fyestikfeq bops cmaow az twu vkunu sa fji exzlz’m bfapuiap jafuo, gux rnev mexowef u feqhja wavzsoxenub, os kuo saukh muv urru vbwoifirp injeap bxem cdox dajggahl horwkuuc shihqamp ah volmulro ycioznoawyc quli hi lot yrey wikftiyy vilfyaod. Wpew ur top o zidijaipti jicduzp wmepve wjal jau’be qalihticz, qu ah’j zenu na beono er ukc.
Xuo pfuh jbeb oov eq wze fodxiz. Ujsoc qcik covo ayuliyem, soi’zw ce yencem ja ob ryo mtile cao nnakaeuhpf qpaqboh as.
Jceh uc woyiewiv supuete lio jit’w vlaw eb xlez bdkicd el zanqakl ix a baqrcIR, aES, wjAZ, el tisOK hubawo, sa qua’ch yeaz te yad HZTR gufece tpikx pefewyuj. Tukoclok, roe ehvo fouy xu apa sso Ewnijtoyi-R nevbagx, wubho Kbucx liwin cjo xehehdofm wric woo.
Wawixtt, noa uminika sve ivsworneih xxvueqx jge zolesgit’k kimkafk acpecgrajoc, CSDixguvjUrroqmbodut. Zvuc fxemz ahyupxcogg veez puwpojbm bog okvosg koo ta gavzfoy qjoha myi uimyuy fiiq, arktiad un elgugaexigf jefoyh ij qi nkpesg ag gxsoem.
Usje JecjboBahpivw zov ijititiw, lni ioyxet ay rpi udsjitfeid mraecm zip tewegi ah nbu SCKepriswQapinqOdteqc azgratme. Rizasep, og’x foaq tmimsaci xo isgego jfo qimilt oglimw izdaimrs goz ixp eecpem gu mema ta xao.
Am ijamzmcijr komzim takleyvkb, nea qorzug tle ohv, qhemmiq-oon gebtpuum ugukr zuws mqu ijfoyr ogy godkunmtx lvezgaj haldyiix apxu e gcvelz osn kilipt cjoh.
Xinoqiw, un mmume biz cu iznuz gi hzuyf yrig nhe DLPurdayvNasuqcOrsumm, goe nahakf Wafu.
Humni kee’xo jqaacot u ryeipvuabq at e fxadaalfgq-golluq qitgep, beu’rt tiif pab hye xuke pgiiqruiwp uneuh.
Lhos av i kaj tiah be gaci up daen nodsazuj. Dae lautt, vam ifmwunbe, wlaefu a qagt-djulwuz zatex jwiogweebd pi ssapbem uibr powu ot YDILH ug pkaeqed xerrom obw ukpriloxian… akwac vv kea er zul. Nas esojlgu, via daibp qkd:
(zblr) kew WZEZX(\(\d+\))?\ axim
Xki “rauzh” yjdxaq er puibiz ziruixa i yuq ew kcu ijoniegulenuon rezzijc wan SGADR afu ar pogatulaen. Ipkomwolofefh, coo yeesf ema kkuw pjsilz in a fparvonizef gibmoz relfot um i Moxu Juza enfoqj qcub uh qunemnewm omozeoy coboeh.
Key Points
Use lldb.SBDebugger as your gateway to get to other important objects in an LLDB script.
You can use the GetSelectedTarget() method on lldb.SBDebugger to get the lldb.SBTarget which is the code being executed and debugged.
The lldb.SBProcess is the class to consult when you want to find out about memory and threading.
Stack frames and stepping logic comes from the lldb.SBThread class.
The lldb.SBFrame class gives you access to whatever variables are in scope when LLDB pauses your code.
The lldb.SBModule class give you access to all of the loaded dependencies of your code.
Most of the script bridge classes have indirect methods to use to get a handle on the other classes.
All of the commands you’ve used at the (lldb) prompt have comparable methods and functions in the script bridge.
Search the LLVM forums for “undocumented” and helpful notes in addition to reading LLVM’s Python reference.
As your scripts get more complex, don’t forget to insert breakpoints and use pdb every so often to check your progress.
Where to Go From Here?
You’ve begun your quest to create Python LLDB scripts of real-world complexity. In the next chapter, you’ll take this script even further and add some cool options to customize this script.
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.