lldb-wasm.patch 213 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815
  1. diff --git a/lldb/include/lldb/Core/Module.h b/lldb/include/lldb/Core/Module.h
  2. index dd7100c46..97d70daad 100644
  3. --- a/lldb/include/lldb/Core/Module.h
  4. +++ b/lldb/include/lldb/Core/Module.h
  5. @@ -41,6 +41,7 @@
  6. namespace lldb_private {
  7. class CompilerDeclContext;
  8. +class DWARFEvaluatorFactory;
  9. class Function;
  10. class Log;
  11. class ObjectFile;
  12. @@ -859,6 +860,8 @@ public:
  13. /// Update the ArchSpec to a more specific variant.
  14. bool MergeArchitecture(const ArchSpec &arch_spec);
  15. + DWARFEvaluatorFactory *GetDWARFExpressionEvaluatorFactory();
  16. +
  17. /// \class LookupInfo Module.h "lldb/Core/Module.h"
  18. /// A class that encapsulates name lookup information.
  19. ///
  20. @@ -985,6 +988,8 @@ protected:
  21. m_first_file_changed_log : 1; /// See if the module was modified after it
  22. /// was initially opened.
  23. + std::unique_ptr<DWARFEvaluatorFactory> m_dwarf_evaluator_factory;
  24. +
  25. /// Resolve a file or load virtual address.
  26. ///
  27. /// Tries to resolve \a vm_addr as a file address (if \a
  28. diff --git a/lldb/include/lldb/Core/PluginManager.h b/lldb/include/lldb/Core/PluginManager.h
  29. index be91929c6..8d876fc1f 100644
  30. --- a/lldb/include/lldb/Core/PluginManager.h
  31. +++ b/lldb/include/lldb/Core/PluginManager.h
  32. @@ -508,6 +508,17 @@ public:
  33. static bool CreateSettingForStructuredDataPlugin(
  34. Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
  35. ConstString description, bool is_global_property);
  36. +
  37. + // DWARFEvaluatorFactory
  38. + static bool
  39. + RegisterPlugin(ConstString name, const char *description,
  40. + DWARFEvaluatorFactoryCreateInstance create_callback);
  41. +
  42. + static bool
  43. + UnregisterPlugin(DWARFEvaluatorFactoryCreateInstance create_callback);
  44. +
  45. + static DWARFEvaluatorFactoryCreateInstance
  46. + GetDWARFEvaluatorFactoryCreateCallbackAtIndex(uint32_t idx);
  47. };
  48. } // namespace lldb_private
  49. diff --git a/lldb/include/lldb/Expression/DWARFEvaluator.h b/lldb/include/lldb/Expression/DWARFEvaluator.h
  50. new file mode 100644
  51. index 000000000..6811cbeae
  52. --- /dev/null
  53. +++ b/lldb/include/lldb/Expression/DWARFEvaluator.h
  54. @@ -0,0 +1,110 @@
  55. +//===-- DWARFEvaluator.h ----------------------------------------*- C++ -*-===//
  56. +//
  57. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  58. +// See https://llvm.org/LICENSE.txt for license information.
  59. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  60. +//
  61. +//===----------------------------------------------------------------------===//
  62. +
  63. +#ifndef LLDB_EXPRESSION_DWARFEVALUATOR_H
  64. +#define LLDB_EXPRESSION_DWARFEVALUATOR_H
  65. +
  66. +#include "lldb/lldb-private.h"
  67. +#include <vector>
  68. +
  69. +namespace lldb_private {
  70. +
  71. +class DWARFExpression;
  72. +
  73. +/// \class DWARFEvaluator DWARFEvaluator.h
  74. +/// "lldb/Expression/DWARFEvaluator.h" Evaluates DWARF opcodes.
  75. +///
  76. +class DWARFEvaluator {
  77. +public:
  78. + /// Crates a DWARF location expression evaluator
  79. + ///
  80. + /// \param[in] dwarf_expression
  81. + /// The DWARF expression to evaluate.
  82. + ///
  83. + /// \param[in] exe_ctx
  84. + /// The execution context in which to evaluate the location
  85. + /// expression. The location expression may access the target's
  86. + /// memory, especially if it comes from the expression parser.
  87. + ///
  88. + /// \param[in] reg_ctx
  89. + /// An optional parameter which provides a RegisterContext for use
  90. + /// when evaluating the expression (i.e. for fetching register values).
  91. + /// Normally this will come from the ExecutionContext's StackFrame but
  92. + /// in the case where an expression needs to be evaluated while building
  93. + /// the stack frame list, this short-cut is available.
  94. + ///
  95. + /// \param[in] initial_value_ptr
  96. + /// A value to put on top of the interpreter stack before evaluating
  97. + /// the expression, if the expression is parametrized. Can be NULL.
  98. + ///
  99. + /// \param[in] object_address_ptr
  100. + ///
  101. + DWARFEvaluator(const DWARFExpression &dwarf_expression,
  102. + ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
  103. + const Value *initial_value_ptr,
  104. + const Value *object_address_ptr);
  105. +
  106. + /// DWARFEvaluator protocol.
  107. + /// \{
  108. +
  109. + /// Evaluate the DWARF location expression
  110. + ///
  111. + /// \param[in] result
  112. + /// A value into which the result of evaluating the expression is
  113. + /// to be placed.
  114. + ///
  115. + /// \param[in] error_ptr
  116. + /// If non-NULL, used to report errors in expression evaluation.
  117. + ///
  118. + /// \return
  119. + /// True on success; false otherwise. If error_ptr is non-NULL,
  120. + /// details of the failure are provided through it.
  121. + virtual bool Evaluate(Value &result, Status *error_ptr);
  122. +
  123. + /// Evaluate the DWARF location expression with the opcodes specified.
  124. + ///
  125. + /// \param[in] opcodes
  126. + /// The DWARF opcodes to evaluate.
  127. + ///
  128. + /// \param[in] result
  129. + /// A value into which the result of evaluating the expression is
  130. + /// to be placed.
  131. + ///
  132. + /// \param[in] error_ptr
  133. + /// If non-NULL, used to report errors in expression evaluation.
  134. + ///
  135. + /// \return
  136. + /// True on success; false otherwise. If error_ptr is non-NULL,
  137. + /// details of the failure are provided through it.
  138. + virtual bool Evaluate(const DataExtractor &opcodes, Value &result,
  139. + Status *error_ptr);
  140. +
  141. + /// Evaluates a specific DWARF opcode in the context of a DWARF expression
  142. + virtual bool Evaluate(const uint8_t op, Process *process, StackFrame *frame,
  143. + std::vector<Value> &stack, const DataExtractor &opcodes,
  144. + lldb::offset_t &offset, Value &pieces,
  145. + uint64_t &op_piece_offset, Log *log, Status *error_ptr);
  146. +
  147. + /// \}
  148. +
  149. +protected:
  150. + const DWARFExpression &m_dwarf_expression;
  151. + ExecutionContext *m_exe_ctx;
  152. + RegisterContext *m_reg_ctx;
  153. + const Value *m_initial_value_ptr;
  154. + const Value *m_object_address_ptr;
  155. +
  156. +private:
  157. + DWARFEvaluator(const DWARFEvaluator &);
  158. + const DWARFEvaluator &operator=(const DWARFEvaluator &) = delete;
  159. +
  160. +};
  161. +
  162. +} // namespace lldb_private
  163. +
  164. +#endif // LLDB_EXPRESSION_DWARFEVALUATOR_H
  165. diff --git a/lldb/include/lldb/Expression/DWARFEvaluatorFactory.h b/lldb/include/lldb/Expression/DWARFEvaluatorFactory.h
  166. new file mode 100644
  167. index 000000000..f3b496c58
  168. --- /dev/null
  169. +++ b/lldb/include/lldb/Expression/DWARFEvaluatorFactory.h
  170. @@ -0,0 +1,56 @@
  171. +//===-- DWARFEvaluatorFactory.h ---------------------------------*- C++ -*-===//
  172. +//
  173. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  174. +// See https://llvm.org/LICENSE.txt for license information.
  175. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  176. +//
  177. +//===----------------------------------------------------------------------===//
  178. +
  179. +#ifndef LLDB_EXPRESSION_DWARFEVALUATORFACTORY_H
  180. +#define LLDB_EXPRESSION_DWARFEVALUATORFACTORY_H
  181. +
  182. +#include "lldb/Core/PluginInterface.h"
  183. +#include "lldb/Utility/ConstString.h"
  184. +#include "lldb/lldb-private.h"
  185. +
  186. +class DWARFUnit;
  187. +
  188. +namespace lldb_private {
  189. +
  190. +class DWARFEvaluator;
  191. +class DWARFExpression;
  192. +
  193. +/// \class DWARFEvaluatorFactory DWARFEvaluatorFactory.h
  194. +/// "lldb/Expression/DWARFEvaluatorFactory.h" Factory class that allows the
  195. +/// registration of platform-specific DWARF expression evaluators, used to
  196. +/// handle platform-specific DWARF opcodes.
  197. +class DWARFEvaluatorFactory : public PluginInterface {
  198. +public:
  199. + static std::unique_ptr<DWARFEvaluatorFactory> FindPlugin(Module *module);
  200. +
  201. + /// PluginInterface protocol.
  202. + /// \{
  203. + ConstString GetPluginName() override;
  204. +
  205. + uint32_t GetPluginVersion() override { return 1; }
  206. + /// \}
  207. +
  208. + DWARFEvaluatorFactory() {}
  209. +
  210. + /// DWARFEvaluatorFactory protocol.
  211. + /// \{
  212. + virtual std::unique_ptr<DWARFEvaluator>
  213. + CreateDWARFEvaluator(const DWARFExpression &dwarf_expression,
  214. + ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
  215. + const Value *initial_value_ptr,
  216. + const Value *object_address_ptr);
  217. + /// \}
  218. +
  219. +private:
  220. + DWARFEvaluatorFactory(const DWARFEvaluatorFactory &);
  221. + const DWARFEvaluatorFactory &operator=(const DWARFEvaluatorFactory &) = delete;
  222. +};
  223. +
  224. +} // namespace lldb_private
  225. +
  226. +#endif // LLDB_EXPRESSION_DWARFEVALUATORFACTORY_H
  227. diff --git a/lldb/include/lldb/Expression/DWARFExpression.h b/lldb/include/lldb/Expression/DWARFExpression.h
  228. index 1490ac2d6..35c741d4e 100644
  229. --- a/lldb/include/lldb/Expression/DWARFExpression.h
  230. +++ b/lldb/include/lldb/Expression/DWARFExpression.h
  231. @@ -120,6 +120,10 @@ public:
  232. void SetModule(const lldb::ModuleSP &module) { m_module_wp = module; }
  233. + lldb::ModuleSP GetModule() const { return m_module_wp.lock(); }
  234. +
  235. + const DWARFUnit *GetDWARFCompileUnit() const { return m_dwarf_cu; }
  236. +
  237. bool ContainsThreadLocalStorage() const;
  238. bool LinkThreadLocalStorage(
  239. @@ -140,7 +144,7 @@ public:
  240. lldb::addr_t func_file_addr);
  241. /// Return the call-frame-info style register kind
  242. - int GetRegisterKind();
  243. + lldb::RegisterKind GetRegisterKind() const;
  244. /// Set the call-frame-info style register kind
  245. ///
  246. @@ -219,6 +223,9 @@ public:
  247. bool MatchesOperand(StackFrame &frame, const Instruction::Operand &op);
  248. + static lldb::addr_t ReadAddressFromDebugAddrSection(const DWARFUnit *dwarf_cu,
  249. + uint32_t index);
  250. +
  251. llvm::Optional<DataExtractor>
  252. GetLocationExpression(lldb::addr_t load_function_start,
  253. lldb::addr_t addr) const;
  254. diff --git a/lldb/include/lldb/Target/Process.h b/lldb/include/lldb/Target/Process.h
  255. index aaa2470d2..c15f2db52 100644
  256. --- a/lldb/include/lldb/Target/Process.h
  257. +++ b/lldb/include/lldb/Target/Process.h
  258. @@ -1434,7 +1434,7 @@ public:
  259. /// vm_addr, \a buf, and \a size updated appropriately. Zero is
  260. /// returned in the case of an error.
  261. virtual size_t ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
  262. - Status &error);
  263. + Status &error, ExecutionContext *exe_ctx = nullptr);
  264. /// Read of memory from a process.
  265. ///
  266. diff --git a/lldb/include/lldb/Target/ProcessTrace.h b/lldb/include/lldb/Target/ProcessTrace.h
  267. index 7b9d6b13d..9525fc975 100644
  268. --- a/lldb/include/lldb/Target/ProcessTrace.h
  269. +++ b/lldb/include/lldb/Target/ProcessTrace.h
  270. @@ -59,7 +59,7 @@ public:
  271. bool WarnBeforeDetach() const override { return false; }
  272. size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size,
  273. - Status &error) override;
  274. + Status &error, ExecutionContext *exe_ctx = nullptr) override;
  275. size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
  276. Status &error) override;
  277. diff --git a/lldb/include/lldb/lldb-forward.h b/lldb/include/lldb/lldb-forward.h
  278. index ad5298151..5a3c0b27a 100644
  279. --- a/lldb/include/lldb/lldb-forward.h
  280. +++ b/lldb/include/lldb/lldb-forward.h
  281. @@ -74,6 +74,7 @@ class Disassembler;
  282. class DumpValueObjectOptions;
  283. class DynamicCheckerFunctions;
  284. class DynamicLoader;
  285. +class DWARFEvaluatorFactory;
  286. class Editline;
  287. class EmulateInstruction;
  288. class Environment;
  289. diff --git a/lldb/include/lldb/lldb-private-interfaces.h b/lldb/include/lldb/lldb-private-interfaces.h
  290. index 2ed083ec8..f4d500d19 100644
  291. --- a/lldb/include/lldb/lldb-private-interfaces.h
  292. +++ b/lldb/include/lldb/lldb-private-interfaces.h
  293. @@ -113,6 +113,8 @@ typedef lldb::REPLSP (*REPLCreateInstance)(Status &error,
  294. const char *repl_options);
  295. typedef int (*ComparisonFunction)(const void *, const void *);
  296. typedef void (*DebuggerInitializeCallback)(Debugger &debugger);
  297. +typedef DWARFEvaluatorFactory *(*DWARFEvaluatorFactoryCreateInstance)(
  298. + Module *module);
  299. /// Trace
  300. /// \{
  301. typedef llvm::Expected<lldb::TraceSP> (*TraceCreateInstanceForSessionFile)(
  302. diff --git a/lldb/source/Core/Module.cpp b/lldb/source/Core/Module.cpp
  303. index 19c97be15..1647f93ec 100644
  304. --- a/lldb/source/Core/Module.cpp
  305. +++ b/lldb/source/Core/Module.cpp
  306. @@ -16,6 +16,7 @@
  307. #include "lldb/Core/ModuleSpec.h"
  308. #include "lldb/Core/SearchFilter.h"
  309. #include "lldb/Core/Section.h"
  310. +#include "lldb/Expression/DWARFEvaluatorFactory.h"
  311. #include "lldb/Host/FileSystem.h"
  312. #include "lldb/Host/Host.h"
  313. #include "lldb/Host/HostInfo.h"
  314. @@ -1659,3 +1660,9 @@ bool Module::GetIsDynamicLinkEditor() {
  315. return false;
  316. }
  317. +
  318. +DWARFEvaluatorFactory *Module::GetDWARFExpressionEvaluatorFactory() {
  319. + if (!m_dwarf_evaluator_factory)
  320. + m_dwarf_evaluator_factory = DWARFEvaluatorFactory::FindPlugin(this);
  321. + return m_dwarf_evaluator_factory.get();
  322. +}
  323. diff --git a/lldb/source/Core/PluginManager.cpp b/lldb/source/Core/PluginManager.cpp
  324. index fcaa868b0..59a404d4a 100644
  325. --- a/lldb/source/Core/PluginManager.cpp
  326. +++ b/lldb/source/Core/PluginManager.cpp
  327. @@ -1597,3 +1597,32 @@ bool PluginManager::CreateSettingForStructuredDataPlugin(
  328. ConstString("Settings for structured data plug-ins"), properties_sp,
  329. description, is_global_property);
  330. }
  331. +
  332. +#pragma mark DWARFEvaluator
  333. +
  334. +typedef PluginInstance<DWARFEvaluatorFactoryCreateInstance>
  335. + DWARFEvaluatorFactoryInstance;
  336. +typedef PluginInstances<DWARFEvaluatorFactoryInstance>
  337. + DWARFEvaluatorFactoryInstances;
  338. +
  339. +static DWARFEvaluatorFactoryInstances &GetDWARFEvaluatorFactoryInstances() {
  340. + static DWARFEvaluatorFactoryInstances g_instances;
  341. + return g_instances;
  342. +}
  343. +
  344. +bool PluginManager::RegisterPlugin(
  345. + ConstString name, const char *description,
  346. + DWARFEvaluatorFactoryCreateInstance create_callback) {
  347. + return GetDWARFEvaluatorFactoryInstances().RegisterPlugin(name, description,
  348. + create_callback);
  349. +}
  350. +
  351. +bool PluginManager::UnregisterPlugin(
  352. + DWARFEvaluatorFactoryCreateInstance create_callback) {
  353. + return GetDWARFEvaluatorFactoryInstances().UnregisterPlugin(create_callback);
  354. +}
  355. +
  356. +DWARFEvaluatorFactoryCreateInstance
  357. +PluginManager::GetDWARFEvaluatorFactoryCreateCallbackAtIndex(uint32_t idx) {
  358. + return GetDWARFEvaluatorFactoryInstances().GetCallbackAtIndex(idx);
  359. +}
  360. diff --git a/lldb/source/Core/Value.cpp b/lldb/source/Core/Value.cpp
  361. index fb57c0fed..f92d6a54d 100644
  362. --- a/lldb/source/Core/Value.cpp
  363. +++ b/lldb/source/Core/Value.cpp
  364. @@ -538,7 +538,7 @@ Status Value::GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data,
  365. if (process) {
  366. const size_t bytes_read =
  367. - process->ReadMemory(address, dst, byte_size, error);
  368. + process->ReadMemory(address, dst, byte_size, error, exe_ctx);
  369. if (bytes_read != byte_size)
  370. error.SetErrorStringWithFormat(
  371. "read memory from 0x%" PRIx64 " failed (%u of %u bytes read)",
  372. diff --git a/lldb/source/Core/ValueObject.cpp b/lldb/source/Core/ValueObject.cpp
  373. index 9c1ba99da..b15b214b2 100644
  374. --- a/lldb/source/Core/ValueObject.cpp
  375. +++ b/lldb/source/Core/ValueObject.cpp
  376. @@ -735,7 +735,7 @@ size_t ValueObject::GetPointeeData(DataExtractor &data, uint32_t item_idx,
  377. if (process) {
  378. heap_buf_ptr->SetByteSize(bytes);
  379. size_t bytes_read = process->ReadMemory(
  380. - addr + offset, heap_buf_ptr->GetBytes(), bytes, error);
  381. + addr + offset, heap_buf_ptr->GetBytes(), bytes, error, &exe_ctx);
  382. if (error.Success() || bytes_read > 0) {
  383. data.SetData(data_sp);
  384. return bytes_read;
  385. diff --git a/lldb/source/Expression/CMakeLists.txt b/lldb/source/Expression/CMakeLists.txt
  386. index bf94361dd..4e76d547a 100644
  387. --- a/lldb/source/Expression/CMakeLists.txt
  388. +++ b/lldb/source/Expression/CMakeLists.txt
  389. @@ -1,5 +1,7 @@
  390. add_lldb_library(lldbExpression
  391. DiagnosticManager.cpp
  392. + DWARFEvaluator.cpp
  393. + DWARFEvaluatorFactory.cpp
  394. DWARFExpression.cpp
  395. Expression.cpp
  396. ExpressionVariable.cpp
  397. diff --git a/lldb/source/Expression/DWARFEvaluator.cpp b/lldb/source/Expression/DWARFEvaluator.cpp
  398. new file mode 100644
  399. index 000000000..06107e136
  400. --- /dev/null
  401. +++ b/lldb/source/Expression/DWARFEvaluator.cpp
  402. @@ -0,0 +1,1952 @@
  403. +//===-- DWARFEvaluator.cpp ------------ -----------------------------------===//
  404. +//
  405. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  406. +// See https://llvm.org/LICENSE.txt for license information.
  407. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  408. +//
  409. +//===----------------------------------------------------------------------===//
  410. +
  411. +#include "lldb/Expression/DWARFEvaluator.h"
  412. +#include "lldb/Expression/DWARFExpression.h"
  413. +
  414. +#include "lldb/Core/Module.h"
  415. +#include "lldb/Core/Value.h"
  416. +#include "lldb/Core/dwarf.h"
  417. +
  418. +#include "lldb/Utility/Log.h"
  419. +#include "lldb/Utility/RegisterValue.h"
  420. +
  421. +#include "lldb/Target/Process.h"
  422. +#include "lldb/Target/RegisterContext.h"
  423. +#include "lldb/Target/StackFrame.h"
  424. +
  425. +#include "Plugins/SymbolFile/DWARF/DWARFUnit.h"
  426. +
  427. +using namespace lldb;
  428. +using namespace lldb_private;
  429. +
  430. +DWARFEvaluator::DWARFEvaluator(const DWARFExpression &dwarf_expression,
  431. + ExecutionContext *exe_ctx,
  432. + RegisterContext *reg_ctx,
  433. + const Value *initial_value_ptr,
  434. + const Value *object_address_ptr)
  435. + : m_dwarf_expression(dwarf_expression), m_exe_ctx(exe_ctx),
  436. + m_reg_ctx(reg_ctx), m_initial_value_ptr(initial_value_ptr),
  437. + m_object_address_ptr(object_address_ptr) {}
  438. +
  439. +static bool ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
  440. + lldb::RegisterKind reg_kind,
  441. + uint32_t reg_num, Status *error_ptr,
  442. + Value &value) {
  443. + if (reg_ctx == nullptr) {
  444. + if (error_ptr)
  445. + error_ptr->SetErrorStringWithFormat("No register context in frame.\n");
  446. + } else {
  447. + uint32_t native_reg =
  448. + reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
  449. + if (native_reg == LLDB_INVALID_REGNUM) {
  450. + if (error_ptr)
  451. + error_ptr->SetErrorStringWithFormat("Unable to convert register "
  452. + "kind=%u reg_num=%u to a native "
  453. + "register number.\n",
  454. + reg_kind, reg_num);
  455. + } else {
  456. + const RegisterInfo *reg_info =
  457. + reg_ctx->GetRegisterInfoAtIndex(native_reg);
  458. + RegisterValue reg_value;
  459. + if (reg_ctx->ReadRegister(reg_info, reg_value)) {
  460. + if (reg_value.GetScalarValue(value.GetScalar())) {
  461. + value.SetValueType(Value::ValueType::Scalar);
  462. + value.SetContext(Value::ContextType::RegisterInfo,
  463. + const_cast<RegisterInfo *>(reg_info));
  464. + if (error_ptr)
  465. + error_ptr->Clear();
  466. + return true;
  467. + } else {
  468. + // If we get this error, then we need to implement a value buffer in
  469. + // the dwarf expression evaluation function...
  470. + if (error_ptr)
  471. + error_ptr->SetErrorStringWithFormat(
  472. + "register %s can't be converted to a scalar value",
  473. + reg_info->name);
  474. + }
  475. + } else {
  476. + if (error_ptr)
  477. + error_ptr->SetErrorStringWithFormat("register %s is not available",
  478. + reg_info->name);
  479. + }
  480. + }
  481. + }
  482. + return false;
  483. +}
  484. +
  485. +static bool Evaluate_DW_OP_entry_value(std::vector<Value> &stack,
  486. + ExecutionContext *exe_ctx,
  487. + RegisterContext *reg_ctx,
  488. + const DataExtractor &opcodes,
  489. + lldb::offset_t &opcode_offset,
  490. + Status *error_ptr, Log *log) {
  491. + // DW_OP_entry_value(sub-expr) describes the location a variable had upon
  492. + // function entry: this variable location is presumed to be optimized out at
  493. + // the current PC value. The caller of the function may have call site
  494. + // information that describes an alternate location for the variable (e.g. a
  495. + // constant literal, or a spilled stack value) in the parent frame.
  496. + //
  497. + // Example (this is pseudo-code & pseudo-DWARF, but hopefully illustrative):
  498. + //
  499. + // void child(int &sink, int x) {
  500. + // ...
  501. + // /* "x" gets optimized out. */
  502. + //
  503. + // /* The location of "x" here is: DW_OP_entry_value($reg2). */
  504. + // ++sink;
  505. + // }
  506. + //
  507. + // void parent() {
  508. + // int sink;
  509. + //
  510. + // /*
  511. + // * The callsite information emitted here is:
  512. + // *
  513. + // * DW_TAG_call_site
  514. + // * DW_AT_return_pc ... (for "child(sink, 123);")
  515. + // * DW_TAG_call_site_parameter (for "sink")
  516. + // * DW_AT_location ($reg1)
  517. + // * DW_AT_call_value ($SP - 8)
  518. + // * DW_TAG_call_site_parameter (for "x")
  519. + // * DW_AT_location ($reg2)
  520. + // * DW_AT_call_value ($literal 123)
  521. + // *
  522. + // * DW_TAG_call_site
  523. + // * DW_AT_return_pc ... (for "child(sink, 456);")
  524. + // * ...
  525. + // */
  526. + // child(sink, 123);
  527. + // child(sink, 456);
  528. + // }
  529. + //
  530. + // When the program stops at "++sink" within `child`, the debugger determines
  531. + // the call site by analyzing the return address. Once the call site is found,
  532. + // the debugger determines which parameter is referenced by DW_OP_entry_value
  533. + // and evaluates the corresponding location for that parameter in `parent`.
  534. +
  535. + // 1. Find the function which pushed the current frame onto the stack.
  536. + if ((!exe_ctx || !exe_ctx->HasTargetScope()) || !reg_ctx) {
  537. + LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no exe/reg context");
  538. + return false;
  539. + }
  540. +
  541. + StackFrame *current_frame = exe_ctx->GetFramePtr();
  542. + Thread *thread = exe_ctx->GetThreadPtr();
  543. + if (!current_frame || !thread) {
  544. + LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no current frame/thread");
  545. + return false;
  546. + }
  547. +
  548. + Target &target = exe_ctx->GetTargetRef();
  549. + StackFrameSP parent_frame = nullptr;
  550. + addr_t return_pc = LLDB_INVALID_ADDRESS;
  551. + uint32_t current_frame_idx = current_frame->GetFrameIndex();
  552. + uint32_t num_frames = thread->GetStackFrameCount();
  553. + for (uint32_t parent_frame_idx = current_frame_idx + 1;
  554. + parent_frame_idx < num_frames; ++parent_frame_idx) {
  555. + parent_frame = thread->GetStackFrameAtIndex(parent_frame_idx);
  556. + // Require a valid sequence of frames.
  557. + if (!parent_frame)
  558. + break;
  559. +
  560. + // Record the first valid return address, even if this is an inlined frame,
  561. + // in order to look up the associated call edge in the first non-inlined
  562. + // parent frame.
  563. + if (return_pc == LLDB_INVALID_ADDRESS) {
  564. + return_pc = parent_frame->GetFrameCodeAddress().GetLoadAddress(&target);
  565. + LLDB_LOG(log,
  566. + "Evaluate_DW_OP_entry_value: immediate ancestor with pc = {0:x}",
  567. + return_pc);
  568. + }
  569. +
  570. + // If we've found an inlined frame, skip it (these have no call site
  571. + // parameters).
  572. + if (parent_frame->IsInlined())
  573. + continue;
  574. +
  575. + // We've found the first non-inlined parent frame.
  576. + break;
  577. + }
  578. + if (!parent_frame || !parent_frame->GetRegisterContext()) {
  579. + LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no parent frame with reg ctx");
  580. + return false;
  581. + }
  582. +
  583. + Function *parent_func =
  584. + parent_frame->GetSymbolContext(eSymbolContextFunction).function;
  585. + if (!parent_func) {
  586. + LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no parent function");
  587. + return false;
  588. + }
  589. +
  590. + // 2. Find the call edge in the parent function responsible for creating the
  591. + // current activation.
  592. + Function *current_func =
  593. + current_frame->GetSymbolContext(eSymbolContextFunction).function;
  594. + if (!current_func) {
  595. + LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no current function");
  596. + return false;
  597. + }
  598. +
  599. + CallEdge *call_edge = nullptr;
  600. + ModuleList &modlist = target.GetImages();
  601. + ExecutionContext parent_exe_ctx = *exe_ctx;
  602. + parent_exe_ctx.SetFrameSP(parent_frame);
  603. + if (!parent_frame->IsArtificial()) {
  604. + // If the parent frame is not artificial, the current activation may be
  605. + // produced by an ambiguous tail call. In this case, refuse to proceed.
  606. + call_edge = parent_func->GetCallEdgeForReturnAddress(return_pc, target);
  607. + if (!call_edge) {
  608. + LLDB_LOG(log,
  609. + "Evaluate_DW_OP_entry_value: no call edge for retn-pc = {0:x} "
  610. + "in parent frame {1}",
  611. + return_pc, parent_func->GetName());
  612. + return false;
  613. + }
  614. + Function *callee_func = call_edge->GetCallee(modlist, parent_exe_ctx);
  615. + if (callee_func != current_func) {
  616. + LLDB_LOG(log, "Evaluate_DW_OP_entry_value: ambiguous call sequence, "
  617. + "can't find real parent frame");
  618. + return false;
  619. + }
  620. + } else {
  621. + // The StackFrameList solver machinery has deduced that an unambiguous tail
  622. + // call sequence that produced the current activation. The first edge in
  623. + // the parent that points to the current function must be valid.
  624. + for (auto &edge : parent_func->GetTailCallingEdges()) {
  625. + if (edge->GetCallee(modlist, parent_exe_ctx) == current_func) {
  626. + call_edge = edge.get();
  627. + break;
  628. + }
  629. + }
  630. + }
  631. + if (!call_edge) {
  632. + LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no unambiguous edge from parent "
  633. + "to current function");
  634. + return false;
  635. + }
  636. +
  637. + // 3. Attempt to locate the DW_OP_entry_value expression in the set of
  638. + // available call site parameters. If found, evaluate the corresponding
  639. + // parameter in the context of the parent frame.
  640. + const uint32_t subexpr_len = opcodes.GetULEB128(&opcode_offset);
  641. + const void *subexpr_data = opcodes.GetData(&opcode_offset, subexpr_len);
  642. + if (!subexpr_data) {
  643. + LLDB_LOG(log, "Evaluate_DW_OP_entry_value: subexpr could not be read");
  644. + return false;
  645. + }
  646. +
  647. + const CallSiteParameter *matched_param = nullptr;
  648. + for (const CallSiteParameter &param : call_edge->GetCallSiteParameters()) {
  649. + DataExtractor param_subexpr_extractor;
  650. + if (!param.LocationInCallee.GetExpressionData(param_subexpr_extractor))
  651. + continue;
  652. + lldb::offset_t param_subexpr_offset = 0;
  653. + const void *param_subexpr_data =
  654. + param_subexpr_extractor.GetData(&param_subexpr_offset, subexpr_len);
  655. + if (!param_subexpr_data ||
  656. + param_subexpr_extractor.BytesLeft(param_subexpr_offset) != 0)
  657. + continue;
  658. +
  659. + // At this point, the DW_OP_entry_value sub-expression and the callee-side
  660. + // expression in the call site parameter are known to have the same length.
  661. + // Check whether they are equal.
  662. + //
  663. + // Note that an equality check is sufficient: the contents of the
  664. + // DW_OP_entry_value subexpression are only used to identify the right call
  665. + // site parameter in the parent, and do not require any special handling.
  666. + if (memcmp(subexpr_data, param_subexpr_data, subexpr_len) == 0) {
  667. + matched_param = &param;
  668. + break;
  669. + }
  670. + }
  671. + if (!matched_param) {
  672. + LLDB_LOG(log,
  673. + "Evaluate_DW_OP_entry_value: no matching call site param found");
  674. + return false;
  675. + }
  676. +
  677. + // TODO: Add support for DW_OP_push_object_address within a DW_OP_entry_value
  678. + // subexpresion whenever llvm does.
  679. + Value result;
  680. + const DWARFExpression &param_expr = matched_param->LocationInCaller;
  681. + if (!param_expr.Evaluate(&parent_exe_ctx,
  682. + parent_frame->GetRegisterContext().get(),
  683. + /*loclist_base_addr=*/LLDB_INVALID_ADDRESS,
  684. + /*initial_value_ptr=*/nullptr,
  685. + /*object_address_ptr=*/nullptr, result, error_ptr)) {
  686. + LLDB_LOG(log,
  687. + "Evaluate_DW_OP_entry_value: call site param evaluation failed");
  688. + return false;
  689. + }
  690. +
  691. + stack.push_back(result);
  692. + return true;
  693. +}
  694. +
  695. +bool DWARFEvaluator::Evaluate(Value &result, Status *error_ptr) {
  696. + DataExtractor opcodes;
  697. + if (!m_dwarf_expression.GetExpressionData(opcodes)) {
  698. + if (error_ptr)
  699. + error_ptr->SetErrorString(
  700. + "no location, value may have been optimized out");
  701. + return false;
  702. + }
  703. + return Evaluate(opcodes, result, error_ptr);
  704. +}
  705. +
  706. +bool DWARFEvaluator::Evaluate(const DataExtractor &opcodes, Value &result,
  707. + Status *error_ptr) {
  708. + if (opcodes.GetByteSize() == 0) {
  709. + if (error_ptr)
  710. + error_ptr->SetErrorString(
  711. + "no location, value may have been optimized out");
  712. + return false;
  713. + }
  714. + std::vector<Value> stack;
  715. +
  716. + Process *process = nullptr;
  717. + StackFrame *frame = nullptr;
  718. +
  719. + if (m_exe_ctx) {
  720. + process = m_exe_ctx->GetProcessPtr();
  721. + frame = m_exe_ctx->GetFramePtr();
  722. + }
  723. + if (m_reg_ctx == nullptr && frame)
  724. + m_reg_ctx = frame->GetRegisterContext().get();
  725. +
  726. + if (m_initial_value_ptr)
  727. + stack.push_back(*m_initial_value_ptr);
  728. +
  729. + lldb::offset_t offset = 0;
  730. +
  731. + /// Insertion point for evaluating multi-piece expression.
  732. + uint64_t op_piece_offset = 0;
  733. + Value pieces; // Used for DW_OP_piece
  734. +
  735. + Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
  736. +
  737. + uint8_t _opcode = 0;
  738. +
  739. + while (opcodes.ValidOffset(offset)) {
  740. + const lldb::offset_t op_offset = offset;
  741. + const uint8_t op = opcodes.GetU8(&offset);
  742. + _opcode = op;
  743. +
  744. + if (log && log->GetVerbose()) {
  745. + size_t count = stack.size();
  746. + LLDB_LOGF(log, "Stack before operation has %" PRIu64 " values:",
  747. + (uint64_t)count);
  748. + for (size_t i = 0; i < count; ++i) {
  749. + StreamString new_value;
  750. + new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
  751. + stack[i].Dump(&new_value);
  752. + LLDB_LOGF(log, " %s", new_value.GetData());
  753. + }
  754. + LLDB_LOGF(log, "0x%8.8" PRIx64 ": %s", op_offset,
  755. + DW_OP_value_to_name(op));
  756. + }
  757. +
  758. + if (!Evaluate(op, process, frame, stack, opcodes, offset, pieces,
  759. + op_piece_offset, log, error_ptr))
  760. + return false;
  761. + }
  762. +
  763. + if (stack.empty()) {
  764. + // Nothing on the stack, check if we created a piece value from DW_OP_piece
  765. + // or DW_OP_bit_piece opcodes
  766. + if (pieces.GetBuffer().GetByteSize())
  767. + result = pieces;
  768. + else {
  769. + if (error_ptr)
  770. + error_ptr->SetErrorString("Stack empty after evaluation.");
  771. + return false;
  772. + }
  773. + } else {
  774. + if (log && log->GetVerbose()) {
  775. + size_t count = stack.size();
  776. + LLDB_LOGF(log, "Stack after operation has %" PRIu64 " values:",
  777. + (uint64_t)count);
  778. + for (size_t i = 0; i < count; ++i) {
  779. + StreamString new_value;
  780. + new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
  781. + stack[i].Dump(&new_value);
  782. + LLDB_LOGF(log, " %s", new_value.GetData());
  783. + }
  784. + }
  785. + result = stack.back();
  786. + }
  787. + return true; // Return true on success
  788. +}
  789. +
  790. +bool DWARFEvaluator::Evaluate(const uint8_t op, Process *process,
  791. + StackFrame *frame, std::vector<Value> &stack,
  792. + const DataExtractor &opcodes,
  793. + lldb::offset_t &offset, Value &pieces,
  794. + uint64_t &op_piece_offset, Log *log,
  795. + Status *error_ptr) {
  796. + Value tmp;
  797. + uint32_t reg_num;
  798. +
  799. + lldb::ModuleSP module_sp = m_dwarf_expression.GetModule();
  800. + const DWARFUnit *dwarf_cu = m_dwarf_expression.GetDWARFCompileUnit();
  801. + const lldb::RegisterKind reg_kind = m_dwarf_expression.GetRegisterKind();
  802. +
  803. + switch (op) {
  804. + // The DW_OP_addr operation has a single operand that encodes a machine
  805. + // address and whose size is the size of an address on the target machine.
  806. + case DW_OP_addr:
  807. + stack.push_back(Scalar(opcodes.GetAddress(&offset)));
  808. + stack.back().SetValueType(Value::ValueType::FileAddress);
  809. + // Convert the file address to a load address, so subsequent
  810. + // DWARF operators can operate on it.
  811. + if (frame)
  812. + stack.back().ConvertToLoadAddress(module_sp.get(),
  813. + frame->CalculateTarget().get());
  814. + break;
  815. +
  816. + // The DW_OP_addr_sect_offset4 is used for any location expressions in
  817. + // shared libraries that have a location like:
  818. + // DW_OP_addr(0x1000)
  819. + // If this address resides in a shared library, then this virtual address
  820. + // won't make sense when it is evaluated in the context of a running
  821. + // process where shared libraries have been slid. To account for this, this
  822. + // new address type where we can store the section pointer and a 4 byte
  823. + // offset.
  824. + // case DW_OP_addr_sect_offset4:
  825. + // {
  826. + // result_type = eResultTypeFileAddress;
  827. + // lldb::Section *sect = (lldb::Section
  828. + // *)opcodes.GetMaxU64(&offset, sizeof(void *));
  829. + // lldb::addr_t sect_offset = opcodes.GetU32(&offset);
  830. + //
  831. + // Address so_addr (sect, sect_offset);
  832. + // lldb::addr_t load_addr = so_addr.GetLoadAddress();
  833. + // if (load_addr != LLDB_INVALID_ADDRESS)
  834. + // {
  835. + // // We successfully resolve a file address to a load
  836. + // // address.
  837. + // stack.push_back(load_addr);
  838. + // break;
  839. + // }
  840. + // else
  841. + // {
  842. + // // We were able
  843. + // if (error_ptr)
  844. + // error_ptr->SetErrorStringWithFormat ("Section %s in
  845. + // %s is not currently loaded.\n",
  846. + // sect->GetName().AsCString(),
  847. + // sect->GetModule()->GetFileSpec().GetFilename().AsCString());
  848. + // return false;
  849. + // }
  850. + // }
  851. + // break;
  852. +
  853. + // OPCODE: DW_OP_deref
  854. + // OPERANDS: none
  855. + // DESCRIPTION: Pops the top stack entry and treats it as an address.
  856. + // The value retrieved from that address is pushed. The size of the data
  857. + // retrieved from the dereferenced address is the size of an address on the
  858. + // target machine.
  859. + case DW_OP_deref: {
  860. + if (stack.empty()) {
  861. + if (error_ptr)
  862. + error_ptr->SetErrorString("Expression stack empty for DW_OP_deref.");
  863. + return false;
  864. + }
  865. + Value::ValueType value_type = stack.back().GetValueType();
  866. + switch (value_type) {
  867. + case Value::ValueType::HostAddress: {
  868. + void *src = (void *)stack.back().GetScalar().ULongLong();
  869. + intptr_t ptr;
  870. + ::memcpy(&ptr, src, sizeof(void *));
  871. + stack.back().GetScalar() = ptr;
  872. + stack.back().ClearContext();
  873. + } break;
  874. + case Value::ValueType::FileAddress: {
  875. + auto file_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
  876. + if (!module_sp) {
  877. + if (error_ptr)
  878. + error_ptr->SetErrorStringWithFormat(
  879. + "need module to resolve file address for DW_OP_deref");
  880. + return false;
  881. + }
  882. + Address so_addr;
  883. + if (!module_sp->ResolveFileAddress(file_addr, so_addr)) {
  884. + if (error_ptr)
  885. + error_ptr->SetErrorStringWithFormat(
  886. + "failed to resolve file address in module");
  887. + return false;
  888. + }
  889. + addr_t load_Addr = so_addr.GetLoadAddress(m_exe_ctx->GetTargetPtr());
  890. + if (load_Addr == LLDB_INVALID_ADDRESS) {
  891. + if (error_ptr)
  892. + error_ptr->SetErrorStringWithFormat("failed to resolve load address");
  893. + return false;
  894. + }
  895. + stack.back().GetScalar() = load_Addr;
  896. + stack.back().SetValueType(Value::ValueType::LoadAddress);
  897. + // Fall through to load address code below...
  898. + }
  899. + LLVM_FALLTHROUGH;
  900. + case Value::ValueType::LoadAddress:
  901. + if (m_exe_ctx) {
  902. + if (process) {
  903. + lldb::addr_t pointer_addr =
  904. + stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
  905. + Status error;
  906. + lldb::addr_t pointer_value =
  907. + process->ReadPointerFromMemory(pointer_addr, error);
  908. + if (pointer_value != LLDB_INVALID_ADDRESS) {
  909. + stack.back().GetScalar() = pointer_value;
  910. + stack.back().ClearContext();
  911. + } else {
  912. + if (error_ptr)
  913. + error_ptr->SetErrorStringWithFormat(
  914. + "Failed to dereference pointer from 0x%" PRIx64
  915. + " for DW_OP_deref: %s\n",
  916. + pointer_addr, error.AsCString());
  917. + return false;
  918. + }
  919. + } else {
  920. + if (error_ptr)
  921. + error_ptr->SetErrorStringWithFormat(
  922. + "NULL process for DW_OP_deref.\n");
  923. + return false;
  924. + }
  925. + } else {
  926. + if (error_ptr)
  927. + error_ptr->SetErrorStringWithFormat(
  928. + "NULL execution context for DW_OP_deref.\n");
  929. + return false;
  930. + }
  931. + break;
  932. +
  933. + default:
  934. + break;
  935. + }
  936. +
  937. + } break;
  938. +
  939. + // OPCODE: DW_OP_deref_size
  940. + // OPERANDS: 1
  941. + // 1 - uint8_t that specifies the size of the data to dereference.
  942. + // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
  943. + // stack entry and treats it as an address. The value retrieved from that
  944. + // address is pushed. In the DW_OP_deref_size operation, however, the size
  945. + // in bytes of the data retrieved from the dereferenced address is
  946. + // specified by the single operand. This operand is a 1-byte unsigned
  947. + // integral constant whose value may not be larger than the size of an
  948. + // address on the target machine. The data retrieved is zero extended to
  949. + // the size of an address on the target machine before being pushed on the
  950. + // expression stack.
  951. + case DW_OP_deref_size: {
  952. + if (stack.empty()) {
  953. + if (error_ptr)
  954. + error_ptr->SetErrorString(
  955. + "Expression stack empty for DW_OP_deref_size.");
  956. + return false;
  957. + }
  958. + uint8_t size = opcodes.GetU8(&offset);
  959. + Value::ValueType value_type = stack.back().GetValueType();
  960. + switch (value_type) {
  961. + case Value::ValueType::HostAddress: {
  962. + void *src = (void *)stack.back().GetScalar().ULongLong();
  963. + intptr_t ptr;
  964. + ::memcpy(&ptr, src, sizeof(void *));
  965. + // I can't decide whether the size operand should apply to the bytes in
  966. + // their
  967. + // lldb-host endianness or the target endianness.. I doubt this'll ever
  968. + // come up but I'll opt for assuming big endian regardless.
  969. + switch (size) {
  970. + case 1:
  971. + ptr = ptr & 0xff;
  972. + break;
  973. + case 2:
  974. + ptr = ptr & 0xffff;
  975. + break;
  976. + case 3:
  977. + ptr = ptr & 0xffffff;
  978. + break;
  979. + case 4:
  980. + ptr = ptr & 0xffffffff;
  981. + break;
  982. + // the casts are added to work around the case where intptr_t is a 32
  983. + // bit quantity;
  984. + // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this
  985. + // program.
  986. + case 5:
  987. + ptr = (intptr_t)ptr & 0xffffffffffULL;
  988. + break;
  989. + case 6:
  990. + ptr = (intptr_t)ptr & 0xffffffffffffULL;
  991. + break;
  992. + case 7:
  993. + ptr = (intptr_t)ptr & 0xffffffffffffffULL;
  994. + break;
  995. + default:
  996. + break;
  997. + }
  998. + stack.back().GetScalar() = ptr;
  999. + stack.back().ClearContext();
  1000. + } break;
  1001. + case Value::ValueType::LoadAddress:
  1002. + if (m_exe_ctx) {
  1003. + if (process) {
  1004. + lldb::addr_t pointer_addr =
  1005. + stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
  1006. + uint8_t addr_bytes[sizeof(lldb::addr_t)];
  1007. + Status error;
  1008. + if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) ==
  1009. + size) {
  1010. + DataExtractor addr_data(addr_bytes, sizeof(addr_bytes),
  1011. + process->GetByteOrder(), size);
  1012. + lldb::offset_t addr_data_offset = 0;
  1013. + switch (size) {
  1014. + case 1:
  1015. + stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset);
  1016. + break;
  1017. + case 2:
  1018. + stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset);
  1019. + break;
  1020. + case 4:
  1021. + stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset);
  1022. + break;
  1023. + case 8:
  1024. + stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset);
  1025. + break;
  1026. + default:
  1027. + stack.back().GetScalar() =
  1028. + addr_data.GetAddress(&addr_data_offset);
  1029. + }
  1030. + stack.back().ClearContext();
  1031. + } else {
  1032. + if (error_ptr)
  1033. + error_ptr->SetErrorStringWithFormat(
  1034. + "Failed to dereference pointer from 0x%" PRIx64
  1035. + " for DW_OP_deref: %s\n",
  1036. + pointer_addr, error.AsCString());
  1037. + return false;
  1038. + }
  1039. + } else {
  1040. + if (error_ptr)
  1041. + error_ptr->SetErrorStringWithFormat(
  1042. + "NULL process for DW_OP_deref.\n");
  1043. + return false;
  1044. + }
  1045. + } else {
  1046. + if (error_ptr)
  1047. + error_ptr->SetErrorStringWithFormat(
  1048. + "NULL execution context for DW_OP_deref.\n");
  1049. + return false;
  1050. + }
  1051. + break;
  1052. +
  1053. + default:
  1054. + break;
  1055. + }
  1056. +
  1057. + } break;
  1058. +
  1059. + // OPCODE: DW_OP_xderef_size
  1060. + // OPERANDS: 1
  1061. + // 1 - uint8_t that specifies the size of the data to dereference.
  1062. + // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
  1063. + // the top of the stack is treated as an address. The second stack entry is
  1064. + // treated as an "address space identifier" for those architectures that
  1065. + // support multiple address spaces. The top two stack elements are popped,
  1066. + // a data item is retrieved through an implementation-defined address
  1067. + // calculation and pushed as the new stack top. In the DW_OP_xderef_size
  1068. + // operation, however, the size in bytes of the data retrieved from the
  1069. + // dereferenced address is specified by the single operand. This operand is
  1070. + // a 1-byte unsigned integral constant whose value may not be larger than
  1071. + // the size of an address on the target machine. The data retrieved is zero
  1072. + // extended to the size of an address on the target machine before being
  1073. + // pushed on the expression stack.
  1074. + case DW_OP_xderef_size:
  1075. + if (error_ptr)
  1076. + error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
  1077. + return false;
  1078. + // OPCODE: DW_OP_xderef
  1079. + // OPERANDS: none
  1080. + // DESCRIPTION: Provides an extended dereference mechanism. The entry at
  1081. + // the top of the stack is treated as an address. The second stack entry is
  1082. + // treated as an "address space identifier" for those architectures that
  1083. + // support multiple address spaces. The top two stack elements are popped,
  1084. + // a data item is retrieved through an implementation-defined address
  1085. + // calculation and pushed as the new stack top. The size of the data
  1086. + // retrieved from the dereferenced address is the size of an address on the
  1087. + // target machine.
  1088. + case DW_OP_xderef:
  1089. + if (error_ptr)
  1090. + error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
  1091. + return false;
  1092. +
  1093. + // All DW_OP_constXXX opcodes have a single operand as noted below:
  1094. + //
  1095. + // Opcode Operand 1
  1096. + // DW_OP_const1u 1-byte unsigned integer constant DW_OP_const1s
  1097. + // 1-byte signed integer constant DW_OP_const2u 2-byte unsigned integer
  1098. + // constant DW_OP_const2s 2-byte signed integer constant DW_OP_const4u
  1099. + // 4-byte unsigned integer constant DW_OP_const4s 4-byte signed integer
  1100. + // constant DW_OP_const8u 8-byte unsigned integer constant DW_OP_const8s
  1101. + // 8-byte signed integer constant DW_OP_constu unsigned LEB128 integer
  1102. + // constant DW_OP_consts signed LEB128 integer constant
  1103. + case DW_OP_const1u:
  1104. + stack.push_back(Scalar((uint8_t)opcodes.GetU8(&offset)));
  1105. + break;
  1106. + case DW_OP_const1s:
  1107. + stack.push_back(Scalar((int8_t)opcodes.GetU8(&offset)));
  1108. + break;
  1109. + case DW_OP_const2u:
  1110. + stack.push_back(Scalar((uint16_t)opcodes.GetU16(&offset)));
  1111. + break;
  1112. + case DW_OP_const2s:
  1113. + stack.push_back(Scalar((int16_t)opcodes.GetU16(&offset)));
  1114. + break;
  1115. + case DW_OP_const4u:
  1116. + stack.push_back(Scalar((uint32_t)opcodes.GetU32(&offset)));
  1117. + break;
  1118. + case DW_OP_const4s:
  1119. + stack.push_back(Scalar((int32_t)opcodes.GetU32(&offset)));
  1120. + break;
  1121. + case DW_OP_const8u:
  1122. + stack.push_back(Scalar((uint64_t)opcodes.GetU64(&offset)));
  1123. + break;
  1124. + case DW_OP_const8s:
  1125. + stack.push_back(Scalar((int64_t)opcodes.GetU64(&offset)));
  1126. + break;
  1127. + case DW_OP_constu:
  1128. + stack.push_back(Scalar(opcodes.GetULEB128(&offset)));
  1129. + break;
  1130. + case DW_OP_consts:
  1131. + stack.push_back(Scalar(opcodes.GetSLEB128(&offset)));
  1132. + break;
  1133. +
  1134. + // OPCODE: DW_OP_dup
  1135. + // OPERANDS: none
  1136. + // DESCRIPTION: duplicates the value at the top of the stack
  1137. + case DW_OP_dup:
  1138. + if (stack.empty()) {
  1139. + if (error_ptr)
  1140. + error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
  1141. + return false;
  1142. + } else
  1143. + stack.push_back(stack.back());
  1144. + break;
  1145. +
  1146. + // OPCODE: DW_OP_drop
  1147. + // OPERANDS: none
  1148. + // DESCRIPTION: pops the value at the top of the stack
  1149. + case DW_OP_drop:
  1150. + if (stack.empty()) {
  1151. + if (error_ptr)
  1152. + error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
  1153. + return false;
  1154. + } else
  1155. + stack.pop_back();
  1156. + break;
  1157. +
  1158. + // OPCODE: DW_OP_over
  1159. + // OPERANDS: none
  1160. + // DESCRIPTION: Duplicates the entry currently second in the stack at
  1161. + // the top of the stack.
  1162. + case DW_OP_over:
  1163. + if (stack.size() < 2) {
  1164. + if (error_ptr)
  1165. + error_ptr->SetErrorString(
  1166. + "Expression stack needs at least 2 items for DW_OP_over.");
  1167. + return false;
  1168. + } else
  1169. + stack.push_back(stack[stack.size() - 2]);
  1170. + break;
  1171. +
  1172. + // OPCODE: DW_OP_pick
  1173. + // OPERANDS: uint8_t index into the current stack
  1174. + // DESCRIPTION: The stack entry with the specified index (0 through 255,
  1175. + // inclusive) is pushed on the stack
  1176. + case DW_OP_pick: {
  1177. + uint8_t pick_idx = opcodes.GetU8(&offset);
  1178. + if (pick_idx < stack.size())
  1179. + stack.push_back(stack[stack.size() - 1 - pick_idx]);
  1180. + else {
  1181. + if (error_ptr)
  1182. + error_ptr->SetErrorStringWithFormat(
  1183. + "Index %u out of range for DW_OP_pick.\n", pick_idx);
  1184. + return false;
  1185. + }
  1186. + } break;
  1187. +
  1188. + // OPCODE: DW_OP_swap
  1189. + // OPERANDS: none
  1190. + // DESCRIPTION: swaps the top two stack entries. The entry at the top
  1191. + // of the stack becomes the second stack entry, and the second entry
  1192. + // becomes the top of the stack
  1193. + case DW_OP_swap:
  1194. + if (stack.size() < 2) {
  1195. + if (error_ptr)
  1196. + error_ptr->SetErrorString(
  1197. + "Expression stack needs at least 2 items for DW_OP_swap.");
  1198. + return false;
  1199. + } else {
  1200. + tmp = stack.back();
  1201. + stack.back() = stack[stack.size() - 2];
  1202. + stack[stack.size() - 2] = tmp;
  1203. + }
  1204. + break;
  1205. +
  1206. + // OPCODE: DW_OP_rot
  1207. + // OPERANDS: none
  1208. + // DESCRIPTION: Rotates the first three stack entries. The entry at
  1209. + // the top of the stack becomes the third stack entry, the second entry
  1210. + // becomes the top of the stack, and the third entry becomes the second
  1211. + // entry.
  1212. + case DW_OP_rot:
  1213. + if (stack.size() < 3) {
  1214. + if (error_ptr)
  1215. + error_ptr->SetErrorString(
  1216. + "Expression stack needs at least 3 items for DW_OP_rot.");
  1217. + return false;
  1218. + } else {
  1219. + size_t last_idx = stack.size() - 1;
  1220. + Value old_top = stack[last_idx];
  1221. + stack[last_idx] = stack[last_idx - 1];
  1222. + stack[last_idx - 1] = stack[last_idx - 2];
  1223. + stack[last_idx - 2] = old_top;
  1224. + }
  1225. + break;
  1226. +
  1227. + // OPCODE: DW_OP_abs
  1228. + // OPERANDS: none
  1229. + // DESCRIPTION: pops the top stack entry, interprets it as a signed
  1230. + // value and pushes its absolute value. If the absolute value can not be
  1231. + // represented, the result is undefined.
  1232. + case DW_OP_abs:
  1233. + if (stack.empty()) {
  1234. + if (error_ptr)
  1235. + error_ptr->SetErrorString(
  1236. + "Expression stack needs at least 1 item for DW_OP_abs.");
  1237. + return false;
  1238. + } else if (!stack.back().ResolveValue(m_exe_ctx).AbsoluteValue()) {
  1239. + if (error_ptr)
  1240. + error_ptr->SetErrorString(
  1241. + "Failed to take the absolute value of the first stack item.");
  1242. + return false;
  1243. + }
  1244. + break;
  1245. +
  1246. + // OPCODE: DW_OP_and
  1247. + // OPERANDS: none
  1248. + // DESCRIPTION: pops the top two stack values, performs a bitwise and
  1249. + // operation on the two, and pushes the result.
  1250. + case DW_OP_and:
  1251. + if (stack.size() < 2) {
  1252. + if (error_ptr)
  1253. + error_ptr->SetErrorString(
  1254. + "Expression stack needs at least 2 items for DW_OP_and.");
  1255. + return false;
  1256. + } else {
  1257. + tmp = stack.back();
  1258. + stack.pop_back();
  1259. + stack.back().ResolveValue(m_exe_ctx) =
  1260. + stack.back().ResolveValue(m_exe_ctx) & tmp.ResolveValue(m_exe_ctx);
  1261. + }
  1262. + break;
  1263. +
  1264. + // OPCODE: DW_OP_div
  1265. + // OPERANDS: none
  1266. + // DESCRIPTION: pops the top two stack values, divides the former second
  1267. + // entry by the former top of the stack using signed division, and pushes
  1268. + // the result.
  1269. + case DW_OP_div:
  1270. + if (stack.size() < 2) {
  1271. + if (error_ptr)
  1272. + error_ptr->SetErrorString(
  1273. + "Expression stack needs at least 2 items for DW_OP_div.");
  1274. + return false;
  1275. + } else {
  1276. + tmp = stack.back();
  1277. + if (tmp.ResolveValue(m_exe_ctx).IsZero()) {
  1278. + if (error_ptr)
  1279. + error_ptr->SetErrorString("Divide by zero.");
  1280. + return false;
  1281. + } else {
  1282. + stack.pop_back();
  1283. + stack.back() =
  1284. + stack.back().ResolveValue(m_exe_ctx) / tmp.ResolveValue(m_exe_ctx);
  1285. + if (!stack.back().ResolveValue(m_exe_ctx).IsValid()) {
  1286. + if (error_ptr)
  1287. + error_ptr->SetErrorString("Divide failed.");
  1288. + return false;
  1289. + }
  1290. + }
  1291. + }
  1292. + break;
  1293. +
  1294. + // OPCODE: DW_OP_minus
  1295. + // OPERANDS: none
  1296. + // DESCRIPTION: pops the top two stack values, subtracts the former top
  1297. + // of the stack from the former second entry, and pushes the result.
  1298. + case DW_OP_minus:
  1299. + if (stack.size() < 2) {
  1300. + if (error_ptr)
  1301. + error_ptr->SetErrorString(
  1302. + "Expression stack needs at least 2 items for DW_OP_minus.");
  1303. + return false;
  1304. + } else {
  1305. + tmp = stack.back();
  1306. + stack.pop_back();
  1307. + stack.back().ResolveValue(m_exe_ctx) =
  1308. + stack.back().ResolveValue(m_exe_ctx) - tmp.ResolveValue(m_exe_ctx);
  1309. + }
  1310. + break;
  1311. +
  1312. + // OPCODE: DW_OP_mod
  1313. + // OPERANDS: none
  1314. + // DESCRIPTION: pops the top two stack values and pushes the result of
  1315. + // the calculation: former second stack entry modulo the former top of the
  1316. + // stack.
  1317. + case DW_OP_mod:
  1318. + if (stack.size() < 2) {
  1319. + if (error_ptr)
  1320. + error_ptr->SetErrorString(
  1321. + "Expression stack needs at least 2 items for DW_OP_mod.");
  1322. + return false;
  1323. + } else {
  1324. + tmp = stack.back();
  1325. + stack.pop_back();
  1326. + stack.back().ResolveValue(m_exe_ctx) =
  1327. + stack.back().ResolveValue(m_exe_ctx) % tmp.ResolveValue(m_exe_ctx);
  1328. + }
  1329. + break;
  1330. +
  1331. + // OPCODE: DW_OP_mul
  1332. + // OPERANDS: none
  1333. + // DESCRIPTION: pops the top two stack entries, multiplies them
  1334. + // together, and pushes the result.
  1335. + case DW_OP_mul:
  1336. + if (stack.size() < 2) {
  1337. + if (error_ptr)
  1338. + error_ptr->SetErrorString(
  1339. + "Expression stack needs at least 2 items for DW_OP_mul.");
  1340. + return false;
  1341. + } else {
  1342. + tmp = stack.back();
  1343. + stack.pop_back();
  1344. + stack.back().ResolveValue(m_exe_ctx) =
  1345. + stack.back().ResolveValue(m_exe_ctx) * tmp.ResolveValue(m_exe_ctx);
  1346. + }
  1347. + break;
  1348. +
  1349. + // OPCODE: DW_OP_neg
  1350. + // OPERANDS: none
  1351. + // DESCRIPTION: pops the top stack entry, and pushes its negation.
  1352. + case DW_OP_neg:
  1353. + if (stack.empty()) {
  1354. + if (error_ptr)
  1355. + error_ptr->SetErrorString(
  1356. + "Expression stack needs at least 1 item for DW_OP_neg.");
  1357. + return false;
  1358. + } else {
  1359. + if (!stack.back().ResolveValue(m_exe_ctx).UnaryNegate()) {
  1360. + if (error_ptr)
  1361. + error_ptr->SetErrorString("Unary negate failed.");
  1362. + return false;
  1363. + }
  1364. + }
  1365. + break;
  1366. +
  1367. + // OPCODE: DW_OP_not
  1368. + // OPERANDS: none
  1369. + // DESCRIPTION: pops the top stack entry, and pushes its bitwise
  1370. + // complement
  1371. + case DW_OP_not:
  1372. + if (stack.empty()) {
  1373. + if (error_ptr)
  1374. + error_ptr->SetErrorString(
  1375. + "Expression stack needs at least 1 item for DW_OP_not.");
  1376. + return false;
  1377. + } else {
  1378. + if (!stack.back().ResolveValue(m_exe_ctx).OnesComplement()) {
  1379. + if (error_ptr)
  1380. + error_ptr->SetErrorString("Logical NOT failed.");
  1381. + return false;
  1382. + }
  1383. + }
  1384. + break;
  1385. +
  1386. + // OPCODE: DW_OP_or
  1387. + // OPERANDS: none
  1388. + // DESCRIPTION: pops the top two stack entries, performs a bitwise or
  1389. + // operation on the two, and pushes the result.
  1390. + case DW_OP_or:
  1391. + if (stack.size() < 2) {
  1392. + if (error_ptr)
  1393. + error_ptr->SetErrorString(
  1394. + "Expression stack needs at least 2 items for DW_OP_or.");
  1395. + return false;
  1396. + } else {
  1397. + tmp = stack.back();
  1398. + stack.pop_back();
  1399. + stack.back().ResolveValue(m_exe_ctx) =
  1400. + stack.back().ResolveValue(m_exe_ctx) | tmp.ResolveValue(m_exe_ctx);
  1401. + }
  1402. + break;
  1403. +
  1404. + // OPCODE: DW_OP_plus
  1405. + // OPERANDS: none
  1406. + // DESCRIPTION: pops the top two stack entries, adds them together, and
  1407. + // pushes the result.
  1408. + case DW_OP_plus:
  1409. + if (stack.size() < 2) {
  1410. + if (error_ptr)
  1411. + error_ptr->SetErrorString(
  1412. + "Expression stack needs at least 2 items for DW_OP_plus.");
  1413. + return false;
  1414. + } else {
  1415. + tmp = stack.back();
  1416. + stack.pop_back();
  1417. + stack.back().GetScalar() += tmp.GetScalar();
  1418. + }
  1419. + break;
  1420. +
  1421. + // OPCODE: DW_OP_plus_uconst
  1422. + // OPERANDS: none
  1423. + // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
  1424. + // constant operand and pushes the result.
  1425. + case DW_OP_plus_uconst:
  1426. + if (stack.empty()) {
  1427. + if (error_ptr)
  1428. + error_ptr->SetErrorString(
  1429. + "Expression stack needs at least 1 item for DW_OP_plus_uconst.");
  1430. + return false;
  1431. + } else {
  1432. + const uint64_t uconst_value = opcodes.GetULEB128(&offset);
  1433. + // Implicit conversion from a UINT to a Scalar...
  1434. + stack.back().GetScalar() += uconst_value;
  1435. + if (!stack.back().GetScalar().IsValid()) {
  1436. + if (error_ptr)
  1437. + error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
  1438. + return false;
  1439. + }
  1440. + }
  1441. + break;
  1442. +
  1443. + // OPCODE: DW_OP_shl
  1444. + // OPERANDS: none
  1445. + // DESCRIPTION: pops the top two stack entries, shifts the former
  1446. + // second entry left by the number of bits specified by the former top of
  1447. + // the stack, and pushes the result.
  1448. + case DW_OP_shl:
  1449. + if (stack.size() < 2) {
  1450. + if (error_ptr)
  1451. + error_ptr->SetErrorString(
  1452. + "Expression stack needs at least 2 items for DW_OP_shl.");
  1453. + return false;
  1454. + } else {
  1455. + tmp = stack.back();
  1456. + stack.pop_back();
  1457. + stack.back().ResolveValue(m_exe_ctx) <<= tmp.ResolveValue(m_exe_ctx);
  1458. + }
  1459. + break;
  1460. +
  1461. + // OPCODE: DW_OP_shr
  1462. + // OPERANDS: none
  1463. + // DESCRIPTION: pops the top two stack entries, shifts the former second
  1464. + // entry right logically (filling with zero bits) by the number of bits
  1465. + // specified by the former top of the stack, and pushes the result.
  1466. + case DW_OP_shr:
  1467. + if (stack.size() < 2) {
  1468. + if (error_ptr)
  1469. + error_ptr->SetErrorString(
  1470. + "Expression stack needs at least 2 items for DW_OP_shr.");
  1471. + return false;
  1472. + } else {
  1473. + tmp = stack.back();
  1474. + stack.pop_back();
  1475. + if (!stack.back().ResolveValue(m_exe_ctx).ShiftRightLogical(
  1476. + tmp.ResolveValue(m_exe_ctx))) {
  1477. + if (error_ptr)
  1478. + error_ptr->SetErrorString("DW_OP_shr failed.");
  1479. + return false;
  1480. + }
  1481. + }
  1482. + break;
  1483. +
  1484. + // OPCODE: DW_OP_shra
  1485. + // OPERANDS: none
  1486. + // DESCRIPTION: pops the top two stack entries, shifts the former second
  1487. + // entry right arithmetically (divide the magnitude by 2, keep the same
  1488. + // sign for the result) by the number of bits specified by the former top
  1489. + // of the stack, and pushes the result.
  1490. + case DW_OP_shra:
  1491. + if (stack.size() < 2) {
  1492. + if (error_ptr)
  1493. + error_ptr->SetErrorString(
  1494. + "Expression stack needs at least 2 items for DW_OP_shra.");
  1495. + return false;
  1496. + } else {
  1497. + tmp = stack.back();
  1498. + stack.pop_back();
  1499. + stack.back().ResolveValue(m_exe_ctx) >>= tmp.ResolveValue(m_exe_ctx);
  1500. + }
  1501. + break;
  1502. +
  1503. + // OPCODE: DW_OP_xor
  1504. + // OPERANDS: none
  1505. + // DESCRIPTION: pops the top two stack entries, performs the bitwise
  1506. + // exclusive-or operation on the two, and pushes the result.
  1507. + case DW_OP_xor:
  1508. + if (stack.size() < 2) {
  1509. + if (error_ptr)
  1510. + error_ptr->SetErrorString(
  1511. + "Expression stack needs at least 2 items for DW_OP_xor.");
  1512. + return false;
  1513. + } else {
  1514. + tmp = stack.back();
  1515. + stack.pop_back();
  1516. + stack.back().ResolveValue(m_exe_ctx) =
  1517. + stack.back().ResolveValue(m_exe_ctx) ^ tmp.ResolveValue(m_exe_ctx);
  1518. + }
  1519. + break;
  1520. +
  1521. + // OPCODE: DW_OP_skip
  1522. + // OPERANDS: int16_t
  1523. + // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
  1524. + // signed integer constant. The 2-byte constant is the number of bytes of
  1525. + // the DWARF expression to skip forward or backward from the current
  1526. + // operation, beginning after the 2-byte constant.
  1527. + case DW_OP_skip: {
  1528. + int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
  1529. + lldb::offset_t new_offset = offset + skip_offset;
  1530. + if (opcodes.ValidOffset(new_offset))
  1531. + offset = new_offset;
  1532. + else {
  1533. + if (error_ptr)
  1534. + error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
  1535. + return false;
  1536. + }
  1537. + } break;
  1538. +
  1539. + // OPCODE: DW_OP_bra
  1540. + // OPERANDS: int16_t
  1541. + // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
  1542. + // signed integer constant. This operation pops the top of stack. If the
  1543. + // value popped is not the constant 0, the 2-byte constant operand is the
  1544. + // number of bytes of the DWARF expression to skip forward or backward from
  1545. + // the current operation, beginning after the 2-byte constant.
  1546. + case DW_OP_bra:
  1547. + if (stack.empty()) {
  1548. + if (error_ptr)
  1549. + error_ptr->SetErrorString(
  1550. + "Expression stack needs at least 1 item for DW_OP_bra.");
  1551. + return false;
  1552. + } else {
  1553. + tmp = stack.back();
  1554. + stack.pop_back();
  1555. + int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
  1556. + Scalar zero(0);
  1557. + if (tmp.ResolveValue(m_exe_ctx) != zero) {
  1558. + lldb::offset_t new_offset = offset + bra_offset;
  1559. + if (opcodes.ValidOffset(new_offset))
  1560. + offset = new_offset;
  1561. + else {
  1562. + if (error_ptr)
  1563. + error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
  1564. + return false;
  1565. + }
  1566. + }
  1567. + }
  1568. + break;
  1569. +
  1570. + // OPCODE: DW_OP_eq
  1571. + // OPERANDS: none
  1572. + // DESCRIPTION: pops the top two stack values, compares using the
  1573. + // equals (==) operator.
  1574. + // STACK RESULT: push the constant value 1 onto the stack if the result
  1575. + // of the operation is true or the constant value 0 if the result of the
  1576. + // operation is false.
  1577. + case DW_OP_eq:
  1578. + if (stack.size() < 2) {
  1579. + if (error_ptr)
  1580. + error_ptr->SetErrorString(
  1581. + "Expression stack needs at least 2 items for DW_OP_eq.");
  1582. + return false;
  1583. + } else {
  1584. + tmp = stack.back();
  1585. + stack.pop_back();
  1586. + stack.back().ResolveValue(m_exe_ctx) =
  1587. + stack.back().ResolveValue(m_exe_ctx) == tmp.ResolveValue(m_exe_ctx);
  1588. + }
  1589. + break;
  1590. +
  1591. + // OPCODE: DW_OP_ge
  1592. + // OPERANDS: none
  1593. + // DESCRIPTION: pops the top two stack values, compares using the
  1594. + // greater than or equal to (>=) operator.
  1595. + // STACK RESULT: push the constant value 1 onto the stack if the result
  1596. + // of the operation is true or the constant value 0 if the result of the
  1597. + // operation is false.
  1598. + case DW_OP_ge:
  1599. + if (stack.size() < 2) {
  1600. + if (error_ptr)
  1601. + error_ptr->SetErrorString(
  1602. + "Expression stack needs at least 2 items for DW_OP_ge.");
  1603. + return false;
  1604. + } else {
  1605. + tmp = stack.back();
  1606. + stack.pop_back();
  1607. + stack.back().ResolveValue(m_exe_ctx) =
  1608. + stack.back().ResolveValue(m_exe_ctx) >= tmp.ResolveValue(m_exe_ctx);
  1609. + }
  1610. + break;
  1611. +
  1612. + // OPCODE: DW_OP_gt
  1613. + // OPERANDS: none
  1614. + // DESCRIPTION: pops the top two stack values, compares using the
  1615. + // greater than (>) operator.
  1616. + // STACK RESULT: push the constant value 1 onto the stack if the result
  1617. + // of the operation is true or the constant value 0 if the result of the
  1618. + // operation is false.
  1619. + case DW_OP_gt:
  1620. + if (stack.size() < 2) {
  1621. + if (error_ptr)
  1622. + error_ptr->SetErrorString(
  1623. + "Expression stack needs at least 2 items for DW_OP_gt.");
  1624. + return false;
  1625. + } else {
  1626. + tmp = stack.back();
  1627. + stack.pop_back();
  1628. + stack.back().ResolveValue(m_exe_ctx) =
  1629. + stack.back().ResolveValue(m_exe_ctx) > tmp.ResolveValue(m_exe_ctx);
  1630. + }
  1631. + break;
  1632. +
  1633. + // OPCODE: DW_OP_le
  1634. + // OPERANDS: none
  1635. + // DESCRIPTION: pops the top two stack values, compares using the
  1636. + // less than or equal to (<=) operator.
  1637. + // STACK RESULT: push the constant value 1 onto the stack if the result
  1638. + // of the operation is true or the constant value 0 if the result of the
  1639. + // operation is false.
  1640. + case DW_OP_le:
  1641. + if (stack.size() < 2) {
  1642. + if (error_ptr)
  1643. + error_ptr->SetErrorString(
  1644. + "Expression stack needs at least 2 items for DW_OP_le.");
  1645. + return false;
  1646. + } else {
  1647. + tmp = stack.back();
  1648. + stack.pop_back();
  1649. + stack.back().ResolveValue(m_exe_ctx) =
  1650. + stack.back().ResolveValue(m_exe_ctx) <= tmp.ResolveValue(m_exe_ctx);
  1651. + }
  1652. + break;
  1653. +
  1654. + // OPCODE: DW_OP_lt
  1655. + // OPERANDS: none
  1656. + // DESCRIPTION: pops the top two stack values, compares using the
  1657. + // less than (<) operator.
  1658. + // STACK RESULT: push the constant value 1 onto the stack if the result
  1659. + // of the operation is true or the constant value 0 if the result of the
  1660. + // operation is false.
  1661. + case DW_OP_lt:
  1662. + if (stack.size() < 2) {
  1663. + if (error_ptr)
  1664. + error_ptr->SetErrorString(
  1665. + "Expression stack needs at least 2 items for DW_OP_lt.");
  1666. + return false;
  1667. + } else {
  1668. + tmp = stack.back();
  1669. + stack.pop_back();
  1670. + stack.back().ResolveValue(m_exe_ctx) =
  1671. + stack.back().ResolveValue(m_exe_ctx) < tmp.ResolveValue(m_exe_ctx);
  1672. + }
  1673. + break;
  1674. +
  1675. + // OPCODE: DW_OP_ne
  1676. + // OPERANDS: none
  1677. + // DESCRIPTION: pops the top two stack values, compares using the
  1678. + // not equal (!=) operator.
  1679. + // STACK RESULT: push the constant value 1 onto the stack if the result
  1680. + // of the operation is true or the constant value 0 if the result of the
  1681. + // operation is false.
  1682. + case DW_OP_ne:
  1683. + if (stack.size() < 2) {
  1684. + if (error_ptr)
  1685. + error_ptr->SetErrorString(
  1686. + "Expression stack needs at least 2 items for DW_OP_ne.");
  1687. + return false;
  1688. + } else {
  1689. + tmp = stack.back();
  1690. + stack.pop_back();
  1691. + stack.back().ResolveValue(m_exe_ctx) =
  1692. + stack.back().ResolveValue(m_exe_ctx) != tmp.ResolveValue(m_exe_ctx);
  1693. + }
  1694. + break;
  1695. +
  1696. + // OPCODE: DW_OP_litn
  1697. + // OPERANDS: none
  1698. + // DESCRIPTION: encode the unsigned literal values from 0 through 31.
  1699. + // STACK RESULT: push the unsigned literal constant value onto the top
  1700. + // of the stack.
  1701. + case DW_OP_lit0:
  1702. + case DW_OP_lit1:
  1703. + case DW_OP_lit2:
  1704. + case DW_OP_lit3:
  1705. + case DW_OP_lit4:
  1706. + case DW_OP_lit5:
  1707. + case DW_OP_lit6:
  1708. + case DW_OP_lit7:
  1709. + case DW_OP_lit8:
  1710. + case DW_OP_lit9:
  1711. + case DW_OP_lit10:
  1712. + case DW_OP_lit11:
  1713. + case DW_OP_lit12:
  1714. + case DW_OP_lit13:
  1715. + case DW_OP_lit14:
  1716. + case DW_OP_lit15:
  1717. + case DW_OP_lit16:
  1718. + case DW_OP_lit17:
  1719. + case DW_OP_lit18:
  1720. + case DW_OP_lit19:
  1721. + case DW_OP_lit20:
  1722. + case DW_OP_lit21:
  1723. + case DW_OP_lit22:
  1724. + case DW_OP_lit23:
  1725. + case DW_OP_lit24:
  1726. + case DW_OP_lit25:
  1727. + case DW_OP_lit26:
  1728. + case DW_OP_lit27:
  1729. + case DW_OP_lit28:
  1730. + case DW_OP_lit29:
  1731. + case DW_OP_lit30:
  1732. + case DW_OP_lit31:
  1733. + stack.push_back(Scalar((uint64_t)(op - DW_OP_lit0)));
  1734. + break;
  1735. +
  1736. + // OPCODE: DW_OP_regN
  1737. + // OPERANDS: none
  1738. + // DESCRIPTION: Push the value in register n on the top of the stack.
  1739. + case DW_OP_reg0:
  1740. + case DW_OP_reg1:
  1741. + case DW_OP_reg2:
  1742. + case DW_OP_reg3:
  1743. + case DW_OP_reg4:
  1744. + case DW_OP_reg5:
  1745. + case DW_OP_reg6:
  1746. + case DW_OP_reg7:
  1747. + case DW_OP_reg8:
  1748. + case DW_OP_reg9:
  1749. + case DW_OP_reg10:
  1750. + case DW_OP_reg11:
  1751. + case DW_OP_reg12:
  1752. + case DW_OP_reg13:
  1753. + case DW_OP_reg14:
  1754. + case DW_OP_reg15:
  1755. + case DW_OP_reg16:
  1756. + case DW_OP_reg17:
  1757. + case DW_OP_reg18:
  1758. + case DW_OP_reg19:
  1759. + case DW_OP_reg20:
  1760. + case DW_OP_reg21:
  1761. + case DW_OP_reg22:
  1762. + case DW_OP_reg23:
  1763. + case DW_OP_reg24:
  1764. + case DW_OP_reg25:
  1765. + case DW_OP_reg26:
  1766. + case DW_OP_reg27:
  1767. + case DW_OP_reg28:
  1768. + case DW_OP_reg29:
  1769. + case DW_OP_reg30:
  1770. + case DW_OP_reg31: {
  1771. + reg_num = op - DW_OP_reg0;
  1772. +
  1773. + if (ReadRegisterValueAsScalar(m_reg_ctx, reg_kind, reg_num, error_ptr, tmp))
  1774. + stack.push_back(tmp);
  1775. + else
  1776. + return false;
  1777. + } break;
  1778. + // OPCODE: DW_OP_regx
  1779. + // OPERANDS:
  1780. + // ULEB128 literal operand that encodes the register.
  1781. + // DESCRIPTION: Push the value in register on the top of the stack.
  1782. + case DW_OP_regx: {
  1783. + reg_num = opcodes.GetULEB128(&offset);
  1784. + if (ReadRegisterValueAsScalar(m_reg_ctx, reg_kind, reg_num, error_ptr, tmp))
  1785. + stack.push_back(tmp);
  1786. + else
  1787. + return false;
  1788. + } break;
  1789. +
  1790. + // OPCODE: DW_OP_bregN
  1791. + // OPERANDS:
  1792. + // SLEB128 offset from register N
  1793. + // DESCRIPTION: Value is in memory at the address specified by register
  1794. + // N plus an offset.
  1795. + case DW_OP_breg0:
  1796. + case DW_OP_breg1:
  1797. + case DW_OP_breg2:
  1798. + case DW_OP_breg3:
  1799. + case DW_OP_breg4:
  1800. + case DW_OP_breg5:
  1801. + case DW_OP_breg6:
  1802. + case DW_OP_breg7:
  1803. + case DW_OP_breg8:
  1804. + case DW_OP_breg9:
  1805. + case DW_OP_breg10:
  1806. + case DW_OP_breg11:
  1807. + case DW_OP_breg12:
  1808. + case DW_OP_breg13:
  1809. + case DW_OP_breg14:
  1810. + case DW_OP_breg15:
  1811. + case DW_OP_breg16:
  1812. + case DW_OP_breg17:
  1813. + case DW_OP_breg18:
  1814. + case DW_OP_breg19:
  1815. + case DW_OP_breg20:
  1816. + case DW_OP_breg21:
  1817. + case DW_OP_breg22:
  1818. + case DW_OP_breg23:
  1819. + case DW_OP_breg24:
  1820. + case DW_OP_breg25:
  1821. + case DW_OP_breg26:
  1822. + case DW_OP_breg27:
  1823. + case DW_OP_breg28:
  1824. + case DW_OP_breg29:
  1825. + case DW_OP_breg30:
  1826. + case DW_OP_breg31: {
  1827. + reg_num = op - DW_OP_breg0;
  1828. +
  1829. + if (ReadRegisterValueAsScalar(m_reg_ctx, reg_kind, reg_num, error_ptr,
  1830. + tmp)) {
  1831. + int64_t breg_offset = opcodes.GetSLEB128(&offset);
  1832. + tmp.ResolveValue(m_exe_ctx) += (uint64_t)breg_offset;
  1833. + tmp.ClearContext();
  1834. + stack.push_back(tmp);
  1835. + stack.back().SetValueType(Value::ValueType::LoadAddress);
  1836. + } else
  1837. + return false;
  1838. + } break;
  1839. + // OPCODE: DW_OP_bregx
  1840. + // OPERANDS: 2
  1841. + // ULEB128 literal operand that encodes the register.
  1842. + // SLEB128 offset from register N
  1843. + // DESCRIPTION: Value is in memory at the address specified by register
  1844. + // N plus an offset.
  1845. + case DW_OP_bregx: {
  1846. + reg_num = opcodes.GetULEB128(&offset);
  1847. +
  1848. + if (ReadRegisterValueAsScalar(m_reg_ctx, reg_kind, reg_num, error_ptr,
  1849. + tmp)) {
  1850. + int64_t breg_offset = opcodes.GetSLEB128(&offset);
  1851. + tmp.ResolveValue(m_exe_ctx) += (uint64_t)breg_offset;
  1852. + tmp.ClearContext();
  1853. + stack.push_back(tmp);
  1854. + stack.back().SetValueType(Value::ValueType::LoadAddress);
  1855. + } else
  1856. + return false;
  1857. + } break;
  1858. +
  1859. + case DW_OP_fbreg:
  1860. + if (m_exe_ctx) {
  1861. + if (frame) {
  1862. + Scalar value;
  1863. + if (frame->GetFrameBaseValue(value, error_ptr)) {
  1864. + int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
  1865. + value += fbreg_offset;
  1866. + stack.push_back(value);
  1867. + stack.back().SetValueType(Value::ValueType::LoadAddress);
  1868. + } else
  1869. + return false;
  1870. + } else {
  1871. + if (error_ptr)
  1872. + error_ptr->SetErrorString(
  1873. + "Invalid stack frame in context for DW_OP_fbreg opcode.");
  1874. + return false;
  1875. + }
  1876. + } else {
  1877. + if (error_ptr)
  1878. + error_ptr->SetErrorStringWithFormat(
  1879. + "NULL execution context for DW_OP_fbreg.\n");
  1880. + return false;
  1881. + }
  1882. +
  1883. + break;
  1884. +
  1885. + // OPCODE: DW_OP_nop
  1886. + // OPERANDS: none
  1887. + // DESCRIPTION: A place holder. It has no effect on the location stack
  1888. + // or any of its values.
  1889. + case DW_OP_nop:
  1890. + break;
  1891. +
  1892. + // OPCODE: DW_OP_piece
  1893. + // OPERANDS: 1
  1894. + // ULEB128: byte size of the piece
  1895. + // DESCRIPTION: The operand describes the size in bytes of the piece of
  1896. + // the object referenced by the DWARF expression whose result is at the top
  1897. + // of the stack. If the piece is located in a register, but does not occupy
  1898. + // the entire register, the placement of the piece within that register is
  1899. + // defined by the ABI.
  1900. + //
  1901. + // Many compilers store a single variable in sets of registers, or store a
  1902. + // variable partially in memory and partially in registers. DW_OP_piece
  1903. + // provides a way of describing how large a part of a variable a particular
  1904. + // DWARF expression refers to.
  1905. + case DW_OP_piece: {
  1906. + const uint64_t piece_byte_size = opcodes.GetULEB128(&offset);
  1907. +
  1908. + if (piece_byte_size > 0) {
  1909. + Value curr_piece;
  1910. +
  1911. + if (stack.empty()) {
  1912. + // In a multi-piece expression, this means that the current piece is
  1913. + // not available. Fill with zeros for now by resizing the data and
  1914. + // appending it
  1915. + curr_piece.ResizeData(piece_byte_size);
  1916. + // Note that "0" is not a correct value for the unknown bits.
  1917. + // It would be better to also return a mask of valid bits together
  1918. + // with the expression result, so the debugger can print missing
  1919. + // members as "<optimized out>" or something.
  1920. + ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
  1921. + pieces.AppendDataToHostBuffer(curr_piece);
  1922. + } else {
  1923. + Status error;
  1924. + // Extract the current piece into "curr_piece"
  1925. + Value curr_piece_source_value(stack.back());
  1926. + stack.pop_back();
  1927. +
  1928. + const Value::ValueType curr_piece_source_value_type =
  1929. + curr_piece_source_value.GetValueType();
  1930. + switch (curr_piece_source_value_type) {
  1931. + case Value::ValueType::LoadAddress:
  1932. + if (process) {
  1933. + if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
  1934. + lldb::addr_t load_addr =
  1935. + curr_piece_source_value.GetScalar().ULongLong(
  1936. + LLDB_INVALID_ADDRESS);
  1937. + if (process->ReadMemory(
  1938. + load_addr, curr_piece.GetBuffer().GetBytes(),
  1939. + piece_byte_size, error) != piece_byte_size) {
  1940. + if (error_ptr)
  1941. + error_ptr->SetErrorStringWithFormat(
  1942. + "failed to read memory DW_OP_piece(%" PRIu64
  1943. + ") from 0x%" PRIx64,
  1944. + piece_byte_size, load_addr);
  1945. + return false;
  1946. + }
  1947. + } else {
  1948. + if (error_ptr)
  1949. + error_ptr->SetErrorStringWithFormat(
  1950. + "failed to resize the piece memory buffer for "
  1951. + "DW_OP_piece(%" PRIu64 ")",
  1952. + piece_byte_size);
  1953. + return false;
  1954. + }
  1955. + }
  1956. + break;
  1957. +
  1958. + case Value::ValueType::FileAddress:
  1959. + case Value::ValueType::HostAddress:
  1960. + if (error_ptr) {
  1961. + lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong(
  1962. + LLDB_INVALID_ADDRESS);
  1963. + error_ptr->SetErrorStringWithFormat(
  1964. + "failed to read memory DW_OP_piece(%" PRIu64
  1965. + ") from %s address 0x%" PRIx64,
  1966. + piece_byte_size,
  1967. + curr_piece_source_value.GetValueType() ==
  1968. + Value::ValueType::FileAddress
  1969. + ? "file"
  1970. + : "host",
  1971. + addr);
  1972. + }
  1973. + return false;
  1974. +
  1975. + case Value::ValueType::Scalar: {
  1976. + uint32_t bit_size = piece_byte_size * 8;
  1977. + uint32_t bit_offset = 0;
  1978. + Scalar &scalar = curr_piece_source_value.GetScalar();
  1979. + if (!scalar.ExtractBitfield(bit_size, bit_offset)) {
  1980. + if (error_ptr)
  1981. + error_ptr->SetErrorStringWithFormat(
  1982. + "unable to extract %" PRIu64 " bytes from a %" PRIu64
  1983. + " byte scalar value.",
  1984. + piece_byte_size,
  1985. + (uint64_t)curr_piece_source_value.GetScalar().GetByteSize());
  1986. + return false;
  1987. + }
  1988. + // Create curr_piece with bit_size. By default Scalar
  1989. + // grows to the nearest host integer type.
  1990. + llvm::APInt fail_value(1, 0, false);
  1991. + llvm::APInt ap_int = scalar.UInt128(fail_value);
  1992. + assert(ap_int.getBitWidth() >= bit_size);
  1993. + llvm::ArrayRef<uint64_t> buf{ap_int.getRawData(),
  1994. + ap_int.getNumWords()};
  1995. + curr_piece.GetScalar() = Scalar(llvm::APInt(bit_size, buf));
  1996. + } break;
  1997. + }
  1998. +
  1999. + // Check if this is the first piece?
  2000. + if (op_piece_offset == 0) {
  2001. + // This is the first piece, we should push it back onto the stack
  2002. + // so subsequent pieces will be able to access this piece and add
  2003. + // to it.
  2004. + if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
  2005. + if (error_ptr)
  2006. + error_ptr->SetErrorString("failed to append piece data");
  2007. + return false;
  2008. + }
  2009. + } else {
  2010. + // If this is the second or later piece there should be a value on
  2011. + // the stack.
  2012. + if (pieces.GetBuffer().GetByteSize() != op_piece_offset) {
  2013. + if (error_ptr)
  2014. + error_ptr->SetErrorStringWithFormat(
  2015. + "DW_OP_piece for offset %" PRIu64
  2016. + " but top of stack is of size %" PRIu64,
  2017. + op_piece_offset, pieces.GetBuffer().GetByteSize());
  2018. + return false;
  2019. + }
  2020. +
  2021. + if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
  2022. + if (error_ptr)
  2023. + error_ptr->SetErrorString("failed to append piece data");
  2024. + return false;
  2025. + }
  2026. + }
  2027. + }
  2028. + op_piece_offset += piece_byte_size;
  2029. + }
  2030. + } break;
  2031. +
  2032. + case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
  2033. + if (stack.size() < 1) {
  2034. + if (error_ptr)
  2035. + error_ptr->SetErrorString(
  2036. + "Expression stack needs at least 1 item for DW_OP_bit_piece.");
  2037. + return false;
  2038. + } else {
  2039. + const uint64_t piece_bit_size = opcodes.GetULEB128(&offset);
  2040. + const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset);
  2041. + switch (stack.back().GetValueType()) {
  2042. + case Value::ValueType::Scalar: {
  2043. + if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
  2044. + piece_bit_offset)) {
  2045. + if (error_ptr)
  2046. + error_ptr->SetErrorStringWithFormat(
  2047. + "unable to extract %" PRIu64 " bit value with %" PRIu64
  2048. + " bit offset from a %" PRIu64 " bit scalar value.",
  2049. + piece_bit_size, piece_bit_offset,
  2050. + (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
  2051. + return false;
  2052. + }
  2053. + } break;
  2054. +
  2055. + case Value::ValueType::FileAddress:
  2056. + case Value::ValueType::LoadAddress:
  2057. + case Value::ValueType::HostAddress:
  2058. + if (error_ptr) {
  2059. + error_ptr->SetErrorStringWithFormat(
  2060. + "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
  2061. + ", bit_offset = %" PRIu64 ") from an address value.",
  2062. + piece_bit_size, piece_bit_offset);
  2063. + }
  2064. + return false;
  2065. + }
  2066. + }
  2067. + break;
  2068. +
  2069. + // OPCODE: DW_OP_push_object_address
  2070. + // OPERANDS: none
  2071. + // DESCRIPTION: Pushes the address of the object currently being
  2072. + // evaluated as part of evaluation of a user presented expression. This
  2073. + // object may correspond to an independent variable described by its own
  2074. + // DIE or it may be a component of an array, structure, or class whose
  2075. + // address has been dynamically determined by an earlier step during user
  2076. + // expression evaluation.
  2077. + case DW_OP_push_object_address:
  2078. + if (m_object_address_ptr)
  2079. + stack.push_back(*m_object_address_ptr);
  2080. + else {
  2081. + if (error_ptr)
  2082. + error_ptr->SetErrorString("DW_OP_push_object_address used without "
  2083. + "specifying an object address");
  2084. + return false;
  2085. + }
  2086. + break;
  2087. +
  2088. + // OPCODE: DW_OP_call2
  2089. + // OPERANDS:
  2090. + // uint16_t compile unit relative offset of a DIE
  2091. + // DESCRIPTION: Performs subroutine calls during evaluation
  2092. + // of a DWARF expression. The operand is the 2-byte unsigned offset of a
  2093. + // debugging information entry in the current compilation unit.
  2094. + //
  2095. + // Operand interpretation is exactly like that for DW_FORM_ref2.
  2096. + //
  2097. + // This operation transfers control of DWARF expression evaluation to the
  2098. + // DW_AT_location attribute of the referenced DIE. If there is no such
  2099. + // attribute, then there is no effect. Execution of the DWARF expression of
  2100. + // a DW_AT_location attribute may add to and/or remove from values on the
  2101. + // stack. Execution returns to the point following the call when the end of
  2102. + // the attribute is reached. Values on the stack at the time of the call
  2103. + // may be used as parameters by the called expression and values left on
  2104. + // the stack by the called expression may be used as return values by prior
  2105. + // agreement between the calling and called expressions.
  2106. + case DW_OP_call2:
  2107. + if (error_ptr)
  2108. + error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2.");
  2109. + return false;
  2110. + // OPCODE: DW_OP_call4
  2111. + // OPERANDS: 1
  2112. + // uint32_t compile unit relative offset of a DIE
  2113. + // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
  2114. + // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of
  2115. + // a debugging information entry in the current compilation unit.
  2116. + //
  2117. + // Operand interpretation DW_OP_call4 is exactly like that for
  2118. + // DW_FORM_ref4.
  2119. + //
  2120. + // This operation transfers control of DWARF expression evaluation to the
  2121. + // DW_AT_location attribute of the referenced DIE. If there is no such
  2122. + // attribute, then there is no effect. Execution of the DWARF expression of
  2123. + // a DW_AT_location attribute may add to and/or remove from values on the
  2124. + // stack. Execution returns to the point following the call when the end of
  2125. + // the attribute is reached. Values on the stack at the time of the call
  2126. + // may be used as parameters by the called expression and values left on
  2127. + // the stack by the called expression may be used as return values by prior
  2128. + // agreement between the calling and called expressions.
  2129. + case DW_OP_call4:
  2130. + if (error_ptr)
  2131. + error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4.");
  2132. + return false;
  2133. +
  2134. + // OPCODE: DW_OP_stack_value
  2135. + // OPERANDS: None
  2136. + // DESCRIPTION: Specifies that the object does not exist in memory but
  2137. + // rather is a constant value. The value from the top of the stack is the
  2138. + // value to be used. This is the actual object value and not the location.
  2139. + case DW_OP_stack_value:
  2140. + if (stack.empty()) {
  2141. + if (error_ptr)
  2142. + error_ptr->SetErrorString(
  2143. + "Expression stack needs at least 1 item for DW_OP_stack_value.");
  2144. + return false;
  2145. + }
  2146. + stack.back().SetValueType(Value::ValueType::Scalar);
  2147. + break;
  2148. +
  2149. + // OPCODE: DW_OP_convert
  2150. + // OPERANDS: 1
  2151. + // A ULEB128 that is either a DIE offset of a
  2152. + // DW_TAG_base_type or 0 for the generic (pointer-sized) type.
  2153. + //
  2154. + // DESCRIPTION: Pop the top stack element, convert it to a
  2155. + // different type, and push the result.
  2156. + case DW_OP_convert: {
  2157. + if (stack.size() < 1) {
  2158. + if (error_ptr)
  2159. + error_ptr->SetErrorString(
  2160. + "Expression stack needs at least 1 item for DW_OP_convert.");
  2161. + return false;
  2162. + }
  2163. + const uint64_t die_offset = opcodes.GetULEB128(&offset);
  2164. + uint64_t bit_size;
  2165. + bool sign;
  2166. + if (die_offset == 0) {
  2167. + // The generic type has the size of an address on the target
  2168. + // machine and an unspecified signedness. Scalar has no
  2169. + // "unspecified signedness", so we use unsigned types.
  2170. + if (!module_sp) {
  2171. + if (error_ptr)
  2172. + error_ptr->SetErrorString("No module");
  2173. + return false;
  2174. + }
  2175. + sign = false;
  2176. + bit_size = module_sp->GetArchitecture().GetAddressByteSize() * 8;
  2177. + if (!bit_size) {
  2178. + if (error_ptr)
  2179. + error_ptr->SetErrorString("unspecified architecture");
  2180. + return false;
  2181. + }
  2182. + } else {
  2183. + // Retrieve the type DIE that the value is being converted to.
  2184. + // FIXME: the constness has annoying ripple effects.
  2185. + DWARFDIE die = const_cast<DWARFUnit *>(dwarf_cu)->GetDIE(die_offset);
  2186. + if (!die) {
  2187. + if (error_ptr)
  2188. + error_ptr->SetErrorString("Cannot resolve DW_OP_convert type DIE");
  2189. + return false;
  2190. + }
  2191. + uint64_t encoding =
  2192. + die.GetAttributeValueAsUnsigned(DW_AT_encoding, DW_ATE_hi_user);
  2193. + bit_size = die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8;
  2194. + if (!bit_size)
  2195. + bit_size = die.GetAttributeValueAsUnsigned(DW_AT_bit_size, 0);
  2196. + if (!bit_size) {
  2197. + if (error_ptr)
  2198. + error_ptr->SetErrorString("Unsupported type size in DW_OP_convert");
  2199. + return false;
  2200. + }
  2201. + switch (encoding) {
  2202. + case DW_ATE_signed:
  2203. + case DW_ATE_signed_char:
  2204. + sign = true;
  2205. + break;
  2206. + case DW_ATE_unsigned:
  2207. + case DW_ATE_unsigned_char:
  2208. + sign = false;
  2209. + break;
  2210. + default:
  2211. + if (error_ptr)
  2212. + error_ptr->SetErrorString("Unsupported encoding in DW_OP_convert");
  2213. + return false;
  2214. + }
  2215. + }
  2216. + Scalar &top = stack.back().ResolveValue(m_exe_ctx);
  2217. + top.TruncOrExtendTo(bit_size, sign);
  2218. + break;
  2219. + }
  2220. +
  2221. + // OPCODE: DW_OP_call_frame_cfa
  2222. + // OPERANDS: None
  2223. + // DESCRIPTION: Specifies a DWARF expression that pushes the value of
  2224. + // the canonical frame address consistent with the call frame information
  2225. + // located in .debug_frame (or in the FDEs of the eh_frame section).
  2226. + case DW_OP_call_frame_cfa:
  2227. + if (frame) {
  2228. + // Note that we don't have to parse FDEs because this DWARF expression
  2229. + // is commonly evaluated with a valid stack frame.
  2230. + StackID id = frame->GetStackID();
  2231. + addr_t cfa = id.GetCallFrameAddress();
  2232. + if (cfa != LLDB_INVALID_ADDRESS) {
  2233. + stack.push_back(Scalar(cfa));
  2234. + stack.back().SetValueType(Value::ValueType::LoadAddress);
  2235. + } else if (error_ptr)
  2236. + error_ptr->SetErrorString("Stack frame does not include a canonical "
  2237. + "frame address for DW_OP_call_frame_cfa "
  2238. + "opcode.");
  2239. + } else {
  2240. + if (error_ptr)
  2241. + error_ptr->SetErrorString("Invalid stack frame in context for "
  2242. + "DW_OP_call_frame_cfa opcode.");
  2243. + return false;
  2244. + }
  2245. + break;
  2246. +
  2247. + // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
  2248. + // opcode, DW_OP_GNU_push_tls_address)
  2249. + // OPERANDS: none
  2250. + // DESCRIPTION: Pops a TLS offset from the stack, converts it to
  2251. + // an address in the current thread's thread-local storage block, and
  2252. + // pushes it on the stack.
  2253. + case DW_OP_form_tls_address:
  2254. + case DW_OP_GNU_push_tls_address: {
  2255. + if (stack.size() < 1) {
  2256. + if (error_ptr) {
  2257. + if (op == DW_OP_form_tls_address)
  2258. + error_ptr->SetErrorString(
  2259. + "DW_OP_form_tls_address needs an argument.");
  2260. + else
  2261. + error_ptr->SetErrorString(
  2262. + "DW_OP_GNU_push_tls_address needs an argument.");
  2263. + }
  2264. + return false;
  2265. + }
  2266. +
  2267. + if (!m_exe_ctx || !module_sp) {
  2268. + if (error_ptr)
  2269. + error_ptr->SetErrorString("No context to evaluate TLS within.");
  2270. + return false;
  2271. + }
  2272. +
  2273. + Thread *thread = m_exe_ctx->GetThreadPtr();
  2274. + if (!thread) {
  2275. + if (error_ptr)
  2276. + error_ptr->SetErrorString("No thread to evaluate TLS within.");
  2277. + return false;
  2278. + }
  2279. +
  2280. + // Lookup the TLS block address for this thread and module.
  2281. + const addr_t tls_file_addr =
  2282. + stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
  2283. + const addr_t tls_load_addr =
  2284. + thread->GetThreadLocalData(module_sp, tls_file_addr);
  2285. +
  2286. + if (tls_load_addr == LLDB_INVALID_ADDRESS) {
  2287. + if (error_ptr)
  2288. + error_ptr->SetErrorString(
  2289. + "No TLS data currently exists for this thread.");
  2290. + return false;
  2291. + }
  2292. +
  2293. + stack.back().GetScalar() = tls_load_addr;
  2294. + stack.back().SetValueType(Value::ValueType::LoadAddress);
  2295. + } break;
  2296. +
  2297. + // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.)
  2298. + // OPERANDS: 1
  2299. + // ULEB128: index to the .debug_addr section
  2300. + // DESCRIPTION: Pushes an address to the stack from the .debug_addr
  2301. + // section with the base address specified by the DW_AT_addr_base attribute
  2302. + // and the 0 based index is the ULEB128 encoded index.
  2303. + case DW_OP_addrx:
  2304. + case DW_OP_GNU_addr_index: {
  2305. + if (!dwarf_cu) {
  2306. + if (error_ptr)
  2307. + error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a "
  2308. + "compile unit being specified");
  2309. + return false;
  2310. + }
  2311. + uint64_t index = opcodes.GetULEB128(&offset);
  2312. + lldb::addr_t value =
  2313. + DWARFExpression::ReadAddressFromDebugAddrSection(dwarf_cu, index);
  2314. + stack.push_back(Scalar(value));
  2315. + stack.back().SetValueType(Value::ValueType::FileAddress);
  2316. + } break;
  2317. +
  2318. + // OPCODE: DW_OP_GNU_const_index
  2319. + // OPERANDS: 1
  2320. + // ULEB128: index to the .debug_addr section
  2321. + // DESCRIPTION: Pushes an constant with the size of a machine address to
  2322. + // the stack from the .debug_addr section with the base address specified
  2323. + // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
  2324. + // encoded index.
  2325. + case DW_OP_GNU_const_index: {
  2326. + if (!dwarf_cu) {
  2327. + if (error_ptr)
  2328. + error_ptr->SetErrorString("DW_OP_GNU_const_index found without a "
  2329. + "compile unit being specified");
  2330. + return false;
  2331. + }
  2332. + uint64_t index = opcodes.GetULEB128(&offset);
  2333. + lldb::addr_t value =
  2334. + DWARFExpression::ReadAddressFromDebugAddrSection(dwarf_cu, index);
  2335. + stack.push_back(Scalar(value));
  2336. + } break;
  2337. +
  2338. + case DW_OP_entry_value: {
  2339. + if (!Evaluate_DW_OP_entry_value(stack, m_exe_ctx, m_reg_ctx, opcodes,
  2340. + offset, error_ptr, log)) {
  2341. + LLDB_ERRORF(error_ptr, "Could not evaluate %s.", DW_OP_value_to_name(op));
  2342. + return false;
  2343. + }
  2344. + break;
  2345. + }
  2346. +
  2347. + default:
  2348. + LLDB_LOGF(log, "Unhandled opcode %s in DWARFExpression.",
  2349. + DW_OP_value_to_name(op));
  2350. + break;
  2351. + }
  2352. +
  2353. + return true;
  2354. +}
  2355. diff --git a/lldb/source/Expression/DWARFEvaluatorFactory.cpp b/lldb/source/Expression/DWARFEvaluatorFactory.cpp
  2356. new file mode 100644
  2357. index 000000000..c06126412
  2358. --- /dev/null
  2359. +++ b/lldb/source/Expression/DWARFEvaluatorFactory.cpp
  2360. @@ -0,0 +1,57 @@
  2361. +//===-- DWARFEvaluatorFactory.cpp -----------------------------------------===//
  2362. +//
  2363. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  2364. +// See https://llvm.org/LICENSE.txt for license information.
  2365. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  2366. +//
  2367. +//===----------------------------------------------------------------------===//
  2368. +
  2369. +#include "lldb/Expression/DWARFEvaluatorFactory.h"
  2370. +#include "lldb/Expression/DWARFEvaluator.h"
  2371. +
  2372. +#include "lldb/Core/PluginManager.h"
  2373. +#include "lldb/Core/Value.h"
  2374. +#include "lldb/Target/RegisterContext.h"
  2375. +
  2376. +using namespace lldb;
  2377. +using namespace lldb_private;
  2378. +
  2379. +// PluginInterface protocol
  2380. +lldb_private::ConstString DWARFEvaluatorFactory::GetPluginName() {
  2381. + static ConstString g_name("vendor-default");
  2382. + return g_name;
  2383. +}
  2384. +
  2385. +// FindPlugin
  2386. +//
  2387. +// Platforms can register a callback to use when creating DWARF expression
  2388. +// evaluators to allow handling platform-specific DWARF codes.
  2389. +std::unique_ptr<DWARFEvaluatorFactory>
  2390. +DWARFEvaluatorFactory::FindPlugin(Module *module) {
  2391. + std::unique_ptr<DWARFEvaluatorFactory> instance_up;
  2392. + DWARFEvaluatorFactoryCreateInstance create_callback;
  2393. +
  2394. + for (size_t idx = 0;
  2395. + (create_callback =
  2396. + PluginManager::GetDWARFEvaluatorFactoryCreateCallbackAtIndex(
  2397. + idx)) != nullptr;
  2398. + ++idx) {
  2399. + instance_up.reset(create_callback(module));
  2400. +
  2401. + if (instance_up) {
  2402. + return instance_up;
  2403. + }
  2404. + }
  2405. +
  2406. + instance_up.reset(new DWARFEvaluatorFactory());
  2407. + return instance_up;
  2408. +}
  2409. +
  2410. +std::unique_ptr<DWARFEvaluator> DWARFEvaluatorFactory::CreateDWARFEvaluator(
  2411. + const DWARFExpression &dwarf_expression, ExecutionContext *exe_ctx,
  2412. + RegisterContext *reg_ctx, const Value *initial_value_ptr,
  2413. + const Value *object_address_ptr) {
  2414. + return std::make_unique<DWARFEvaluator>(dwarf_expression, exe_ctx, reg_ctx,
  2415. + initial_value_ptr,
  2416. + object_address_ptr);
  2417. +}
  2418. diff --git a/lldb/source/Expression/DWARFExpression.cpp b/lldb/source/Expression/DWARFExpression.cpp
  2419. index a10546c1d..4d13e4642 100644
  2420. --- a/lldb/source/Expression/DWARFExpression.cpp
  2421. +++ b/lldb/source/Expression/DWARFExpression.cpp
  2422. @@ -15,6 +15,8 @@
  2423. #include "lldb/Core/Module.h"
  2424. #include "lldb/Core/Value.h"
  2425. #include "lldb/Core/dwarf.h"
  2426. +#include "lldb/Expression/DWARFEvaluator.h"
  2427. +#include "lldb/Expression/DWARFEvaluatorFactory.h"
  2428. #include "lldb/Utility/DataEncoder.h"
  2429. #include "lldb/Utility/Log.h"
  2430. #include "lldb/Utility/RegisterValue.h"
  2431. @@ -41,8 +43,8 @@
  2432. using namespace lldb;
  2433. using namespace lldb_private;
  2434. -static lldb::addr_t
  2435. -ReadAddressFromDebugAddrSection(const DWARFUnit *dwarf_cu,
  2436. +lldb::addr_t
  2437. +DWARFExpression::ReadAddressFromDebugAddrSection(const DWARFUnit *dwarf_cu,
  2438. uint32_t index) {
  2439. uint32_t index_size = dwarf_cu->GetAddressByteSize();
  2440. dw_offset_t addr_base = dwarf_cu->GetAddrBase();
  2441. @@ -96,7 +98,7 @@ void DWARFExpression::SetLocationListAddresses(addr_t cu_file_addr,
  2442. m_loclist_addresses = LoclistAddresses{cu_file_addr, func_file_addr};
  2443. }
  2444. -int DWARFExpression::GetRegisterKind() { return m_reg_kind; }
  2445. +RegisterKind DWARFExpression::GetRegisterKind() const { return m_reg_kind; }
  2446. void DWARFExpression::SetRegisterKind(RegisterKind reg_kind) {
  2447. m_reg_kind = reg_kind;
  2448. @@ -150,52 +152,6 @@ void DWARFExpression::GetDescription(Stream *s, lldb::DescriptionLevel level,
  2449. }
  2450. }
  2451. -static bool ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
  2452. - lldb::RegisterKind reg_kind,
  2453. - uint32_t reg_num, Status *error_ptr,
  2454. - Value &value) {
  2455. - if (reg_ctx == nullptr) {
  2456. - if (error_ptr)
  2457. - error_ptr->SetErrorString("No register context in frame.\n");
  2458. - } else {
  2459. - uint32_t native_reg =
  2460. - reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
  2461. - if (native_reg == LLDB_INVALID_REGNUM) {
  2462. - if (error_ptr)
  2463. - error_ptr->SetErrorStringWithFormat("Unable to convert register "
  2464. - "kind=%u reg_num=%u to a native "
  2465. - "register number.\n",
  2466. - reg_kind, reg_num);
  2467. - } else {
  2468. - const RegisterInfo *reg_info =
  2469. - reg_ctx->GetRegisterInfoAtIndex(native_reg);
  2470. - RegisterValue reg_value;
  2471. - if (reg_ctx->ReadRegister(reg_info, reg_value)) {
  2472. - if (reg_value.GetScalarValue(value.GetScalar())) {
  2473. - value.SetValueType(Value::ValueType::Scalar);
  2474. - value.SetContext(Value::ContextType::RegisterInfo,
  2475. - const_cast<RegisterInfo *>(reg_info));
  2476. - if (error_ptr)
  2477. - error_ptr->Clear();
  2478. - return true;
  2479. - } else {
  2480. - // If we get this error, then we need to implement a value buffer in
  2481. - // the dwarf expression evaluation function...
  2482. - if (error_ptr)
  2483. - error_ptr->SetErrorStringWithFormat(
  2484. - "register %s can't be converted to a scalar value",
  2485. - reg_info->name);
  2486. - }
  2487. - } else {
  2488. - if (error_ptr)
  2489. - error_ptr->SetErrorStringWithFormat("register %s is not available",
  2490. - reg_info->name);
  2491. - }
  2492. - }
  2493. - }
  2494. - return false;
  2495. -}
  2496. -
  2497. /// Return the length in bytes of the set of operands for \p op. No guarantees
  2498. /// are made on the state of \p data after this call.
  2499. static offset_t GetOpcodeDataSize(const DataExtractor &data,
  2500. @@ -955,1719 +911,17 @@ bool DWARFExpression::Evaluate(
  2501. const Value *initial_value_ptr, const Value *object_address_ptr,
  2502. Value &result, Status *error_ptr) {
  2503. - if (opcodes.GetByteSize() == 0) {
  2504. - if (error_ptr)
  2505. - error_ptr->SetErrorString(
  2506. - "no location, value may have been optimized out");
  2507. - return false;
  2508. - }
  2509. - std::vector<Value> stack;
  2510. -
  2511. - Process *process = nullptr;
  2512. - StackFrame *frame = nullptr;
  2513. -
  2514. - if (exe_ctx) {
  2515. - process = exe_ctx->GetProcessPtr();
  2516. - frame = exe_ctx->GetFramePtr();
  2517. - }
  2518. - if (reg_ctx == nullptr && frame)
  2519. - reg_ctx = frame->GetRegisterContext().get();
  2520. -
  2521. - if (initial_value_ptr)
  2522. - stack.push_back(*initial_value_ptr);
  2523. -
  2524. - lldb::offset_t offset = 0;
  2525. - Value tmp;
  2526. - uint32_t reg_num;
  2527. -
  2528. - /// Insertion point for evaluating multi-piece expression.
  2529. - uint64_t op_piece_offset = 0;
  2530. - Value pieces; // Used for DW_OP_piece
  2531. -
  2532. - Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
  2533. - // A generic type is "an integral type that has the size of an address and an
  2534. - // unspecified signedness". For now, just use the signedness of the operand.
  2535. - // TODO: Implement a real typed stack, and store the genericness of the value
  2536. - // there.
  2537. - auto to_generic = [&](auto v) {
  2538. - bool is_signed = std::is_signed<decltype(v)>::value;
  2539. - return Scalar(llvm::APSInt(
  2540. - llvm::APInt(8 * opcodes.GetAddressByteSize(), v, is_signed),
  2541. - !is_signed));
  2542. - };
  2543. -
  2544. - // The default kind is a memory location. This is updated by any
  2545. - // operation that changes this, such as DW_OP_stack_value, and reset
  2546. - // by composition operations like DW_OP_piece.
  2547. - LocationDescriptionKind dwarf4_location_description_kind = Memory;
  2548. -
  2549. - while (opcodes.ValidOffset(offset)) {
  2550. - const lldb::offset_t op_offset = offset;
  2551. - const uint8_t op = opcodes.GetU8(&offset);
  2552. -
  2553. - if (log && log->GetVerbose()) {
  2554. - size_t count = stack.size();
  2555. - LLDB_LOGF(log, "Stack before operation has %" PRIu64 " values:",
  2556. - (uint64_t)count);
  2557. - for (size_t i = 0; i < count; ++i) {
  2558. - StreamString new_value;
  2559. - new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
  2560. - stack[i].Dump(&new_value);
  2561. - LLDB_LOGF(log, " %s", new_value.GetData());
  2562. - }
  2563. - LLDB_LOGF(log, "0x%8.8" PRIx64 ": %s", op_offset,
  2564. - DW_OP_value_to_name(op));
  2565. - }
  2566. -
  2567. - switch (op) {
  2568. - // The DW_OP_addr operation has a single operand that encodes a machine
  2569. - // address and whose size is the size of an address on the target machine.
  2570. - case DW_OP_addr:
  2571. - stack.push_back(Scalar(opcodes.GetAddress(&offset)));
  2572. - stack.back().SetValueType(Value::ValueType::FileAddress);
  2573. - // Convert the file address to a load address, so subsequent
  2574. - // DWARF operators can operate on it.
  2575. - if (frame)
  2576. - stack.back().ConvertToLoadAddress(module_sp.get(),
  2577. - frame->CalculateTarget().get());
  2578. - break;
  2579. -
  2580. - // The DW_OP_addr_sect_offset4 is used for any location expressions in
  2581. - // shared libraries that have a location like:
  2582. - // DW_OP_addr(0x1000)
  2583. - // If this address resides in a shared library, then this virtual address
  2584. - // won't make sense when it is evaluated in the context of a running
  2585. - // process where shared libraries have been slid. To account for this, this
  2586. - // new address type where we can store the section pointer and a 4 byte
  2587. - // offset.
  2588. - // case DW_OP_addr_sect_offset4:
  2589. - // {
  2590. - // result_type = eResultTypeFileAddress;
  2591. - // lldb::Section *sect = (lldb::Section
  2592. - // *)opcodes.GetMaxU64(&offset, sizeof(void *));
  2593. - // lldb::addr_t sect_offset = opcodes.GetU32(&offset);
  2594. - //
  2595. - // Address so_addr (sect, sect_offset);
  2596. - // lldb::addr_t load_addr = so_addr.GetLoadAddress();
  2597. - // if (load_addr != LLDB_INVALID_ADDRESS)
  2598. - // {
  2599. - // // We successfully resolve a file address to a load
  2600. - // // address.
  2601. - // stack.push_back(load_addr);
  2602. - // break;
  2603. - // }
  2604. - // else
  2605. - // {
  2606. - // // We were able
  2607. - // if (error_ptr)
  2608. - // error_ptr->SetErrorStringWithFormat ("Section %s in
  2609. - // %s is not currently loaded.\n",
  2610. - // sect->GetName().AsCString(),
  2611. - // sect->GetModule()->GetFileSpec().GetFilename().AsCString());
  2612. - // return false;
  2613. - // }
  2614. - // }
  2615. - // break;
  2616. -
  2617. - // OPCODE: DW_OP_deref
  2618. - // OPERANDS: none
  2619. - // DESCRIPTION: Pops the top stack entry and treats it as an address.
  2620. - // The value retrieved from that address is pushed. The size of the data
  2621. - // retrieved from the dereferenced address is the size of an address on the
  2622. - // target machine.
  2623. - case DW_OP_deref: {
  2624. - if (stack.empty()) {
  2625. - if (error_ptr)
  2626. - error_ptr->SetErrorString("Expression stack empty for DW_OP_deref.");
  2627. - return false;
  2628. - }
  2629. - Value::ValueType value_type = stack.back().GetValueType();
  2630. - switch (value_type) {
  2631. - case Value::ValueType::HostAddress: {
  2632. - void *src = (void *)stack.back().GetScalar().ULongLong();
  2633. - intptr_t ptr;
  2634. - ::memcpy(&ptr, src, sizeof(void *));
  2635. - stack.back().GetScalar() = ptr;
  2636. - stack.back().ClearContext();
  2637. - } break;
  2638. - case Value::ValueType::FileAddress: {
  2639. - auto file_addr = stack.back().GetScalar().ULongLong(
  2640. - LLDB_INVALID_ADDRESS);
  2641. - if (!module_sp) {
  2642. - if (error_ptr)
  2643. - error_ptr->SetErrorString(
  2644. - "need module to resolve file address for DW_OP_deref");
  2645. - return false;
  2646. - }
  2647. - Address so_addr;
  2648. - if (!module_sp->ResolveFileAddress(file_addr, so_addr)) {
  2649. - if (error_ptr)
  2650. - error_ptr->SetErrorString(
  2651. - "failed to resolve file address in module");
  2652. - return false;
  2653. - }
  2654. - addr_t load_Addr = so_addr.GetLoadAddress(exe_ctx->GetTargetPtr());
  2655. - if (load_Addr == LLDB_INVALID_ADDRESS) {
  2656. - if (error_ptr)
  2657. - error_ptr->SetErrorString("failed to resolve load address");
  2658. - return false;
  2659. - }
  2660. - stack.back().GetScalar() = load_Addr;
  2661. - // Fall through to load address promotion code below.
  2662. - } LLVM_FALLTHROUGH;
  2663. - case Value::ValueType::Scalar:
  2664. - // Promote Scalar to LoadAddress and fall through.
  2665. - stack.back().SetValueType(Value::ValueType::LoadAddress);
  2666. - LLVM_FALLTHROUGH;
  2667. - case Value::ValueType::LoadAddress:
  2668. - if (exe_ctx) {
  2669. - if (process) {
  2670. - lldb::addr_t pointer_addr =
  2671. - stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
  2672. - Status error;
  2673. - lldb::addr_t pointer_value =
  2674. - process->ReadPointerFromMemory(pointer_addr, error);
  2675. - if (pointer_value != LLDB_INVALID_ADDRESS) {
  2676. - if (ABISP abi_sp = process->GetABI())
  2677. - pointer_value = abi_sp->FixCodeAddress(pointer_value);
  2678. - stack.back().GetScalar() = pointer_value;
  2679. - stack.back().ClearContext();
  2680. - } else {
  2681. - if (error_ptr)
  2682. - error_ptr->SetErrorStringWithFormat(
  2683. - "Failed to dereference pointer from 0x%" PRIx64
  2684. - " for DW_OP_deref: %s\n",
  2685. - pointer_addr, error.AsCString());
  2686. - return false;
  2687. - }
  2688. - } else {
  2689. - if (error_ptr)
  2690. - error_ptr->SetErrorString("NULL process for DW_OP_deref.\n");
  2691. - return false;
  2692. - }
  2693. - } else {
  2694. - if (error_ptr)
  2695. - error_ptr->SetErrorString(
  2696. - "NULL execution context for DW_OP_deref.\n");
  2697. - return false;
  2698. - }
  2699. - break;
  2700. -
  2701. - case Value::ValueType::Invalid:
  2702. - if (error_ptr)
  2703. - error_ptr->SetErrorString("Invalid value type for DW_OP_deref.\n");
  2704. - return false;
  2705. - }
  2706. -
  2707. - } break;
  2708. -
  2709. - // OPCODE: DW_OP_deref_size
  2710. - // OPERANDS: 1
  2711. - // 1 - uint8_t that specifies the size of the data to dereference.
  2712. - // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
  2713. - // stack entry and treats it as an address. The value retrieved from that
  2714. - // address is pushed. In the DW_OP_deref_size operation, however, the size
  2715. - // in bytes of the data retrieved from the dereferenced address is
  2716. - // specified by the single operand. This operand is a 1-byte unsigned
  2717. - // integral constant whose value may not be larger than the size of an
  2718. - // address on the target machine. The data retrieved is zero extended to
  2719. - // the size of an address on the target machine before being pushed on the
  2720. - // expression stack.
  2721. - case DW_OP_deref_size: {
  2722. - if (stack.empty()) {
  2723. - if (error_ptr)
  2724. - error_ptr->SetErrorString(
  2725. - "Expression stack empty for DW_OP_deref_size.");
  2726. - return false;
  2727. - }
  2728. - uint8_t size = opcodes.GetU8(&offset);
  2729. - Value::ValueType value_type = stack.back().GetValueType();
  2730. - switch (value_type) {
  2731. - case Value::ValueType::HostAddress: {
  2732. - void *src = (void *)stack.back().GetScalar().ULongLong();
  2733. - intptr_t ptr;
  2734. - ::memcpy(&ptr, src, sizeof(void *));
  2735. - // I can't decide whether the size operand should apply to the bytes in
  2736. - // their
  2737. - // lldb-host endianness or the target endianness.. I doubt this'll ever
  2738. - // come up but I'll opt for assuming big endian regardless.
  2739. - switch (size) {
  2740. - case 1:
  2741. - ptr = ptr & 0xff;
  2742. - break;
  2743. - case 2:
  2744. - ptr = ptr & 0xffff;
  2745. - break;
  2746. - case 3:
  2747. - ptr = ptr & 0xffffff;
  2748. - break;
  2749. - case 4:
  2750. - ptr = ptr & 0xffffffff;
  2751. - break;
  2752. - // the casts are added to work around the case where intptr_t is a 32
  2753. - // bit quantity;
  2754. - // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this
  2755. - // program.
  2756. - case 5:
  2757. - ptr = (intptr_t)ptr & 0xffffffffffULL;
  2758. - break;
  2759. - case 6:
  2760. - ptr = (intptr_t)ptr & 0xffffffffffffULL;
  2761. - break;
  2762. - case 7:
  2763. - ptr = (intptr_t)ptr & 0xffffffffffffffULL;
  2764. - break;
  2765. - default:
  2766. - break;
  2767. - }
  2768. - stack.back().GetScalar() = ptr;
  2769. - stack.back().ClearContext();
  2770. - } break;
  2771. - case Value::ValueType::Scalar:
  2772. - case Value::ValueType::LoadAddress:
  2773. - if (exe_ctx) {
  2774. - if (process) {
  2775. - lldb::addr_t pointer_addr =
  2776. - stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
  2777. - uint8_t addr_bytes[sizeof(lldb::addr_t)];
  2778. - Status error;
  2779. - if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) ==
  2780. - size) {
  2781. - DataExtractor addr_data(addr_bytes, sizeof(addr_bytes),
  2782. - process->GetByteOrder(), size);
  2783. - lldb::offset_t addr_data_offset = 0;
  2784. - switch (size) {
  2785. - case 1:
  2786. - stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset);
  2787. - break;
  2788. - case 2:
  2789. - stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset);
  2790. - break;
  2791. - case 4:
  2792. - stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset);
  2793. - break;
  2794. - case 8:
  2795. - stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset);
  2796. - break;
  2797. - default:
  2798. - stack.back().GetScalar() =
  2799. - addr_data.GetAddress(&addr_data_offset);
  2800. - }
  2801. - stack.back().ClearContext();
  2802. - } else {
  2803. - if (error_ptr)
  2804. - error_ptr->SetErrorStringWithFormat(
  2805. - "Failed to dereference pointer from 0x%" PRIx64
  2806. - " for DW_OP_deref: %s\n",
  2807. - pointer_addr, error.AsCString());
  2808. - return false;
  2809. - }
  2810. - } else {
  2811. - if (error_ptr)
  2812. - error_ptr->SetErrorString("NULL process for DW_OP_deref_size.\n");
  2813. - return false;
  2814. - }
  2815. - } else {
  2816. - if (error_ptr)
  2817. - error_ptr->SetErrorString(
  2818. - "NULL execution context for DW_OP_deref_size.\n");
  2819. - return false;
  2820. - }
  2821. - break;
  2822. -
  2823. - case Value::ValueType::FileAddress:
  2824. - case Value::ValueType::Invalid:
  2825. - if (error_ptr)
  2826. - error_ptr->SetErrorString("Invalid value for DW_OP_deref_size.\n");
  2827. - return false;
  2828. - }
  2829. -
  2830. - } break;
  2831. -
  2832. - // OPCODE: DW_OP_xderef_size
  2833. - // OPERANDS: 1
  2834. - // 1 - uint8_t that specifies the size of the data to dereference.
  2835. - // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
  2836. - // the top of the stack is treated as an address. The second stack entry is
  2837. - // treated as an "address space identifier" for those architectures that
  2838. - // support multiple address spaces. The top two stack elements are popped,
  2839. - // a data item is retrieved through an implementation-defined address
  2840. - // calculation and pushed as the new stack top. In the DW_OP_xderef_size
  2841. - // operation, however, the size in bytes of the data retrieved from the
  2842. - // dereferenced address is specified by the single operand. This operand is
  2843. - // a 1-byte unsigned integral constant whose value may not be larger than
  2844. - // the size of an address on the target machine. The data retrieved is zero
  2845. - // extended to the size of an address on the target machine before being
  2846. - // pushed on the expression stack.
  2847. - case DW_OP_xderef_size:
  2848. - if (error_ptr)
  2849. - error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
  2850. - return false;
  2851. - // OPCODE: DW_OP_xderef
  2852. - // OPERANDS: none
  2853. - // DESCRIPTION: Provides an extended dereference mechanism. The entry at
  2854. - // the top of the stack is treated as an address. The second stack entry is
  2855. - // treated as an "address space identifier" for those architectures that
  2856. - // support multiple address spaces. The top two stack elements are popped,
  2857. - // a data item is retrieved through an implementation-defined address
  2858. - // calculation and pushed as the new stack top. The size of the data
  2859. - // retrieved from the dereferenced address is the size of an address on the
  2860. - // target machine.
  2861. - case DW_OP_xderef:
  2862. - if (error_ptr)
  2863. - error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
  2864. - return false;
  2865. -
  2866. - // All DW_OP_constXXX opcodes have a single operand as noted below:
  2867. - //
  2868. - // Opcode Operand 1
  2869. - // DW_OP_const1u 1-byte unsigned integer constant
  2870. - // DW_OP_const1s 1-byte signed integer constant
  2871. - // DW_OP_const2u 2-byte unsigned integer constant
  2872. - // DW_OP_const2s 2-byte signed integer constant
  2873. - // DW_OP_const4u 4-byte unsigned integer constant
  2874. - // DW_OP_const4s 4-byte signed integer constant
  2875. - // DW_OP_const8u 8-byte unsigned integer constant
  2876. - // DW_OP_const8s 8-byte signed integer constant
  2877. - // DW_OP_constu unsigned LEB128 integer constant
  2878. - // DW_OP_consts signed LEB128 integer constant
  2879. - case DW_OP_const1u:
  2880. - stack.push_back(to_generic(opcodes.GetU8(&offset)));
  2881. - break;
  2882. - case DW_OP_const1s:
  2883. - stack.push_back(to_generic((int8_t)opcodes.GetU8(&offset)));
  2884. - break;
  2885. - case DW_OP_const2u:
  2886. - stack.push_back(to_generic(opcodes.GetU16(&offset)));
  2887. - break;
  2888. - case DW_OP_const2s:
  2889. - stack.push_back(to_generic((int16_t)opcodes.GetU16(&offset)));
  2890. - break;
  2891. - case DW_OP_const4u:
  2892. - stack.push_back(to_generic(opcodes.GetU32(&offset)));
  2893. - break;
  2894. - case DW_OP_const4s:
  2895. - stack.push_back(to_generic((int32_t)opcodes.GetU32(&offset)));
  2896. - break;
  2897. - case DW_OP_const8u:
  2898. - stack.push_back(to_generic(opcodes.GetU64(&offset)));
  2899. - break;
  2900. - case DW_OP_const8s:
  2901. - stack.push_back(to_generic((int64_t)opcodes.GetU64(&offset)));
  2902. - break;
  2903. - // These should also use to_generic, but we can't do that due to a
  2904. - // producer-side bug in llvm. See llvm.org/pr48087.
  2905. - case DW_OP_constu:
  2906. - stack.push_back(Scalar(opcodes.GetULEB128(&offset)));
  2907. - break;
  2908. - case DW_OP_consts:
  2909. - stack.push_back(Scalar(opcodes.GetSLEB128(&offset)));
  2910. - break;
  2911. -
  2912. - // OPCODE: DW_OP_dup
  2913. - // OPERANDS: none
  2914. - // DESCRIPTION: duplicates the value at the top of the stack
  2915. - case DW_OP_dup:
  2916. - if (stack.empty()) {
  2917. - if (error_ptr)
  2918. - error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
  2919. - return false;
  2920. - } else
  2921. - stack.push_back(stack.back());
  2922. - break;
  2923. -
  2924. - // OPCODE: DW_OP_drop
  2925. - // OPERANDS: none
  2926. - // DESCRIPTION: pops the value at the top of the stack
  2927. - case DW_OP_drop:
  2928. - if (stack.empty()) {
  2929. - if (error_ptr)
  2930. - error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
  2931. - return false;
  2932. - } else
  2933. - stack.pop_back();
  2934. - break;
  2935. -
  2936. - // OPCODE: DW_OP_over
  2937. - // OPERANDS: none
  2938. - // DESCRIPTION: Duplicates the entry currently second in the stack at
  2939. - // the top of the stack.
  2940. - case DW_OP_over:
  2941. - if (stack.size() < 2) {
  2942. - if (error_ptr)
  2943. - error_ptr->SetErrorString(
  2944. - "Expression stack needs at least 2 items for DW_OP_over.");
  2945. - return false;
  2946. - } else
  2947. - stack.push_back(stack[stack.size() - 2]);
  2948. - break;
  2949. -
  2950. - // OPCODE: DW_OP_pick
  2951. - // OPERANDS: uint8_t index into the current stack
  2952. - // DESCRIPTION: The stack entry with the specified index (0 through 255,
  2953. - // inclusive) is pushed on the stack
  2954. - case DW_OP_pick: {
  2955. - uint8_t pick_idx = opcodes.GetU8(&offset);
  2956. - if (pick_idx < stack.size())
  2957. - stack.push_back(stack[stack.size() - 1 - pick_idx]);
  2958. - else {
  2959. - if (error_ptr)
  2960. - error_ptr->SetErrorStringWithFormat(
  2961. - "Index %u out of range for DW_OP_pick.\n", pick_idx);
  2962. - return false;
  2963. - }
  2964. - } break;
  2965. -
  2966. - // OPCODE: DW_OP_swap
  2967. - // OPERANDS: none
  2968. - // DESCRIPTION: swaps the top two stack entries. The entry at the top
  2969. - // of the stack becomes the second stack entry, and the second entry
  2970. - // becomes the top of the stack
  2971. - case DW_OP_swap:
  2972. - if (stack.size() < 2) {
  2973. - if (error_ptr)
  2974. - error_ptr->SetErrorString(
  2975. - "Expression stack needs at least 2 items for DW_OP_swap.");
  2976. - return false;
  2977. - } else {
  2978. - tmp = stack.back();
  2979. - stack.back() = stack[stack.size() - 2];
  2980. - stack[stack.size() - 2] = tmp;
  2981. - }
  2982. - break;
  2983. -
  2984. - // OPCODE: DW_OP_rot
  2985. - // OPERANDS: none
  2986. - // DESCRIPTION: Rotates the first three stack entries. The entry at
  2987. - // the top of the stack becomes the third stack entry, the second entry
  2988. - // becomes the top of the stack, and the third entry becomes the second
  2989. - // entry.
  2990. - case DW_OP_rot:
  2991. - if (stack.size() < 3) {
  2992. - if (error_ptr)
  2993. - error_ptr->SetErrorString(
  2994. - "Expression stack needs at least 3 items for DW_OP_rot.");
  2995. - return false;
  2996. - } else {
  2997. - size_t last_idx = stack.size() - 1;
  2998. - Value old_top = stack[last_idx];
  2999. - stack[last_idx] = stack[last_idx - 1];
  3000. - stack[last_idx - 1] = stack[last_idx - 2];
  3001. - stack[last_idx - 2] = old_top;
  3002. - }
  3003. - break;
  3004. -
  3005. - // OPCODE: DW_OP_abs
  3006. - // OPERANDS: none
  3007. - // DESCRIPTION: pops the top stack entry, interprets it as a signed
  3008. - // value and pushes its absolute value. If the absolute value can not be
  3009. - // represented, the result is undefined.
  3010. - case DW_OP_abs:
  3011. - if (stack.empty()) {
  3012. - if (error_ptr)
  3013. - error_ptr->SetErrorString(
  3014. - "Expression stack needs at least 1 item for DW_OP_abs.");
  3015. - return false;
  3016. - } else if (!stack.back().ResolveValue(exe_ctx).AbsoluteValue()) {
  3017. - if (error_ptr)
  3018. - error_ptr->SetErrorString(
  3019. - "Failed to take the absolute value of the first stack item.");
  3020. - return false;
  3021. - }
  3022. - break;
  3023. -
  3024. - // OPCODE: DW_OP_and
  3025. - // OPERANDS: none
  3026. - // DESCRIPTION: pops the top two stack values, performs a bitwise and
  3027. - // operation on the two, and pushes the result.
  3028. - case DW_OP_and:
  3029. - if (stack.size() < 2) {
  3030. - if (error_ptr)
  3031. - error_ptr->SetErrorString(
  3032. - "Expression stack needs at least 2 items for DW_OP_and.");
  3033. - return false;
  3034. - } else {
  3035. - tmp = stack.back();
  3036. - stack.pop_back();
  3037. - stack.back().ResolveValue(exe_ctx) =
  3038. - stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx);
  3039. - }
  3040. - break;
  3041. -
  3042. - // OPCODE: DW_OP_div
  3043. - // OPERANDS: none
  3044. - // DESCRIPTION: pops the top two stack values, divides the former second
  3045. - // entry by the former top of the stack using signed division, and pushes
  3046. - // the result.
  3047. - case DW_OP_div:
  3048. - if (stack.size() < 2) {
  3049. - if (error_ptr)
  3050. - error_ptr->SetErrorString(
  3051. - "Expression stack needs at least 2 items for DW_OP_div.");
  3052. - return false;
  3053. - } else {
  3054. - tmp = stack.back();
  3055. - if (tmp.ResolveValue(exe_ctx).IsZero()) {
  3056. - if (error_ptr)
  3057. - error_ptr->SetErrorString("Divide by zero.");
  3058. - return false;
  3059. - } else {
  3060. - stack.pop_back();
  3061. - stack.back() =
  3062. - stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx);
  3063. - if (!stack.back().ResolveValue(exe_ctx).IsValid()) {
  3064. - if (error_ptr)
  3065. - error_ptr->SetErrorString("Divide failed.");
  3066. - return false;
  3067. - }
  3068. - }
  3069. - }
  3070. - break;
  3071. -
  3072. - // OPCODE: DW_OP_minus
  3073. - // OPERANDS: none
  3074. - // DESCRIPTION: pops the top two stack values, subtracts the former top
  3075. - // of the stack from the former second entry, and pushes the result.
  3076. - case DW_OP_minus:
  3077. - if (stack.size() < 2) {
  3078. - if (error_ptr)
  3079. - error_ptr->SetErrorString(
  3080. - "Expression stack needs at least 2 items for DW_OP_minus.");
  3081. - return false;
  3082. - } else {
  3083. - tmp = stack.back();
  3084. - stack.pop_back();
  3085. - stack.back().ResolveValue(exe_ctx) =
  3086. - stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx);
  3087. - }
  3088. - break;
  3089. -
  3090. - // OPCODE: DW_OP_mod
  3091. - // OPERANDS: none
  3092. - // DESCRIPTION: pops the top two stack values and pushes the result of
  3093. - // the calculation: former second stack entry modulo the former top of the
  3094. - // stack.
  3095. - case DW_OP_mod:
  3096. - if (stack.size() < 2) {
  3097. - if (error_ptr)
  3098. - error_ptr->SetErrorString(
  3099. - "Expression stack needs at least 2 items for DW_OP_mod.");
  3100. - return false;
  3101. - } else {
  3102. - tmp = stack.back();
  3103. - stack.pop_back();
  3104. - stack.back().ResolveValue(exe_ctx) =
  3105. - stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx);
  3106. - }
  3107. - break;
  3108. -
  3109. - // OPCODE: DW_OP_mul
  3110. - // OPERANDS: none
  3111. - // DESCRIPTION: pops the top two stack entries, multiplies them
  3112. - // together, and pushes the result.
  3113. - case DW_OP_mul:
  3114. - if (stack.size() < 2) {
  3115. - if (error_ptr)
  3116. - error_ptr->SetErrorString(
  3117. - "Expression stack needs at least 2 items for DW_OP_mul.");
  3118. - return false;
  3119. - } else {
  3120. - tmp = stack.back();
  3121. - stack.pop_back();
  3122. - stack.back().ResolveValue(exe_ctx) =
  3123. - stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx);
  3124. - }
  3125. - break;
  3126. -
  3127. - // OPCODE: DW_OP_neg
  3128. - // OPERANDS: none
  3129. - // DESCRIPTION: pops the top stack entry, and pushes its negation.
  3130. - case DW_OP_neg:
  3131. - if (stack.empty()) {
  3132. - if (error_ptr)
  3133. - error_ptr->SetErrorString(
  3134. - "Expression stack needs at least 1 item for DW_OP_neg.");
  3135. - return false;
  3136. - } else {
  3137. - if (!stack.back().ResolveValue(exe_ctx).UnaryNegate()) {
  3138. - if (error_ptr)
  3139. - error_ptr->SetErrorString("Unary negate failed.");
  3140. - return false;
  3141. - }
  3142. - }
  3143. - break;
  3144. -
  3145. - // OPCODE: DW_OP_not
  3146. - // OPERANDS: none
  3147. - // DESCRIPTION: pops the top stack entry, and pushes its bitwise
  3148. - // complement
  3149. - case DW_OP_not:
  3150. - if (stack.empty()) {
  3151. - if (error_ptr)
  3152. - error_ptr->SetErrorString(
  3153. - "Expression stack needs at least 1 item for DW_OP_not.");
  3154. - return false;
  3155. - } else {
  3156. - if (!stack.back().ResolveValue(exe_ctx).OnesComplement()) {
  3157. - if (error_ptr)
  3158. - error_ptr->SetErrorString("Logical NOT failed.");
  3159. - return false;
  3160. - }
  3161. - }
  3162. - break;
  3163. -
  3164. - // OPCODE: DW_OP_or
  3165. - // OPERANDS: none
  3166. - // DESCRIPTION: pops the top two stack entries, performs a bitwise or
  3167. - // operation on the two, and pushes the result.
  3168. - case DW_OP_or:
  3169. - if (stack.size() < 2) {
  3170. - if (error_ptr)
  3171. - error_ptr->SetErrorString(
  3172. - "Expression stack needs at least 2 items for DW_OP_or.");
  3173. - return false;
  3174. - } else {
  3175. - tmp = stack.back();
  3176. - stack.pop_back();
  3177. - stack.back().ResolveValue(exe_ctx) =
  3178. - stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx);
  3179. - }
  3180. - break;
  3181. -
  3182. - // OPCODE: DW_OP_plus
  3183. - // OPERANDS: none
  3184. - // DESCRIPTION: pops the top two stack entries, adds them together, and
  3185. - // pushes the result.
  3186. - case DW_OP_plus:
  3187. - if (stack.size() < 2) {
  3188. - if (error_ptr)
  3189. - error_ptr->SetErrorString(
  3190. - "Expression stack needs at least 2 items for DW_OP_plus.");
  3191. - return false;
  3192. - } else {
  3193. - tmp = stack.back();
  3194. - stack.pop_back();
  3195. - stack.back().GetScalar() += tmp.GetScalar();
  3196. - }
  3197. - break;
  3198. -
  3199. - // OPCODE: DW_OP_plus_uconst
  3200. - // OPERANDS: none
  3201. - // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
  3202. - // constant operand and pushes the result.
  3203. - case DW_OP_plus_uconst:
  3204. - if (stack.empty()) {
  3205. - if (error_ptr)
  3206. - error_ptr->SetErrorString(
  3207. - "Expression stack needs at least 1 item for DW_OP_plus_uconst.");
  3208. - return false;
  3209. - } else {
  3210. - const uint64_t uconst_value = opcodes.GetULEB128(&offset);
  3211. - // Implicit conversion from a UINT to a Scalar...
  3212. - stack.back().GetScalar() += uconst_value;
  3213. - if (!stack.back().GetScalar().IsValid()) {
  3214. - if (error_ptr)
  3215. - error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
  3216. - return false;
  3217. - }
  3218. - }
  3219. - break;
  3220. -
  3221. - // OPCODE: DW_OP_shl
  3222. - // OPERANDS: none
  3223. - // DESCRIPTION: pops the top two stack entries, shifts the former
  3224. - // second entry left by the number of bits specified by the former top of
  3225. - // the stack, and pushes the result.
  3226. - case DW_OP_shl:
  3227. - if (stack.size() < 2) {
  3228. - if (error_ptr)
  3229. - error_ptr->SetErrorString(
  3230. - "Expression stack needs at least 2 items for DW_OP_shl.");
  3231. - return false;
  3232. - } else {
  3233. - tmp = stack.back();
  3234. - stack.pop_back();
  3235. - stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx);
  3236. - }
  3237. - break;
  3238. -
  3239. - // OPCODE: DW_OP_shr
  3240. - // OPERANDS: none
  3241. - // DESCRIPTION: pops the top two stack entries, shifts the former second
  3242. - // entry right logically (filling with zero bits) by the number of bits
  3243. - // specified by the former top of the stack, and pushes the result.
  3244. - case DW_OP_shr:
  3245. - if (stack.size() < 2) {
  3246. - if (error_ptr)
  3247. - error_ptr->SetErrorString(
  3248. - "Expression stack needs at least 2 items for DW_OP_shr.");
  3249. - return false;
  3250. - } else {
  3251. - tmp = stack.back();
  3252. - stack.pop_back();
  3253. - if (!stack.back().ResolveValue(exe_ctx).ShiftRightLogical(
  3254. - tmp.ResolveValue(exe_ctx))) {
  3255. - if (error_ptr)
  3256. - error_ptr->SetErrorString("DW_OP_shr failed.");
  3257. - return false;
  3258. - }
  3259. - }
  3260. - break;
  3261. -
  3262. - // OPCODE: DW_OP_shra
  3263. - // OPERANDS: none
  3264. - // DESCRIPTION: pops the top two stack entries, shifts the former second
  3265. - // entry right arithmetically (divide the magnitude by 2, keep the same
  3266. - // sign for the result) by the number of bits specified by the former top
  3267. - // of the stack, and pushes the result.
  3268. - case DW_OP_shra:
  3269. - if (stack.size() < 2) {
  3270. - if (error_ptr)
  3271. - error_ptr->SetErrorString(
  3272. - "Expression stack needs at least 2 items for DW_OP_shra.");
  3273. - return false;
  3274. - } else {
  3275. - tmp = stack.back();
  3276. - stack.pop_back();
  3277. - stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx);
  3278. - }
  3279. - break;
  3280. -
  3281. - // OPCODE: DW_OP_xor
  3282. - // OPERANDS: none
  3283. - // DESCRIPTION: pops the top two stack entries, performs the bitwise
  3284. - // exclusive-or operation on the two, and pushes the result.
  3285. - case DW_OP_xor:
  3286. - if (stack.size() < 2) {
  3287. - if (error_ptr)
  3288. - error_ptr->SetErrorString(
  3289. - "Expression stack needs at least 2 items for DW_OP_xor.");
  3290. - return false;
  3291. - } else {
  3292. - tmp = stack.back();
  3293. - stack.pop_back();
  3294. - stack.back().ResolveValue(exe_ctx) =
  3295. - stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx);
  3296. - }
  3297. - break;
  3298. -
  3299. - // OPCODE: DW_OP_skip
  3300. - // OPERANDS: int16_t
  3301. - // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
  3302. - // signed integer constant. The 2-byte constant is the number of bytes of
  3303. - // the DWARF expression to skip forward or backward from the current
  3304. - // operation, beginning after the 2-byte constant.
  3305. - case DW_OP_skip: {
  3306. - int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
  3307. - lldb::offset_t new_offset = offset + skip_offset;
  3308. - if (opcodes.ValidOffset(new_offset))
  3309. - offset = new_offset;
  3310. - else {
  3311. - if (error_ptr)
  3312. - error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
  3313. - return false;
  3314. - }
  3315. - } break;
  3316. -
  3317. - // OPCODE: DW_OP_bra
  3318. - // OPERANDS: int16_t
  3319. - // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
  3320. - // signed integer constant. This operation pops the top of stack. If the
  3321. - // value popped is not the constant 0, the 2-byte constant operand is the
  3322. - // number of bytes of the DWARF expression to skip forward or backward from
  3323. - // the current operation, beginning after the 2-byte constant.
  3324. - case DW_OP_bra:
  3325. - if (stack.empty()) {
  3326. - if (error_ptr)
  3327. - error_ptr->SetErrorString(
  3328. - "Expression stack needs at least 1 item for DW_OP_bra.");
  3329. - return false;
  3330. - } else {
  3331. - tmp = stack.back();
  3332. - stack.pop_back();
  3333. - int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
  3334. - Scalar zero(0);
  3335. - if (tmp.ResolveValue(exe_ctx) != zero) {
  3336. - lldb::offset_t new_offset = offset + bra_offset;
  3337. - if (opcodes.ValidOffset(new_offset))
  3338. - offset = new_offset;
  3339. - else {
  3340. - if (error_ptr)
  3341. - error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
  3342. - return false;
  3343. - }
  3344. - }
  3345. - }
  3346. - break;
  3347. -
  3348. - // OPCODE: DW_OP_eq
  3349. - // OPERANDS: none
  3350. - // DESCRIPTION: pops the top two stack values, compares using the
  3351. - // equals (==) operator.
  3352. - // STACK RESULT: push the constant value 1 onto the stack if the result
  3353. - // of the operation is true or the constant value 0 if the result of the
  3354. - // operation is false.
  3355. - case DW_OP_eq:
  3356. - if (stack.size() < 2) {
  3357. - if (error_ptr)
  3358. - error_ptr->SetErrorString(
  3359. - "Expression stack needs at least 2 items for DW_OP_eq.");
  3360. - return false;
  3361. - } else {
  3362. - tmp = stack.back();
  3363. - stack.pop_back();
  3364. - stack.back().ResolveValue(exe_ctx) =
  3365. - stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx);
  3366. - }
  3367. - break;
  3368. -
  3369. - // OPCODE: DW_OP_ge
  3370. - // OPERANDS: none
  3371. - // DESCRIPTION: pops the top two stack values, compares using the
  3372. - // greater than or equal to (>=) operator.
  3373. - // STACK RESULT: push the constant value 1 onto the stack if the result
  3374. - // of the operation is true or the constant value 0 if the result of the
  3375. - // operation is false.
  3376. - case DW_OP_ge:
  3377. - if (stack.size() < 2) {
  3378. - if (error_ptr)
  3379. - error_ptr->SetErrorString(
  3380. - "Expression stack needs at least 2 items for DW_OP_ge.");
  3381. - return false;
  3382. - } else {
  3383. - tmp = stack.back();
  3384. - stack.pop_back();
  3385. - stack.back().ResolveValue(exe_ctx) =
  3386. - stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx);
  3387. - }
  3388. - break;
  3389. -
  3390. - // OPCODE: DW_OP_gt
  3391. - // OPERANDS: none
  3392. - // DESCRIPTION: pops the top two stack values, compares using the
  3393. - // greater than (>) operator.
  3394. - // STACK RESULT: push the constant value 1 onto the stack if the result
  3395. - // of the operation is true or the constant value 0 if the result of the
  3396. - // operation is false.
  3397. - case DW_OP_gt:
  3398. - if (stack.size() < 2) {
  3399. - if (error_ptr)
  3400. - error_ptr->SetErrorString(
  3401. - "Expression stack needs at least 2 items for DW_OP_gt.");
  3402. - return false;
  3403. - } else {
  3404. - tmp = stack.back();
  3405. - stack.pop_back();
  3406. - stack.back().ResolveValue(exe_ctx) =
  3407. - stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx);
  3408. - }
  3409. - break;
  3410. -
  3411. - // OPCODE: DW_OP_le
  3412. - // OPERANDS: none
  3413. - // DESCRIPTION: pops the top two stack values, compares using the
  3414. - // less than or equal to (<=) operator.
  3415. - // STACK RESULT: push the constant value 1 onto the stack if the result
  3416. - // of the operation is true or the constant value 0 if the result of the
  3417. - // operation is false.
  3418. - case DW_OP_le:
  3419. - if (stack.size() < 2) {
  3420. - if (error_ptr)
  3421. - error_ptr->SetErrorString(
  3422. - "Expression stack needs at least 2 items for DW_OP_le.");
  3423. - return false;
  3424. - } else {
  3425. - tmp = stack.back();
  3426. - stack.pop_back();
  3427. - stack.back().ResolveValue(exe_ctx) =
  3428. - stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx);
  3429. - }
  3430. - break;
  3431. -
  3432. - // OPCODE: DW_OP_lt
  3433. - // OPERANDS: none
  3434. - // DESCRIPTION: pops the top two stack values, compares using the
  3435. - // less than (<) operator.
  3436. - // STACK RESULT: push the constant value 1 onto the stack if the result
  3437. - // of the operation is true or the constant value 0 if the result of the
  3438. - // operation is false.
  3439. - case DW_OP_lt:
  3440. - if (stack.size() < 2) {
  3441. - if (error_ptr)
  3442. - error_ptr->SetErrorString(
  3443. - "Expression stack needs at least 2 items for DW_OP_lt.");
  3444. - return false;
  3445. - } else {
  3446. - tmp = stack.back();
  3447. - stack.pop_back();
  3448. - stack.back().ResolveValue(exe_ctx) =
  3449. - stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx);
  3450. - }
  3451. - break;
  3452. -
  3453. - // OPCODE: DW_OP_ne
  3454. - // OPERANDS: none
  3455. - // DESCRIPTION: pops the top two stack values, compares using the
  3456. - // not equal (!=) operator.
  3457. - // STACK RESULT: push the constant value 1 onto the stack if the result
  3458. - // of the operation is true or the constant value 0 if the result of the
  3459. - // operation is false.
  3460. - case DW_OP_ne:
  3461. - if (stack.size() < 2) {
  3462. - if (error_ptr)
  3463. - error_ptr->SetErrorString(
  3464. - "Expression stack needs at least 2 items for DW_OP_ne.");
  3465. - return false;
  3466. - } else {
  3467. - tmp = stack.back();
  3468. - stack.pop_back();
  3469. - stack.back().ResolveValue(exe_ctx) =
  3470. - stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx);
  3471. - }
  3472. - break;
  3473. -
  3474. - // OPCODE: DW_OP_litn
  3475. - // OPERANDS: none
  3476. - // DESCRIPTION: encode the unsigned literal values from 0 through 31.
  3477. - // STACK RESULT: push the unsigned literal constant value onto the top
  3478. - // of the stack.
  3479. - case DW_OP_lit0:
  3480. - case DW_OP_lit1:
  3481. - case DW_OP_lit2:
  3482. - case DW_OP_lit3:
  3483. - case DW_OP_lit4:
  3484. - case DW_OP_lit5:
  3485. - case DW_OP_lit6:
  3486. - case DW_OP_lit7:
  3487. - case DW_OP_lit8:
  3488. - case DW_OP_lit9:
  3489. - case DW_OP_lit10:
  3490. - case DW_OP_lit11:
  3491. - case DW_OP_lit12:
  3492. - case DW_OP_lit13:
  3493. - case DW_OP_lit14:
  3494. - case DW_OP_lit15:
  3495. - case DW_OP_lit16:
  3496. - case DW_OP_lit17:
  3497. - case DW_OP_lit18:
  3498. - case DW_OP_lit19:
  3499. - case DW_OP_lit20:
  3500. - case DW_OP_lit21:
  3501. - case DW_OP_lit22:
  3502. - case DW_OP_lit23:
  3503. - case DW_OP_lit24:
  3504. - case DW_OP_lit25:
  3505. - case DW_OP_lit26:
  3506. - case DW_OP_lit27:
  3507. - case DW_OP_lit28:
  3508. - case DW_OP_lit29:
  3509. - case DW_OP_lit30:
  3510. - case DW_OP_lit31:
  3511. - stack.push_back(to_generic(op - DW_OP_lit0));
  3512. - break;
  3513. -
  3514. - // OPCODE: DW_OP_regN
  3515. - // OPERANDS: none
  3516. - // DESCRIPTION: Push the value in register n on the top of the stack.
  3517. - case DW_OP_reg0:
  3518. - case DW_OP_reg1:
  3519. - case DW_OP_reg2:
  3520. - case DW_OP_reg3:
  3521. - case DW_OP_reg4:
  3522. - case DW_OP_reg5:
  3523. - case DW_OP_reg6:
  3524. - case DW_OP_reg7:
  3525. - case DW_OP_reg8:
  3526. - case DW_OP_reg9:
  3527. - case DW_OP_reg10:
  3528. - case DW_OP_reg11:
  3529. - case DW_OP_reg12:
  3530. - case DW_OP_reg13:
  3531. - case DW_OP_reg14:
  3532. - case DW_OP_reg15:
  3533. - case DW_OP_reg16:
  3534. - case DW_OP_reg17:
  3535. - case DW_OP_reg18:
  3536. - case DW_OP_reg19:
  3537. - case DW_OP_reg20:
  3538. - case DW_OP_reg21:
  3539. - case DW_OP_reg22:
  3540. - case DW_OP_reg23:
  3541. - case DW_OP_reg24:
  3542. - case DW_OP_reg25:
  3543. - case DW_OP_reg26:
  3544. - case DW_OP_reg27:
  3545. - case DW_OP_reg28:
  3546. - case DW_OP_reg29:
  3547. - case DW_OP_reg30:
  3548. - case DW_OP_reg31: {
  3549. - dwarf4_location_description_kind = Register;
  3550. - reg_num = op - DW_OP_reg0;
  3551. -
  3552. - if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
  3553. - stack.push_back(tmp);
  3554. - else
  3555. - return false;
  3556. - } break;
  3557. - // OPCODE: DW_OP_regx
  3558. - // OPERANDS:
  3559. - // ULEB128 literal operand that encodes the register.
  3560. - // DESCRIPTION: Push the value in register on the top of the stack.
  3561. - case DW_OP_regx: {
  3562. - dwarf4_location_description_kind = Register;
  3563. - reg_num = opcodes.GetULEB128(&offset);
  3564. - if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
  3565. - stack.push_back(tmp);
  3566. - else
  3567. - return false;
  3568. - } break;
  3569. -
  3570. - // OPCODE: DW_OP_bregN
  3571. - // OPERANDS:
  3572. - // SLEB128 offset from register N
  3573. - // DESCRIPTION: Value is in memory at the address specified by register
  3574. - // N plus an offset.
  3575. - case DW_OP_breg0:
  3576. - case DW_OP_breg1:
  3577. - case DW_OP_breg2:
  3578. - case DW_OP_breg3:
  3579. - case DW_OP_breg4:
  3580. - case DW_OP_breg5:
  3581. - case DW_OP_breg6:
  3582. - case DW_OP_breg7:
  3583. - case DW_OP_breg8:
  3584. - case DW_OP_breg9:
  3585. - case DW_OP_breg10:
  3586. - case DW_OP_breg11:
  3587. - case DW_OP_breg12:
  3588. - case DW_OP_breg13:
  3589. - case DW_OP_breg14:
  3590. - case DW_OP_breg15:
  3591. - case DW_OP_breg16:
  3592. - case DW_OP_breg17:
  3593. - case DW_OP_breg18:
  3594. - case DW_OP_breg19:
  3595. - case DW_OP_breg20:
  3596. - case DW_OP_breg21:
  3597. - case DW_OP_breg22:
  3598. - case DW_OP_breg23:
  3599. - case DW_OP_breg24:
  3600. - case DW_OP_breg25:
  3601. - case DW_OP_breg26:
  3602. - case DW_OP_breg27:
  3603. - case DW_OP_breg28:
  3604. - case DW_OP_breg29:
  3605. - case DW_OP_breg30:
  3606. - case DW_OP_breg31: {
  3607. - reg_num = op - DW_OP_breg0;
  3608. -
  3609. - if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
  3610. - tmp)) {
  3611. - int64_t breg_offset = opcodes.GetSLEB128(&offset);
  3612. - tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
  3613. - tmp.ClearContext();
  3614. - stack.push_back(tmp);
  3615. - stack.back().SetValueType(Value::ValueType::LoadAddress);
  3616. - } else
  3617. - return false;
  3618. - } break;
  3619. - // OPCODE: DW_OP_bregx
  3620. - // OPERANDS: 2
  3621. - // ULEB128 literal operand that encodes the register.
  3622. - // SLEB128 offset from register N
  3623. - // DESCRIPTION: Value is in memory at the address specified by register
  3624. - // N plus an offset.
  3625. - case DW_OP_bregx: {
  3626. - reg_num = opcodes.GetULEB128(&offset);
  3627. -
  3628. - if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
  3629. - tmp)) {
  3630. - int64_t breg_offset = opcodes.GetSLEB128(&offset);
  3631. - tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
  3632. - tmp.ClearContext();
  3633. - stack.push_back(tmp);
  3634. - stack.back().SetValueType(Value::ValueType::LoadAddress);
  3635. - } else
  3636. - return false;
  3637. - } break;
  3638. -
  3639. - case DW_OP_fbreg:
  3640. - if (exe_ctx) {
  3641. - if (frame) {
  3642. - Scalar value;
  3643. - if (frame->GetFrameBaseValue(value, error_ptr)) {
  3644. - int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
  3645. - value += fbreg_offset;
  3646. - stack.push_back(value);
  3647. - stack.back().SetValueType(Value::ValueType::LoadAddress);
  3648. - } else
  3649. - return false;
  3650. - } else {
  3651. - if (error_ptr)
  3652. - error_ptr->SetErrorString(
  3653. - "Invalid stack frame in context for DW_OP_fbreg opcode.");
  3654. - return false;
  3655. - }
  3656. - } else {
  3657. - if (error_ptr)
  3658. - error_ptr->SetErrorString(
  3659. - "NULL execution context for DW_OP_fbreg.\n");
  3660. - return false;
  3661. - }
  3662. -
  3663. - break;
  3664. -
  3665. - // OPCODE: DW_OP_nop
  3666. - // OPERANDS: none
  3667. - // DESCRIPTION: A place holder. It has no effect on the location stack
  3668. - // or any of its values.
  3669. - case DW_OP_nop:
  3670. - break;
  3671. -
  3672. - // OPCODE: DW_OP_piece
  3673. - // OPERANDS: 1
  3674. - // ULEB128: byte size of the piece
  3675. - // DESCRIPTION: The operand describes the size in bytes of the piece of
  3676. - // the object referenced by the DWARF expression whose result is at the top
  3677. - // of the stack. If the piece is located in a register, but does not occupy
  3678. - // the entire register, the placement of the piece within that register is
  3679. - // defined by the ABI.
  3680. - //
  3681. - // Many compilers store a single variable in sets of registers, or store a
  3682. - // variable partially in memory and partially in registers. DW_OP_piece
  3683. - // provides a way of describing how large a part of a variable a particular
  3684. - // DWARF expression refers to.
  3685. - case DW_OP_piece: {
  3686. - LocationDescriptionKind piece_locdesc = dwarf4_location_description_kind;
  3687. - // Reset for the next piece.
  3688. - dwarf4_location_description_kind = Memory;
  3689. -
  3690. - const uint64_t piece_byte_size = opcodes.GetULEB128(&offset);
  3691. -
  3692. - if (piece_byte_size > 0) {
  3693. - Value curr_piece;
  3694. -
  3695. - if (stack.empty()) {
  3696. - UpdateValueTypeFromLocationDescription(
  3697. - log, dwarf_cu, LocationDescriptionKind::Empty);
  3698. - // In a multi-piece expression, this means that the current piece is
  3699. - // not available. Fill with zeros for now by resizing the data and
  3700. - // appending it
  3701. - curr_piece.ResizeData(piece_byte_size);
  3702. - // Note that "0" is not a correct value for the unknown bits.
  3703. - // It would be better to also return a mask of valid bits together
  3704. - // with the expression result, so the debugger can print missing
  3705. - // members as "<optimized out>" or something.
  3706. - ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
  3707. - pieces.AppendDataToHostBuffer(curr_piece);
  3708. - } else {
  3709. - Status error;
  3710. - // Extract the current piece into "curr_piece"
  3711. - Value curr_piece_source_value(stack.back());
  3712. - stack.pop_back();
  3713. - UpdateValueTypeFromLocationDescription(log, dwarf_cu, piece_locdesc,
  3714. - &curr_piece_source_value);
  3715. -
  3716. - const Value::ValueType curr_piece_source_value_type =
  3717. - curr_piece_source_value.GetValueType();
  3718. - switch (curr_piece_source_value_type) {
  3719. - case Value::ValueType::Invalid:
  3720. - return false;
  3721. - case Value::ValueType::LoadAddress:
  3722. - if (process) {
  3723. - if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
  3724. - lldb::addr_t load_addr =
  3725. - curr_piece_source_value.GetScalar().ULongLong(
  3726. - LLDB_INVALID_ADDRESS);
  3727. - if (process->ReadMemory(
  3728. - load_addr, curr_piece.GetBuffer().GetBytes(),
  3729. - piece_byte_size, error) != piece_byte_size) {
  3730. - if (error_ptr)
  3731. - error_ptr->SetErrorStringWithFormat(
  3732. - "failed to read memory DW_OP_piece(%" PRIu64
  3733. - ") from 0x%" PRIx64,
  3734. - piece_byte_size, load_addr);
  3735. - return false;
  3736. - }
  3737. - } else {
  3738. - if (error_ptr)
  3739. - error_ptr->SetErrorStringWithFormat(
  3740. - "failed to resize the piece memory buffer for "
  3741. - "DW_OP_piece(%" PRIu64 ")",
  3742. - piece_byte_size);
  3743. - return false;
  3744. - }
  3745. - }
  3746. - break;
  3747. -
  3748. - case Value::ValueType::FileAddress:
  3749. - case Value::ValueType::HostAddress:
  3750. - if (error_ptr) {
  3751. - lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong(
  3752. - LLDB_INVALID_ADDRESS);
  3753. - error_ptr->SetErrorStringWithFormat(
  3754. - "failed to read memory DW_OP_piece(%" PRIu64
  3755. - ") from %s address 0x%" PRIx64,
  3756. - piece_byte_size, curr_piece_source_value.GetValueType() ==
  3757. - Value::ValueType::FileAddress
  3758. - ? "file"
  3759. - : "host",
  3760. - addr);
  3761. - }
  3762. - return false;
  3763. -
  3764. - case Value::ValueType::Scalar: {
  3765. - uint32_t bit_size = piece_byte_size * 8;
  3766. - uint32_t bit_offset = 0;
  3767. - Scalar &scalar = curr_piece_source_value.GetScalar();
  3768. - if (!scalar.ExtractBitfield(
  3769. - bit_size, bit_offset)) {
  3770. - if (error_ptr)
  3771. - error_ptr->SetErrorStringWithFormat(
  3772. - "unable to extract %" PRIu64 " bytes from a %" PRIu64
  3773. - " byte scalar value.",
  3774. - piece_byte_size,
  3775. - (uint64_t)curr_piece_source_value.GetScalar()
  3776. - .GetByteSize());
  3777. - return false;
  3778. - }
  3779. - // Create curr_piece with bit_size. By default Scalar
  3780. - // grows to the nearest host integer type.
  3781. - llvm::APInt fail_value(1, 0, false);
  3782. - llvm::APInt ap_int = scalar.UInt128(fail_value);
  3783. - assert(ap_int.getBitWidth() >= bit_size);
  3784. - llvm::ArrayRef<uint64_t> buf{ap_int.getRawData(),
  3785. - ap_int.getNumWords()};
  3786. - curr_piece.GetScalar() = Scalar(llvm::APInt(bit_size, buf));
  3787. - } break;
  3788. - }
  3789. -
  3790. - // Check if this is the first piece?
  3791. - if (op_piece_offset == 0) {
  3792. - // This is the first piece, we should push it back onto the stack
  3793. - // so subsequent pieces will be able to access this piece and add
  3794. - // to it.
  3795. - if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
  3796. - if (error_ptr)
  3797. - error_ptr->SetErrorString("failed to append piece data");
  3798. - return false;
  3799. - }
  3800. - } else {
  3801. - // If this is the second or later piece there should be a value on
  3802. - // the stack.
  3803. - if (pieces.GetBuffer().GetByteSize() != op_piece_offset) {
  3804. - if (error_ptr)
  3805. - error_ptr->SetErrorStringWithFormat(
  3806. - "DW_OP_piece for offset %" PRIu64
  3807. - " but top of stack is of size %" PRIu64,
  3808. - op_piece_offset, pieces.GetBuffer().GetByteSize());
  3809. - return false;
  3810. - }
  3811. -
  3812. - if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
  3813. - if (error_ptr)
  3814. - error_ptr->SetErrorString("failed to append piece data");
  3815. - return false;
  3816. - }
  3817. - }
  3818. - }
  3819. - op_piece_offset += piece_byte_size;
  3820. - }
  3821. - } break;
  3822. -
  3823. - case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
  3824. - if (stack.size() < 1) {
  3825. - UpdateValueTypeFromLocationDescription(log, dwarf_cu,
  3826. - LocationDescriptionKind::Empty);
  3827. - // Reset for the next piece.
  3828. - dwarf4_location_description_kind = Memory;
  3829. - if (error_ptr)
  3830. - error_ptr->SetErrorString(
  3831. - "Expression stack needs at least 1 item for DW_OP_bit_piece.");
  3832. - return false;
  3833. - } else {
  3834. - UpdateValueTypeFromLocationDescription(
  3835. - log, dwarf_cu, dwarf4_location_description_kind, &stack.back());
  3836. - // Reset for the next piece.
  3837. - dwarf4_location_description_kind = Memory;
  3838. - const uint64_t piece_bit_size = opcodes.GetULEB128(&offset);
  3839. - const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset);
  3840. - switch (stack.back().GetValueType()) {
  3841. - case Value::ValueType::Invalid:
  3842. - return false;
  3843. - case Value::ValueType::Scalar: {
  3844. - if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
  3845. - piece_bit_offset)) {
  3846. - if (error_ptr)
  3847. - error_ptr->SetErrorStringWithFormat(
  3848. - "unable to extract %" PRIu64 " bit value with %" PRIu64
  3849. - " bit offset from a %" PRIu64 " bit scalar value.",
  3850. - piece_bit_size, piece_bit_offset,
  3851. - (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
  3852. - return false;
  3853. - }
  3854. - } break;
  3855. -
  3856. - case Value::ValueType::FileAddress:
  3857. - case Value::ValueType::LoadAddress:
  3858. - case Value::ValueType::HostAddress:
  3859. - if (error_ptr) {
  3860. - error_ptr->SetErrorStringWithFormat(
  3861. - "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
  3862. - ", bit_offset = %" PRIu64 ") from an address value.",
  3863. - piece_bit_size, piece_bit_offset);
  3864. - }
  3865. - return false;
  3866. - }
  3867. - }
  3868. - break;
  3869. -
  3870. - // OPCODE: DW_OP_implicit_value
  3871. - // OPERANDS: 2
  3872. - // ULEB128 size of the value block in bytes
  3873. - // uint8_t* block bytes encoding value in target's memory
  3874. - // representation
  3875. - // DESCRIPTION: Value is immediately stored in block in the debug info with
  3876. - // the memory representation of the target.
  3877. - case DW_OP_implicit_value: {
  3878. - dwarf4_location_description_kind = Implicit;
  3879. -
  3880. - const uint32_t len = opcodes.GetULEB128(&offset);
  3881. - const void *data = opcodes.GetData(&offset, len);
  3882. -
  3883. - if (!data) {
  3884. - LLDB_LOG(log, "Evaluate_DW_OP_implicit_value: could not be read data");
  3885. - LLDB_ERRORF(error_ptr, "Could not evaluate %s.",
  3886. - DW_OP_value_to_name(op));
  3887. - return false;
  3888. - }
  3889. -
  3890. - Value result(data, len);
  3891. - stack.push_back(result);
  3892. - break;
  3893. - }
  3894. -
  3895. - case DW_OP_implicit_pointer: {
  3896. - dwarf4_location_description_kind = Implicit;
  3897. - LLDB_ERRORF(error_ptr, "Could not evaluate %s.", DW_OP_value_to_name(op));
  3898. - return false;
  3899. - }
  3900. -
  3901. - // OPCODE: DW_OP_push_object_address
  3902. - // OPERANDS: none
  3903. - // DESCRIPTION: Pushes the address of the object currently being
  3904. - // evaluated as part of evaluation of a user presented expression. This
  3905. - // object may correspond to an independent variable described by its own
  3906. - // DIE or it may be a component of an array, structure, or class whose
  3907. - // address has been dynamically determined by an earlier step during user
  3908. - // expression evaluation.
  3909. - case DW_OP_push_object_address:
  3910. - if (object_address_ptr)
  3911. - stack.push_back(*object_address_ptr);
  3912. - else {
  3913. - if (error_ptr)
  3914. - error_ptr->SetErrorString("DW_OP_push_object_address used without "
  3915. - "specifying an object address");
  3916. - return false;
  3917. - }
  3918. - break;
  3919. -
  3920. - // OPCODE: DW_OP_call2
  3921. - // OPERANDS:
  3922. - // uint16_t compile unit relative offset of a DIE
  3923. - // DESCRIPTION: Performs subroutine calls during evaluation
  3924. - // of a DWARF expression. The operand is the 2-byte unsigned offset of a
  3925. - // debugging information entry in the current compilation unit.
  3926. - //
  3927. - // Operand interpretation is exactly like that for DW_FORM_ref2.
  3928. - //
  3929. - // This operation transfers control of DWARF expression evaluation to the
  3930. - // DW_AT_location attribute of the referenced DIE. If there is no such
  3931. - // attribute, then there is no effect. Execution of the DWARF expression of
  3932. - // a DW_AT_location attribute may add to and/or remove from values on the
  3933. - // stack. Execution returns to the point following the call when the end of
  3934. - // the attribute is reached. Values on the stack at the time of the call
  3935. - // may be used as parameters by the called expression and values left on
  3936. - // the stack by the called expression may be used as return values by prior
  3937. - // agreement between the calling and called expressions.
  3938. - case DW_OP_call2:
  3939. - if (error_ptr)
  3940. - error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2.");
  3941. - return false;
  3942. - // OPCODE: DW_OP_call4
  3943. - // OPERANDS: 1
  3944. - // uint32_t compile unit relative offset of a DIE
  3945. - // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
  3946. - // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of
  3947. - // a debugging information entry in the current compilation unit.
  3948. - //
  3949. - // Operand interpretation DW_OP_call4 is exactly like that for
  3950. - // DW_FORM_ref4.
  3951. - //
  3952. - // This operation transfers control of DWARF expression evaluation to the
  3953. - // DW_AT_location attribute of the referenced DIE. If there is no such
  3954. - // attribute, then there is no effect. Execution of the DWARF expression of
  3955. - // a DW_AT_location attribute may add to and/or remove from values on the
  3956. - // stack. Execution returns to the point following the call when the end of
  3957. - // the attribute is reached. Values on the stack at the time of the call
  3958. - // may be used as parameters by the called expression and values left on
  3959. - // the stack by the called expression may be used as return values by prior
  3960. - // agreement between the calling and called expressions.
  3961. - case DW_OP_call4:
  3962. - if (error_ptr)
  3963. - error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4.");
  3964. - return false;
  3965. -
  3966. - // OPCODE: DW_OP_stack_value
  3967. - // OPERANDS: None
  3968. - // DESCRIPTION: Specifies that the object does not exist in memory but
  3969. - // rather is a constant value. The value from the top of the stack is the
  3970. - // value to be used. This is the actual object value and not the location.
  3971. - case DW_OP_stack_value:
  3972. - dwarf4_location_description_kind = Implicit;
  3973. - if (stack.empty()) {
  3974. - if (error_ptr)
  3975. - error_ptr->SetErrorString(
  3976. - "Expression stack needs at least 1 item for DW_OP_stack_value.");
  3977. - return false;
  3978. - }
  3979. - stack.back().SetValueType(Value::ValueType::Scalar);
  3980. - break;
  3981. -
  3982. - // OPCODE: DW_OP_convert
  3983. - // OPERANDS: 1
  3984. - // A ULEB128 that is either a DIE offset of a
  3985. - // DW_TAG_base_type or 0 for the generic (pointer-sized) type.
  3986. - //
  3987. - // DESCRIPTION: Pop the top stack element, convert it to a
  3988. - // different type, and push the result.
  3989. - case DW_OP_convert: {
  3990. - if (stack.size() < 1) {
  3991. - if (error_ptr)
  3992. - error_ptr->SetErrorString(
  3993. - "Expression stack needs at least 1 item for DW_OP_convert.");
  3994. - return false;
  3995. - }
  3996. - const uint64_t die_offset = opcodes.GetULEB128(&offset);
  3997. - uint64_t bit_size;
  3998. - bool sign;
  3999. - if (die_offset == 0) {
  4000. - // The generic type has the size of an address on the target
  4001. - // machine and an unspecified signedness. Scalar has no
  4002. - // "unspecified signedness", so we use unsigned types.
  4003. - if (!module_sp) {
  4004. - if (error_ptr)
  4005. - error_ptr->SetErrorString("No module");
  4006. - return false;
  4007. - }
  4008. - sign = false;
  4009. - bit_size = module_sp->GetArchitecture().GetAddressByteSize() * 8;
  4010. - if (!bit_size) {
  4011. - if (error_ptr)
  4012. - error_ptr->SetErrorString("unspecified architecture");
  4013. - return false;
  4014. - }
  4015. - } else {
  4016. - // Retrieve the type DIE that the value is being converted to.
  4017. - // FIXME: the constness has annoying ripple effects.
  4018. - DWARFDIE die = const_cast<DWARFUnit *>(dwarf_cu)->GetDIE(die_offset);
  4019. - if (!die) {
  4020. - if (error_ptr)
  4021. - error_ptr->SetErrorString("Cannot resolve DW_OP_convert type DIE");
  4022. - return false;
  4023. - }
  4024. - uint64_t encoding =
  4025. - die.GetAttributeValueAsUnsigned(DW_AT_encoding, DW_ATE_hi_user);
  4026. - bit_size = die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8;
  4027. - if (!bit_size)
  4028. - bit_size = die.GetAttributeValueAsUnsigned(DW_AT_bit_size, 0);
  4029. - if (!bit_size) {
  4030. - if (error_ptr)
  4031. - error_ptr->SetErrorString("Unsupported type size in DW_OP_convert");
  4032. - return false;
  4033. - }
  4034. - switch (encoding) {
  4035. - case DW_ATE_signed:
  4036. - case DW_ATE_signed_char:
  4037. - sign = true;
  4038. - break;
  4039. - case DW_ATE_unsigned:
  4040. - case DW_ATE_unsigned_char:
  4041. - sign = false;
  4042. - break;
  4043. - default:
  4044. - if (error_ptr)
  4045. - error_ptr->SetErrorString("Unsupported encoding in DW_OP_convert");
  4046. - return false;
  4047. - }
  4048. - }
  4049. - Scalar &top = stack.back().ResolveValue(exe_ctx);
  4050. - top.TruncOrExtendTo(bit_size, sign);
  4051. - break;
  4052. - }
  4053. -
  4054. - // OPCODE: DW_OP_call_frame_cfa
  4055. - // OPERANDS: None
  4056. - // DESCRIPTION: Specifies a DWARF expression that pushes the value of
  4057. - // the canonical frame address consistent with the call frame information
  4058. - // located in .debug_frame (or in the FDEs of the eh_frame section).
  4059. - case DW_OP_call_frame_cfa:
  4060. - if (frame) {
  4061. - // Note that we don't have to parse FDEs because this DWARF expression
  4062. - // is commonly evaluated with a valid stack frame.
  4063. - StackID id = frame->GetStackID();
  4064. - addr_t cfa = id.GetCallFrameAddress();
  4065. - if (cfa != LLDB_INVALID_ADDRESS) {
  4066. - stack.push_back(Scalar(cfa));
  4067. - stack.back().SetValueType(Value::ValueType::LoadAddress);
  4068. - } else if (error_ptr)
  4069. - error_ptr->SetErrorString("Stack frame does not include a canonical "
  4070. - "frame address for DW_OP_call_frame_cfa "
  4071. - "opcode.");
  4072. - } else {
  4073. - if (error_ptr)
  4074. - error_ptr->SetErrorString("Invalid stack frame in context for "
  4075. - "DW_OP_call_frame_cfa opcode.");
  4076. - return false;
  4077. - }
  4078. - break;
  4079. -
  4080. - // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
  4081. - // opcode, DW_OP_GNU_push_tls_address)
  4082. - // OPERANDS: none
  4083. - // DESCRIPTION: Pops a TLS offset from the stack, converts it to
  4084. - // an address in the current thread's thread-local storage block, and
  4085. - // pushes it on the stack.
  4086. - case DW_OP_form_tls_address:
  4087. - case DW_OP_GNU_push_tls_address: {
  4088. - if (stack.size() < 1) {
  4089. - if (error_ptr) {
  4090. - if (op == DW_OP_form_tls_address)
  4091. - error_ptr->SetErrorString(
  4092. - "DW_OP_form_tls_address needs an argument.");
  4093. - else
  4094. - error_ptr->SetErrorString(
  4095. - "DW_OP_GNU_push_tls_address needs an argument.");
  4096. - }
  4097. - return false;
  4098. - }
  4099. -
  4100. - if (!exe_ctx || !module_sp) {
  4101. - if (error_ptr)
  4102. - error_ptr->SetErrorString("No context to evaluate TLS within.");
  4103. - return false;
  4104. - }
  4105. -
  4106. - Thread *thread = exe_ctx->GetThreadPtr();
  4107. - if (!thread) {
  4108. - if (error_ptr)
  4109. - error_ptr->SetErrorString("No thread to evaluate TLS within.");
  4110. - return false;
  4111. - }
  4112. -
  4113. - // Lookup the TLS block address for this thread and module.
  4114. - const addr_t tls_file_addr =
  4115. - stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
  4116. - const addr_t tls_load_addr =
  4117. - thread->GetThreadLocalData(module_sp, tls_file_addr);
  4118. -
  4119. - if (tls_load_addr == LLDB_INVALID_ADDRESS) {
  4120. - if (error_ptr)
  4121. - error_ptr->SetErrorString(
  4122. - "No TLS data currently exists for this thread.");
  4123. - return false;
  4124. - }
  4125. -
  4126. - stack.back().GetScalar() = tls_load_addr;
  4127. - stack.back().SetValueType(Value::ValueType::LoadAddress);
  4128. - } break;
  4129. -
  4130. - // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.)
  4131. - // OPERANDS: 1
  4132. - // ULEB128: index to the .debug_addr section
  4133. - // DESCRIPTION: Pushes an address to the stack from the .debug_addr
  4134. - // section with the base address specified by the DW_AT_addr_base attribute
  4135. - // and the 0 based index is the ULEB128 encoded index.
  4136. - case DW_OP_addrx:
  4137. - case DW_OP_GNU_addr_index: {
  4138. - if (!dwarf_cu) {
  4139. - if (error_ptr)
  4140. - error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a "
  4141. - "compile unit being specified");
  4142. - return false;
  4143. - }
  4144. - uint64_t index = opcodes.GetULEB128(&offset);
  4145. - lldb::addr_t value = ReadAddressFromDebugAddrSection(dwarf_cu, index);
  4146. - stack.push_back(Scalar(value));
  4147. - stack.back().SetValueType(Value::ValueType::FileAddress);
  4148. - } break;
  4149. -
  4150. - // OPCODE: DW_OP_GNU_const_index
  4151. - // OPERANDS: 1
  4152. - // ULEB128: index to the .debug_addr section
  4153. - // DESCRIPTION: Pushes an constant with the size of a machine address to
  4154. - // the stack from the .debug_addr section with the base address specified
  4155. - // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
  4156. - // encoded index.
  4157. - case DW_OP_GNU_const_index: {
  4158. - if (!dwarf_cu) {
  4159. - if (error_ptr)
  4160. - error_ptr->SetErrorString("DW_OP_GNU_const_index found without a "
  4161. - "compile unit being specified");
  4162. - return false;
  4163. - }
  4164. - uint64_t index = opcodes.GetULEB128(&offset);
  4165. - lldb::addr_t value = ReadAddressFromDebugAddrSection(dwarf_cu, index);
  4166. - stack.push_back(Scalar(value));
  4167. - } break;
  4168. -
  4169. - case DW_OP_GNU_entry_value:
  4170. - case DW_OP_entry_value: {
  4171. - if (!Evaluate_DW_OP_entry_value(stack, exe_ctx, reg_ctx, opcodes, offset,
  4172. - error_ptr, log)) {
  4173. - LLDB_ERRORF(error_ptr, "Could not evaluate %s.",
  4174. - DW_OP_value_to_name(op));
  4175. - return false;
  4176. - }
  4177. - break;
  4178. - }
  4179. -
  4180. - default:
  4181. - if (error_ptr)
  4182. - error_ptr->SetErrorStringWithFormatv(
  4183. - "Unhandled opcode {0} in DWARFExpression", LocationAtom(op));
  4184. - return false;
  4185. - }
  4186. - }
  4187. -
  4188. - if (stack.empty()) {
  4189. - // Nothing on the stack, check if we created a piece value from DW_OP_piece
  4190. - // or DW_OP_bit_piece opcodes
  4191. - if (pieces.GetBuffer().GetByteSize()) {
  4192. - result = pieces;
  4193. - return true;
  4194. - }
  4195. - if (error_ptr)
  4196. - error_ptr->SetErrorString("Stack empty after evaluation.");
  4197. - return false;
  4198. - }
  4199. -
  4200. - UpdateValueTypeFromLocationDescription(
  4201. - log, dwarf_cu, dwarf4_location_description_kind, &stack.back());
  4202. -
  4203. - if (log && log->GetVerbose()) {
  4204. - size_t count = stack.size();
  4205. - LLDB_LOGF(log,
  4206. - "Stack after operation has %" PRIu64 " values:", (uint64_t)count);
  4207. - for (size_t i = 0; i < count; ++i) {
  4208. - StreamString new_value;
  4209. - new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
  4210. - stack[i].Dump(&new_value);
  4211. - LLDB_LOGF(log, " %s", new_value.GetData());
  4212. - }
  4213. - }
  4214. - result = stack.back();
  4215. - return true; // Return true on success
  4216. + DWARFExpression expr(module_sp, opcodes, dwarf_cu);
  4217. + expr.SetRegisterKind(reg_kind);
  4218. +
  4219. + // Use the DWARF expression evaluator registered for this module (or
  4220. + // DWARFEvaluator by default).
  4221. + DWARFEvaluatorFactory *evaluator_factory =
  4222. + module_sp->GetDWARFExpressionEvaluatorFactory();
  4223. + std::unique_ptr<DWARFEvaluator> evaluator =
  4224. + evaluator_factory->CreateDWARFEvaluator(
  4225. + expr, exe_ctx, reg_ctx, initial_value_ptr, object_address_ptr);
  4226. + return evaluator->Evaluate(result, error_ptr);
  4227. }
  4228. static DataExtractor ToDataExtractor(const llvm::DWARFLocationExpression &loc,
  4229. diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp
  4230. index 00e9ccb76..2137a1ac8 100644
  4231. --- a/lldb/source/Interpreter/CommandInterpreter.cpp
  4232. +++ b/lldb/source/Interpreter/CommandInterpreter.cpp
  4233. @@ -759,6 +759,24 @@ void CommandInterpreter::LoadCommandDictionary() {
  4234. }
  4235. }
  4236. + std::unique_ptr<CommandObjectRegexCommand> connect_wasm_cmd_up(
  4237. + new CommandObjectRegexCommand(
  4238. + *this, "wasm",
  4239. + "Connect to a WebAssembly process via remote GDB server. "
  4240. + "If no host is specifed, localhost is assumed.",
  4241. + "wasm [<hostname>:]<portnum>", 2, 0, false));
  4242. + if (connect_wasm_cmd_up) {
  4243. + if (connect_wasm_cmd_up->AddRegexCommand(
  4244. + "^([^:]+|\\[[0-9a-fA-F:]+.*\\]):([0-9]+)$",
  4245. + "process connect --plugin wasm connect://%1:%2") &&
  4246. + connect_wasm_cmd_up->AddRegexCommand(
  4247. + "^([[:digit:]]+)$",
  4248. + "process connect --plugin wasm connect://localhost:%1")) {
  4249. + CommandObjectSP command_sp(connect_wasm_cmd_up.release());
  4250. + m_command_dict[std::string(command_sp->GetCommandName())] = command_sp;
  4251. + }
  4252. + }
  4253. +
  4254. std::unique_ptr<CommandObjectRegexCommand> connect_kdp_remote_cmd_up(
  4255. new CommandObjectRegexCommand(
  4256. *this, "kdp-remote",
  4257. diff --git a/lldb/source/Plugins/CMakeLists.txt b/lldb/source/Plugins/CMakeLists.txt
  4258. index 9181a4e47..2be6ec365 100644
  4259. --- a/lldb/source/Plugins/CMakeLists.txt
  4260. +++ b/lldb/source/Plugins/CMakeLists.txt
  4261. @@ -2,6 +2,7 @@ add_subdirectory(ABI)
  4262. add_subdirectory(Architecture)
  4263. add_subdirectory(Disassembler)
  4264. add_subdirectory(DynamicLoader)
  4265. +add_subdirectory(DWARFEvaluator)
  4266. add_subdirectory(ExpressionParser)
  4267. add_subdirectory(Instruction)
  4268. add_subdirectory(InstrumentationRuntime)
  4269. @@ -32,6 +33,7 @@ set(LLDB_ENUM_PLUGINS "")
  4270. # FIXME: ProcessWindowsCommon needs to be initialized after all other process
  4271. # plugins but before ProcessGDBRemote.
  4272. set(LLDB_PROCESS_WINDOWS_PLUGIN "")
  4273. +set(LLDB_PROCESS_WASM_PLUGIN "")
  4274. set(LLDB_PROCESS_GDB_PLUGIN "")
  4275. foreach(p ${LLDB_ALL_PLUGINS})
  4276. @@ -43,6 +45,8 @@ foreach(p ${LLDB_ALL_PLUGINS})
  4277. set(LLDB_PROCESS_WINDOWS_PLUGIN "LLDB_PLUGIN(${pStripped})\n")
  4278. elseif(${pStripped} STREQUAL "ProcessGDBRemote")
  4279. set(LLDB_PROCESS_GDB_PLUGIN "LLDB_PLUGIN(${pStripped})\n")
  4280. + elseif(${pStripped} STREQUAL "ProcessWasm")
  4281. + set(LLDB_PROCESS_WASM_PLUGIN "LLDB_PLUGIN(${pStripped})\n")
  4282. else()
  4283. set(LLDB_ENUM_PLUGINS "${LLDB_ENUM_PLUGINS}LLDB_PLUGIN(${pStripped})\n")
  4284. endif()
  4285. diff --git a/lldb/source/Plugins/DWARFEvaluator/CMakeLists.txt b/lldb/source/Plugins/DWARFEvaluator/CMakeLists.txt
  4286. new file mode 100644
  4287. index 000000000..73fad41e1
  4288. --- /dev/null
  4289. +++ b/lldb/source/Plugins/DWARFEvaluator/CMakeLists.txt
  4290. @@ -0,0 +1 @@
  4291. +add_subdirectory(wasm)
  4292. diff --git a/lldb/source/Plugins/DWARFEvaluator/wasm/CMakeLists.txt b/lldb/source/Plugins/DWARFEvaluator/wasm/CMakeLists.txt
  4293. new file mode 100644
  4294. index 000000000..e50b1bef7
  4295. --- /dev/null
  4296. +++ b/lldb/source/Plugins/DWARFEvaluator/wasm/CMakeLists.txt
  4297. @@ -0,0 +1,10 @@
  4298. +add_lldb_library(lldbPluginWasmDWARFEvaluatorFactory PLUGIN
  4299. + WasmDWARFEvaluator.cpp
  4300. + WasmDWARFEvaluatorFactory.cpp
  4301. +
  4302. + LINK_LIBS
  4303. + lldbCore
  4304. + lldbHost
  4305. + lldbSymbol
  4306. + lldbPluginObjectFileWasm
  4307. + )
  4308. diff --git a/lldb/source/Plugins/DWARFEvaluator/wasm/WasmDWARFEvaluator.cpp b/lldb/source/Plugins/DWARFEvaluator/wasm/WasmDWARFEvaluator.cpp
  4309. new file mode 100644
  4310. index 000000000..fdda1991d
  4311. --- /dev/null
  4312. +++ b/lldb/source/Plugins/DWARFEvaluator/wasm/WasmDWARFEvaluator.cpp
  4313. @@ -0,0 +1,126 @@
  4314. +//===-- WasmDWARFEvaluator.cpp --------------------------------------------===//
  4315. +//
  4316. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4317. +// See https://llvm.org/LICENSE.txt for license information.
  4318. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4319. +//
  4320. +//===----------------------------------------------------------------------===//
  4321. +
  4322. +#include "WasmDWARFEvaluator.h"
  4323. +
  4324. +#include "Plugins/ObjectFile/wasm/ObjectFileWasm.h"
  4325. +#include "Plugins/Process/wasm/ProcessWasm.h"
  4326. +#include "lldb/Core/Module.h"
  4327. +#include "lldb/Core/PluginManager.h"
  4328. +#include "lldb/Core/Value.h"
  4329. +#include "lldb/Core/dwarf.h"
  4330. +#include "lldb/Expression/DWARFExpression.h"
  4331. +
  4332. +using namespace lldb;
  4333. +using namespace lldb_private;
  4334. +using namespace lldb_private::wasm;
  4335. +
  4336. +bool WasmDWARFEvaluator::Evaluate(const uint8_t op, Process *process,
  4337. + StackFrame *frame, std::vector<Value> &stack,
  4338. + const DataExtractor &opcodes,
  4339. + lldb::offset_t &offset, Value &pieces,
  4340. + uint64_t &op_piece_offset, Log *log,
  4341. + Status *error_ptr) {
  4342. + lldb::ModuleSP module_sp = m_dwarf_expression.GetModule();
  4343. +
  4344. + switch (op) {
  4345. + case DW_OP_WASM_location: {
  4346. + if (frame) {
  4347. + const llvm::Triple::ArchType machine =
  4348. + frame->CalculateTarget()->GetArchitecture().GetMachine();
  4349. + if (machine != llvm::Triple::wasm32) {
  4350. + if (error_ptr)
  4351. + error_ptr->SetErrorString("Invalid target architecture for "
  4352. + "DW_OP_WASM_location opcode.");
  4353. + return false;
  4354. + }
  4355. +
  4356. + ProcessWasm *wasm_process =
  4357. + static_cast<wasm::ProcessWasm *>(frame->CalculateProcess().get());
  4358. + int frame_index = frame->GetConcreteFrameIndex();
  4359. + uint64_t wasm_op = opcodes.GetULEB128(&offset);
  4360. + uint64_t index = opcodes.GetULEB128(&offset);
  4361. + uint8_t buf[16];
  4362. + size_t size = 0;
  4363. + switch (wasm_op) {
  4364. + case 0: // Local
  4365. + if (!wasm_process->GetWasmLocal(frame_index, index, buf, 16, size)) {
  4366. + return false;
  4367. + }
  4368. + break;
  4369. + case 1: // Global
  4370. + if (!wasm_process->GetWasmGlobal(frame_index, index, buf, 16, size)) {
  4371. + return false;
  4372. + }
  4373. + break;
  4374. + case 2: // Operand Stack
  4375. + if (!wasm_process->GetWasmStackValue(frame_index, index, buf, 16,
  4376. + size)) {
  4377. + return false;
  4378. + }
  4379. + break;
  4380. + default:
  4381. + return false;
  4382. + }
  4383. +
  4384. + if (size == sizeof(uint32_t)) {
  4385. + uint32_t value;
  4386. + memcpy(&value, buf, size);
  4387. + stack.push_back(Scalar(value));
  4388. + } else if (size == sizeof(uint64_t)) {
  4389. + uint64_t value;
  4390. + memcpy(&value, buf, size);
  4391. + stack.push_back(Scalar(value));
  4392. + } else
  4393. + return false;
  4394. + } else {
  4395. + if (error_ptr)
  4396. + error_ptr->SetErrorString("Invalid stack frame in context for "
  4397. + "DW_OP_WASM_location opcode.");
  4398. + return false;
  4399. + }
  4400. + } break;
  4401. +
  4402. + case DW_OP_addr: {
  4403. + /// {addr} is an offset in the module Data section.
  4404. + lldb::addr_t addr = opcodes.GetAddress(&offset);
  4405. + stack.push_back(Scalar(addr));
  4406. + stack.back().SetValueType(Value::ValueType::LoadAddress);
  4407. + } break;
  4408. +
  4409. + case DW_OP_fbreg:
  4410. + if (m_exe_ctx) {
  4411. + if (frame) {
  4412. + Scalar value;
  4413. + if (frame->GetFrameBaseValue(value, error_ptr)) {
  4414. + // The value is an address in the Wasm Memory space.
  4415. + int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
  4416. + stack.push_back(Scalar(value.ULong() + fbreg_offset));
  4417. + stack.back().SetValueType(Value::ValueType::LoadAddress);
  4418. + } else
  4419. + return false;
  4420. + } else {
  4421. + if (error_ptr)
  4422. + error_ptr->SetErrorString(
  4423. + "Invalid stack frame in context for DW_OP_fbreg opcode.");
  4424. + return false;
  4425. + }
  4426. + } else {
  4427. + if (error_ptr)
  4428. + error_ptr->SetErrorStringWithFormat(
  4429. + "NULL execution context for DW_OP_fbreg.\n");
  4430. + return false;
  4431. + }
  4432. + break;
  4433. +
  4434. + default:
  4435. + return DWARFEvaluator::Evaluate(op, process, frame, stack, opcodes, offset,
  4436. + pieces, op_piece_offset, log, error_ptr);
  4437. + }
  4438. + return true;
  4439. +}
  4440. diff --git a/lldb/source/Plugins/DWARFEvaluator/wasm/WasmDWARFEvaluator.h b/lldb/source/Plugins/DWARFEvaluator/wasm/WasmDWARFEvaluator.h
  4441. new file mode 100644
  4442. index 000000000..a01159064
  4443. --- /dev/null
  4444. +++ b/lldb/source/Plugins/DWARFEvaluator/wasm/WasmDWARFEvaluator.h
  4445. @@ -0,0 +1,47 @@
  4446. +//===-- WasmDWARFEvaluator.h ------------------------------------*- C++ -*-===//
  4447. +//
  4448. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4449. +// See https://llvm.org/LICENSE.txt for license information.
  4450. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4451. +//
  4452. +//===----------------------------------------------------------------------===//
  4453. +
  4454. +#ifndef LLDB_SOURCE_PLUGINS_DWARFEVALUATOR_WASM_WASMDWARFEVALUATOR_H
  4455. +#define LLDB_SOURCE_PLUGINS_DWARFEVALUATOR_WASM_WASMDWARFEVALUATOR_H
  4456. +
  4457. +#include "lldb/Expression/DWARFEvaluator.h"
  4458. +#include "lldb/lldb-private.h"
  4459. +
  4460. +namespace lldb_private {
  4461. +namespace wasm {
  4462. +
  4463. +/// \class WasmDWARFEvaluator evaluates DWARF expressions in the context of a
  4464. +/// WebAssembly process.
  4465. +///
  4466. +class WasmDWARFEvaluator : public DWARFEvaluator {
  4467. +public:
  4468. + WasmDWARFEvaluator(const DWARFExpression &dwarf_expression,
  4469. + ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
  4470. + const Value *initial_value_ptr,
  4471. + const Value *object_address_ptr)
  4472. + : DWARFEvaluator(dwarf_expression, exe_ctx, reg_ctx, initial_value_ptr,
  4473. + object_address_ptr) {}
  4474. +
  4475. + /// DWARFEvaluator protocol.
  4476. + /// \{
  4477. + bool Evaluate(const uint8_t op, Process *process, StackFrame *frame,
  4478. + std::vector<Value> &stack, const DataExtractor &opcodes,
  4479. + lldb::offset_t &offset, Value &pieces,
  4480. + uint64_t &op_piece_offset, Log *log,
  4481. + Status *error_ptr) override;
  4482. + /// \}
  4483. +
  4484. +private:
  4485. + WasmDWARFEvaluator(const WasmDWARFEvaluator &);
  4486. + const WasmDWARFEvaluator &operator=(const WasmDWARFEvaluator &) = delete;
  4487. +};
  4488. +
  4489. +} // namespace wasm
  4490. +} // namespace lldb_private
  4491. +
  4492. +#endif // LLDB_SOURCE_PLUGINS_DWARFEVALUATOR_WASM_WASMDWARFEVALUATOR_H
  4493. diff --git a/lldb/source/Plugins/DWARFEvaluator/wasm/WasmDWARFEvaluatorFactory.cpp b/lldb/source/Plugins/DWARFEvaluator/wasm/WasmDWARFEvaluatorFactory.cpp
  4494. new file mode 100644
  4495. index 000000000..d43e96a34
  4496. --- /dev/null
  4497. +++ b/lldb/source/Plugins/DWARFEvaluator/wasm/WasmDWARFEvaluatorFactory.cpp
  4498. @@ -0,0 +1,64 @@
  4499. +//===-- WasmDWARFEvaluatorFactory.cpp -------------------------------------===//
  4500. +//
  4501. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4502. +// See https://llvm.org/LICENSE.txt for license information.
  4503. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4504. +//
  4505. +//===----------------------------------------------------------------------===//
  4506. +
  4507. +#include "WasmDWARFEvaluatorFactory.h"
  4508. +#include "WasmDWARFEvaluator.h"
  4509. +
  4510. +#include "Plugins/ObjectFile/wasm/ObjectFileWasm.h"
  4511. +#include "lldb/Core/Module.h"
  4512. +#include "lldb/Core/PluginManager.h"
  4513. +
  4514. +using namespace lldb;
  4515. +using namespace lldb_private;
  4516. +using namespace lldb_private::wasm;
  4517. +
  4518. +LLDB_PLUGIN_DEFINE(WasmDWARFEvaluatorFactory)
  4519. +
  4520. +void WasmDWARFEvaluatorFactory::Initialize() {
  4521. + PluginManager::RegisterPlugin(GetPluginNameStatic(),
  4522. + GetPluginDescriptionStatic(), CreateInstance);
  4523. +}
  4524. +
  4525. +void WasmDWARFEvaluatorFactory::Terminate() {
  4526. + PluginManager::UnregisterPlugin(CreateInstance);
  4527. +}
  4528. +
  4529. +lldb_private::ConstString WasmDWARFEvaluatorFactory::GetPluginNameStatic() {
  4530. + static ConstString g_name("WASM");
  4531. + return g_name;
  4532. +}
  4533. +
  4534. +const char *WasmDWARFEvaluatorFactory::GetPluginDescriptionStatic() {
  4535. + return "DWARF expression evaluator factory for WASM.";
  4536. +}
  4537. +
  4538. +// CreateInstance
  4539. +//
  4540. +// Platforms can register a callback to use when creating DWARF expression
  4541. +// evaluators to allow handling platform-specific DWARF codes.
  4542. +DWARFEvaluatorFactory *
  4543. +WasmDWARFEvaluatorFactory::CreateInstance(Module *module) {
  4544. + if (!module)
  4545. + return nullptr;
  4546. +
  4547. + ObjectFileWasm *obj_file =
  4548. + llvm::dyn_cast_or_null<ObjectFileWasm>(module->GetObjectFile());
  4549. + if (!obj_file)
  4550. + return nullptr;
  4551. +
  4552. + return new WasmDWARFEvaluatorFactory();
  4553. +}
  4554. +
  4555. +std::unique_ptr<DWARFEvaluator> WasmDWARFEvaluatorFactory::CreateDWARFEvaluator(
  4556. + const DWARFExpression &dwarf_expression, ExecutionContext *exe_ctx,
  4557. + RegisterContext *reg_ctx, const Value *initial_value_ptr,
  4558. + const Value *object_address_ptr) {
  4559. + return std::make_unique<WasmDWARFEvaluator>(dwarf_expression, exe_ctx,
  4560. + reg_ctx, initial_value_ptr,
  4561. + object_address_ptr);
  4562. +}
  4563. diff --git a/lldb/source/Plugins/DWARFEvaluator/wasm/WasmDWARFEvaluatorFactory.h b/lldb/source/Plugins/DWARFEvaluator/wasm/WasmDWARFEvaluatorFactory.h
  4564. new file mode 100644
  4565. index 000000000..8a946592a
  4566. --- /dev/null
  4567. +++ b/lldb/source/Plugins/DWARFEvaluator/wasm/WasmDWARFEvaluatorFactory.h
  4568. @@ -0,0 +1,55 @@
  4569. +//===-- WasmDWARFEvaluatorFactory.h -----------------------------*- C++ -*-===//
  4570. +//
  4571. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4572. +// See https://llvm.org/LICENSE.txt for license information.
  4573. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4574. +//
  4575. +//===----------------------------------------------------------------------===//
  4576. +
  4577. +#ifndef LLDB_SOURCE_PLUGINS_DWARFEVALUATOR_WASM_WASMDWARFEVALUATORFACTORY_H
  4578. +#define LLDB_SOURCE_PLUGINS_DWARFEVALUATOR_WASM_WASMDWARFEVALUATORFACTORY_H
  4579. +
  4580. +#include "lldb/Expression/DWARFEvaluatorFactory.h"
  4581. +
  4582. +namespace lldb_private {
  4583. +namespace wasm {
  4584. +
  4585. +/// \class WasmDWARFEvaluatorFactory creates DWARF evaluators specialized to
  4586. +/// manage DWARF-specific opcodes.
  4587. +class WasmDWARFEvaluatorFactory : public DWARFEvaluatorFactory {
  4588. +public:
  4589. + static void Initialize();
  4590. + static void Terminate();
  4591. + static lldb_private::ConstString GetPluginNameStatic();
  4592. + static const char *GetPluginDescriptionStatic();
  4593. +
  4594. + static lldb_private::DWARFEvaluatorFactory *CreateInstance(Module *module);
  4595. +
  4596. + /// PluginInterface protocol.
  4597. + /// \{
  4598. + lldb_private::ConstString GetPluginName() override {
  4599. + return GetPluginNameStatic();
  4600. + }
  4601. + uint32_t GetPluginVersion() override { return 1; }
  4602. + /// \}
  4603. +
  4604. + WasmDWARFEvaluatorFactory() {}
  4605. +
  4606. + /// DWARFEvaluatorFactory protocol.
  4607. + /// \{
  4608. + std::unique_ptr<DWARFEvaluator>
  4609. + CreateDWARFEvaluator(const DWARFExpression &dwarf_expression,
  4610. + ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
  4611. + const Value *initial_value_ptr,
  4612. + const Value *object_address_ptr) override;
  4613. + /// \}
  4614. +
  4615. +private:
  4616. + WasmDWARFEvaluatorFactory(const WasmDWARFEvaluatorFactory &);
  4617. + const WasmDWARFEvaluatorFactory &operator=(const WasmDWARFEvaluatorFactory &) = delete;
  4618. +};
  4619. +
  4620. +} // namespace wasm
  4621. +} // namespace lldb_private
  4622. +
  4623. +#endif // LLDB_SOURCE_PLUGINS_DWARFEVALUATOR_WASM_WASMDWARFEVALUATORFACTORY_H
  4624. diff --git a/lldb/source/Plugins/DynamicLoader/wasm-DYLD/DynamicLoaderWasmDYLD.cpp b/lldb/source/Plugins/DynamicLoader/wasm-DYLD/DynamicLoaderWasmDYLD.cpp
  4625. index ae7e011ea..24ea75d19 100644
  4626. --- a/lldb/source/Plugins/DynamicLoader/wasm-DYLD/DynamicLoaderWasmDYLD.cpp
  4627. +++ b/lldb/source/Plugins/DynamicLoader/wasm-DYLD/DynamicLoaderWasmDYLD.cpp
  4628. @@ -62,6 +62,15 @@ void DynamicLoaderWasmDYLD::DidAttach() {
  4629. // Ask the process for the list of loaded WebAssembly modules.
  4630. auto error = m_process->LoadModules();
  4631. LLDB_LOG_ERROR(log, std::move(error), "Couldn't load modules: {0}");
  4632. +
  4633. + // TODO: multi-modules support ?
  4634. + Target &target = m_process->GetTarget();
  4635. + const ModuleList &modules = target.GetImages();
  4636. + ModuleSP module_sp(modules.GetModuleAtIndex(0));
  4637. + // module_sp is nullptr if without libxml2
  4638. + if(module_sp) {
  4639. + module_sp->PreloadSymbols();
  4640. + }
  4641. }
  4642. ThreadPlanSP DynamicLoaderWasmDYLD::GetStepThroughTrampolinePlan(Thread &thread,
  4643. diff --git a/lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp b/lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp
  4644. index 5272da9ab..abc5523bf 100644
  4645. --- a/lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp
  4646. +++ b/lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp
  4647. @@ -23,6 +23,7 @@
  4648. #include "llvm/BinaryFormat/Wasm.h"
  4649. #include "llvm/Support/Endian.h"
  4650. #include "llvm/Support/Format.h"
  4651. +#include "Plugins/Process/wasm/ProcessWasm.h"
  4652. using namespace lldb;
  4653. using namespace lldb_private;
  4654. @@ -341,6 +342,8 @@ void ObjectFileWasm::CreateSections(SectionList &unified_section_list) {
  4655. 0, // Alignment of the section
  4656. 0, // Flags for this section.
  4657. 1)); // Number of host bytes per target byte
  4658. + if (section_type == eSectionTypeCode)
  4659. + section_sp->SetPermissions(ePermissionsReadable|ePermissionsExecutable);
  4660. m_sections_up->AddSection(section_sp);
  4661. unified_section_list.AddSection(section_sp);
  4662. }
  4663. @@ -367,6 +370,7 @@ bool ObjectFileWasm::SetLoadAddress(Target &target, lldb::addr_t load_address,
  4664. assert(m_memory_addr == LLDB_INVALID_ADDRESS ||
  4665. m_memory_addr == load_address);
  4666. + lldb::addr_t adjust_addr;
  4667. ModuleSP module_sp = GetModule();
  4668. if (!module_sp)
  4669. return false;
  4670. @@ -381,8 +385,9 @@ bool ObjectFileWasm::SetLoadAddress(Target &target, lldb::addr_t load_address,
  4671. const size_t num_sections = section_list->GetSize();
  4672. for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
  4673. SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
  4674. + adjust_addr = load_address;
  4675. if (target.SetSectionLoadAddress(
  4676. - section_sp, load_address | section_sp->GetFileOffset())) {
  4677. + section_sp, adjust_addr | section_sp->GetFileOffset())) {
  4678. ++num_loaded_sections;
  4679. }
  4680. }
  4681. diff --git a/lldb/source/Plugins/Platform/CMakeLists.txt b/lldb/source/Plugins/Platform/CMakeLists.txt
  4682. index 5f284e517..6084cbc93 100644
  4683. --- a/lldb/source/Plugins/Platform/CMakeLists.txt
  4684. +++ b/lldb/source/Plugins/Platform/CMakeLists.txt
  4685. @@ -15,3 +15,4 @@
  4686. add_subdirectory(POSIX)
  4687. add_subdirectory(gdb-server)
  4688. add_subdirectory(Android)
  4689. +add_subdirectory(wasm-remote)
  4690. diff --git a/lldb/source/Plugins/Platform/wasm-remote/CMakeLists.txt b/lldb/source/Plugins/Platform/wasm-remote/CMakeLists.txt
  4691. new file mode 100644
  4692. index 000000000..4a65765a5
  4693. --- /dev/null
  4694. +++ b/lldb/source/Plugins/Platform/wasm-remote/CMakeLists.txt
  4695. @@ -0,0 +1,10 @@
  4696. +add_lldb_library(lldbPluginPlatformWasm PLUGIN
  4697. + PlatformRemoteWasmServer.cpp
  4698. +
  4699. + LINK_LIBS
  4700. + lldbBreakpoint
  4701. + lldbCore
  4702. + lldbHost
  4703. + lldbTarget
  4704. + lldbPluginProcessUtility
  4705. + )
  4706. diff --git a/lldb/source/Plugins/Platform/wasm-remote/PlatformRemoteWasmServer.cpp b/lldb/source/Plugins/Platform/wasm-remote/PlatformRemoteWasmServer.cpp
  4707. new file mode 100644
  4708. index 000000000..f26d11f00
  4709. --- /dev/null
  4710. +++ b/lldb/source/Plugins/Platform/wasm-remote/PlatformRemoteWasmServer.cpp
  4711. @@ -0,0 +1,139 @@
  4712. +#include "PlatformRemoteWasmServer.h"
  4713. +#include "lldb/Host/Config.h"
  4714. +
  4715. +#include "lldb/Breakpoint/BreakpointLocation.h"
  4716. +#include "lldb/Core/Debugger.h"
  4717. +#include "lldb/Core/Module.h"
  4718. +#include "lldb/Core/ModuleList.h"
  4719. +#include "lldb/Core/ModuleSpec.h"
  4720. +#include "lldb/Core/PluginManager.h"
  4721. +#include "lldb/Core/StreamFile.h"
  4722. +#include "lldb/Host/ConnectionFileDescriptor.h"
  4723. +#include "lldb/Host/Host.h"
  4724. +#include "lldb/Host/HostInfo.h"
  4725. +#include "lldb/Host/PosixApi.h"
  4726. +#include "lldb/Target/Process.h"
  4727. +#include "lldb/Target/Target.h"
  4728. +#include "lldb/Utility/FileSpec.h"
  4729. +#include "lldb/Utility/Log.h"
  4730. +#include "lldb/Utility/ProcessInfo.h"
  4731. +#include "lldb/Utility/Status.h"
  4732. +#include "lldb/Utility/StreamString.h"
  4733. +#include "lldb/Utility/UriParser.h"
  4734. +
  4735. +#include "Plugins/Process/Utility/GDBRemoteSignals.h"
  4736. +
  4737. +using namespace lldb;
  4738. +using namespace lldb_private;
  4739. +using namespace lldb_private::platform_wasm_server;
  4740. +
  4741. +LLDB_PLUGIN_DEFINE_ADV(PlatformRemoteWASMServer, PlatformWasm)
  4742. +
  4743. +static bool g_initialized = false;
  4744. +
  4745. +void PlatformRemoteWASMServer::Initialize() {
  4746. + Platform::Initialize();
  4747. +
  4748. + if (!g_initialized) {
  4749. + g_initialized = true;
  4750. + PluginManager::RegisterPlugin(
  4751. + PlatformRemoteWASMServer::GetPluginNameStatic(),
  4752. + PlatformRemoteWASMServer::GetDescriptionStatic(),
  4753. + PlatformRemoteWASMServer::CreateInstance);
  4754. + }
  4755. +}
  4756. +
  4757. +void PlatformRemoteWASMServer::Terminate() {
  4758. + if (g_initialized) {
  4759. + g_initialized = false;
  4760. + PluginManager::UnregisterPlugin(PlatformRemoteWASMServer::CreateInstance);
  4761. + }
  4762. +
  4763. + Platform::Terminate();
  4764. +}
  4765. +
  4766. +PlatformSP PlatformRemoteWASMServer::CreateInstance(bool force,
  4767. + const ArchSpec *arch) {
  4768. + Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
  4769. + if (log) {
  4770. + const char *arch_name;
  4771. + if (arch && arch->GetArchitectureName())
  4772. + arch_name = arch->GetArchitectureName();
  4773. + else
  4774. + arch_name = "<null>";
  4775. +
  4776. + const char *triple_cstr =
  4777. + arch ? arch->GetTriple().getTriple().c_str() : "<null>";
  4778. +
  4779. + LLDB_LOGF(log, "PlatformRemoteWASMServer::%s(force=%s, arch={%s,%s})",
  4780. + __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr);
  4781. + }
  4782. +
  4783. + bool create = force;
  4784. + if (!create && arch && arch->IsValid()) {
  4785. + const llvm::Triple &triple = arch->GetTriple();
  4786. + if (arch->GetMachine() == llvm::Triple::wasm32 &&
  4787. + triple.getOS() == llvm::Triple::WASI) {
  4788. + create = true;
  4789. + }
  4790. + }
  4791. +
  4792. + if (create) {
  4793. + if (log)
  4794. + LLDB_LOGF(log, "PlatformRemoteWASMServer::%s() creating platform",
  4795. + __FUNCTION__);
  4796. + return PlatformSP(new PlatformRemoteWASMServer());
  4797. + }
  4798. +
  4799. + if (log)
  4800. + LLDB_LOGF(log,
  4801. + "PlatformRemoteWASMServer::%s() aborting creation of platform",
  4802. + __FUNCTION__);
  4803. + return PlatformSP();
  4804. +}
  4805. +
  4806. +ConstString PlatformRemoteWASMServer::GetPluginNameStatic() {
  4807. + static ConstString g_name("remote-wasm-server");
  4808. + return g_name;
  4809. +}
  4810. +
  4811. +ConstString PlatformRemoteWASMServer::GetPluginName() {
  4812. + return GetPluginNameStatic();
  4813. +}
  4814. +
  4815. +const char *PlatformRemoteWASMServer::GetDescriptionStatic() {
  4816. + return "A platform that uses the GDB remote protocol as the communication "
  4817. + "transport for Wasm Runtime";
  4818. +}
  4819. +
  4820. +size_t PlatformRemoteWASMServer::ConnectToWaitingProcesses(Debugger &debugger,
  4821. + Status &error) {
  4822. + std::vector<std::string> connection_urls;
  4823. + GetPendingGdbServerList(connection_urls);
  4824. +
  4825. + for (size_t i = 0; i < connection_urls.size(); ++i) {
  4826. + ConnectProcess(connection_urls[i].c_str(), "wasm", debugger, nullptr, error);
  4827. + if (error.Fail())
  4828. + return i; // We already connected to i process succsessfully
  4829. + }
  4830. + return connection_urls.size();
  4831. +}
  4832. +
  4833. +bool PlatformRemoteWASMServer::GetSupportedArchitectureAtIndex(uint32_t idx,
  4834. + ArchSpec &arch) {
  4835. + ArchSpec remote_arch = m_gdb_client.GetSystemArchitecture();
  4836. + if (idx == 0) {
  4837. + arch = remote_arch;
  4838. + return arch.IsValid();
  4839. + } else if (idx == 1 && remote_arch.IsValid() &&
  4840. + remote_arch.GetTriple().getOS() == llvm::Triple::WASI) {
  4841. + return arch.IsValid();
  4842. + }
  4843. + return false;
  4844. +}
  4845. +
  4846. +/// Default Constructor
  4847. +PlatformRemoteWASMServer::PlatformRemoteWASMServer()
  4848. + : PlatformRemoteGDBServer()
  4849. + {
  4850. + }
  4851. \ No newline at end of file
  4852. diff --git a/lldb/source/Plugins/Platform/wasm-remote/PlatformRemoteWasmServer.h b/lldb/source/Plugins/Platform/wasm-remote/PlatformRemoteWasmServer.h
  4853. new file mode 100644
  4854. index 000000000..f306a79d3
  4855. --- /dev/null
  4856. +++ b/lldb/source/Plugins/Platform/wasm-remote/PlatformRemoteWasmServer.h
  4857. @@ -0,0 +1,37 @@
  4858. +#ifndef LLDB_SOURCE_PLUGINS_PLATFORM_GDB_SERVER_PLATFORMREMOTEWASMSERVER_H
  4859. +#define LLDB_SOURCE_PLUGINS_PLATFORM_GDB_SERVER_PLATFORMREMOTEWASMSERVER_H
  4860. +
  4861. +#include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
  4862. +#include "lldb/Target/Platform.h"
  4863. +
  4864. +namespace lldb_private {
  4865. +namespace platform_wasm_server {
  4866. +
  4867. +class PlatformRemoteWASMServer : public lldb_private::platform_gdb_server::PlatformRemoteGDBServer{
  4868. +
  4869. +public:
  4870. + static void Initialize();
  4871. +
  4872. + static void Terminate();
  4873. +
  4874. + static lldb::PlatformSP CreateInstance(bool force, const ArchSpec *arch);
  4875. +
  4876. + static ConstString GetPluginNameStatic();
  4877. +
  4878. + static const char *GetDescriptionStatic();
  4879. +
  4880. + size_t ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
  4881. + lldb_private::Status &error) override;
  4882. +
  4883. + bool GetSupportedArchitectureAtIndex(uint32_t idx, ArchSpec &arch) override;
  4884. +
  4885. + ConstString GetPluginName() override;
  4886. +
  4887. + PlatformRemoteWASMServer();
  4888. +
  4889. +};
  4890. +
  4891. +} // namespace platform_wasm_server
  4892. +} // namespace lldb_private
  4893. +
  4894. +#endif
  4895. \ No newline at end of file
  4896. diff --git a/lldb/source/Plugins/Plugins.def.in b/lldb/source/Plugins/Plugins.def.in
  4897. index bf54598fb..b0bd7b996 100644
  4898. --- a/lldb/source/Plugins/Plugins.def.in
  4899. +++ b/lldb/source/Plugins/Plugins.def.in
  4900. @@ -31,6 +31,7 @@
  4901. @LLDB_ENUM_PLUGINS@
  4902. @LLDB_PROCESS_WINDOWS_PLUGIN@
  4903. +@LLDB_PROCESS_WASM_PLUGIN@
  4904. @LLDB_PROCESS_GDB_PLUGIN@
  4905. #undef LLDB_PLUGIN
  4906. diff --git a/lldb/source/Plugins/Process/CMakeLists.txt b/lldb/source/Plugins/Process/CMakeLists.txt
  4907. index bea5bac9e..7a0855e02 100644
  4908. --- a/lldb/source/Plugins/Process/CMakeLists.txt
  4909. +++ b/lldb/source/Plugins/Process/CMakeLists.txt
  4910. @@ -18,3 +18,4 @@ add_subdirectory(Utility)
  4911. add_subdirectory(elf-core)
  4912. add_subdirectory(mach-core)
  4913. add_subdirectory(minidump)
  4914. +add_subdirectory(wasm)
  4915. diff --git a/lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp b/lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
  4916. index 12bc7390c..707ab85e5 100644
  4917. --- a/lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
  4918. +++ b/lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
  4919. @@ -285,7 +285,7 @@ bool ProcessElfCore::IsAlive() { return true; }
  4920. // Process Memory
  4921. size_t ProcessElfCore::ReadMemory(lldb::addr_t addr, void *buf, size_t size,
  4922. - Status &error) {
  4923. + Status &error, ExecutionContext *exe_ctx) {
  4924. // Don't allow the caching that lldb_private::Process::ReadMemory does since
  4925. // in core files we have it all cached our our core file anyway.
  4926. return DoReadMemory(addr, buf, size, error);
  4927. diff --git a/lldb/source/Plugins/Process/elf-core/ProcessElfCore.h b/lldb/source/Plugins/Process/elf-core/ProcessElfCore.h
  4928. index d8e3cc9ae..f0bf9c4d3 100644
  4929. --- a/lldb/source/Plugins/Process/elf-core/ProcessElfCore.h
  4930. +++ b/lldb/source/Plugins/Process/elf-core/ProcessElfCore.h
  4931. @@ -84,7 +84,8 @@ public:
  4932. // Process Memory
  4933. size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size,
  4934. - lldb_private::Status &error) override;
  4935. + lldb_private::Status &error,
  4936. + lldb_private::ExecutionContext *exe_ctx = nullptr) override;
  4937. size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
  4938. lldb_private::Status &error) override;
  4939. diff --git a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
  4940. index 6914b3734..bb8a05604 100644
  4941. --- a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
  4942. +++ b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
  4943. @@ -334,6 +334,11 @@ ConstString ProcessGDBRemote::GetPluginName() { return GetPluginNameStatic(); }
  4944. uint32_t ProcessGDBRemote::GetPluginVersion() { return 1; }
  4945. +std::shared_ptr<ThreadGDBRemote>
  4946. +ProcessGDBRemote::CreateThread(lldb::tid_t tid) {
  4947. + return std::make_shared<ThreadGDBRemote>(*this, tid);
  4948. +}
  4949. +
  4950. bool ProcessGDBRemote::ParsePythonTargetDefinition(
  4951. const FileSpec &target_definition_fspec) {
  4952. ScriptInterpreter *interpreter =
  4953. @@ -1626,7 +1631,7 @@ bool ProcessGDBRemote::DoUpdateThreadList(ThreadList &old_thread_list,
  4954. ThreadSP thread_sp(
  4955. old_thread_list_copy.RemoveThreadByProtocolID(tid, false));
  4956. if (!thread_sp) {
  4957. - thread_sp = std::make_shared<ThreadGDBRemote>(*this, tid);
  4958. + thread_sp = CreateThread(tid);
  4959. LLDB_LOGV(log, "Making new thread: {0} for thread ID: {1:x}.",
  4960. thread_sp.get(), thread_sp->GetID());
  4961. } else {
  4962. @@ -1742,7 +1747,7 @@ ThreadSP ProcessGDBRemote::SetThreadStopInfo(
  4963. if (!thread_sp) {
  4964. // Create the thread if we need to
  4965. - thread_sp = std::make_shared<ThreadGDBRemote>(*this, tid);
  4966. + thread_sp = CreateThread(tid);
  4967. m_thread_list_real.AddThread(thread_sp);
  4968. }
  4969. }
  4970. diff --git a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
  4971. index fe04cdddd..e4a14c645 100644
  4972. --- a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
  4973. +++ b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
  4974. @@ -237,6 +237,8 @@ protected:
  4975. bool SupportsMemoryTagging() override;
  4976. + virtual std::shared_ptr<ThreadGDBRemote> CreateThread(lldb::tid_t tid);
  4977. +
  4978. /// Broadcaster event bits definitions.
  4979. enum {
  4980. eBroadcastBitAsyncContinue = (1 << 0),
  4981. diff --git a/lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp b/lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
  4982. index 84548edb5..0ae6f7e4a 100644
  4983. --- a/lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
  4984. +++ b/lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
  4985. @@ -596,7 +596,7 @@ bool ProcessMachCore::WarnBeforeDetach() const { return false; }
  4986. // Process Memory
  4987. size_t ProcessMachCore::ReadMemory(addr_t addr, void *buf, size_t size,
  4988. - Status &error) {
  4989. + Status &error, ExecutionContext *exe_ctx) {
  4990. // Don't allow the caching that lldb_private::Process::ReadMemory does since
  4991. // in core files we have it all cached our our core file anyway.
  4992. return DoReadMemory(addr, buf, size, error);
  4993. diff --git a/lldb/source/Plugins/Process/mach-core/ProcessMachCore.h b/lldb/source/Plugins/Process/mach-core/ProcessMachCore.h
  4994. index db77e96f1..1c930896c 100644
  4995. --- a/lldb/source/Plugins/Process/mach-core/ProcessMachCore.h
  4996. +++ b/lldb/source/Plugins/Process/mach-core/ProcessMachCore.h
  4997. @@ -65,7 +65,8 @@ public:
  4998. // Process Memory
  4999. size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size,
  5000. - lldb_private::Status &error) override;
  5001. + lldb_private::Status &error,
  5002. + lldb_private::ExecutionContext *exe_ctx = nullptr) override;
  5003. size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
  5004. lldb_private::Status &error) override;
  5005. diff --git a/lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp b/lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp
  5006. index 385557422..d8bb21581 100644
  5007. --- a/lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp
  5008. +++ b/lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp
  5009. @@ -374,7 +374,7 @@ bool ProcessMinidump::IsAlive() { return true; }
  5010. bool ProcessMinidump::WarnBeforeDetach() const { return false; }
  5011. size_t ProcessMinidump::ReadMemory(lldb::addr_t addr, void *buf, size_t size,
  5012. - Status &error) {
  5013. + Status &error, ExecutionContext *exe_ctx) {
  5014. // Don't allow the caching that lldb_private::Process::ReadMemory does since
  5015. // we have it all cached in our dump file anyway.
  5016. return DoReadMemory(addr, buf, size, error);
  5017. diff --git a/lldb/source/Plugins/Process/minidump/ProcessMinidump.h b/lldb/source/Plugins/Process/minidump/ProcessMinidump.h
  5018. index 27b0da004..e94ecab43 100644
  5019. --- a/lldb/source/Plugins/Process/minidump/ProcessMinidump.h
  5020. +++ b/lldb/source/Plugins/Process/minidump/ProcessMinidump.h
  5021. @@ -69,8 +69,8 @@ public:
  5022. bool WarnBeforeDetach() const override;
  5023. - size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size,
  5024. - Status &error) override;
  5025. + size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size, Status &error,
  5026. + ExecutionContext *exe_ctx = nullptr) override;
  5027. size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
  5028. Status &error) override;
  5029. diff --git a/lldb/source/Plugins/Process/wasm/CMakeLists.txt b/lldb/source/Plugins/Process/wasm/CMakeLists.txt
  5030. new file mode 100644
  5031. index 000000000..61efb933f
  5032. --- /dev/null
  5033. +++ b/lldb/source/Plugins/Process/wasm/CMakeLists.txt
  5034. @@ -0,0 +1,12 @@
  5035. +
  5036. +add_lldb_library(lldbPluginProcessWasm PLUGIN
  5037. + ProcessWasm.cpp
  5038. + ThreadWasm.cpp
  5039. + UnwindWasm.cpp
  5040. +
  5041. + LINK_LIBS
  5042. + lldbCore
  5043. + ${LLDB_PLUGINS}
  5044. + LINK_COMPONENTS
  5045. + Support
  5046. + )
  5047. diff --git a/lldb/source/Plugins/Process/wasm/ProcessWasm.cpp b/lldb/source/Plugins/Process/wasm/ProcessWasm.cpp
  5048. new file mode 100644
  5049. index 000000000..9c0fc7b7f
  5050. --- /dev/null
  5051. +++ b/lldb/source/Plugins/Process/wasm/ProcessWasm.cpp
  5052. @@ -0,0 +1,261 @@
  5053. +//===-- ProcessWasm.cpp ---------------------------------------------------===//
  5054. +//
  5055. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5056. +// See https://llvm.org/LICENSE.txt for license information.
  5057. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5058. +//
  5059. +//===----------------------------------------------------------------------===//
  5060. +
  5061. +#include "ProcessWasm.h"
  5062. +#include "ThreadWasm.h"
  5063. +#include "lldb/Core/Module.h"
  5064. +#include "lldb/Core/PluginManager.h"
  5065. +#include "lldb/Utility/DataBufferHeap.h"
  5066. +
  5067. +#include "lldb/Target/UnixSignals.h"
  5068. +
  5069. +using namespace lldb;
  5070. +using namespace lldb_private;
  5071. +using namespace lldb_private::process_gdb_remote;
  5072. +using namespace lldb_private::wasm;
  5073. +
  5074. +LLDB_PLUGIN_DEFINE(ProcessWasm)
  5075. +
  5076. +// ProcessGDBRemote constructor
  5077. +ProcessWasm::ProcessWasm(lldb::TargetSP target_sp, ListenerSP listener_sp)
  5078. + : ProcessGDBRemote(target_sp, listener_sp) {
  5079. + /* always use linux signals for wasm process */
  5080. + m_unix_signals_sp = UnixSignals::Create(ArchSpec{"wasm32-Ant-wasi-wasm"});
  5081. +}
  5082. +
  5083. +void ProcessWasm::Initialize() {
  5084. + static llvm::once_flag g_once_flag;
  5085. +
  5086. + llvm::call_once(g_once_flag, []() {
  5087. + PluginManager::RegisterPlugin(GetPluginNameStatic(),
  5088. + GetPluginDescriptionStatic(), CreateInstance,
  5089. + DebuggerInitialize);
  5090. + });
  5091. +}
  5092. +
  5093. +void ProcessWasm::DebuggerInitialize(Debugger &debugger) {
  5094. + ProcessGDBRemote::DebuggerInitialize(debugger);
  5095. +}
  5096. +
  5097. +// PluginInterface
  5098. +ConstString ProcessWasm::GetPluginName() { return GetPluginNameStatic(); }
  5099. +
  5100. +uint32_t ProcessWasm::GetPluginVersion() { return 1; }
  5101. +
  5102. +ConstString ProcessWasm::GetPluginNameStatic() {
  5103. + static ConstString g_name("wasm");
  5104. + return g_name;
  5105. +}
  5106. +
  5107. +const char *ProcessWasm::GetPluginDescriptionStatic() {
  5108. + return "GDB Remote protocol based WebAssembly debugging plug-in.";
  5109. +}
  5110. +
  5111. +void ProcessWasm::Terminate() {
  5112. + PluginManager::UnregisterPlugin(ProcessWasm::CreateInstance);
  5113. +}
  5114. +
  5115. +lldb::ProcessSP ProcessWasm::CreateInstance(lldb::TargetSP target_sp,
  5116. + ListenerSP listener_sp,
  5117. + const FileSpec *crash_file_path,
  5118. + bool can_connect) {
  5119. + lldb::ProcessSP process_sp;
  5120. + if (crash_file_path == nullptr)
  5121. + process_sp = std::make_shared<ProcessWasm>(target_sp, listener_sp);
  5122. + return process_sp;
  5123. +}
  5124. +
  5125. +bool ProcessWasm::CanDebug(lldb::TargetSP target_sp,
  5126. + bool plugin_specified_by_name) {
  5127. + if (plugin_specified_by_name)
  5128. + return true;
  5129. +
  5130. + Module *exe_module = target_sp->GetExecutableModulePointer();
  5131. + if (exe_module) {
  5132. + ObjectFile *exe_objfile = exe_module->GetObjectFile();
  5133. + return exe_objfile->GetArchitecture().GetMachine() == llvm::Triple::wasm32;
  5134. + }
  5135. + // However, if there is no wasm module, we return false, otherwise,
  5136. + // we might use ProcessWasm to attach gdb remote.
  5137. + return false;
  5138. +}
  5139. +
  5140. +
  5141. +
  5142. +std::shared_ptr<ThreadGDBRemote> ProcessWasm::CreateThread(lldb::tid_t tid) {
  5143. + return std::make_shared<ThreadWasm>(*this, tid);
  5144. +}
  5145. +
  5146. +size_t ProcessWasm::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
  5147. + Status &error, ExecutionContext *exe_ctx) {
  5148. + wasm_addr_t wasm_addr(vm_addr);
  5149. + size_t nread = 0;
  5150. +
  5151. + switch (wasm_addr.GetType()) {
  5152. + case WasmAddressType::Memory:
  5153. + case WasmAddressType::Object:
  5154. + return ProcessGDBRemote::ReadMemory(vm_addr, buf, size, error);
  5155. + case WasmAddressType::Invalid:
  5156. + default:
  5157. + error.SetErrorStringWithFormat(
  5158. + "Wasm read failed for invalid address 0x%" PRIx64, vm_addr);
  5159. + return 0;
  5160. + }
  5161. +}
  5162. +
  5163. +size_t ProcessWasm::WasmReadMemory(uint32_t wasm_module_id, lldb::addr_t addr,
  5164. + void *buf, size_t buffer_size) {
  5165. + char packet[64];
  5166. + int packet_len =
  5167. + ::snprintf(packet, sizeof(packet), "qWasmMem:%d;%" PRIx64 ";%" PRIx64,
  5168. + wasm_module_id, static_cast<uint64_t>(addr),
  5169. + static_cast<uint64_t>(buffer_size));
  5170. + assert(packet_len + 1 < (int)sizeof(packet));
  5171. + UNUSED_IF_ASSERT_DISABLED(packet_len);
  5172. + StringExtractorGDBRemote response;
  5173. + if (m_gdb_comm.SendPacketAndWaitForResponse(packet, response, GetInterruptTimeout()) ==
  5174. + GDBRemoteCommunication::PacketResult::Success) {
  5175. + if (response.IsNormalResponse()) {
  5176. + return response.GetHexBytes(llvm::MutableArrayRef<uint8_t>(
  5177. + static_cast<uint8_t *>(buf), buffer_size),
  5178. + '\xdd');
  5179. + }
  5180. + }
  5181. + return 0;
  5182. +}
  5183. +
  5184. +size_t ProcessWasm::WasmReadData(uint32_t wasm_module_id, lldb::addr_t addr,
  5185. + void *buf, size_t buffer_size) {
  5186. + char packet[64];
  5187. + int packet_len =
  5188. + ::snprintf(packet, sizeof(packet), "qWasmData:%d;%" PRIx64 ";%" PRIx64,
  5189. + wasm_module_id, static_cast<uint64_t>(addr),
  5190. + static_cast<uint64_t>(buffer_size));
  5191. + assert(packet_len + 1 < (int)sizeof(packet));
  5192. + UNUSED_IF_ASSERT_DISABLED(packet_len);
  5193. + StringExtractorGDBRemote response;
  5194. + if (m_gdb_comm.SendPacketAndWaitForResponse(packet, response, GetInterruptTimeout()) ==
  5195. + GDBRemoteCommunication::PacketResult::Success) {
  5196. + if (response.IsNormalResponse()) {
  5197. + return response.GetHexBytes(llvm::MutableArrayRef<uint8_t>(
  5198. + static_cast<uint8_t *>(buf), buffer_size),
  5199. + '\xdd');
  5200. + }
  5201. + }
  5202. + return 0;
  5203. +}
  5204. +
  5205. +bool ProcessWasm::GetWasmLocal(int frame_index, int index, void *buf,
  5206. + size_t buffer_size, size_t &size) {
  5207. + StreamString packet;
  5208. + packet.Printf("qWasmLocal:");
  5209. + packet.Printf("%d;%d", frame_index, index);
  5210. + StringExtractorGDBRemote response;
  5211. + if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetString(), response) !=
  5212. + GDBRemoteCommunication::PacketResult::Success) {
  5213. + return false;
  5214. + }
  5215. +
  5216. + if (!response.IsNormalResponse()) {
  5217. + return false;
  5218. + }
  5219. +
  5220. + DataBufferSP buffer_sp(
  5221. + new DataBufferHeap(response.GetStringRef().size() / 2, 0));
  5222. + response.GetHexBytes(buffer_sp->GetData(), '\xcc');
  5223. + size = buffer_sp->GetByteSize();
  5224. + if (size <= buffer_size) {
  5225. + memcpy(buf, buffer_sp->GetBytes(), size);
  5226. + return true;
  5227. + }
  5228. +
  5229. + return false;
  5230. +}
  5231. +
  5232. +bool ProcessWasm::GetWasmGlobal(int frame_index, int index, void *buf,
  5233. + size_t buffer_size, size_t &size) {
  5234. + StreamString packet;
  5235. + packet.PutCString("qWasmGlobal:");
  5236. + packet.Printf("%d;%d", frame_index, index);
  5237. + StringExtractorGDBRemote response;
  5238. + if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetString(), response) !=
  5239. + GDBRemoteCommunication::PacketResult::Success) {
  5240. + return false;
  5241. + }
  5242. +
  5243. + if (!response.IsNormalResponse()) {
  5244. + return false;
  5245. + }
  5246. +
  5247. + DataBufferSP buffer_sp(
  5248. + new DataBufferHeap(response.GetStringRef().size() / 2, 0));
  5249. + response.GetHexBytes(buffer_sp->GetData(), '\xcc');
  5250. + size = buffer_sp->GetByteSize();
  5251. + if (size <= buffer_size) {
  5252. + memcpy(buf, buffer_sp->GetBytes(), size);
  5253. + return true;
  5254. + }
  5255. +
  5256. + return false;
  5257. +}
  5258. +
  5259. +bool ProcessWasm::GetWasmStackValue(int frame_index, int index, void *buf,
  5260. + size_t buffer_size, size_t &size) {
  5261. + StreamString packet;
  5262. + packet.PutCString("qWasmStackValue:");
  5263. + packet.Printf("%d;%d", frame_index, index);
  5264. + StringExtractorGDBRemote response;
  5265. + if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetString(), response) !=
  5266. + GDBRemoteCommunication::PacketResult::Success) {
  5267. + return false;
  5268. + }
  5269. +
  5270. + if (!response.IsNormalResponse()) {
  5271. + return false;
  5272. + }
  5273. +
  5274. + DataBufferSP buffer_sp(
  5275. + new DataBufferHeap(response.GetStringRef().size() / 2, 0));
  5276. + response.GetHexBytes(buffer_sp->GetData(), '\xcc');
  5277. + size = buffer_sp->GetByteSize();
  5278. + if (size <= buffer_size) {
  5279. + memcpy(buf, buffer_sp->GetBytes(), size);
  5280. + return true;
  5281. + }
  5282. +
  5283. + return false;
  5284. +}
  5285. +
  5286. +bool ProcessWasm::GetWasmCallStack(lldb::tid_t tid,
  5287. + std::vector<lldb::addr_t> &call_stack_pcs) {
  5288. + call_stack_pcs.clear();
  5289. + StreamString packet;
  5290. + packet.Printf("qWasmCallStack:");
  5291. + packet.Printf("%llx", tid);
  5292. + StringExtractorGDBRemote response;
  5293. + if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetString(), response) !=
  5294. + GDBRemoteCommunication::PacketResult::Success) {
  5295. + return false;
  5296. + }
  5297. +
  5298. + if (!response.IsNormalResponse()) {
  5299. + return false;
  5300. + }
  5301. +
  5302. + addr_t buf[1024 / sizeof(addr_t)];
  5303. + size_t bytes = response.GetHexBytes(
  5304. + llvm::MutableArrayRef<uint8_t>((uint8_t *)buf, sizeof(buf)), '\xdd');
  5305. + if (bytes == 0) {
  5306. + return false;
  5307. + }
  5308. +
  5309. + for (size_t i = 0; i < bytes / sizeof(addr_t); i++) {
  5310. + call_stack_pcs.push_back(buf[i]);
  5311. + }
  5312. + return true;
  5313. +}
  5314. diff --git a/lldb/source/Plugins/Process/wasm/ProcessWasm.h b/lldb/source/Plugins/Process/wasm/ProcessWasm.h
  5315. new file mode 100644
  5316. index 000000000..d3aece7a6
  5317. --- /dev/null
  5318. +++ b/lldb/source/Plugins/Process/wasm/ProcessWasm.h
  5319. @@ -0,0 +1,128 @@
  5320. +//===-- ProcessWasm.h -------------------------------------------*- C++ -*-===//
  5321. +//
  5322. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5323. +// See https://llvm.org/LICENSE.txt for license information.
  5324. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5325. +//
  5326. +//===----------------------------------------------------------------------===//
  5327. +
  5328. +#ifndef LLDB_SOURCE_PLUGINS_PROCESS_WASM_PROCESSWASM_H
  5329. +#define LLDB_SOURCE_PLUGINS_PROCESS_WASM_PROCESSWASM_H
  5330. +
  5331. +#include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
  5332. +#include "lldb/Target/RegisterContext.h"
  5333. +
  5334. +namespace lldb_private {
  5335. +namespace wasm {
  5336. +
  5337. +// Each WebAssembly module has separated address spaces for Code and Memory.
  5338. +// A WebAssembly module also has a Data section which, when the module is
  5339. +// loaded, gets mapped into a region in the module Memory.
  5340. +// For the purpose of debugging, we can represent all these separated 32-bit
  5341. +// address spaces with a single virtual 64-bit address space.
  5342. +//
  5343. +// Struct wasm_addr_t provides this encoding using bitfields
  5344. +//
  5345. +enum WasmAddressType {
  5346. + Memory = 0x00,
  5347. + Object = 0x01,
  5348. + Invalid = 0x03
  5349. +};
  5350. +struct wasm_addr_t {
  5351. + uint64_t offset : 32;
  5352. + uint64_t module_id : 30;
  5353. + uint64_t type : 2;
  5354. +
  5355. + wasm_addr_t(lldb::addr_t addr)
  5356. + : type(addr >> 62), module_id((addr & 0x00ffffff00000000) >> 32),
  5357. + offset(addr & 0x00000000ffffffff) {}
  5358. +
  5359. + wasm_addr_t(WasmAddressType type_, uint32_t module_id_, uint32_t offset_)
  5360. + : type(type_), module_id(module_id_), offset(offset_) {}
  5361. +
  5362. + WasmAddressType GetType() { return static_cast<WasmAddressType>(type); }
  5363. + operator lldb::addr_t() { return *(uint64_t *)this; }
  5364. +};
  5365. +
  5366. +/// ProcessWasm provides the access to the Wasm program state
  5367. +/// retrieved from the Wasm engine.
  5368. +class ProcessWasm : public process_gdb_remote::ProcessGDBRemote {
  5369. +public:
  5370. + ProcessWasm(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp);
  5371. + ~ProcessWasm() override = default;
  5372. +
  5373. + static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp,
  5374. + lldb::ListenerSP listener_sp,
  5375. + const FileSpec *crash_file_path,
  5376. + bool can_connect);
  5377. +
  5378. + static void Initialize();
  5379. + static void DebuggerInitialize(Debugger &debugger);
  5380. + static void Terminate();
  5381. + static ConstString GetPluginNameStatic();
  5382. + static const char *GetPluginDescriptionStatic();
  5383. +
  5384. + /// PluginInterface protocol.
  5385. + /// \{
  5386. + ConstString GetPluginName() override;
  5387. + uint32_t GetPluginVersion() override;
  5388. + /// \}
  5389. +
  5390. + /// Process protocol.
  5391. + /// \{
  5392. + size_t ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, Status &error,
  5393. + ExecutionContext *exe_ctx = nullptr) override;
  5394. + /// \}
  5395. +
  5396. + /// Query the value of a WebAssembly local variable from the WebAssembly
  5397. + /// remote process.
  5398. + bool GetWasmLocal(int frame_index, int index, void *buf, size_t buffer_size,
  5399. + size_t &size);
  5400. +
  5401. + /// Query the value of a WebAssembly global variable from the WebAssembly
  5402. + /// remote process.
  5403. + bool GetWasmGlobal(int frame_index, int index, void *buf, size_t buffer_size,
  5404. + size_t &size);
  5405. +
  5406. + /// Query the value of an item in the WebAssembly operand stack from the
  5407. + /// WebAssembly remote process.
  5408. + bool GetWasmStackValue(int frame_index, int index, void *buf,
  5409. + size_t buffer_size, size_t &size);
  5410. +
  5411. + /// Read from the WebAssembly Memory space.
  5412. + size_t WasmReadMemory(uint32_t wasm_module_id, lldb::addr_t addr, void *buf,
  5413. + size_t buffer_size);
  5414. +
  5415. + /// Read from the WebAssembly Data space.
  5416. + size_t WasmReadData(uint32_t wasm_module_id, lldb::addr_t addr, void *buf,
  5417. + size_t buffer_size);
  5418. +
  5419. + /// Retrieve the current call stack from the WebAssembly remote process.
  5420. + bool GetWasmCallStack(lldb::tid_t tid,
  5421. + std::vector<lldb::addr_t> &call_stack_pcs);
  5422. +
  5423. + // Check if a given Process
  5424. + bool CanDebug(lldb::TargetSP target_sp,
  5425. + bool plugin_specified_by_name) override;
  5426. +
  5427. +protected:
  5428. + /// ProcessGDBRemote protocol.
  5429. + /// \{
  5430. + std::shared_ptr<process_gdb_remote::ThreadGDBRemote>
  5431. + CreateThread(lldb::tid_t tid) override;
  5432. + /// \}
  5433. +
  5434. +private:
  5435. + friend class UnwindWasm;
  5436. + process_gdb_remote::GDBRemoteDynamicRegisterInfoSP &GetRegisterInfo() {
  5437. + return m_register_info_sp;
  5438. + }
  5439. +
  5440. + ProcessWasm(const ProcessWasm &);
  5441. + const ProcessWasm &operator=(const ProcessWasm &) = delete;
  5442. +};
  5443. +
  5444. +} // namespace wasm
  5445. +} // namespace lldb_private
  5446. +
  5447. +#endif // LLDB_SOURCE_PLUGINS_PROCESS_WASM_PROCESSWASM_H
  5448. diff --git a/lldb/source/Plugins/Process/wasm/ThreadWasm.cpp b/lldb/source/Plugins/Process/wasm/ThreadWasm.cpp
  5449. new file mode 100644
  5450. index 000000000..fa02073e7
  5451. --- /dev/null
  5452. +++ b/lldb/source/Plugins/Process/wasm/ThreadWasm.cpp
  5453. @@ -0,0 +1,35 @@
  5454. +//===-- ThreadWasm.cpp ----------------------------------------------------===//
  5455. +//
  5456. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5457. +// See https://llvm.org/LICENSE.txt for license information.
  5458. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5459. +//
  5460. +//===----------------------------------------------------------------------===//
  5461. +
  5462. +#include "ThreadWasm.h"
  5463. +
  5464. +#include "ProcessWasm.h"
  5465. +#include "UnwindWasm.h"
  5466. +#include "lldb/Target/Target.h"
  5467. +
  5468. +using namespace lldb;
  5469. +using namespace lldb_private;
  5470. +using namespace lldb_private::wasm;
  5471. +
  5472. +Unwind &ThreadWasm::GetUnwinder() {
  5473. + if (!m_unwinder_up) {
  5474. + assert(CalculateTarget()->GetArchitecture().GetMachine() ==
  5475. + llvm::Triple::wasm32);
  5476. + m_unwinder_up.reset(new wasm::UnwindWasm(*this));
  5477. + }
  5478. + return *m_unwinder_up;
  5479. +}
  5480. +
  5481. +bool ThreadWasm::GetWasmCallStack(std::vector<lldb::addr_t> &call_stack_pcs) {
  5482. + ProcessSP process_sp(GetProcess());
  5483. + if (process_sp) {
  5484. + ProcessWasm *wasm_process = static_cast<ProcessWasm *>(process_sp.get());
  5485. + return wasm_process->GetWasmCallStack(GetID(), call_stack_pcs);
  5486. + }
  5487. + return false;
  5488. +}
  5489. diff --git a/lldb/source/Plugins/Process/wasm/ThreadWasm.h b/lldb/source/Plugins/Process/wasm/ThreadWasm.h
  5490. new file mode 100644
  5491. index 000000000..0a33c07de
  5492. --- /dev/null
  5493. +++ b/lldb/source/Plugins/Process/wasm/ThreadWasm.h
  5494. @@ -0,0 +1,41 @@
  5495. +//===-- ThreadWasm.h --------------------------------------------*- C++ -*-===//
  5496. +//
  5497. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5498. +// See https://llvm.org/LICENSE.txt for license information.
  5499. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5500. +//
  5501. +//===----------------------------------------------------------------------===//
  5502. +
  5503. +#ifndef LLDB_SOURCE_PLUGINS_PROCESS_WASM_THREADWASM_H
  5504. +#define LLDB_SOURCE_PLUGINS_PROCESS_WASM_THREADWASM_H
  5505. +
  5506. +#include "Plugins/Process/gdb-remote/ThreadGDBRemote.h"
  5507. +
  5508. +namespace lldb_private {
  5509. +namespace wasm {
  5510. +
  5511. +/// ProcessWasm provides the access to the Wasm program state
  5512. +/// retrieved from the Wasm engine.
  5513. +class ThreadWasm : public process_gdb_remote::ThreadGDBRemote {
  5514. +public:
  5515. + ThreadWasm(Process &process, lldb::tid_t tid)
  5516. + : process_gdb_remote::ThreadGDBRemote(process, tid) {}
  5517. + ~ThreadWasm() override = default;
  5518. +
  5519. + /// Retrieve the current call stack from the WebAssembly remote process.
  5520. + bool GetWasmCallStack(std::vector<lldb::addr_t> &call_stack_pcs);
  5521. +
  5522. +protected:
  5523. + /// Thread protocol.
  5524. + /// \{
  5525. + Unwind &GetUnwinder() override;
  5526. + /// \}
  5527. +
  5528. + ThreadWasm(const ThreadWasm &);
  5529. + const ThreadWasm &operator=(const ThreadWasm &) = delete;
  5530. +};
  5531. +
  5532. +} // namespace wasm
  5533. +} // namespace lldb_private
  5534. +
  5535. +#endif // LLDB_SOURCE_PLUGINS_PROCESS_WASM_THREADWASM_H
  5536. diff --git a/lldb/source/Plugins/Process/wasm/UnwindWasm.cpp b/lldb/source/Plugins/Process/wasm/UnwindWasm.cpp
  5537. new file mode 100644
  5538. index 000000000..1a195cb93
  5539. --- /dev/null
  5540. +++ b/lldb/source/Plugins/Process/wasm/UnwindWasm.cpp
  5541. @@ -0,0 +1,74 @@
  5542. +//===-- UnwindWasm.cpp ----------------------------------------------------===//
  5543. +//
  5544. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5545. +// See https://llvm.org/LICENSE.txt for license information.
  5546. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5547. +//
  5548. +//===----------------------------------------------------------------------===//
  5549. +
  5550. +#include "UnwindWasm.h"
  5551. +#include "Plugins/Process/gdb-remote/ThreadGDBRemote.h"
  5552. +#include "Plugins/Process/wasm/ProcessWasm.h"
  5553. +#include "Plugins/Process/wasm/ThreadWasm.h"
  5554. +
  5555. +using namespace lldb;
  5556. +using namespace lldb_private;
  5557. +using namespace process_gdb_remote;
  5558. +using namespace wasm;
  5559. +
  5560. +class WasmGDBRemoteRegisterContext : public GDBRemoteRegisterContext {
  5561. +public:
  5562. + WasmGDBRemoteRegisterContext(ThreadGDBRemote &thread,
  5563. + uint32_t concrete_frame_idx,
  5564. + GDBRemoteDynamicRegisterInfoSP &reg_info_sp,
  5565. + uint64_t pc)
  5566. + : GDBRemoteRegisterContext(thread, concrete_frame_idx, reg_info_sp, false,
  5567. + false) {
  5568. + PrivateSetRegisterValue(0, pc);
  5569. + }
  5570. +};
  5571. +
  5572. +lldb::RegisterContextSP
  5573. +UnwindWasm::DoCreateRegisterContextForFrame(lldb_private::StackFrame *frame) {
  5574. + if (m_frames.size() <= frame->GetFrameIndex()) {
  5575. + return lldb::RegisterContextSP();
  5576. + }
  5577. +
  5578. + ThreadSP thread = frame->GetThread();
  5579. + ThreadGDBRemote *gdb_thread = static_cast<ThreadGDBRemote *>(thread.get());
  5580. + ProcessWasm *wasm_process =
  5581. + static_cast<ProcessWasm *>(thread->GetProcess().get());
  5582. + std::shared_ptr<GDBRemoteRegisterContext> reg_ctx_sp =
  5583. + std::make_shared<WasmGDBRemoteRegisterContext>(
  5584. + *gdb_thread, frame->GetConcreteFrameIndex(),
  5585. + wasm_process->GetRegisterInfo(), m_frames[frame->GetFrameIndex()]);
  5586. + return reg_ctx_sp;
  5587. +}
  5588. +
  5589. +uint32_t UnwindWasm::DoGetFrameCount() {
  5590. + if (!m_unwind_complete) {
  5591. + m_unwind_complete = true;
  5592. + m_frames.clear();
  5593. +
  5594. + ThreadWasm &wasm_thread = static_cast<ThreadWasm &>(GetThread());
  5595. + if (!wasm_thread.GetWasmCallStack(m_frames))
  5596. + m_frames.clear();
  5597. + }
  5598. + return m_frames.size();
  5599. +}
  5600. +
  5601. +bool UnwindWasm::DoGetFrameInfoAtIndex(uint32_t frame_idx, lldb::addr_t &cfa,
  5602. + lldb::addr_t &pc,
  5603. + bool &behaves_like_zeroth_frame) {
  5604. + if (m_frames.size() == 0) {
  5605. + DoGetFrameCount();
  5606. + }
  5607. +
  5608. + if (frame_idx < m_frames.size()) {
  5609. + behaves_like_zeroth_frame = (frame_idx == 0);
  5610. + cfa = 0;
  5611. + pc = m_frames[frame_idx];
  5612. + return true;
  5613. + }
  5614. + return false;
  5615. +}
  5616. \ No newline at end of file
  5617. diff --git a/lldb/source/Plugins/Process/wasm/UnwindWasm.h b/lldb/source/Plugins/Process/wasm/UnwindWasm.h
  5618. new file mode 100644
  5619. index 000000000..9bd1dac9a
  5620. --- /dev/null
  5621. +++ b/lldb/source/Plugins/Process/wasm/UnwindWasm.h
  5622. @@ -0,0 +1,55 @@
  5623. +//===-- UnwindWasm.h --------------------------------------------*- C++ -*-===//
  5624. +//
  5625. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5626. +// See https://llvm.org/LICENSE.txt for license information.
  5627. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5628. +//
  5629. +//===----------------------------------------------------------------------===//
  5630. +
  5631. +#ifndef lldb_UnwindWasm_h_
  5632. +#define lldb_UnwindWasm_h_
  5633. +
  5634. +#include "lldb/Target/RegisterContext.h"
  5635. +#include "lldb/Target/Unwind.h"
  5636. +#include <vector>
  5637. +
  5638. +namespace lldb_private {
  5639. +namespace wasm {
  5640. +
  5641. +/// UnwindWasm manages stack unwinding for a WebAssembly process.
  5642. +class UnwindWasm : public lldb_private::Unwind {
  5643. +public:
  5644. + UnwindWasm(lldb_private::Thread &thread)
  5645. + : Unwind(thread), m_frames(), m_unwind_complete(false) {}
  5646. + ~UnwindWasm() override = default;
  5647. +
  5648. +protected:
  5649. + /// Unwind protocol.
  5650. + /// \{
  5651. + void DoClear() override {
  5652. + m_frames.clear();
  5653. + m_unwind_complete = false;
  5654. + }
  5655. +
  5656. + uint32_t DoGetFrameCount() override;
  5657. +
  5658. + bool DoGetFrameInfoAtIndex(uint32_t frame_idx, lldb::addr_t &cfa,
  5659. + lldb::addr_t &pc,
  5660. + bool &behaves_like_zeroth_frame) override;
  5661. +
  5662. + lldb::RegisterContextSP
  5663. + DoCreateRegisterContextForFrame(lldb_private::StackFrame *frame) override;
  5664. + /// \}
  5665. +
  5666. +private:
  5667. + std::vector<lldb::addr_t> m_frames;
  5668. + bool m_unwind_complete;
  5669. +
  5670. + UnwindWasm(const UnwindWasm &);
  5671. + const UnwindWasm &operator=(const UnwindWasm &) = delete;
  5672. +};
  5673. +
  5674. +} // namespace wasm
  5675. +} // namespace lldb_private
  5676. +
  5677. +#endif // lldb_UnwindWasm_h_
  5678. diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
  5679. index ccaf31317..c3ef5aebd 100644
  5680. --- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
  5681. +++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
  5682. @@ -3212,8 +3212,13 @@ VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
  5683. GetDWARFDeclContext(die).GetQualifiedNameAsConstString().GetCString();
  5684. }
  5685. - if (tag == DW_TAG_formal_parameter)
  5686. + if (tag == DW_TAG_formal_parameter) {
  5687. scope = eValueTypeVariableArgument;
  5688. + // For Wasm dwarft, pamameter may don't have location attr,
  5689. + // so set module here
  5690. + if (!location.GetModule())
  5691. + location.SetModule(module);
  5692. + }
  5693. else {
  5694. // DWARF doesn't specify if a DW_TAG_variable is a local, global
  5695. // or static variable, so we have to do a little digging:
  5696. diff --git a/lldb/source/Target/PathMappingList.cpp b/lldb/source/Target/PathMappingList.cpp
  5697. index b660c310e..cd76421ce 100644
  5698. --- a/lldb/source/Target/PathMappingList.cpp
  5699. +++ b/lldb/source/Target/PathMappingList.cpp
  5700. @@ -218,7 +218,12 @@ bool PathMappingList::ReverseRemapPath(const FileSpec &file, FileSpec &fixed) co
  5701. }
  5702. llvm::Optional<FileSpec> PathMappingList::FindFile(const FileSpec &orig_spec) const {
  5703. - if (auto remapped = RemapPath(orig_spec.GetPath(), /*only_if_exists=*/true))
  5704. + // We must normalize the orig_spec again using the host's path style,
  5705. + // otherwise there will be mismatch between the host and remote platform
  5706. + // if they use different path styles.
  5707. + if (auto remapped = RemapPath(
  5708. + NormalizePath(ConstString(orig_spec.GetCString())).GetStringRef(),
  5709. + /*only_if_exists=*/true))
  5710. return remapped;
  5711. return {};
  5712. diff --git a/lldb/source/Target/Platform.cpp b/lldb/source/Target/Platform.cpp
  5713. index a77ecddfb..e257f9350 100644
  5714. --- a/lldb/source/Target/Platform.cpp
  5715. +++ b/lldb/source/Target/Platform.cpp
  5716. @@ -1970,6 +1970,12 @@ size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
  5717. trap_opcode_size = sizeof(g_i386_opcode);
  5718. } break;
  5719. + case llvm::Triple::wasm32: {
  5720. + static const uint8_t g_wasm_opcode[] = {0x00}; // unreachable
  5721. + trap_opcode = g_wasm_opcode;
  5722. + trap_opcode_size = sizeof(g_wasm_opcode);
  5723. + } break;
  5724. +
  5725. default:
  5726. return 0;
  5727. }
  5728. diff --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp
  5729. index 8ecc66b59..f14898791 100644
  5730. --- a/lldb/source/Target/Process.cpp
  5731. +++ b/lldb/source/Target/Process.cpp
  5732. @@ -1892,7 +1892,8 @@ Status Process::DisableSoftwareBreakpoint(BreakpointSite *bp_site) {
  5733. // code
  5734. //#define VERIFY_MEMORY_READS
  5735. -size_t Process::ReadMemory(addr_t addr, void *buf, size_t size, Status &error) {
  5736. +size_t Process::ReadMemory(addr_t addr, void *buf, size_t size, Status &error,
  5737. + ExecutionContext *exe_ctx) {
  5738. error.Clear();
  5739. if (!GetDisableMemoryCache()) {
  5740. #if defined(VERIFY_MEMORY_READS)
  5741. diff --git a/lldb/source/Target/ProcessTrace.cpp b/lldb/source/Target/ProcessTrace.cpp
  5742. index c878a2ac4..ad5945b0a 100644
  5743. --- a/lldb/source/Target/ProcessTrace.cpp
  5744. +++ b/lldb/source/Target/ProcessTrace.cpp
  5745. @@ -88,7 +88,7 @@ void ProcessTrace::RefreshStateAfterStop() {}
  5746. Status ProcessTrace::DoDestroy() { return Status(); }
  5747. size_t ProcessTrace::ReadMemory(addr_t addr, void *buf, size_t size,
  5748. - Status &error) {
  5749. + Status &error, ExecutionContext *exe_ctx) {
  5750. // Don't allow the caching that lldb_private::Process::ReadMemory does since
  5751. // we have it all cached in the trace files.
  5752. return DoReadMemory(addr, buf, size, error);
  5753. diff --git a/lldb/source/Target/UnixSignals.cpp b/lldb/source/Target/UnixSignals.cpp
  5754. index 4ec2e25c7..24c88fe9a 100644
  5755. --- a/lldb/source/Target/UnixSignals.cpp
  5756. +++ b/lldb/source/Target/UnixSignals.cpp
  5757. @@ -46,6 +46,8 @@ lldb::UnixSignalsSP UnixSignals::Create(const ArchSpec &arch) {
  5758. return std::make_shared<FreeBSDSignals>();
  5759. case llvm::Triple::NetBSD:
  5760. return std::make_shared<NetBSDSignals>();
  5761. + case llvm::Triple::WASI:
  5762. + return std::make_shared<LinuxSignals>();
  5763. default:
  5764. return std::make_shared<UnixSignals>();
  5765. }