tracepoint.cc 201 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459
  1. /* Tracepoint code for remote server for GDB.
  2. Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "server.h"
  15. #include "tracepoint.h"
  16. #include "gdbthread.h"
  17. #include "gdbsupport/rsp-low.h"
  18. #include <ctype.h>
  19. #include <fcntl.h>
  20. #include <unistd.h>
  21. #include <chrono>
  22. #include <inttypes.h>
  23. #include "ax.h"
  24. #include "tdesc.h"
  25. #define IPA_SYM_STRUCT_NAME ipa_sym_addresses
  26. #include "gdbsupport/agent.h"
  27. #define DEFAULT_TRACE_BUFFER_SIZE 5242880 /* 5*1024*1024 */
  28. /* This file is built for both GDBserver, and the in-process
  29. agent (IPA), a shared library that includes a tracing agent that is
  30. loaded by the inferior to support fast tracepoints. Fast
  31. tracepoints (or more accurately, jump based tracepoints) are
  32. implemented by patching the tracepoint location with a jump into a
  33. small trampoline function whose job is to save the register state,
  34. call the in-process tracing agent, and then execute the original
  35. instruction that was under the tracepoint jump (possibly adjusted,
  36. if PC-relative, or some such).
  37. The current synchronization design is pull based. That means,
  38. GDBserver does most of the work, by peeking/poking at the inferior
  39. agent's memory directly for downloading tracepoint and associated
  40. objects, and for uploading trace frames. Whenever the IPA needs
  41. something from GDBserver (trace buffer is full, tracing stopped for
  42. some reason, etc.) the IPA calls a corresponding hook function
  43. where GDBserver has placed a breakpoint.
  44. Each of the agents has its own trace buffer. When browsing the
  45. trace frames built from slow and fast tracepoints from GDB (tfind
  46. mode), there's no guarantee the user is seeing the trace frames in
  47. strict chronological creation order, although, GDBserver tries to
  48. keep the order relatively reasonable, by syncing the trace buffers
  49. at appropriate times.
  50. */
  51. #ifdef IN_PROCESS_AGENT
  52. static void trace_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
  53. static void
  54. trace_vdebug (const char *fmt, ...)
  55. {
  56. char buf[1024];
  57. va_list ap;
  58. va_start (ap, fmt);
  59. vsprintf (buf, fmt, ap);
  60. fprintf (stderr, PROG "/tracepoint: %s\n", buf);
  61. va_end (ap);
  62. }
  63. #define trace_debug(fmt, args...) \
  64. do { \
  65. if (debug_threads) \
  66. trace_vdebug ((fmt), ##args); \
  67. } while (0)
  68. #else
  69. #define trace_debug(fmt, args...) \
  70. do { \
  71. threads_debug_printf ((fmt), ##args); \
  72. } while (0)
  73. #endif
  74. /* Prefix exported symbols, for good citizenship. All the symbols
  75. that need exporting are defined in this module. Note that all
  76. these symbols must be tagged with IP_AGENT_EXPORT_*. */
  77. #ifdef IN_PROCESS_AGENT
  78. # define gdb_tp_heap_buffer IPA_SYM_EXPORTED_NAME (gdb_tp_heap_buffer)
  79. # define gdb_jump_pad_buffer IPA_SYM_EXPORTED_NAME (gdb_jump_pad_buffer)
  80. # define gdb_jump_pad_buffer_end IPA_SYM_EXPORTED_NAME (gdb_jump_pad_buffer_end)
  81. # define gdb_trampoline_buffer IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer)
  82. # define gdb_trampoline_buffer_end IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer_end)
  83. # define gdb_trampoline_buffer_error IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer_error)
  84. # define collecting IPA_SYM_EXPORTED_NAME (collecting)
  85. # define gdb_collect_ptr IPA_SYM_EXPORTED_NAME (gdb_collect_ptr)
  86. # define stop_tracing IPA_SYM_EXPORTED_NAME (stop_tracing)
  87. # define flush_trace_buffer IPA_SYM_EXPORTED_NAME (flush_trace_buffer)
  88. # define about_to_request_buffer_space IPA_SYM_EXPORTED_NAME (about_to_request_buffer_space)
  89. # define trace_buffer_is_full IPA_SYM_EXPORTED_NAME (trace_buffer_is_full)
  90. # define stopping_tracepoint IPA_SYM_EXPORTED_NAME (stopping_tracepoint)
  91. # define expr_eval_result IPA_SYM_EXPORTED_NAME (expr_eval_result)
  92. # define error_tracepoint IPA_SYM_EXPORTED_NAME (error_tracepoint)
  93. # define tracepoints IPA_SYM_EXPORTED_NAME (tracepoints)
  94. # define tracing IPA_SYM_EXPORTED_NAME (tracing)
  95. # define trace_buffer_ctrl IPA_SYM_EXPORTED_NAME (trace_buffer_ctrl)
  96. # define trace_buffer_ctrl_curr IPA_SYM_EXPORTED_NAME (trace_buffer_ctrl_curr)
  97. # define trace_buffer_lo IPA_SYM_EXPORTED_NAME (trace_buffer_lo)
  98. # define trace_buffer_hi IPA_SYM_EXPORTED_NAME (trace_buffer_hi)
  99. # define traceframe_read_count IPA_SYM_EXPORTED_NAME (traceframe_read_count)
  100. # define traceframe_write_count IPA_SYM_EXPORTED_NAME (traceframe_write_count)
  101. # define traceframes_created IPA_SYM_EXPORTED_NAME (traceframes_created)
  102. # define trace_state_variables IPA_SYM_EXPORTED_NAME (trace_state_variables)
  103. # define get_raw_reg_ptr IPA_SYM_EXPORTED_NAME (get_raw_reg_ptr)
  104. # define get_trace_state_variable_value_ptr \
  105. IPA_SYM_EXPORTED_NAME (get_trace_state_variable_value_ptr)
  106. # define set_trace_state_variable_value_ptr \
  107. IPA_SYM_EXPORTED_NAME (set_trace_state_variable_value_ptr)
  108. # define ust_loaded IPA_SYM_EXPORTED_NAME (ust_loaded)
  109. # define helper_thread_id IPA_SYM_EXPORTED_NAME (helper_thread_id)
  110. # define cmd_buf IPA_SYM_EXPORTED_NAME (cmd_buf)
  111. # define ipa_tdesc_idx IPA_SYM_EXPORTED_NAME (ipa_tdesc_idx)
  112. #endif
  113. #ifndef IN_PROCESS_AGENT
  114. /* Addresses of in-process agent's symbols GDBserver cares about. */
  115. struct ipa_sym_addresses
  116. {
  117. CORE_ADDR addr_gdb_tp_heap_buffer;
  118. CORE_ADDR addr_gdb_jump_pad_buffer;
  119. CORE_ADDR addr_gdb_jump_pad_buffer_end;
  120. CORE_ADDR addr_gdb_trampoline_buffer;
  121. CORE_ADDR addr_gdb_trampoline_buffer_end;
  122. CORE_ADDR addr_gdb_trampoline_buffer_error;
  123. CORE_ADDR addr_collecting;
  124. CORE_ADDR addr_gdb_collect_ptr;
  125. CORE_ADDR addr_stop_tracing;
  126. CORE_ADDR addr_flush_trace_buffer;
  127. CORE_ADDR addr_about_to_request_buffer_space;
  128. CORE_ADDR addr_trace_buffer_is_full;
  129. CORE_ADDR addr_stopping_tracepoint;
  130. CORE_ADDR addr_expr_eval_result;
  131. CORE_ADDR addr_error_tracepoint;
  132. CORE_ADDR addr_tracepoints;
  133. CORE_ADDR addr_tracing;
  134. CORE_ADDR addr_trace_buffer_ctrl;
  135. CORE_ADDR addr_trace_buffer_ctrl_curr;
  136. CORE_ADDR addr_trace_buffer_lo;
  137. CORE_ADDR addr_trace_buffer_hi;
  138. CORE_ADDR addr_traceframe_read_count;
  139. CORE_ADDR addr_traceframe_write_count;
  140. CORE_ADDR addr_traceframes_created;
  141. CORE_ADDR addr_trace_state_variables;
  142. CORE_ADDR addr_get_raw_reg_ptr;
  143. CORE_ADDR addr_get_trace_state_variable_value_ptr;
  144. CORE_ADDR addr_set_trace_state_variable_value_ptr;
  145. CORE_ADDR addr_ust_loaded;
  146. CORE_ADDR addr_ipa_tdesc_idx;
  147. };
  148. static struct
  149. {
  150. const char *name;
  151. int offset;
  152. } symbol_list[] = {
  153. IPA_SYM(gdb_tp_heap_buffer),
  154. IPA_SYM(gdb_jump_pad_buffer),
  155. IPA_SYM(gdb_jump_pad_buffer_end),
  156. IPA_SYM(gdb_trampoline_buffer),
  157. IPA_SYM(gdb_trampoline_buffer_end),
  158. IPA_SYM(gdb_trampoline_buffer_error),
  159. IPA_SYM(collecting),
  160. IPA_SYM(gdb_collect_ptr),
  161. IPA_SYM(stop_tracing),
  162. IPA_SYM(flush_trace_buffer),
  163. IPA_SYM(about_to_request_buffer_space),
  164. IPA_SYM(trace_buffer_is_full),
  165. IPA_SYM(stopping_tracepoint),
  166. IPA_SYM(expr_eval_result),
  167. IPA_SYM(error_tracepoint),
  168. IPA_SYM(tracepoints),
  169. IPA_SYM(tracing),
  170. IPA_SYM(trace_buffer_ctrl),
  171. IPA_SYM(trace_buffer_ctrl_curr),
  172. IPA_SYM(trace_buffer_lo),
  173. IPA_SYM(trace_buffer_hi),
  174. IPA_SYM(traceframe_read_count),
  175. IPA_SYM(traceframe_write_count),
  176. IPA_SYM(traceframes_created),
  177. IPA_SYM(trace_state_variables),
  178. IPA_SYM(get_raw_reg_ptr),
  179. IPA_SYM(get_trace_state_variable_value_ptr),
  180. IPA_SYM(set_trace_state_variable_value_ptr),
  181. IPA_SYM(ust_loaded),
  182. IPA_SYM(ipa_tdesc_idx),
  183. };
  184. static struct ipa_sym_addresses ipa_sym_addrs;
  185. static int read_inferior_integer (CORE_ADDR symaddr, int *val);
  186. /* Returns true if both the in-process agent library and the static
  187. tracepoints libraries are loaded in the inferior, and agent has
  188. capability on static tracepoints. */
  189. static int
  190. in_process_agent_supports_ust (void)
  191. {
  192. int loaded = 0;
  193. if (!agent_loaded_p ())
  194. {
  195. warning ("In-process agent not loaded");
  196. return 0;
  197. }
  198. if (agent_capability_check (AGENT_CAPA_STATIC_TRACE))
  199. {
  200. /* Agent understands static tracepoint, then check whether UST is in
  201. fact loaded in the inferior. */
  202. if (read_inferior_integer (ipa_sym_addrs.addr_ust_loaded, &loaded))
  203. {
  204. warning ("Error reading ust_loaded in lib");
  205. return 0;
  206. }
  207. return loaded;
  208. }
  209. else
  210. return 0;
  211. }
  212. static void
  213. write_e_ipa_not_loaded (char *buffer)
  214. {
  215. sprintf (buffer,
  216. "E.In-process agent library not loaded in process. "
  217. "Fast and static tracepoints unavailable.");
  218. }
  219. /* Write an error to BUFFER indicating that UST isn't loaded in the
  220. inferior. */
  221. static void
  222. write_e_ust_not_loaded (char *buffer)
  223. {
  224. #ifdef HAVE_UST
  225. sprintf (buffer,
  226. "E.UST library not loaded in process. "
  227. "Static tracepoints unavailable.");
  228. #else
  229. sprintf (buffer, "E.GDBserver was built without static tracepoints support");
  230. #endif
  231. }
  232. /* If the in-process agent library isn't loaded in the inferior, write
  233. an error to BUFFER, and return 1. Otherwise, return 0. */
  234. static int
  235. maybe_write_ipa_not_loaded (char *buffer)
  236. {
  237. if (!agent_loaded_p ())
  238. {
  239. write_e_ipa_not_loaded (buffer);
  240. return 1;
  241. }
  242. return 0;
  243. }
  244. /* If the in-process agent library and the ust (static tracepoints)
  245. library aren't loaded in the inferior, write an error to BUFFER,
  246. and return 1. Otherwise, return 0. */
  247. static int
  248. maybe_write_ipa_ust_not_loaded (char *buffer)
  249. {
  250. if (!agent_loaded_p ())
  251. {
  252. write_e_ipa_not_loaded (buffer);
  253. return 1;
  254. }
  255. else if (!in_process_agent_supports_ust ())
  256. {
  257. write_e_ust_not_loaded (buffer);
  258. return 1;
  259. }
  260. return 0;
  261. }
  262. /* Cache all future symbols that the tracepoints module might request.
  263. We can not request symbols at arbitrary states in the remote
  264. protocol, only when the client tells us that new symbols are
  265. available. So when we load the in-process library, make sure to
  266. check the entire list. */
  267. void
  268. tracepoint_look_up_symbols (void)
  269. {
  270. int i;
  271. if (agent_loaded_p ())
  272. return;
  273. for (i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++)
  274. {
  275. CORE_ADDR *addrp =
  276. (CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset);
  277. if (look_up_one_symbol (symbol_list[i].name, addrp, 1) == 0)
  278. {
  279. threads_debug_printf ("symbol `%s' not found", symbol_list[i].name);
  280. return;
  281. }
  282. }
  283. agent_look_up_symbols (NULL);
  284. }
  285. #endif
  286. /* GDBserver places a breakpoint on the IPA's version (which is a nop)
  287. of the "stop_tracing" function. When this breakpoint is hit,
  288. tracing stopped in the IPA for some reason. E.g., due to
  289. tracepoint reaching the pass count, hitting conditional expression
  290. evaluation error, etc.
  291. The IPA's trace buffer is never in circular tracing mode: instead,
  292. GDBserver's is, and whenever the in-process buffer fills, it calls
  293. "flush_trace_buffer", which triggers an internal breakpoint.
  294. GDBserver reacts to this breakpoint by pulling the meanwhile
  295. collected data. Old frames discarding is always handled on the
  296. GDBserver side. */
  297. #ifdef IN_PROCESS_AGENT
  298. int
  299. read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
  300. {
  301. memcpy (myaddr, (void *) (uintptr_t) memaddr, len);
  302. return 0;
  303. }
  304. /* Call this in the functions where GDBserver places a breakpoint, so
  305. that the compiler doesn't try to be clever and skip calling the
  306. function at all. This is necessary, even if we tell the compiler
  307. to not inline said functions. */
  308. #if defined(__GNUC__)
  309. # define UNKNOWN_SIDE_EFFECTS() asm ("")
  310. #else
  311. # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
  312. #endif
  313. /* This is needed for -Wmissing-declarations. */
  314. IP_AGENT_EXPORT_FUNC void stop_tracing (void);
  315. IP_AGENT_EXPORT_FUNC void
  316. stop_tracing (void)
  317. {
  318. /* GDBserver places breakpoint here. */
  319. UNKNOWN_SIDE_EFFECTS();
  320. }
  321. /* This is needed for -Wmissing-declarations. */
  322. IP_AGENT_EXPORT_FUNC void flush_trace_buffer (void);
  323. IP_AGENT_EXPORT_FUNC void
  324. flush_trace_buffer (void)
  325. {
  326. /* GDBserver places breakpoint here. */
  327. UNKNOWN_SIDE_EFFECTS();
  328. }
  329. #endif
  330. #ifndef IN_PROCESS_AGENT
  331. static int
  332. tracepoint_handler (CORE_ADDR address)
  333. {
  334. trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
  335. paddress (address));
  336. return 0;
  337. }
  338. /* Breakpoint at "stop_tracing" in the inferior lib. */
  339. static struct breakpoint *stop_tracing_bkpt;
  340. static int stop_tracing_handler (CORE_ADDR);
  341. /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
  342. static struct breakpoint *flush_trace_buffer_bkpt;
  343. static int flush_trace_buffer_handler (CORE_ADDR);
  344. static void download_trace_state_variables (void);
  345. static void upload_fast_traceframes (void);
  346. static int run_inferior_command (char *cmd, int len);
  347. static int
  348. read_inferior_integer (CORE_ADDR symaddr, int *val)
  349. {
  350. return read_inferior_memory (symaddr, (unsigned char *) val,
  351. sizeof (*val));
  352. }
  353. struct tracepoint;
  354. static int tracepoint_send_agent (struct tracepoint *tpoint);
  355. static int
  356. read_inferior_uinteger (CORE_ADDR symaddr, unsigned int *val)
  357. {
  358. return read_inferior_memory (symaddr, (unsigned char *) val,
  359. sizeof (*val));
  360. }
  361. static int
  362. read_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR *val)
  363. {
  364. void *pval = (void *) (uintptr_t) val;
  365. int ret;
  366. ret = read_inferior_memory (symaddr, (unsigned char *) &pval, sizeof (pval));
  367. *val = (uintptr_t) pval;
  368. return ret;
  369. }
  370. static int
  371. write_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR val)
  372. {
  373. void *pval = (void *) (uintptr_t) val;
  374. return target_write_memory (symaddr,
  375. (unsigned char *) &pval, sizeof (pval));
  376. }
  377. static int
  378. write_inferior_integer (CORE_ADDR symaddr, int val)
  379. {
  380. return target_write_memory (symaddr, (unsigned char *) &val, sizeof (val));
  381. }
  382. static int
  383. write_inferior_int8 (CORE_ADDR symaddr, int8_t val)
  384. {
  385. return target_write_memory (symaddr, (unsigned char *) &val, sizeof (val));
  386. }
  387. static int
  388. write_inferior_uinteger (CORE_ADDR symaddr, unsigned int val)
  389. {
  390. return target_write_memory (symaddr, (unsigned char *) &val, sizeof (val));
  391. }
  392. static CORE_ADDR target_malloc (ULONGEST size);
  393. #define COPY_FIELD_TO_BUF(BUF, OBJ, FIELD) \
  394. do { \
  395. memcpy (BUF, &(OBJ)->FIELD, sizeof ((OBJ)->FIELD)); \
  396. BUF += sizeof ((OBJ)->FIELD); \
  397. } while (0)
  398. #endif
  399. /* Base action. Concrete actions inherit this. */
  400. struct tracepoint_action
  401. {
  402. char type;
  403. };
  404. /* An 'M' (collect memory) action. */
  405. struct collect_memory_action
  406. {
  407. struct tracepoint_action base;
  408. ULONGEST addr;
  409. ULONGEST len;
  410. int32_t basereg;
  411. };
  412. /* An 'R' (collect registers) action. */
  413. struct collect_registers_action
  414. {
  415. struct tracepoint_action base;
  416. };
  417. /* An 'X' (evaluate expression) action. */
  418. struct eval_expr_action
  419. {
  420. struct tracepoint_action base;
  421. struct agent_expr *expr;
  422. };
  423. /* An 'L' (collect static trace data) action. */
  424. struct collect_static_trace_data_action
  425. {
  426. struct tracepoint_action base;
  427. };
  428. #ifndef IN_PROCESS_AGENT
  429. static CORE_ADDR
  430. m_tracepoint_action_download (const struct tracepoint_action *action)
  431. {
  432. CORE_ADDR ipa_action = target_malloc (sizeof (struct collect_memory_action));
  433. target_write_memory (ipa_action, (unsigned char *) action,
  434. sizeof (struct collect_memory_action));
  435. return ipa_action;
  436. }
  437. static char *
  438. m_tracepoint_action_send (char *buffer, const struct tracepoint_action *action)
  439. {
  440. struct collect_memory_action *maction
  441. = (struct collect_memory_action *) action;
  442. COPY_FIELD_TO_BUF (buffer, maction, addr);
  443. COPY_FIELD_TO_BUF (buffer, maction, len);
  444. COPY_FIELD_TO_BUF (buffer, maction, basereg);
  445. return buffer;
  446. }
  447. static CORE_ADDR
  448. r_tracepoint_action_download (const struct tracepoint_action *action)
  449. {
  450. CORE_ADDR ipa_action = target_malloc (sizeof (struct collect_registers_action));
  451. target_write_memory (ipa_action, (unsigned char *) action,
  452. sizeof (struct collect_registers_action));
  453. return ipa_action;
  454. }
  455. static char *
  456. r_tracepoint_action_send (char *buffer, const struct tracepoint_action *action)
  457. {
  458. return buffer;
  459. }
  460. static CORE_ADDR download_agent_expr (struct agent_expr *expr);
  461. static CORE_ADDR
  462. x_tracepoint_action_download (const struct tracepoint_action *action)
  463. {
  464. CORE_ADDR ipa_action = target_malloc (sizeof (struct eval_expr_action));
  465. CORE_ADDR expr;
  466. target_write_memory (ipa_action, (unsigned char *) action,
  467. sizeof (struct eval_expr_action));
  468. expr = download_agent_expr (((struct eval_expr_action *) action)->expr);
  469. write_inferior_data_pointer (ipa_action
  470. + offsetof (struct eval_expr_action, expr),
  471. expr);
  472. return ipa_action;
  473. }
  474. /* Copy agent expression AEXPR to buffer pointed by P. If AEXPR is NULL,
  475. copy 0 to P. Return updated header of buffer. */
  476. static char *
  477. agent_expr_send (char *p, const struct agent_expr *aexpr)
  478. {
  479. /* Copy the length of condition first, and then copy its
  480. content. */
  481. if (aexpr == NULL)
  482. {
  483. memset (p, 0, 4);
  484. p += 4;
  485. }
  486. else
  487. {
  488. memcpy (p, &aexpr->length, 4);
  489. p +=4;
  490. memcpy (p, aexpr->bytes, aexpr->length);
  491. p += aexpr->length;
  492. }
  493. return p;
  494. }
  495. static char *
  496. x_tracepoint_action_send ( char *buffer, const struct tracepoint_action *action)
  497. {
  498. struct eval_expr_action *eaction = (struct eval_expr_action *) action;
  499. return agent_expr_send (buffer, eaction->expr);
  500. }
  501. static CORE_ADDR
  502. l_tracepoint_action_download (const struct tracepoint_action *action)
  503. {
  504. CORE_ADDR ipa_action
  505. = target_malloc (sizeof (struct collect_static_trace_data_action));
  506. target_write_memory (ipa_action, (unsigned char *) action,
  507. sizeof (struct collect_static_trace_data_action));
  508. return ipa_action;
  509. }
  510. static char *
  511. l_tracepoint_action_send (char *buffer, const struct tracepoint_action *action)
  512. {
  513. return buffer;
  514. }
  515. static char *
  516. tracepoint_action_send (char *buffer, const struct tracepoint_action *action)
  517. {
  518. switch (action->type)
  519. {
  520. case 'M':
  521. return m_tracepoint_action_send (buffer, action);
  522. case 'R':
  523. return r_tracepoint_action_send (buffer, action);
  524. case 'X':
  525. return x_tracepoint_action_send (buffer, action);
  526. case 'L':
  527. return l_tracepoint_action_send (buffer, action);
  528. }
  529. error ("Unknown trace action '%c'.", action->type);
  530. }
  531. static CORE_ADDR
  532. tracepoint_action_download (const struct tracepoint_action *action)
  533. {
  534. switch (action->type)
  535. {
  536. case 'M':
  537. return m_tracepoint_action_download (action);
  538. case 'R':
  539. return r_tracepoint_action_download (action);
  540. case 'X':
  541. return x_tracepoint_action_download (action);
  542. case 'L':
  543. return l_tracepoint_action_download (action);
  544. }
  545. error ("Unknown trace action '%c'.", action->type);
  546. }
  547. #endif
  548. /* This structure describes a piece of the source-level definition of
  549. the tracepoint. The contents are not interpreted by the target,
  550. but preserved verbatim for uploading upon reconnection. */
  551. struct source_string
  552. {
  553. /* The type of string, such as "cond" for a conditional. */
  554. char *type;
  555. /* The source-level string itself. For the sake of target
  556. debugging, we store it in plaintext, even though it is always
  557. transmitted in hex. */
  558. char *str;
  559. /* Link to the next one in the list. We link them in the order
  560. received, in case some make up an ordered list of commands or
  561. some such. */
  562. struct source_string *next;
  563. };
  564. enum tracepoint_type
  565. {
  566. /* Trap based tracepoint. */
  567. trap_tracepoint,
  568. /* A fast tracepoint implemented with a jump instead of a trap. */
  569. fast_tracepoint,
  570. /* A static tracepoint, implemented by a program call into a tracing
  571. library. */
  572. static_tracepoint
  573. };
  574. struct tracepoint_hit_ctx;
  575. typedef enum eval_result_type (*condfn) (unsigned char *,
  576. ULONGEST *);
  577. /* The definition of a tracepoint. */
  578. /* Tracepoints may have multiple locations, each at a different
  579. address. This can occur with optimizations, template
  580. instantiation, etc. Since the locations may be in different
  581. scopes, the conditions and actions may be different for each
  582. location. Our target version of tracepoints is more like GDB's
  583. notion of "breakpoint locations", but we have almost nothing that
  584. is not per-location, so we bother having two kinds of objects. The
  585. key consequence is that numbers are not unique, and that it takes
  586. both number and address to identify a tracepoint uniquely. */
  587. struct tracepoint
  588. {
  589. /* The number of the tracepoint, as specified by GDB. Several
  590. tracepoint objects here may share a number. */
  591. uint32_t number;
  592. /* Address at which the tracepoint is supposed to trigger. Several
  593. tracepoints may share an address. */
  594. CORE_ADDR address;
  595. /* Tracepoint type. */
  596. enum tracepoint_type type;
  597. /* True if the tracepoint is currently enabled. */
  598. int8_t enabled;
  599. /* The number of single steps that will be performed after each
  600. tracepoint hit. */
  601. uint64_t step_count;
  602. /* The number of times the tracepoint may be hit before it will
  603. terminate the entire tracing run. */
  604. uint64_t pass_count;
  605. /* Pointer to the agent expression that is the tracepoint's
  606. conditional, or NULL if the tracepoint is unconditional. */
  607. struct agent_expr *cond;
  608. /* The list of actions to take when the tracepoint triggers. */
  609. uint32_t numactions;
  610. struct tracepoint_action **actions;
  611. /* Count of the times we've hit this tracepoint during the run.
  612. Note that while-stepping steps are not counted as "hits". */
  613. uint64_t hit_count;
  614. /* Cached sum of the sizes of traceframes created by this point. */
  615. uint64_t traceframe_usage;
  616. CORE_ADDR compiled_cond;
  617. /* Link to the next tracepoint in the list. */
  618. struct tracepoint *next;
  619. #ifndef IN_PROCESS_AGENT
  620. /* The list of actions to take when the tracepoint triggers, in
  621. string/packet form. */
  622. char **actions_str;
  623. /* The collection of strings that describe the tracepoint as it was
  624. entered into GDB. These are not used by the target, but are
  625. reported back to GDB upon reconnection. */
  626. struct source_string *source_strings;
  627. /* The number of bytes displaced by fast tracepoints. It may subsume
  628. multiple instructions, for multi-byte fast tracepoints. This
  629. field is only valid for fast tracepoints. */
  630. uint32_t orig_size;
  631. /* Only for fast tracepoints. */
  632. CORE_ADDR obj_addr_on_target;
  633. /* Address range where the original instruction under a fast
  634. tracepoint was relocated to. (_end is actually one byte past
  635. the end). */
  636. CORE_ADDR adjusted_insn_addr;
  637. CORE_ADDR adjusted_insn_addr_end;
  638. /* The address range of the piece of the jump pad buffer that was
  639. assigned to this fast tracepoint. (_end is actually one byte
  640. past the end).*/
  641. CORE_ADDR jump_pad;
  642. CORE_ADDR jump_pad_end;
  643. /* The address range of the piece of the trampoline buffer that was
  644. assigned to this fast tracepoint. (_end is actually one byte
  645. past the end). */
  646. CORE_ADDR trampoline;
  647. CORE_ADDR trampoline_end;
  648. /* The list of actions to take while in a stepping loop. These
  649. fields are only valid for patch-based tracepoints. */
  650. int num_step_actions;
  651. struct tracepoint_action **step_actions;
  652. /* Same, but in string/packet form. */
  653. char **step_actions_str;
  654. /* Handle returned by the breakpoint or tracepoint module when we
  655. inserted the trap or jump, or hooked into a static tracepoint.
  656. NULL if we haven't inserted it yet. */
  657. void *handle;
  658. #endif
  659. };
  660. #ifndef IN_PROCESS_AGENT
  661. /* Given `while-stepping', a thread may be collecting data for more
  662. than one tracepoint simultaneously. On the other hand, the same
  663. tracepoint with a while-stepping action may be hit by more than one
  664. thread simultaneously (but not quite, each thread could be handling
  665. a different step). Each thread holds a list of these objects,
  666. representing the current step of each while-stepping action being
  667. collected. */
  668. struct wstep_state
  669. {
  670. struct wstep_state *next;
  671. /* The tracepoint number. */
  672. int tp_number;
  673. /* The tracepoint's address. */
  674. CORE_ADDR tp_address;
  675. /* The number of the current step in this 'while-stepping'
  676. action. */
  677. long current_step;
  678. };
  679. #endif
  680. EXTERN_C_PUSH
  681. /* The linked list of all tracepoints. Marked explicitly as used as
  682. the in-process library doesn't use it for the fast tracepoints
  683. support. */
  684. IP_AGENT_EXPORT_VAR struct tracepoint *tracepoints;
  685. /* The first tracepoint to exceed its pass count. */
  686. IP_AGENT_EXPORT_VAR struct tracepoint *stopping_tracepoint;
  687. /* True if the trace buffer is full or otherwise no longer usable. */
  688. IP_AGENT_EXPORT_VAR int trace_buffer_is_full;
  689. /* The first error that occurred during expression evaluation. */
  690. /* Stored as an int to avoid the IPA ABI being dependent on whatever
  691. the compiler decides to use for the enum's underlying type. Holds
  692. enum eval_result_type values. */
  693. IP_AGENT_EXPORT_VAR int expr_eval_result = expr_eval_no_error;
  694. EXTERN_C_POP
  695. #ifndef IN_PROCESS_AGENT
  696. /* Pointer to the last tracepoint in the list, new tracepoints are
  697. linked in at the end. */
  698. static struct tracepoint *last_tracepoint;
  699. static const char * const eval_result_names[] =
  700. {
  701. "terror:in the attic", /* this should never be reported */
  702. "terror:empty expression",
  703. "terror:empty stack",
  704. "terror:stack overflow",
  705. "terror:stack underflow",
  706. "terror:unhandled opcode",
  707. "terror:unrecognized opcode",
  708. "terror:divide by zero"
  709. };
  710. #endif
  711. /* The tracepoint in which the error occurred. */
  712. EXTERN_C_PUSH
  713. IP_AGENT_EXPORT_VAR struct tracepoint *error_tracepoint;
  714. EXTERN_C_POP
  715. struct trace_state_variable
  716. {
  717. /* This is the name of the variable as used in GDB. The target
  718. doesn't use the name, but needs to have it for saving and
  719. reconnection purposes. */
  720. char *name;
  721. /* This number identifies the variable uniquely. Numbers may be
  722. assigned either by the target (in the case of builtin variables),
  723. or by GDB, and are presumed unique during the course of a trace
  724. experiment. */
  725. int number;
  726. /* The variable's initial value, a 64-bit signed integer always. */
  727. LONGEST initial_value;
  728. /* The variable's value, a 64-bit signed integer always. */
  729. LONGEST value;
  730. /* Pointer to a getter function, used to supply computed values. */
  731. LONGEST (*getter) (void);
  732. /* Link to the next variable. */
  733. struct trace_state_variable *next;
  734. };
  735. /* Linked list of all trace state variables. */
  736. #ifdef IN_PROCESS_AGENT
  737. static struct trace_state_variable *alloced_trace_state_variables;
  738. #endif
  739. IP_AGENT_EXPORT_VAR struct trace_state_variable *trace_state_variables;
  740. /* The results of tracing go into a fixed-size space known as the
  741. "trace buffer". Because usage follows a limited number of
  742. patterns, we manage it ourselves rather than with malloc. Basic
  743. rules are that we create only one trace frame at a time, each is
  744. variable in size, they are never moved once created, and we only
  745. discard if we are doing a circular buffer, and then only the oldest
  746. ones. Each trace frame includes its own size, so we don't need to
  747. link them together, and the trace frame number is relative to the
  748. first one, so we don't need to record numbers. A trace frame also
  749. records the number of the tracepoint that created it. The data
  750. itself is a series of blocks, each introduced by a single character
  751. and with a defined format. Each type of block has enough
  752. type/length info to allow scanners to jump quickly from one block
  753. to the next without reading each byte in the block. */
  754. /* Trace buffer management would be simple - advance a free pointer
  755. from beginning to end, then stop - were it not for the circular
  756. buffer option, which is a useful way to prevent a trace run from
  757. stopping prematurely because the buffer filled up. In the circular
  758. case, the location of the first trace frame (trace_buffer_start)
  759. moves as old trace frames are discarded. Also, since we grow trace
  760. frames incrementally as actions are performed, we wrap around to
  761. the beginning of the trace buffer. This is per-block, so each
  762. block within a trace frame remains contiguous. Things get messy
  763. when the wrapped-around trace frame is the one being discarded; the
  764. free space ends up in two parts at opposite ends of the buffer. */
  765. #ifndef ATTR_PACKED
  766. # if defined(__GNUC__)
  767. # define ATTR_PACKED __attribute__ ((packed))
  768. # else
  769. # define ATTR_PACKED /* nothing */
  770. # endif
  771. #endif
  772. /* The data collected at a tracepoint hit. This object should be as
  773. small as possible, since there may be a great many of them. We do
  774. not need to keep a frame number, because they are all sequential
  775. and there are no deletions; so the Nth frame in the buffer is
  776. always frame number N. */
  777. struct traceframe
  778. {
  779. /* Number of the tracepoint that collected this traceframe. A value
  780. of 0 indicates the current end of the trace buffer. We make this
  781. a 16-bit field because it's never going to happen that GDB's
  782. numbering of tracepoints reaches 32,000. */
  783. int tpnum : 16;
  784. /* The size of the data in this trace frame. We limit this to 32
  785. bits, even on a 64-bit target, because it's just implausible that
  786. one is validly going to collect 4 gigabytes of data at a single
  787. tracepoint hit. */
  788. unsigned int data_size : 32;
  789. /* The base of the trace data, which is contiguous from this point. */
  790. unsigned char data[0];
  791. } ATTR_PACKED;
  792. /* The size of the EOB marker, in bytes. A traceframe with zeroed
  793. fields (and no data) marks the end of trace data. */
  794. #define TRACEFRAME_EOB_MARKER_SIZE offsetof (struct traceframe, data)
  795. /* This flag is true if the trace buffer is circular, meaning that
  796. when it fills, the oldest trace frames are discarded in order to
  797. make room. */
  798. #ifndef IN_PROCESS_AGENT
  799. static int circular_trace_buffer;
  800. #endif
  801. /* Size of the trace buffer. */
  802. static LONGEST trace_buffer_size;
  803. EXTERN_C_PUSH
  804. /* Pointer to the block of memory that traceframes all go into. */
  805. IP_AGENT_EXPORT_VAR unsigned char *trace_buffer_lo;
  806. /* Pointer to the end of the trace buffer, more precisely to the byte
  807. after the end of the buffer. */
  808. IP_AGENT_EXPORT_VAR unsigned char *trace_buffer_hi;
  809. EXTERN_C_POP
  810. /* Control structure holding the read/write/etc. pointers into the
  811. trace buffer. We need more than one of these to implement a
  812. transaction-like mechanism to guarantees that both GDBserver and the
  813. in-process agent can try to change the trace buffer
  814. simultaneously. */
  815. struct trace_buffer_control
  816. {
  817. /* Pointer to the first trace frame in the buffer. In the
  818. non-circular case, this is equal to trace_buffer_lo, otherwise it
  819. moves around in the buffer. */
  820. unsigned char *start;
  821. /* Pointer to the free part of the trace buffer. Note that we clear
  822. several bytes at and after this pointer, so that traceframe
  823. scans/searches terminate properly. */
  824. unsigned char *free;
  825. /* Pointer to the byte after the end of the free part. Note that
  826. this may be smaller than trace_buffer_free in the circular case,
  827. and means that the free part is in two pieces. Initially it is
  828. equal to trace_buffer_hi, then is generally equivalent to
  829. trace_buffer_start. */
  830. unsigned char *end_free;
  831. /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
  832. this is the point at which the trace data breaks, and resumes at
  833. trace_buffer_lo. */
  834. unsigned char *wrap;
  835. };
  836. /* Same as above, to be used by GDBserver when updating the in-process
  837. agent. */
  838. struct ipa_trace_buffer_control
  839. {
  840. uintptr_t start;
  841. uintptr_t free;
  842. uintptr_t end_free;
  843. uintptr_t wrap;
  844. };
  845. /* We have possibly both GDBserver and an inferior thread accessing
  846. the same IPA trace buffer memory. The IPA is the producer (tries
  847. to put new frames in the buffer), while GDBserver occasionally
  848. consumes them, that is, flushes the IPA's buffer into its own
  849. buffer. Both sides need to update the trace buffer control
  850. pointers (current head, tail, etc.). We can't use a global lock to
  851. synchronize the accesses, as otherwise we could deadlock GDBserver
  852. (if the thread holding the lock stops for a signal, say). So
  853. instead of that, we use a transaction scheme where GDBserver writes
  854. always prevail over the IPAs writes, and, we have the IPA detect
  855. the commit failure/overwrite, and retry the whole attempt. This is
  856. mainly implemented by having a global token object that represents
  857. who wrote last to the buffer control structure. We need to freeze
  858. any inferior writing to the buffer while GDBserver touches memory,
  859. so that the inferior can correctly detect that GDBserver had been
  860. there, otherwise, it could mistakingly think its commit was
  861. successful; that's implemented by simply having GDBserver set a
  862. breakpoint the inferior hits if it is the critical region.
  863. There are three cycling trace buffer control structure copies
  864. (buffer head, tail, etc.), with the token object including an index
  865. indicating which is current live copy. The IPA tentatively builds
  866. an updated copy in a non-current control structure, while GDBserver
  867. always clobbers the current version directly. The IPA then tries
  868. to atomically "commit" its version; if GDBserver clobbered the
  869. structure meanwhile, that will fail, and the IPA restarts the
  870. allocation process.
  871. Listing the step in further detail, we have:
  872. In-process agent (producer):
  873. - passes by `about_to_request_buffer_space' breakpoint/lock
  874. - reads current token, extracts current trace buffer control index,
  875. and starts tentatively updating the rightmost one (0->1, 1->2,
  876. 2->0). Note that only one inferior thread is executing this code
  877. at any given time, due to an outer lock in the jump pads.
  878. - updates counters, and tries to commit the token.
  879. - passes by second `about_to_request_buffer_space' breakpoint/lock,
  880. leaving the sync region.
  881. - checks if the update was effective.
  882. - if trace buffer was found full, hits flush_trace_buffer
  883. breakpoint, and restarts later afterwards.
  884. GDBserver (consumer):
  885. - sets `about_to_request_buffer_space' breakpoint/lock.
  886. - updates the token unconditionally, using the current buffer
  887. control index, since it knows that the IP agent always writes to
  888. the rightmost, and due to the breakpoint, at most one IP thread
  889. can try to update the trace buffer concurrently to GDBserver, so
  890. there will be no danger of trace buffer control index wrap making
  891. the IPA write to the same index as GDBserver.
  892. - flushes the IP agent's trace buffer completely, and updates the
  893. current trace buffer control structure. GDBserver *always* wins.
  894. - removes the `about_to_request_buffer_space' breakpoint.
  895. The token is stored in the `trace_buffer_ctrl_curr' variable.
  896. Internally, it's bits are defined as:
  897. |-------------+-----+-------------+--------+-------------+--------------|
  898. | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
  899. |-------------+-----+-------------+--------+-------------+--------------|
  900. | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
  901. |-------------+-----+-------------+--------+-------------+--------------|
  902. GSB - GDBserver Stamp Bit
  903. PC - Previous Counter
  904. CC - Current Counter
  905. TBCI - Trace Buffer Control Index
  906. An IPA update of `trace_buffer_ctrl_curr' does:
  907. - read CC from the current token, save as PC.
  908. - updates pointers
  909. - atomically tries to write PC+1,CC
  910. A GDBserver update of `trace_buffer_ctrl_curr' does:
  911. - reads PC and CC from the current token.
  912. - updates pointers
  913. - writes GSB,PC,CC
  914. */
  915. /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
  916. for the counters described below. The cleared bits are used to
  917. hold the index of the items of the `trace_buffer_ctrl' array that
  918. is "current". */
  919. #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
  920. /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
  921. counter, and the `current' counter. */
  922. #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
  923. #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
  924. /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
  925. always stamps this bit as set. */
  926. #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
  927. #ifdef IN_PROCESS_AGENT
  928. IP_AGENT_EXPORT_VAR struct trace_buffer_control trace_buffer_ctrl[3];
  929. IP_AGENT_EXPORT_VAR unsigned int trace_buffer_ctrl_curr;
  930. # define TRACE_BUFFER_CTRL_CURR \
  931. (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
  932. #else
  933. /* The GDBserver side agent only needs one instance of this object, as
  934. it doesn't need to sync with itself. Define it as array anyway so
  935. that the rest of the code base doesn't need to care for the
  936. difference. */
  937. static trace_buffer_control trace_buffer_ctrl[1];
  938. # define TRACE_BUFFER_CTRL_CURR 0
  939. #endif
  940. /* These are convenience macros used to access the current trace
  941. buffer control in effect. */
  942. #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
  943. #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
  944. #define trace_buffer_end_free \
  945. (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
  946. #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
  947. /* Macro that returns a pointer to the first traceframe in the buffer. */
  948. #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
  949. /* Macro that returns a pointer to the next traceframe in the buffer.
  950. If the computed location is beyond the wraparound point, subtract
  951. the offset of the wraparound. */
  952. #define NEXT_TRACEFRAME_1(TF) \
  953. (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
  954. #define NEXT_TRACEFRAME(TF) \
  955. ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
  956. - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
  957. ? (trace_buffer_wrap - trace_buffer_lo) \
  958. : 0)))
  959. /* The difference between these counters represents the total number
  960. of complete traceframes present in the trace buffer. The IP agent
  961. writes to the write count, GDBserver writes to read count. */
  962. IP_AGENT_EXPORT_VAR unsigned int traceframe_write_count;
  963. IP_AGENT_EXPORT_VAR unsigned int traceframe_read_count;
  964. /* Convenience macro. */
  965. #define traceframe_count \
  966. ((unsigned int) (traceframe_write_count - traceframe_read_count))
  967. /* The count of all traceframes created in the current run, including
  968. ones that were discarded to make room. */
  969. IP_AGENT_EXPORT_VAR int traceframes_created;
  970. #ifndef IN_PROCESS_AGENT
  971. /* Read-only regions are address ranges whose contents don't change,
  972. and so can be read from target memory even while looking at a trace
  973. frame. Without these, disassembly for instance will likely fail,
  974. because the program code is not usually collected into a trace
  975. frame. This data structure does not need to be very complicated or
  976. particularly efficient, it's only going to be used occasionally,
  977. and only by some commands. */
  978. struct readonly_region
  979. {
  980. /* The bounds of the region. */
  981. CORE_ADDR start, end;
  982. /* Link to the next one. */
  983. struct readonly_region *next;
  984. };
  985. /* Linked list of readonly regions. This list stays in effect from
  986. one tstart to the next. */
  987. static struct readonly_region *readonly_regions;
  988. #endif
  989. /* The global that controls tracing overall. */
  990. IP_AGENT_EXPORT_VAR int tracing;
  991. #ifndef IN_PROCESS_AGENT
  992. /* Controls whether tracing should continue after GDB disconnects. */
  993. int disconnected_tracing;
  994. /* The reason for the last tracing run to have stopped. We initialize
  995. to a distinct string so that GDB can distinguish between "stopped
  996. after running" and "stopped because never run" cases. */
  997. static const char *tracing_stop_reason = "tnotrun";
  998. static int tracing_stop_tpnum;
  999. /* 64-bit timestamps for the trace run's start and finish, expressed
  1000. in microseconds from the Unix epoch. */
  1001. static LONGEST tracing_start_time;
  1002. static LONGEST tracing_stop_time;
  1003. /* The (optional) user-supplied name of the user that started the run.
  1004. This is an arbitrary string, and may be NULL. */
  1005. static char *tracing_user_name;
  1006. /* Optional user-supplied text describing the run. This is
  1007. an arbitrary string, and may be NULL. */
  1008. static char *tracing_notes;
  1009. /* Optional user-supplied text explaining a tstop command. This is an
  1010. arbitrary string, and may be NULL. */
  1011. static char *tracing_stop_note;
  1012. #endif
  1013. /* Functions local to this file. */
  1014. /* Base "class" for tracepoint type specific data to be passed down to
  1015. collect_data_at_tracepoint. */
  1016. struct tracepoint_hit_ctx
  1017. {
  1018. enum tracepoint_type type;
  1019. };
  1020. #ifdef IN_PROCESS_AGENT
  1021. /* Fast/jump tracepoint specific data to be passed down to
  1022. collect_data_at_tracepoint. */
  1023. struct fast_tracepoint_ctx
  1024. {
  1025. struct tracepoint_hit_ctx base;
  1026. struct regcache regcache;
  1027. int regcache_initted;
  1028. unsigned char *regspace;
  1029. unsigned char *regs;
  1030. struct tracepoint *tpoint;
  1031. };
  1032. /* Static tracepoint specific data to be passed down to
  1033. collect_data_at_tracepoint. */
  1034. struct static_tracepoint_ctx
  1035. {
  1036. struct tracepoint_hit_ctx base;
  1037. /* The regcache corresponding to the registers state at the time of
  1038. the tracepoint hit. Initialized lazily, from REGS. */
  1039. struct regcache regcache;
  1040. int regcache_initted;
  1041. /* The buffer space REGCACHE above uses. We use a separate buffer
  1042. instead of letting the regcache malloc for both signal safety and
  1043. performance reasons; this is allocated on the stack instead. */
  1044. unsigned char *regspace;
  1045. /* The register buffer as passed on by lttng/ust. */
  1046. struct registers *regs;
  1047. /* The "printf" formatter and the args the user passed to the marker
  1048. call. We use this to be able to collect "static trace data"
  1049. ($_sdata). */
  1050. const char *fmt;
  1051. va_list *args;
  1052. /* The GDB tracepoint matching the probed marker that was "hit". */
  1053. struct tracepoint *tpoint;
  1054. };
  1055. #else
  1056. /* Static tracepoint specific data to be passed down to
  1057. collect_data_at_tracepoint. */
  1058. struct trap_tracepoint_ctx
  1059. {
  1060. struct tracepoint_hit_ctx base;
  1061. struct regcache *regcache;
  1062. };
  1063. #endif
  1064. #ifndef IN_PROCESS_AGENT
  1065. static CORE_ADDR traceframe_get_pc (struct traceframe *tframe);
  1066. static int traceframe_read_tsv (int num, LONGEST *val);
  1067. #endif
  1068. static int condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
  1069. struct tracepoint *tpoint);
  1070. #ifndef IN_PROCESS_AGENT
  1071. static void clear_readonly_regions (void);
  1072. static void clear_installed_tracepoints (void);
  1073. #endif
  1074. static void collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
  1075. CORE_ADDR stop_pc,
  1076. struct tracepoint *tpoint);
  1077. #ifndef IN_PROCESS_AGENT
  1078. static void collect_data_at_step (struct tracepoint_hit_ctx *ctx,
  1079. CORE_ADDR stop_pc,
  1080. struct tracepoint *tpoint, int current_step);
  1081. static void compile_tracepoint_condition (struct tracepoint *tpoint,
  1082. CORE_ADDR *jump_entry);
  1083. #endif
  1084. static void do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
  1085. CORE_ADDR stop_pc,
  1086. struct tracepoint *tpoint,
  1087. struct traceframe *tframe,
  1088. struct tracepoint_action *taction);
  1089. #ifndef IN_PROCESS_AGENT
  1090. static struct tracepoint *fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR);
  1091. static void install_tracepoint (struct tracepoint *, char *own_buf);
  1092. static void download_tracepoint (struct tracepoint *);
  1093. static int install_fast_tracepoint (struct tracepoint *, char *errbuf);
  1094. static void clone_fast_tracepoint (struct tracepoint *to,
  1095. const struct tracepoint *from);
  1096. #endif
  1097. static LONGEST get_timestamp (void);
  1098. #if defined(__GNUC__)
  1099. # define memory_barrier() asm volatile ("" : : : "memory")
  1100. #else
  1101. # define memory_barrier() do {} while (0)
  1102. #endif
  1103. /* We only build the IPA if this builtin is supported, and there are
  1104. no uses of this in GDBserver itself, so we're safe in defining this
  1105. unconditionally. */
  1106. #define cmpxchg(mem, oldval, newval) \
  1107. __sync_val_compare_and_swap (mem, oldval, newval)
  1108. /* Record that an error occurred during expression evaluation. */
  1109. static void
  1110. record_tracepoint_error (struct tracepoint *tpoint, const char *which,
  1111. enum eval_result_type rtype)
  1112. {
  1113. trace_debug ("Tracepoint %d at %s %s eval reports error %d",
  1114. tpoint->number, paddress (tpoint->address), which, rtype);
  1115. #ifdef IN_PROCESS_AGENT
  1116. /* Only record the first error we get. */
  1117. if (cmpxchg (&expr_eval_result,
  1118. expr_eval_no_error,
  1119. rtype) != expr_eval_no_error)
  1120. return;
  1121. #else
  1122. if (expr_eval_result != expr_eval_no_error)
  1123. return;
  1124. #endif
  1125. error_tracepoint = tpoint;
  1126. }
  1127. /* Trace buffer management. */
  1128. static void
  1129. clear_trace_buffer (void)
  1130. {
  1131. trace_buffer_start = trace_buffer_lo;
  1132. trace_buffer_free = trace_buffer_lo;
  1133. trace_buffer_end_free = trace_buffer_hi;
  1134. trace_buffer_wrap = trace_buffer_hi;
  1135. /* A traceframe with zeroed fields marks the end of trace data. */
  1136. ((struct traceframe *) trace_buffer_free)->tpnum = 0;
  1137. ((struct traceframe *) trace_buffer_free)->data_size = 0;
  1138. traceframe_read_count = traceframe_write_count = 0;
  1139. traceframes_created = 0;
  1140. }
  1141. #ifndef IN_PROCESS_AGENT
  1142. static void
  1143. clear_inferior_trace_buffer (void)
  1144. {
  1145. CORE_ADDR ipa_trace_buffer_lo;
  1146. CORE_ADDR ipa_trace_buffer_hi;
  1147. struct traceframe ipa_traceframe = { 0 };
  1148. struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
  1149. read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
  1150. &ipa_trace_buffer_lo);
  1151. read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
  1152. &ipa_trace_buffer_hi);
  1153. ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
  1154. ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
  1155. ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
  1156. ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
  1157. /* A traceframe with zeroed fields marks the end of trace data. */
  1158. target_write_memory (ipa_sym_addrs.addr_trace_buffer_ctrl,
  1159. (unsigned char *) &ipa_trace_buffer_ctrl,
  1160. sizeof (ipa_trace_buffer_ctrl));
  1161. write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr, 0);
  1162. /* A traceframe with zeroed fields marks the end of trace data. */
  1163. target_write_memory (ipa_trace_buffer_lo,
  1164. (unsigned char *) &ipa_traceframe,
  1165. sizeof (ipa_traceframe));
  1166. write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count, 0);
  1167. write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count, 0);
  1168. write_inferior_integer (ipa_sym_addrs.addr_traceframes_created, 0);
  1169. }
  1170. #endif
  1171. static void
  1172. init_trace_buffer (LONGEST bufsize)
  1173. {
  1174. size_t alloc_size;
  1175. trace_buffer_size = bufsize;
  1176. /* Make sure to internally allocate at least space for the EOB
  1177. marker. */
  1178. alloc_size = (bufsize < TRACEFRAME_EOB_MARKER_SIZE
  1179. ? TRACEFRAME_EOB_MARKER_SIZE : bufsize);
  1180. trace_buffer_lo = (unsigned char *) xrealloc (trace_buffer_lo, alloc_size);
  1181. trace_buffer_hi = trace_buffer_lo + trace_buffer_size;
  1182. clear_trace_buffer ();
  1183. }
  1184. #ifdef IN_PROCESS_AGENT
  1185. /* This is needed for -Wmissing-declarations. */
  1186. IP_AGENT_EXPORT_FUNC void about_to_request_buffer_space (void);
  1187. IP_AGENT_EXPORT_FUNC void
  1188. about_to_request_buffer_space (void)
  1189. {
  1190. /* GDBserver places breakpoint here while it goes about to flush
  1191. data at random times. */
  1192. UNKNOWN_SIDE_EFFECTS();
  1193. }
  1194. #endif
  1195. /* Carve out a piece of the trace buffer, returning NULL in case of
  1196. failure. */
  1197. static void *
  1198. trace_buffer_alloc (size_t amt)
  1199. {
  1200. unsigned char *rslt;
  1201. struct trace_buffer_control *tbctrl;
  1202. unsigned int curr;
  1203. #ifdef IN_PROCESS_AGENT
  1204. unsigned int prev, prev_filtered;
  1205. unsigned int commit_count;
  1206. unsigned int commit;
  1207. unsigned int readout;
  1208. #else
  1209. struct traceframe *oldest;
  1210. unsigned char *new_start;
  1211. #endif
  1212. trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
  1213. (long) amt, (long) sizeof (struct traceframe));
  1214. /* Account for the EOB marker. */
  1215. amt += TRACEFRAME_EOB_MARKER_SIZE;
  1216. #ifdef IN_PROCESS_AGENT
  1217. again:
  1218. memory_barrier ();
  1219. /* Read the current token and extract the index to try to write to,
  1220. storing it in CURR. */
  1221. prev = trace_buffer_ctrl_curr;
  1222. prev_filtered = prev & ~GDBSERVER_FLUSH_COUNT_MASK;
  1223. curr = prev_filtered + 1;
  1224. if (curr > 2)
  1225. curr = 0;
  1226. about_to_request_buffer_space ();
  1227. /* Start out with a copy of the current state. GDBserver may be
  1228. midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
  1229. be able to commit anyway if that happens. */
  1230. trace_buffer_ctrl[curr]
  1231. = trace_buffer_ctrl[prev_filtered];
  1232. trace_debug ("trying curr=%u", curr);
  1233. #else
  1234. /* The GDBserver's agent doesn't need all that syncing, and always
  1235. updates TCB 0 (there's only one, mind you). */
  1236. curr = 0;
  1237. #endif
  1238. tbctrl = &trace_buffer_ctrl[curr];
  1239. /* Offsets are easier to grok for debugging than raw addresses,
  1240. especially for the small trace buffer sizes that are useful for
  1241. testing. */
  1242. trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
  1243. curr,
  1244. (int) (tbctrl->start - trace_buffer_lo),
  1245. (int) (tbctrl->free - trace_buffer_lo),
  1246. (int) (tbctrl->end_free - trace_buffer_lo),
  1247. (int) (tbctrl->wrap - trace_buffer_lo),
  1248. (int) (trace_buffer_hi - trace_buffer_lo));
  1249. /* The algorithm here is to keep trying to get a contiguous block of
  1250. the requested size, possibly discarding older traceframes to free
  1251. up space. Since free space might come in one or two pieces,
  1252. depending on whether discarded traceframes wrapped around at the
  1253. high end of the buffer, we test both pieces after each
  1254. discard. */
  1255. while (1)
  1256. {
  1257. /* First, if we have two free parts, try the upper one first. */
  1258. if (tbctrl->end_free < tbctrl->free)
  1259. {
  1260. if (tbctrl->free + amt <= trace_buffer_hi)
  1261. /* We have enough in the upper part. */
  1262. break;
  1263. else
  1264. {
  1265. /* Our high part of free space wasn't enough. Give up
  1266. on it for now, set wraparound. We will recover the
  1267. space later, if/when the wrapped-around traceframe is
  1268. discarded. */
  1269. trace_debug ("Upper part too small, setting wraparound");
  1270. tbctrl->wrap = tbctrl->free;
  1271. tbctrl->free = trace_buffer_lo;
  1272. }
  1273. }
  1274. /* The normal case. */
  1275. if (tbctrl->free + amt <= tbctrl->end_free)
  1276. break;
  1277. #ifdef IN_PROCESS_AGENT
  1278. /* The IP Agent's buffer is always circular. It isn't used
  1279. currently, but `circular_trace_buffer' could represent
  1280. GDBserver's mode. If we didn't find space, ask GDBserver to
  1281. flush. */
  1282. flush_trace_buffer ();
  1283. memory_barrier ();
  1284. if (tracing)
  1285. {
  1286. trace_debug ("gdbserver flushed buffer, retrying");
  1287. goto again;
  1288. }
  1289. /* GDBserver cancelled the tracing. Bail out as well. */
  1290. return NULL;
  1291. #else
  1292. /* If we're here, then neither part is big enough, and
  1293. non-circular trace buffers are now full. */
  1294. if (!circular_trace_buffer)
  1295. {
  1296. trace_debug ("Not enough space in the trace buffer");
  1297. return NULL;
  1298. }
  1299. trace_debug ("Need more space in the trace buffer");
  1300. /* If we have a circular buffer, we can try discarding the
  1301. oldest traceframe and see if that helps. */
  1302. oldest = FIRST_TRACEFRAME ();
  1303. if (oldest->tpnum == 0)
  1304. {
  1305. /* Not good; we have no traceframes to free. Perhaps we're
  1306. asking for a block that is larger than the buffer? In
  1307. any case, give up. */
  1308. trace_debug ("No traceframes to discard");
  1309. return NULL;
  1310. }
  1311. /* We don't run this code in the in-process agent currently.
  1312. E.g., we could leave the in-process agent in autonomous
  1313. circular mode if we only have fast tracepoints. If we do
  1314. that, then this bit becomes racy with GDBserver, which also
  1315. writes to this counter. */
  1316. --traceframe_write_count;
  1317. new_start = (unsigned char *) NEXT_TRACEFRAME (oldest);
  1318. /* If we freed the traceframe that wrapped around, go back
  1319. to the non-wrap case. */
  1320. if (new_start < tbctrl->start)
  1321. {
  1322. trace_debug ("Discarding past the wraparound");
  1323. tbctrl->wrap = trace_buffer_hi;
  1324. }
  1325. tbctrl->start = new_start;
  1326. tbctrl->end_free = tbctrl->start;
  1327. trace_debug ("Discarded a traceframe\n"
  1328. "Trace buffer [%d], start=%d free=%d "
  1329. "endfree=%d wrap=%d hi=%d",
  1330. curr,
  1331. (int) (tbctrl->start - trace_buffer_lo),
  1332. (int) (tbctrl->free - trace_buffer_lo),
  1333. (int) (tbctrl->end_free - trace_buffer_lo),
  1334. (int) (tbctrl->wrap - trace_buffer_lo),
  1335. (int) (trace_buffer_hi - trace_buffer_lo));
  1336. /* Now go back around the loop. The discard might have resulted
  1337. in either one or two pieces of free space, so we want to try
  1338. both before freeing any more traceframes. */
  1339. #endif
  1340. }
  1341. /* If we get here, we know we can provide the asked-for space. */
  1342. rslt = tbctrl->free;
  1343. /* Adjust the request back down, now that we know we have space for
  1344. the marker, but don't commit to AMT yet, we may still need to
  1345. restart the operation if GDBserver touches the trace buffer
  1346. (obviously only important in the in-process agent's version). */
  1347. tbctrl->free += (amt - sizeof (struct traceframe));
  1348. /* Or not. If GDBserver changed the trace buffer behind our back,
  1349. we get to restart a new allocation attempt. */
  1350. #ifdef IN_PROCESS_AGENT
  1351. /* Build the tentative token. */
  1352. commit_count = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) + 0x100)
  1353. & GDBSERVER_FLUSH_COUNT_MASK_CURR);
  1354. commit = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) << 12)
  1355. | commit_count
  1356. | curr);
  1357. /* Try to commit it. */
  1358. readout = cmpxchg (&trace_buffer_ctrl_curr, prev, commit);
  1359. if (readout != prev)
  1360. {
  1361. trace_debug ("GDBserver has touched the trace buffer, restarting."
  1362. " (prev=%08x, commit=%08x, readout=%08x)",
  1363. prev, commit, readout);
  1364. goto again;
  1365. }
  1366. /* Hold your horses here. Even if that change was committed,
  1367. GDBserver could come in, and clobber it. We need to hold to be
  1368. able to tell if GDBserver clobbers before or after we committed
  1369. the change. Whenever GDBserver goes about touching the IPA
  1370. buffer, it sets a breakpoint in this routine, so we have a sync
  1371. point here. */
  1372. about_to_request_buffer_space ();
  1373. /* Check if the change has been effective, even if GDBserver stopped
  1374. us at the breakpoint. */
  1375. {
  1376. unsigned int refetch;
  1377. memory_barrier ();
  1378. refetch = trace_buffer_ctrl_curr;
  1379. if (refetch == commit
  1380. || ((refetch & GDBSERVER_FLUSH_COUNT_MASK_PREV) >> 12) == commit_count)
  1381. {
  1382. /* effective */
  1383. trace_debug ("change is effective: (prev=%08x, commit=%08x, "
  1384. "readout=%08x, refetch=%08x)",
  1385. prev, commit, readout, refetch);
  1386. }
  1387. else
  1388. {
  1389. trace_debug ("GDBserver has touched the trace buffer, not effective."
  1390. " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
  1391. prev, commit, readout, refetch);
  1392. goto again;
  1393. }
  1394. }
  1395. #endif
  1396. /* We have a new piece of the trace buffer. Hurray! */
  1397. /* Add an EOB marker just past this allocation. */
  1398. ((struct traceframe *) tbctrl->free)->tpnum = 0;
  1399. ((struct traceframe *) tbctrl->free)->data_size = 0;
  1400. /* Adjust the request back down, now that we know we have space for
  1401. the marker. */
  1402. amt -= sizeof (struct traceframe);
  1403. if (debug_threads)
  1404. {
  1405. trace_debug ("Allocated %d bytes", (int) amt);
  1406. trace_debug ("Trace buffer [%d] start=%d free=%d "
  1407. "endfree=%d wrap=%d hi=%d",
  1408. curr,
  1409. (int) (tbctrl->start - trace_buffer_lo),
  1410. (int) (tbctrl->free - trace_buffer_lo),
  1411. (int) (tbctrl->end_free - trace_buffer_lo),
  1412. (int) (tbctrl->wrap - trace_buffer_lo),
  1413. (int) (trace_buffer_hi - trace_buffer_lo));
  1414. }
  1415. return rslt;
  1416. }
  1417. #ifndef IN_PROCESS_AGENT
  1418. /* Return the total free space. This is not necessarily the largest
  1419. block we can allocate, because of the two-part case. */
  1420. static int
  1421. free_space (void)
  1422. {
  1423. if (trace_buffer_free <= trace_buffer_end_free)
  1424. return trace_buffer_end_free - trace_buffer_free;
  1425. else
  1426. return ((trace_buffer_end_free - trace_buffer_lo)
  1427. + (trace_buffer_hi - trace_buffer_free));
  1428. }
  1429. /* An 'S' in continuation packets indicates remainder are for
  1430. while-stepping. */
  1431. static int seen_step_action_flag;
  1432. /* Create a tracepoint (location) with given number and address. Add this
  1433. new tracepoint to list and sort this list. */
  1434. static struct tracepoint *
  1435. add_tracepoint (int num, CORE_ADDR addr)
  1436. {
  1437. struct tracepoint *tpoint, **tp_next;
  1438. tpoint = XNEW (struct tracepoint);
  1439. tpoint->number = num;
  1440. tpoint->address = addr;
  1441. tpoint->numactions = 0;
  1442. tpoint->actions = NULL;
  1443. tpoint->actions_str = NULL;
  1444. tpoint->cond = NULL;
  1445. tpoint->num_step_actions = 0;
  1446. tpoint->step_actions = NULL;
  1447. tpoint->step_actions_str = NULL;
  1448. /* Start all off as regular (slow) tracepoints. */
  1449. tpoint->type = trap_tracepoint;
  1450. tpoint->orig_size = -1;
  1451. tpoint->source_strings = NULL;
  1452. tpoint->compiled_cond = 0;
  1453. tpoint->handle = NULL;
  1454. tpoint->next = NULL;
  1455. /* Find a place to insert this tracepoint into list in order to keep
  1456. the tracepoint list still in the ascending order. There may be
  1457. multiple tracepoints at the same address as TPOINT's, and this
  1458. guarantees TPOINT is inserted after all the tracepoints which are
  1459. set at the same address. For example, fast tracepoints A, B, C are
  1460. set at the same address, and D is to be insert at the same place as
  1461. well,
  1462. -->| A |--> | B |-->| C |->...
  1463. One jump pad was created for tracepoint A, B, and C, and the target
  1464. address of A is referenced/used in jump pad. So jump pad will let
  1465. inferior jump to A. If D is inserted in front of A, like this,
  1466. -->| D |-->| A |--> | B |-->| C |->...
  1467. without updating jump pad, D is not reachable during collect, which
  1468. is wrong. As we can see, the order of B, C and D doesn't matter, but
  1469. A should always be the `first' one. */
  1470. for (tp_next = &tracepoints;
  1471. (*tp_next) != NULL && (*tp_next)->address <= tpoint->address;
  1472. tp_next = &(*tp_next)->next)
  1473. ;
  1474. tpoint->next = *tp_next;
  1475. *tp_next = tpoint;
  1476. last_tracepoint = tpoint;
  1477. seen_step_action_flag = 0;
  1478. return tpoint;
  1479. }
  1480. #ifndef IN_PROCESS_AGENT
  1481. /* Return the tracepoint with the given number and address, or NULL. */
  1482. static struct tracepoint *
  1483. find_tracepoint (int id, CORE_ADDR addr)
  1484. {
  1485. struct tracepoint *tpoint;
  1486. for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
  1487. if (tpoint->number == id && tpoint->address == addr)
  1488. return tpoint;
  1489. return NULL;
  1490. }
  1491. /* Remove TPOINT from global list. */
  1492. static void
  1493. remove_tracepoint (struct tracepoint *tpoint)
  1494. {
  1495. struct tracepoint *tp, *tp_prev;
  1496. for (tp = tracepoints, tp_prev = NULL; tp && tp != tpoint;
  1497. tp_prev = tp, tp = tp->next)
  1498. ;
  1499. if (tp)
  1500. {
  1501. if (tp_prev)
  1502. tp_prev->next = tp->next;
  1503. else
  1504. tracepoints = tp->next;
  1505. xfree (tp);
  1506. }
  1507. }
  1508. /* There may be several tracepoints with the same number (because they
  1509. are "locations", in GDB parlance); return the next one after the
  1510. given tracepoint, or search from the beginning of the list if the
  1511. first argument is NULL. */
  1512. static struct tracepoint *
  1513. find_next_tracepoint_by_number (struct tracepoint *prev_tp, int num)
  1514. {
  1515. struct tracepoint *tpoint;
  1516. if (prev_tp)
  1517. tpoint = prev_tp->next;
  1518. else
  1519. tpoint = tracepoints;
  1520. for (; tpoint; tpoint = tpoint->next)
  1521. if (tpoint->number == num)
  1522. return tpoint;
  1523. return NULL;
  1524. }
  1525. #endif
  1526. /* Append another action to perform when the tracepoint triggers. */
  1527. static void
  1528. add_tracepoint_action (struct tracepoint *tpoint, const char *packet)
  1529. {
  1530. const char *act;
  1531. if (*packet == 'S')
  1532. {
  1533. seen_step_action_flag = 1;
  1534. ++packet;
  1535. }
  1536. act = packet;
  1537. while (*act)
  1538. {
  1539. const char *act_start = act;
  1540. struct tracepoint_action *action = NULL;
  1541. switch (*act)
  1542. {
  1543. case 'M':
  1544. {
  1545. struct collect_memory_action *maction =
  1546. XNEW (struct collect_memory_action);
  1547. ULONGEST basereg;
  1548. int is_neg;
  1549. maction->base.type = *act;
  1550. action = &maction->base;
  1551. ++act;
  1552. is_neg = (*act == '-');
  1553. if (*act == '-')
  1554. ++act;
  1555. act = unpack_varlen_hex (act, &basereg);
  1556. ++act;
  1557. act = unpack_varlen_hex (act, &maction->addr);
  1558. ++act;
  1559. act = unpack_varlen_hex (act, &maction->len);
  1560. maction->basereg = (is_neg
  1561. ? - (int) basereg
  1562. : (int) basereg);
  1563. trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
  1564. pulongest (maction->len),
  1565. paddress (maction->addr), maction->basereg);
  1566. break;
  1567. }
  1568. case 'R':
  1569. {
  1570. struct collect_registers_action *raction =
  1571. XNEW (struct collect_registers_action);
  1572. raction->base.type = *act;
  1573. action = &raction->base;
  1574. trace_debug ("Want to collect registers");
  1575. ++act;
  1576. /* skip past hex digits of mask for now */
  1577. while (isxdigit(*act))
  1578. ++act;
  1579. break;
  1580. }
  1581. case 'L':
  1582. {
  1583. struct collect_static_trace_data_action *raction =
  1584. XNEW (struct collect_static_trace_data_action);
  1585. raction->base.type = *act;
  1586. action = &raction->base;
  1587. trace_debug ("Want to collect static trace data");
  1588. ++act;
  1589. break;
  1590. }
  1591. case 'S':
  1592. trace_debug ("Unexpected step action, ignoring");
  1593. ++act;
  1594. break;
  1595. case 'X':
  1596. {
  1597. struct eval_expr_action *xaction = XNEW (struct eval_expr_action);
  1598. xaction->base.type = *act;
  1599. action = &xaction->base;
  1600. trace_debug ("Want to evaluate expression");
  1601. xaction->expr = gdb_parse_agent_expr (&act);
  1602. break;
  1603. }
  1604. default:
  1605. trace_debug ("unknown trace action '%c', ignoring...", *act);
  1606. break;
  1607. case '-':
  1608. break;
  1609. }
  1610. if (action == NULL)
  1611. break;
  1612. if (seen_step_action_flag)
  1613. {
  1614. tpoint->num_step_actions++;
  1615. tpoint->step_actions
  1616. = XRESIZEVEC (struct tracepoint_action *, tpoint->step_actions,
  1617. tpoint->num_step_actions);
  1618. tpoint->step_actions_str
  1619. = XRESIZEVEC (char *, tpoint->step_actions_str,
  1620. tpoint->num_step_actions);
  1621. tpoint->step_actions[tpoint->num_step_actions - 1] = action;
  1622. tpoint->step_actions_str[tpoint->num_step_actions - 1]
  1623. = savestring (act_start, act - act_start);
  1624. }
  1625. else
  1626. {
  1627. tpoint->numactions++;
  1628. tpoint->actions
  1629. = XRESIZEVEC (struct tracepoint_action *, tpoint->actions,
  1630. tpoint->numactions);
  1631. tpoint->actions_str
  1632. = XRESIZEVEC (char *, tpoint->actions_str, tpoint->numactions);
  1633. tpoint->actions[tpoint->numactions - 1] = action;
  1634. tpoint->actions_str[tpoint->numactions - 1]
  1635. = savestring (act_start, act - act_start);
  1636. }
  1637. }
  1638. }
  1639. #endif
  1640. /* Find or create a trace state variable with the given number. */
  1641. static struct trace_state_variable *
  1642. get_trace_state_variable (int num)
  1643. {
  1644. struct trace_state_variable *tsv;
  1645. #ifdef IN_PROCESS_AGENT
  1646. /* Search for an existing variable. */
  1647. for (tsv = alloced_trace_state_variables; tsv; tsv = tsv->next)
  1648. if (tsv->number == num)
  1649. return tsv;
  1650. #endif
  1651. /* Search for an existing variable. */
  1652. for (tsv = trace_state_variables; tsv; tsv = tsv->next)
  1653. if (tsv->number == num)
  1654. return tsv;
  1655. return NULL;
  1656. }
  1657. /* Find or create a trace state variable with the given number. */
  1658. static struct trace_state_variable *
  1659. create_trace_state_variable (int num, int gdb)
  1660. {
  1661. struct trace_state_variable *tsv;
  1662. tsv = get_trace_state_variable (num);
  1663. if (tsv != NULL)
  1664. return tsv;
  1665. /* Create a new variable. */
  1666. tsv = XNEW (struct trace_state_variable);
  1667. tsv->number = num;
  1668. tsv->initial_value = 0;
  1669. tsv->value = 0;
  1670. tsv->getter = NULL;
  1671. tsv->name = NULL;
  1672. #ifdef IN_PROCESS_AGENT
  1673. if (!gdb)
  1674. {
  1675. tsv->next = alloced_trace_state_variables;
  1676. alloced_trace_state_variables = tsv;
  1677. }
  1678. else
  1679. #endif
  1680. {
  1681. tsv->next = trace_state_variables;
  1682. trace_state_variables = tsv;
  1683. }
  1684. return tsv;
  1685. }
  1686. /* This is needed for -Wmissing-declarations. */
  1687. IP_AGENT_EXPORT_FUNC LONGEST get_trace_state_variable_value (int num);
  1688. IP_AGENT_EXPORT_FUNC LONGEST
  1689. get_trace_state_variable_value (int num)
  1690. {
  1691. struct trace_state_variable *tsv;
  1692. tsv = get_trace_state_variable (num);
  1693. if (!tsv)
  1694. {
  1695. trace_debug ("No trace state variable %d, skipping value get", num);
  1696. return 0;
  1697. }
  1698. /* Call a getter function if we have one. While it's tempting to
  1699. set up something to only call the getter once per tracepoint hit,
  1700. it could run afoul of thread races. Better to let the getter
  1701. handle it directly, if necessary to worry about it. */
  1702. if (tsv->getter)
  1703. tsv->value = (tsv->getter) ();
  1704. trace_debug ("get_trace_state_variable_value(%d) ==> %s",
  1705. num, plongest (tsv->value));
  1706. return tsv->value;
  1707. }
  1708. /* This is needed for -Wmissing-declarations. */
  1709. IP_AGENT_EXPORT_FUNC void set_trace_state_variable_value (int num,
  1710. LONGEST val);
  1711. IP_AGENT_EXPORT_FUNC void
  1712. set_trace_state_variable_value (int num, LONGEST val)
  1713. {
  1714. struct trace_state_variable *tsv;
  1715. tsv = get_trace_state_variable (num);
  1716. if (!tsv)
  1717. {
  1718. trace_debug ("No trace state variable %d, skipping value set", num);
  1719. return;
  1720. }
  1721. tsv->value = val;
  1722. }
  1723. LONGEST
  1724. agent_get_trace_state_variable_value (int num)
  1725. {
  1726. return get_trace_state_variable_value (num);
  1727. }
  1728. void
  1729. agent_set_trace_state_variable_value (int num, LONGEST val)
  1730. {
  1731. set_trace_state_variable_value (num, val);
  1732. }
  1733. static void
  1734. set_trace_state_variable_name (int num, const char *name)
  1735. {
  1736. struct trace_state_variable *tsv;
  1737. tsv = get_trace_state_variable (num);
  1738. if (!tsv)
  1739. {
  1740. trace_debug ("No trace state variable %d, skipping name set", num);
  1741. return;
  1742. }
  1743. tsv->name = (char *) name;
  1744. }
  1745. static void
  1746. set_trace_state_variable_getter (int num, LONGEST (*getter) (void))
  1747. {
  1748. struct trace_state_variable *tsv;
  1749. tsv = get_trace_state_variable (num);
  1750. if (!tsv)
  1751. {
  1752. trace_debug ("No trace state variable %d, skipping getter set", num);
  1753. return;
  1754. }
  1755. tsv->getter = getter;
  1756. }
  1757. /* Add a raw traceframe for the given tracepoint. */
  1758. static struct traceframe *
  1759. add_traceframe (struct tracepoint *tpoint)
  1760. {
  1761. struct traceframe *tframe;
  1762. tframe
  1763. = (struct traceframe *) trace_buffer_alloc (sizeof (struct traceframe));
  1764. if (tframe == NULL)
  1765. return NULL;
  1766. tframe->tpnum = tpoint->number;
  1767. tframe->data_size = 0;
  1768. return tframe;
  1769. }
  1770. /* Add a block to the traceframe currently being worked on. */
  1771. static unsigned char *
  1772. add_traceframe_block (struct traceframe *tframe,
  1773. struct tracepoint *tpoint, int amt)
  1774. {
  1775. unsigned char *block;
  1776. if (!tframe)
  1777. return NULL;
  1778. block = (unsigned char *) trace_buffer_alloc (amt);
  1779. if (!block)
  1780. return NULL;
  1781. gdb_assert (tframe->tpnum == tpoint->number);
  1782. tframe->data_size += amt;
  1783. tpoint->traceframe_usage += amt;
  1784. return block;
  1785. }
  1786. /* Flag that the current traceframe is finished. */
  1787. static void
  1788. finish_traceframe (struct traceframe *tframe)
  1789. {
  1790. ++traceframe_write_count;
  1791. ++traceframes_created;
  1792. }
  1793. #ifndef IN_PROCESS_AGENT
  1794. /* Given a traceframe number NUM, find the NUMth traceframe in the
  1795. buffer. */
  1796. static struct traceframe *
  1797. find_traceframe (int num)
  1798. {
  1799. struct traceframe *tframe;
  1800. int tfnum = 0;
  1801. for (tframe = FIRST_TRACEFRAME ();
  1802. tframe->tpnum != 0;
  1803. tframe = NEXT_TRACEFRAME (tframe))
  1804. {
  1805. if (tfnum == num)
  1806. return tframe;
  1807. ++tfnum;
  1808. }
  1809. return NULL;
  1810. }
  1811. static CORE_ADDR
  1812. get_traceframe_address (struct traceframe *tframe)
  1813. {
  1814. CORE_ADDR addr;
  1815. struct tracepoint *tpoint;
  1816. addr = traceframe_get_pc (tframe);
  1817. if (addr)
  1818. return addr;
  1819. /* Fallback strategy, will be incorrect for while-stepping frames
  1820. and multi-location tracepoints. */
  1821. tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
  1822. return tpoint->address;
  1823. }
  1824. /* Search for the next traceframe whose address is inside or outside
  1825. the given range. */
  1826. static struct traceframe *
  1827. find_next_traceframe_in_range (CORE_ADDR lo, CORE_ADDR hi, int inside_p,
  1828. int *tfnump)
  1829. {
  1830. client_state &cs = get_client_state ();
  1831. struct traceframe *tframe;
  1832. CORE_ADDR tfaddr;
  1833. *tfnump = cs.current_traceframe + 1;
  1834. tframe = find_traceframe (*tfnump);
  1835. /* The search is not supposed to wrap around. */
  1836. if (!tframe)
  1837. {
  1838. *tfnump = -1;
  1839. return NULL;
  1840. }
  1841. for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
  1842. {
  1843. tfaddr = get_traceframe_address (tframe);
  1844. if (inside_p
  1845. ? (lo <= tfaddr && tfaddr <= hi)
  1846. : (lo > tfaddr || tfaddr > hi))
  1847. return tframe;
  1848. ++*tfnump;
  1849. }
  1850. *tfnump = -1;
  1851. return NULL;
  1852. }
  1853. /* Search for the next traceframe recorded by the given tracepoint.
  1854. Note that for multi-location tracepoints, this will find whatever
  1855. location appears first. */
  1856. static struct traceframe *
  1857. find_next_traceframe_by_tracepoint (int num, int *tfnump)
  1858. {
  1859. client_state &cs = get_client_state ();
  1860. struct traceframe *tframe;
  1861. *tfnump = cs.current_traceframe + 1;
  1862. tframe = find_traceframe (*tfnump);
  1863. /* The search is not supposed to wrap around. */
  1864. if (!tframe)
  1865. {
  1866. *tfnump = -1;
  1867. return NULL;
  1868. }
  1869. for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
  1870. {
  1871. if (tframe->tpnum == num)
  1872. return tframe;
  1873. ++*tfnump;
  1874. }
  1875. *tfnump = -1;
  1876. return NULL;
  1877. }
  1878. #endif
  1879. #ifndef IN_PROCESS_AGENT
  1880. /* Clear all past trace state. */
  1881. static void
  1882. cmd_qtinit (char *packet)
  1883. {
  1884. client_state &cs = get_client_state ();
  1885. struct trace_state_variable *tsv, *prev, *next;
  1886. /* Can't do this command without a pid attached. */
  1887. if (current_thread == NULL)
  1888. {
  1889. write_enn (packet);
  1890. return;
  1891. }
  1892. /* Make sure we don't try to read from a trace frame. */
  1893. cs.current_traceframe = -1;
  1894. stop_tracing ();
  1895. trace_debug ("Initializing the trace");
  1896. clear_installed_tracepoints ();
  1897. clear_readonly_regions ();
  1898. tracepoints = NULL;
  1899. last_tracepoint = NULL;
  1900. /* Clear out any leftover trace state variables. Ones with target
  1901. defined getters should be kept however. */
  1902. prev = NULL;
  1903. tsv = trace_state_variables;
  1904. while (tsv)
  1905. {
  1906. trace_debug ("Looking at var %d", tsv->number);
  1907. if (tsv->getter == NULL)
  1908. {
  1909. next = tsv->next;
  1910. if (prev)
  1911. prev->next = next;
  1912. else
  1913. trace_state_variables = next;
  1914. trace_debug ("Deleting var %d", tsv->number);
  1915. free (tsv);
  1916. tsv = next;
  1917. }
  1918. else
  1919. {
  1920. prev = tsv;
  1921. tsv = tsv->next;
  1922. }
  1923. }
  1924. clear_trace_buffer ();
  1925. clear_inferior_trace_buffer ();
  1926. write_ok (packet);
  1927. }
  1928. /* Unprobe the UST marker at ADDRESS. */
  1929. static void
  1930. unprobe_marker_at (CORE_ADDR address)
  1931. {
  1932. char cmd[IPA_CMD_BUF_SIZE];
  1933. sprintf (cmd, "unprobe_marker_at:%s", paddress (address));
  1934. run_inferior_command (cmd, strlen (cmd) + 1);
  1935. }
  1936. /* Restore the program to its pre-tracing state. This routine may be called
  1937. in error situations, so it needs to be careful about only restoring
  1938. from known-valid bits. */
  1939. static void
  1940. clear_installed_tracepoints (void)
  1941. {
  1942. struct tracepoint *tpoint;
  1943. struct tracepoint *prev_stpoint;
  1944. target_pause_all (true);
  1945. prev_stpoint = NULL;
  1946. /* Restore any bytes overwritten by tracepoints. */
  1947. for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
  1948. {
  1949. /* Catch the case where we might try to remove a tracepoint that
  1950. was never actually installed. */
  1951. if (tpoint->handle == NULL)
  1952. {
  1953. trace_debug ("Tracepoint %d at 0x%s was "
  1954. "never installed, nothing to clear",
  1955. tpoint->number, paddress (tpoint->address));
  1956. continue;
  1957. }
  1958. switch (tpoint->type)
  1959. {
  1960. case trap_tracepoint:
  1961. {
  1962. struct breakpoint *bp
  1963. = (struct breakpoint *) tpoint->handle;
  1964. delete_breakpoint (bp);
  1965. }
  1966. break;
  1967. case fast_tracepoint:
  1968. {
  1969. struct fast_tracepoint_jump *jump
  1970. = (struct fast_tracepoint_jump *) tpoint->handle;
  1971. delete_fast_tracepoint_jump (jump);
  1972. }
  1973. break;
  1974. case static_tracepoint:
  1975. if (prev_stpoint != NULL
  1976. && prev_stpoint->address == tpoint->address)
  1977. /* Nothing to do. We already unprobed a tracepoint set at
  1978. this marker address (and there can only be one probe
  1979. per marker). */
  1980. ;
  1981. else
  1982. {
  1983. unprobe_marker_at (tpoint->address);
  1984. prev_stpoint = tpoint;
  1985. }
  1986. break;
  1987. }
  1988. tpoint->handle = NULL;
  1989. }
  1990. target_unpause_all (true);
  1991. }
  1992. /* Parse a packet that defines a tracepoint. */
  1993. static void
  1994. cmd_qtdp (char *own_buf)
  1995. {
  1996. int tppacket;
  1997. /* Whether there is a trailing hyphen at the end of the QTDP packet. */
  1998. int trail_hyphen = 0;
  1999. ULONGEST num;
  2000. ULONGEST addr;
  2001. ULONGEST count;
  2002. struct tracepoint *tpoint;
  2003. const char *packet = own_buf;
  2004. packet += strlen ("QTDP:");
  2005. /* A hyphen at the beginning marks a packet specifying actions for a
  2006. tracepoint already supplied. */
  2007. tppacket = 1;
  2008. if (*packet == '-')
  2009. {
  2010. tppacket = 0;
  2011. ++packet;
  2012. }
  2013. packet = unpack_varlen_hex (packet, &num);
  2014. ++packet; /* skip a colon */
  2015. packet = unpack_varlen_hex (packet, &addr);
  2016. ++packet; /* skip a colon */
  2017. /* See if we already have this tracepoint. */
  2018. tpoint = find_tracepoint (num, addr);
  2019. if (tppacket)
  2020. {
  2021. /* Duplicate tracepoints are never allowed. */
  2022. if (tpoint)
  2023. {
  2024. trace_debug ("Tracepoint error: tracepoint %d"
  2025. " at 0x%s already exists",
  2026. (int) num, paddress (addr));
  2027. write_enn (own_buf);
  2028. return;
  2029. }
  2030. tpoint = add_tracepoint (num, addr);
  2031. tpoint->enabled = (*packet == 'E');
  2032. ++packet; /* skip 'E' */
  2033. ++packet; /* skip a colon */
  2034. packet = unpack_varlen_hex (packet, &count);
  2035. tpoint->step_count = count;
  2036. ++packet; /* skip a colon */
  2037. packet = unpack_varlen_hex (packet, &count);
  2038. tpoint->pass_count = count;
  2039. /* See if we have any of the additional optional fields. */
  2040. while (*packet == ':')
  2041. {
  2042. ++packet;
  2043. if (*packet == 'F')
  2044. {
  2045. tpoint->type = fast_tracepoint;
  2046. ++packet;
  2047. packet = unpack_varlen_hex (packet, &count);
  2048. tpoint->orig_size = count;
  2049. }
  2050. else if (*packet == 'S')
  2051. {
  2052. tpoint->type = static_tracepoint;
  2053. ++packet;
  2054. }
  2055. else if (*packet == 'X')
  2056. {
  2057. tpoint->cond = gdb_parse_agent_expr (&packet);
  2058. }
  2059. else if (*packet == '-')
  2060. break;
  2061. else if (*packet == '\0')
  2062. break;
  2063. else
  2064. trace_debug ("Unknown optional tracepoint field");
  2065. }
  2066. if (*packet == '-')
  2067. {
  2068. trail_hyphen = 1;
  2069. trace_debug ("Also has actions\n");
  2070. }
  2071. trace_debug ("Defined %stracepoint %d at 0x%s, "
  2072. "enabled %d step %" PRIu64 " pass %" PRIu64,
  2073. tpoint->type == fast_tracepoint ? "fast "
  2074. : tpoint->type == static_tracepoint ? "static " : "",
  2075. tpoint->number, paddress (tpoint->address), tpoint->enabled,
  2076. tpoint->step_count, tpoint->pass_count);
  2077. }
  2078. else if (tpoint)
  2079. add_tracepoint_action (tpoint, packet);
  2080. else
  2081. {
  2082. trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
  2083. (int) num, paddress (addr));
  2084. write_enn (own_buf);
  2085. return;
  2086. }
  2087. /* Install tracepoint during tracing only once for each tracepoint location.
  2088. For each tracepoint loc, GDB may send multiple QTDP packets, and we can
  2089. determine the last QTDP packet for one tracepoint location by checking
  2090. trailing hyphen in QTDP packet. */
  2091. if (tracing && !trail_hyphen)
  2092. {
  2093. struct tracepoint *tp = NULL;
  2094. /* Pause all threads temporarily while we patch tracepoints. */
  2095. target_pause_all (false);
  2096. /* download_tracepoint will update global `tracepoints'
  2097. list, so it is unsafe to leave threads in jump pad. */
  2098. target_stabilize_threads ();
  2099. /* Freeze threads. */
  2100. target_pause_all (true);
  2101. if (tpoint->type != trap_tracepoint)
  2102. {
  2103. /* Find another fast or static tracepoint at the same address. */
  2104. for (tp = tracepoints; tp; tp = tp->next)
  2105. {
  2106. if (tp->address == tpoint->address && tp->type == tpoint->type
  2107. && tp->number != tpoint->number)
  2108. break;
  2109. }
  2110. /* TPOINT is installed at the same address as TP. */
  2111. if (tp)
  2112. {
  2113. if (tpoint->type == fast_tracepoint)
  2114. clone_fast_tracepoint (tpoint, tp);
  2115. else if (tpoint->type == static_tracepoint)
  2116. tpoint->handle = (void *) -1;
  2117. }
  2118. }
  2119. if (use_agent && tpoint->type == fast_tracepoint
  2120. && agent_capability_check (AGENT_CAPA_FAST_TRACE))
  2121. {
  2122. /* Download and install fast tracepoint by agent. */
  2123. if (tracepoint_send_agent (tpoint) == 0)
  2124. write_ok (own_buf);
  2125. else
  2126. {
  2127. write_enn (own_buf);
  2128. remove_tracepoint (tpoint);
  2129. }
  2130. }
  2131. else
  2132. {
  2133. download_tracepoint (tpoint);
  2134. if (tpoint->type == trap_tracepoint || tp == NULL)
  2135. {
  2136. install_tracepoint (tpoint, own_buf);
  2137. if (strcmp (own_buf, "OK") != 0)
  2138. remove_tracepoint (tpoint);
  2139. }
  2140. else
  2141. write_ok (own_buf);
  2142. }
  2143. target_unpause_all (true);
  2144. return;
  2145. }
  2146. write_ok (own_buf);
  2147. }
  2148. static void
  2149. cmd_qtdpsrc (char *own_buf)
  2150. {
  2151. ULONGEST num, addr, start, slen;
  2152. struct tracepoint *tpoint;
  2153. const char *packet = own_buf;
  2154. const char *saved;
  2155. char *srctype, *src;
  2156. size_t nbytes;
  2157. struct source_string *last, *newlast;
  2158. packet += strlen ("QTDPsrc:");
  2159. packet = unpack_varlen_hex (packet, &num);
  2160. ++packet; /* skip a colon */
  2161. packet = unpack_varlen_hex (packet, &addr);
  2162. ++packet; /* skip a colon */
  2163. /* See if we already have this tracepoint. */
  2164. tpoint = find_tracepoint (num, addr);
  2165. if (!tpoint)
  2166. {
  2167. trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
  2168. (int) num, paddress (addr));
  2169. write_enn (own_buf);
  2170. return;
  2171. }
  2172. saved = packet;
  2173. packet = strchr (packet, ':');
  2174. srctype = (char *) xmalloc (packet - saved + 1);
  2175. memcpy (srctype, saved, packet - saved);
  2176. srctype[packet - saved] = '\0';
  2177. ++packet;
  2178. packet = unpack_varlen_hex (packet, &start);
  2179. ++packet; /* skip a colon */
  2180. packet = unpack_varlen_hex (packet, &slen);
  2181. ++packet; /* skip a colon */
  2182. src = (char *) xmalloc (slen + 1);
  2183. nbytes = hex2bin (packet, (gdb_byte *) src, strlen (packet) / 2);
  2184. src[nbytes] = '\0';
  2185. newlast = XNEW (struct source_string);
  2186. newlast->type = srctype;
  2187. newlast->str = src;
  2188. newlast->next = NULL;
  2189. /* Always add a source string to the end of the list;
  2190. this keeps sequences of actions/commands in the right
  2191. order. */
  2192. if (tpoint->source_strings)
  2193. {
  2194. for (last = tpoint->source_strings; last->next; last = last->next)
  2195. ;
  2196. last->next = newlast;
  2197. }
  2198. else
  2199. tpoint->source_strings = newlast;
  2200. write_ok (own_buf);
  2201. }
  2202. static void
  2203. cmd_qtdv (char *own_buf)
  2204. {
  2205. ULONGEST num, val, builtin;
  2206. char *varname;
  2207. size_t nbytes;
  2208. struct trace_state_variable *tsv;
  2209. const char *packet = own_buf;
  2210. packet += strlen ("QTDV:");
  2211. packet = unpack_varlen_hex (packet, &num);
  2212. ++packet; /* skip a colon */
  2213. packet = unpack_varlen_hex (packet, &val);
  2214. ++packet; /* skip a colon */
  2215. packet = unpack_varlen_hex (packet, &builtin);
  2216. ++packet; /* skip a colon */
  2217. nbytes = strlen (packet) / 2;
  2218. varname = (char *) xmalloc (nbytes + 1);
  2219. nbytes = hex2bin (packet, (gdb_byte *) varname, nbytes);
  2220. varname[nbytes] = '\0';
  2221. tsv = create_trace_state_variable (num, 1);
  2222. tsv->initial_value = (LONGEST) val;
  2223. tsv->name = varname;
  2224. set_trace_state_variable_value (num, (LONGEST) val);
  2225. write_ok (own_buf);
  2226. }
  2227. static void
  2228. cmd_qtenable_disable (char *own_buf, int enable)
  2229. {
  2230. const char *packet = own_buf;
  2231. ULONGEST num, addr;
  2232. struct tracepoint *tp;
  2233. packet += strlen (enable ? "QTEnable:" : "QTDisable:");
  2234. packet = unpack_varlen_hex (packet, &num);
  2235. ++packet; /* skip a colon */
  2236. packet = unpack_varlen_hex (packet, &addr);
  2237. tp = find_tracepoint (num, addr);
  2238. if (tp)
  2239. {
  2240. if ((enable && tp->enabled) || (!enable && !tp->enabled))
  2241. {
  2242. trace_debug ("Tracepoint %d at 0x%s is already %s",
  2243. (int) num, paddress (addr),
  2244. enable ? "enabled" : "disabled");
  2245. write_ok (own_buf);
  2246. return;
  2247. }
  2248. trace_debug ("%s tracepoint %d at 0x%s",
  2249. enable ? "Enabling" : "Disabling",
  2250. (int) num, paddress (addr));
  2251. tp->enabled = enable;
  2252. if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
  2253. {
  2254. int ret;
  2255. int offset = offsetof (struct tracepoint, enabled);
  2256. CORE_ADDR obj_addr = tp->obj_addr_on_target + offset;
  2257. ret = prepare_to_access_memory ();
  2258. if (ret)
  2259. {
  2260. trace_debug ("Failed to temporarily stop inferior threads");
  2261. write_enn (own_buf);
  2262. return;
  2263. }
  2264. ret = write_inferior_int8 (obj_addr, enable);
  2265. done_accessing_memory ();
  2266. if (ret)
  2267. {
  2268. trace_debug ("Cannot write enabled flag into "
  2269. "inferior process memory");
  2270. write_enn (own_buf);
  2271. return;
  2272. }
  2273. }
  2274. write_ok (own_buf);
  2275. }
  2276. else
  2277. {
  2278. trace_debug ("Tracepoint %d at 0x%s not found",
  2279. (int) num, paddress (addr));
  2280. write_enn (own_buf);
  2281. }
  2282. }
  2283. static void
  2284. cmd_qtv (char *own_buf)
  2285. {
  2286. client_state &cs = get_client_state ();
  2287. ULONGEST num;
  2288. LONGEST val = 0;
  2289. int err;
  2290. char *packet = own_buf;
  2291. packet += strlen ("qTV:");
  2292. unpack_varlen_hex (packet, &num);
  2293. if (cs.current_traceframe >= 0)
  2294. {
  2295. err = traceframe_read_tsv ((int) num, &val);
  2296. if (err)
  2297. {
  2298. strcpy (own_buf, "U");
  2299. return;
  2300. }
  2301. }
  2302. /* Only make tsv's be undefined before the first trace run. After a
  2303. trace run is over, the user might want to see the last value of
  2304. the tsv, and it might not be available in a traceframe. */
  2305. else if (!tracing && strcmp (tracing_stop_reason, "tnotrun") == 0)
  2306. {
  2307. strcpy (own_buf, "U");
  2308. return;
  2309. }
  2310. else
  2311. val = get_trace_state_variable_value (num);
  2312. sprintf (own_buf, "V%s", phex_nz (val, 0));
  2313. }
  2314. /* Clear out the list of readonly regions. */
  2315. static void
  2316. clear_readonly_regions (void)
  2317. {
  2318. struct readonly_region *roreg;
  2319. while (readonly_regions)
  2320. {
  2321. roreg = readonly_regions;
  2322. readonly_regions = readonly_regions->next;
  2323. free (roreg);
  2324. }
  2325. }
  2326. /* Parse the collection of address ranges whose contents GDB believes
  2327. to be unchanging and so can be read directly from target memory
  2328. even while looking at a traceframe. */
  2329. static void
  2330. cmd_qtro (char *own_buf)
  2331. {
  2332. ULONGEST start, end;
  2333. struct readonly_region *roreg;
  2334. const char *packet = own_buf;
  2335. trace_debug ("Want to mark readonly regions");
  2336. clear_readonly_regions ();
  2337. packet += strlen ("QTro");
  2338. while (*packet == ':')
  2339. {
  2340. ++packet; /* skip a colon */
  2341. packet = unpack_varlen_hex (packet, &start);
  2342. ++packet; /* skip a comma */
  2343. packet = unpack_varlen_hex (packet, &end);
  2344. roreg = XNEW (struct readonly_region);
  2345. roreg->start = start;
  2346. roreg->end = end;
  2347. roreg->next = readonly_regions;
  2348. readonly_regions = roreg;
  2349. trace_debug ("Added readonly region from 0x%s to 0x%s",
  2350. paddress (roreg->start), paddress (roreg->end));
  2351. }
  2352. write_ok (own_buf);
  2353. }
  2354. /* Test to see if the given range is in our list of readonly ranges.
  2355. We only test for being entirely within a range, GDB is not going to
  2356. send a single memory packet that spans multiple regions. */
  2357. int
  2358. in_readonly_region (CORE_ADDR addr, ULONGEST length)
  2359. {
  2360. struct readonly_region *roreg;
  2361. for (roreg = readonly_regions; roreg; roreg = roreg->next)
  2362. if (roreg->start <= addr && (addr + length - 1) <= roreg->end)
  2363. return 1;
  2364. return 0;
  2365. }
  2366. static CORE_ADDR gdb_jump_pad_head;
  2367. /* Return the address of the next free jump space. */
  2368. static CORE_ADDR
  2369. get_jump_space_head (void)
  2370. {
  2371. if (gdb_jump_pad_head == 0)
  2372. {
  2373. if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
  2374. &gdb_jump_pad_head))
  2375. {
  2376. internal_error (__FILE__, __LINE__,
  2377. "error extracting jump_pad_buffer");
  2378. }
  2379. }
  2380. return gdb_jump_pad_head;
  2381. }
  2382. /* Reserve USED bytes from the jump space. */
  2383. static void
  2384. claim_jump_space (ULONGEST used)
  2385. {
  2386. trace_debug ("claim_jump_space reserves %s bytes at %s",
  2387. pulongest (used), paddress (gdb_jump_pad_head));
  2388. gdb_jump_pad_head += used;
  2389. }
  2390. static CORE_ADDR trampoline_buffer_head = 0;
  2391. static CORE_ADDR trampoline_buffer_tail;
  2392. /* Reserve USED bytes from the trampoline buffer and return the
  2393. address of the start of the reserved space in TRAMPOLINE. Returns
  2394. non-zero if the space is successfully claimed. */
  2395. int
  2396. claim_trampoline_space (ULONGEST used, CORE_ADDR *trampoline)
  2397. {
  2398. if (!trampoline_buffer_head)
  2399. {
  2400. if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
  2401. &trampoline_buffer_tail))
  2402. {
  2403. internal_error (__FILE__, __LINE__,
  2404. "error extracting trampoline_buffer");
  2405. }
  2406. if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
  2407. &trampoline_buffer_head))
  2408. {
  2409. internal_error (__FILE__, __LINE__,
  2410. "error extracting trampoline_buffer_end");
  2411. }
  2412. }
  2413. /* Start claiming space from the top of the trampoline space. If
  2414. the space is located at the bottom of the virtual address space,
  2415. this reduces the possibility that corruption will occur if a null
  2416. pointer is used to write to memory. */
  2417. if (trampoline_buffer_head - trampoline_buffer_tail < used)
  2418. {
  2419. trace_debug ("claim_trampoline_space failed to reserve %s bytes",
  2420. pulongest (used));
  2421. return 0;
  2422. }
  2423. trampoline_buffer_head -= used;
  2424. trace_debug ("claim_trampoline_space reserves %s bytes at %s",
  2425. pulongest (used), paddress (trampoline_buffer_head));
  2426. *trampoline = trampoline_buffer_head;
  2427. return 1;
  2428. }
  2429. /* Returns non-zero if there is space allocated for use in trampolines
  2430. for fast tracepoints. */
  2431. int
  2432. have_fast_tracepoint_trampoline_buffer (char *buf)
  2433. {
  2434. CORE_ADDR trampoline_end, errbuf;
  2435. if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
  2436. &trampoline_end))
  2437. {
  2438. internal_error (__FILE__, __LINE__,
  2439. "error extracting trampoline_buffer_end");
  2440. }
  2441. if (buf)
  2442. {
  2443. buf[0] = '\0';
  2444. strcpy (buf, "was claiming");
  2445. if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_error,
  2446. &errbuf))
  2447. {
  2448. internal_error (__FILE__, __LINE__,
  2449. "error extracting errbuf");
  2450. }
  2451. read_inferior_memory (errbuf, (unsigned char *) buf, 100);
  2452. }
  2453. return trampoline_end != 0;
  2454. }
  2455. /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
  2456. the command fails, or 0 otherwise. If the command ran
  2457. successfully, but probing the marker failed, ERROUT will be filled
  2458. with the error to reply to GDB, and -1 is also returned. This
  2459. allows directly passing IPA errors to GDB. */
  2460. static int
  2461. probe_marker_at (CORE_ADDR address, char *errout)
  2462. {
  2463. char cmd[IPA_CMD_BUF_SIZE];
  2464. int err;
  2465. sprintf (cmd, "probe_marker_at:%s", paddress (address));
  2466. err = run_inferior_command (cmd, strlen (cmd) + 1);
  2467. if (err == 0)
  2468. {
  2469. if (*cmd == 'E')
  2470. {
  2471. strcpy (errout, cmd);
  2472. return -1;
  2473. }
  2474. }
  2475. return err;
  2476. }
  2477. static void
  2478. clone_fast_tracepoint (struct tracepoint *to, const struct tracepoint *from)
  2479. {
  2480. to->jump_pad = from->jump_pad;
  2481. to->jump_pad_end = from->jump_pad_end;
  2482. to->trampoline = from->trampoline;
  2483. to->trampoline_end = from->trampoline_end;
  2484. to->adjusted_insn_addr = from->adjusted_insn_addr;
  2485. to->adjusted_insn_addr_end = from->adjusted_insn_addr_end;
  2486. to->handle = from->handle;
  2487. gdb_assert (from->handle);
  2488. inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump *) from->handle);
  2489. }
  2490. #define MAX_JUMP_SIZE 20
  2491. /* Install fast tracepoint. Return 0 if successful, otherwise return
  2492. non-zero. */
  2493. static int
  2494. install_fast_tracepoint (struct tracepoint *tpoint, char *errbuf)
  2495. {
  2496. CORE_ADDR jentry, jump_entry;
  2497. CORE_ADDR trampoline;
  2498. CORE_ADDR collect;
  2499. ULONGEST trampoline_size;
  2500. int err = 0;
  2501. /* The jump to the jump pad of the last fast tracepoint
  2502. installed. */
  2503. unsigned char fjump[MAX_JUMP_SIZE];
  2504. ULONGEST fjump_size;
  2505. if (tpoint->orig_size < target_get_min_fast_tracepoint_insn_len ())
  2506. {
  2507. trace_debug ("Requested a fast tracepoint on an instruction "
  2508. "that is of less than the minimum length.");
  2509. return 0;
  2510. }
  2511. if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_collect_ptr,
  2512. &collect))
  2513. {
  2514. error ("error extracting gdb_collect_ptr");
  2515. return 1;
  2516. }
  2517. jentry = jump_entry = get_jump_space_head ();
  2518. trampoline = 0;
  2519. trampoline_size = 0;
  2520. /* Install the jump pad. */
  2521. err = target_install_fast_tracepoint_jump_pad
  2522. (tpoint->obj_addr_on_target, tpoint->address, collect,
  2523. ipa_sym_addrs.addr_collecting, tpoint->orig_size, &jentry,
  2524. &trampoline, &trampoline_size, fjump, &fjump_size,
  2525. &tpoint->adjusted_insn_addr, &tpoint->adjusted_insn_addr_end, errbuf);
  2526. if (err)
  2527. return 1;
  2528. /* Wire it in. */
  2529. tpoint->handle = set_fast_tracepoint_jump (tpoint->address, fjump,
  2530. fjump_size);
  2531. if (tpoint->handle != NULL)
  2532. {
  2533. tpoint->jump_pad = jump_entry;
  2534. tpoint->jump_pad_end = jentry;
  2535. tpoint->trampoline = trampoline;
  2536. tpoint->trampoline_end = trampoline + trampoline_size;
  2537. /* Pad to 8-byte alignment. */
  2538. jentry = ((jentry + 7) & ~0x7);
  2539. claim_jump_space (jentry - jump_entry);
  2540. }
  2541. return 0;
  2542. }
  2543. /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
  2544. static void
  2545. install_tracepoint (struct tracepoint *tpoint, char *own_buf)
  2546. {
  2547. tpoint->handle = NULL;
  2548. *own_buf = '\0';
  2549. if (tpoint->type == trap_tracepoint)
  2550. {
  2551. /* Tracepoints are installed as memory breakpoints. Just go
  2552. ahead and install the trap. The breakpoints module
  2553. handles duplicated breakpoints, and the memory read
  2554. routine handles un-patching traps from memory reads. */
  2555. tpoint->handle = set_breakpoint_at (tpoint->address,
  2556. tracepoint_handler);
  2557. }
  2558. else if (tpoint->type == fast_tracepoint || tpoint->type == static_tracepoint)
  2559. {
  2560. if (!agent_loaded_p ())
  2561. {
  2562. trace_debug ("Requested a %s tracepoint, but fast "
  2563. "tracepoints aren't supported.",
  2564. tpoint->type == static_tracepoint ? "static" : "fast");
  2565. write_e_ipa_not_loaded (own_buf);
  2566. return;
  2567. }
  2568. if (tpoint->type == static_tracepoint
  2569. && !in_process_agent_supports_ust ())
  2570. {
  2571. trace_debug ("Requested a static tracepoint, but static "
  2572. "tracepoints are not supported.");
  2573. write_e_ust_not_loaded (own_buf);
  2574. return;
  2575. }
  2576. if (tpoint->type == fast_tracepoint)
  2577. install_fast_tracepoint (tpoint, own_buf);
  2578. else
  2579. {
  2580. if (probe_marker_at (tpoint->address, own_buf) == 0)
  2581. tpoint->handle = (void *) -1;
  2582. }
  2583. }
  2584. else
  2585. internal_error (__FILE__, __LINE__, "Unknown tracepoint type");
  2586. if (tpoint->handle == NULL)
  2587. {
  2588. if (*own_buf == '\0')
  2589. write_enn (own_buf);
  2590. }
  2591. else
  2592. write_ok (own_buf);
  2593. }
  2594. static void download_tracepoint_1 (struct tracepoint *tpoint);
  2595. static void
  2596. cmd_qtstart (char *packet)
  2597. {
  2598. struct tracepoint *tpoint, *prev_ftpoint, *prev_stpoint;
  2599. CORE_ADDR tpptr = 0, prev_tpptr = 0;
  2600. trace_debug ("Starting the trace");
  2601. /* Pause all threads temporarily while we patch tracepoints. */
  2602. target_pause_all (false);
  2603. /* Get threads out of jump pads. Safe to do here, since this is a
  2604. top level command. And, required to do here, since we're
  2605. deleting/rewriting jump pads. */
  2606. target_stabilize_threads ();
  2607. /* Freeze threads. */
  2608. target_pause_all (true);
  2609. /* Sync the fast tracepoints list in the inferior ftlib. */
  2610. if (agent_loaded_p ())
  2611. download_trace_state_variables ();
  2612. /* No previous fast tpoint yet. */
  2613. prev_ftpoint = NULL;
  2614. /* No previous static tpoint yet. */
  2615. prev_stpoint = NULL;
  2616. *packet = '\0';
  2617. if (agent_loaded_p ())
  2618. {
  2619. /* Tell IPA about the correct tdesc. */
  2620. if (write_inferior_integer (ipa_sym_addrs.addr_ipa_tdesc_idx,
  2621. target_get_ipa_tdesc_idx ()))
  2622. error ("Error setting ipa_tdesc_idx variable in lib");
  2623. }
  2624. /* Start out empty. */
  2625. if (agent_loaded_p ())
  2626. write_inferior_data_pointer (ipa_sym_addrs.addr_tracepoints, 0);
  2627. /* Download and install tracepoints. */
  2628. for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
  2629. {
  2630. /* Ensure all the hit counts start at zero. */
  2631. tpoint->hit_count = 0;
  2632. tpoint->traceframe_usage = 0;
  2633. if (tpoint->type == trap_tracepoint)
  2634. {
  2635. /* Tracepoints are installed as memory breakpoints. Just go
  2636. ahead and install the trap. The breakpoints module
  2637. handles duplicated breakpoints, and the memory read
  2638. routine handles un-patching traps from memory reads. */
  2639. tpoint->handle = set_breakpoint_at (tpoint->address,
  2640. tracepoint_handler);
  2641. }
  2642. else if (tpoint->type == fast_tracepoint
  2643. || tpoint->type == static_tracepoint)
  2644. {
  2645. if (maybe_write_ipa_not_loaded (packet))
  2646. {
  2647. trace_debug ("Requested a %s tracepoint, but fast "
  2648. "tracepoints aren't supported.",
  2649. tpoint->type == static_tracepoint
  2650. ? "static" : "fast");
  2651. break;
  2652. }
  2653. if (tpoint->type == fast_tracepoint)
  2654. {
  2655. int use_agent_p
  2656. = use_agent && agent_capability_check (AGENT_CAPA_FAST_TRACE);
  2657. if (prev_ftpoint != NULL
  2658. && prev_ftpoint->address == tpoint->address)
  2659. {
  2660. if (use_agent_p)
  2661. tracepoint_send_agent (tpoint);
  2662. else
  2663. download_tracepoint_1 (tpoint);
  2664. clone_fast_tracepoint (tpoint, prev_ftpoint);
  2665. }
  2666. else
  2667. {
  2668. /* Tracepoint is installed successfully? */
  2669. int installed = 0;
  2670. /* Download and install fast tracepoint by agent. */
  2671. if (use_agent_p)
  2672. installed = !tracepoint_send_agent (tpoint);
  2673. else
  2674. {
  2675. download_tracepoint_1 (tpoint);
  2676. installed = !install_fast_tracepoint (tpoint, packet);
  2677. }
  2678. if (installed)
  2679. prev_ftpoint = tpoint;
  2680. }
  2681. }
  2682. else
  2683. {
  2684. if (!in_process_agent_supports_ust ())
  2685. {
  2686. trace_debug ("Requested a static tracepoint, but static "
  2687. "tracepoints are not supported.");
  2688. break;
  2689. }
  2690. download_tracepoint_1 (tpoint);
  2691. /* Can only probe a given marker once. */
  2692. if (prev_stpoint != NULL
  2693. && prev_stpoint->address == tpoint->address)
  2694. tpoint->handle = (void *) -1;
  2695. else
  2696. {
  2697. if (probe_marker_at (tpoint->address, packet) == 0)
  2698. {
  2699. tpoint->handle = (void *) -1;
  2700. /* So that we can handle multiple static tracepoints
  2701. at the same address easily. */
  2702. prev_stpoint = tpoint;
  2703. }
  2704. }
  2705. }
  2706. prev_tpptr = tpptr;
  2707. tpptr = tpoint->obj_addr_on_target;
  2708. if (tpoint == tracepoints)
  2709. /* First object in list, set the head pointer in the
  2710. inferior. */
  2711. write_inferior_data_pointer (ipa_sym_addrs.addr_tracepoints, tpptr);
  2712. else
  2713. write_inferior_data_pointer (prev_tpptr
  2714. + offsetof (struct tracepoint, next),
  2715. tpptr);
  2716. }
  2717. /* Any failure in the inner loop is sufficient cause to give
  2718. up. */
  2719. if (tpoint->handle == NULL)
  2720. break;
  2721. }
  2722. /* Any error in tracepoint insertion is unacceptable; better to
  2723. address the problem now, than end up with a useless or misleading
  2724. trace run. */
  2725. if (tpoint != NULL)
  2726. {
  2727. clear_installed_tracepoints ();
  2728. if (*packet == '\0')
  2729. write_enn (packet);
  2730. target_unpause_all (true);
  2731. return;
  2732. }
  2733. stopping_tracepoint = NULL;
  2734. trace_buffer_is_full = 0;
  2735. expr_eval_result = expr_eval_no_error;
  2736. error_tracepoint = NULL;
  2737. tracing_start_time = get_timestamp ();
  2738. /* Tracing is now active, hits will now start being logged. */
  2739. tracing = 1;
  2740. if (agent_loaded_p ())
  2741. {
  2742. if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 1))
  2743. {
  2744. internal_error (__FILE__, __LINE__,
  2745. "Error setting tracing variable in lib");
  2746. }
  2747. if (write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
  2748. 0))
  2749. {
  2750. internal_error (__FILE__, __LINE__,
  2751. "Error clearing stopping_tracepoint variable"
  2752. " in lib");
  2753. }
  2754. if (write_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full, 0))
  2755. {
  2756. internal_error (__FILE__, __LINE__,
  2757. "Error clearing trace_buffer_is_full variable"
  2758. " in lib");
  2759. }
  2760. stop_tracing_bkpt = set_breakpoint_at (ipa_sym_addrs.addr_stop_tracing,
  2761. stop_tracing_handler);
  2762. if (stop_tracing_bkpt == NULL)
  2763. error ("Error setting stop_tracing breakpoint");
  2764. flush_trace_buffer_bkpt
  2765. = set_breakpoint_at (ipa_sym_addrs.addr_flush_trace_buffer,
  2766. flush_trace_buffer_handler);
  2767. if (flush_trace_buffer_bkpt == NULL)
  2768. error ("Error setting flush_trace_buffer breakpoint");
  2769. }
  2770. target_unpause_all (true);
  2771. write_ok (packet);
  2772. }
  2773. /* End a tracing run, filling in a stop reason to report back to GDB,
  2774. and removing the tracepoints from the code. */
  2775. void
  2776. stop_tracing (void)
  2777. {
  2778. if (!tracing)
  2779. {
  2780. trace_debug ("Tracing is already off, ignoring");
  2781. return;
  2782. }
  2783. trace_debug ("Stopping the trace");
  2784. /* Pause all threads before removing fast jumps from memory,
  2785. breakpoints, and touching IPA state variables (inferior memory).
  2786. Some thread may hit the internal tracing breakpoints, or be
  2787. collecting this moment, but that's ok, we don't release the
  2788. tpoint object's memory or the jump pads here (we only do that
  2789. when we're sure we can move all threads out of the jump pads).
  2790. We can't now, since we may be getting here due to the inferior
  2791. agent calling us. */
  2792. target_pause_all (true);
  2793. /* Stop logging. Tracepoints can still be hit, but they will not be
  2794. recorded. */
  2795. tracing = 0;
  2796. if (agent_loaded_p ())
  2797. {
  2798. if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 0))
  2799. {
  2800. internal_error (__FILE__, __LINE__,
  2801. "Error clearing tracing variable in lib");
  2802. }
  2803. }
  2804. tracing_stop_time = get_timestamp ();
  2805. tracing_stop_reason = "t???";
  2806. tracing_stop_tpnum = 0;
  2807. if (stopping_tracepoint)
  2808. {
  2809. trace_debug ("Stopping the trace because "
  2810. "tracepoint %d was hit %" PRIu64 " times",
  2811. stopping_tracepoint->number,
  2812. stopping_tracepoint->pass_count);
  2813. tracing_stop_reason = "tpasscount";
  2814. tracing_stop_tpnum = stopping_tracepoint->number;
  2815. }
  2816. else if (trace_buffer_is_full)
  2817. {
  2818. trace_debug ("Stopping the trace because the trace buffer is full");
  2819. tracing_stop_reason = "tfull";
  2820. }
  2821. else if (expr_eval_result != expr_eval_no_error)
  2822. {
  2823. trace_debug ("Stopping the trace because of an expression eval error");
  2824. tracing_stop_reason = eval_result_names[expr_eval_result];
  2825. tracing_stop_tpnum = error_tracepoint->number;
  2826. }
  2827. #ifndef IN_PROCESS_AGENT
  2828. else if (!gdb_connected ())
  2829. {
  2830. trace_debug ("Stopping the trace because GDB disconnected");
  2831. tracing_stop_reason = "tdisconnected";
  2832. }
  2833. #endif
  2834. else
  2835. {
  2836. trace_debug ("Stopping the trace because of a tstop command");
  2837. tracing_stop_reason = "tstop";
  2838. }
  2839. stopping_tracepoint = NULL;
  2840. error_tracepoint = NULL;
  2841. /* Clear out the tracepoints. */
  2842. clear_installed_tracepoints ();
  2843. if (agent_loaded_p ())
  2844. {
  2845. /* Pull in fast tracepoint trace frames from the inferior lib
  2846. buffer into our buffer, even if our buffer is already full,
  2847. because we want to present the full number of created frames
  2848. in addition to what fit in the trace buffer. */
  2849. upload_fast_traceframes ();
  2850. }
  2851. if (stop_tracing_bkpt != NULL)
  2852. {
  2853. delete_breakpoint (stop_tracing_bkpt);
  2854. stop_tracing_bkpt = NULL;
  2855. }
  2856. if (flush_trace_buffer_bkpt != NULL)
  2857. {
  2858. delete_breakpoint (flush_trace_buffer_bkpt);
  2859. flush_trace_buffer_bkpt = NULL;
  2860. }
  2861. target_unpause_all (true);
  2862. }
  2863. static int
  2864. stop_tracing_handler (CORE_ADDR addr)
  2865. {
  2866. trace_debug ("lib hit stop_tracing");
  2867. /* Don't actually handle it here. When we stop tracing we remove
  2868. breakpoints from the inferior, and that is not allowed in a
  2869. breakpoint handler (as the caller is walking the breakpoint
  2870. list). */
  2871. return 0;
  2872. }
  2873. static int
  2874. flush_trace_buffer_handler (CORE_ADDR addr)
  2875. {
  2876. trace_debug ("lib hit flush_trace_buffer");
  2877. return 0;
  2878. }
  2879. static void
  2880. cmd_qtstop (char *packet)
  2881. {
  2882. stop_tracing ();
  2883. write_ok (packet);
  2884. }
  2885. static void
  2886. cmd_qtdisconnected (char *own_buf)
  2887. {
  2888. ULONGEST setting;
  2889. char *packet = own_buf;
  2890. packet += strlen ("QTDisconnected:");
  2891. unpack_varlen_hex (packet, &setting);
  2892. write_ok (own_buf);
  2893. disconnected_tracing = setting;
  2894. }
  2895. static void
  2896. cmd_qtframe (char *own_buf)
  2897. {
  2898. client_state &cs = get_client_state ();
  2899. ULONGEST frame, pc, lo, hi, num;
  2900. int tfnum, tpnum;
  2901. struct traceframe *tframe;
  2902. const char *packet = own_buf;
  2903. packet += strlen ("QTFrame:");
  2904. if (startswith (packet, "pc:"))
  2905. {
  2906. packet += strlen ("pc:");
  2907. unpack_varlen_hex (packet, &pc);
  2908. trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc));
  2909. tframe = find_next_traceframe_in_range (pc, pc, 1, &tfnum);
  2910. }
  2911. else if (startswith (packet, "range:"))
  2912. {
  2913. packet += strlen ("range:");
  2914. packet = unpack_varlen_hex (packet, &lo);
  2915. ++packet;
  2916. unpack_varlen_hex (packet, &hi);
  2917. trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
  2918. paddress (lo), paddress (hi));
  2919. tframe = find_next_traceframe_in_range (lo, hi, 1, &tfnum);
  2920. }
  2921. else if (startswith (packet, "outside:"))
  2922. {
  2923. packet += strlen ("outside:");
  2924. packet = unpack_varlen_hex (packet, &lo);
  2925. ++packet;
  2926. unpack_varlen_hex (packet, &hi);
  2927. trace_debug ("Want to find next traceframe "
  2928. "outside the range 0x%s to 0x%s",
  2929. paddress (lo), paddress (hi));
  2930. tframe = find_next_traceframe_in_range (lo, hi, 0, &tfnum);
  2931. }
  2932. else if (startswith (packet, "tdp:"))
  2933. {
  2934. packet += strlen ("tdp:");
  2935. unpack_varlen_hex (packet, &num);
  2936. tpnum = (int) num;
  2937. trace_debug ("Want to find next traceframe for tracepoint %d", tpnum);
  2938. tframe = find_next_traceframe_by_tracepoint (tpnum, &tfnum);
  2939. }
  2940. else
  2941. {
  2942. unpack_varlen_hex (packet, &frame);
  2943. tfnum = (int) frame;
  2944. if (tfnum == -1)
  2945. {
  2946. trace_debug ("Want to stop looking at traceframes");
  2947. cs.current_traceframe = -1;
  2948. write_ok (own_buf);
  2949. return;
  2950. }
  2951. trace_debug ("Want to look at traceframe %d", tfnum);
  2952. tframe = find_traceframe (tfnum);
  2953. }
  2954. if (tframe)
  2955. {
  2956. cs.current_traceframe = tfnum;
  2957. sprintf (own_buf, "F%xT%x", tfnum, tframe->tpnum);
  2958. }
  2959. else
  2960. sprintf (own_buf, "F-1");
  2961. }
  2962. static void
  2963. cmd_qtstatus (char *packet)
  2964. {
  2965. char *stop_reason_rsp = NULL;
  2966. char *buf1, *buf2, *buf3;
  2967. const char *str;
  2968. int slen;
  2969. /* Translate the plain text of the notes back into hex for
  2970. transmission. */
  2971. str = (tracing_user_name ? tracing_user_name : "");
  2972. slen = strlen (str);
  2973. buf1 = (char *) alloca (slen * 2 + 1);
  2974. bin2hex ((gdb_byte *) str, buf1, slen);
  2975. str = (tracing_notes ? tracing_notes : "");
  2976. slen = strlen (str);
  2977. buf2 = (char *) alloca (slen * 2 + 1);
  2978. bin2hex ((gdb_byte *) str, buf2, slen);
  2979. str = (tracing_stop_note ? tracing_stop_note : "");
  2980. slen = strlen (str);
  2981. buf3 = (char *) alloca (slen * 2 + 1);
  2982. bin2hex ((gdb_byte *) str, buf3, slen);
  2983. trace_debug ("Returning trace status as %d, stop reason %s",
  2984. tracing, tracing_stop_reason);
  2985. if (agent_loaded_p ())
  2986. {
  2987. target_pause_all (true);
  2988. upload_fast_traceframes ();
  2989. target_unpause_all (true);
  2990. }
  2991. stop_reason_rsp = (char *) tracing_stop_reason;
  2992. /* The user visible error string in terror needs to be hex encoded.
  2993. We leave it as plain string in `tracing_stop_reason' to ease
  2994. debugging. */
  2995. if (startswith (stop_reason_rsp, "terror:"))
  2996. {
  2997. const char *result_name;
  2998. int hexstr_len;
  2999. char *p;
  3000. result_name = stop_reason_rsp + strlen ("terror:");
  3001. hexstr_len = strlen (result_name) * 2;
  3002. p = stop_reason_rsp
  3003. = (char *) alloca (strlen ("terror:") + hexstr_len + 1);
  3004. strcpy (p, "terror:");
  3005. p += strlen (p);
  3006. bin2hex ((gdb_byte *) result_name, p, strlen (result_name));
  3007. }
  3008. /* If this was a forced stop, include any stop note that was supplied. */
  3009. if (strcmp (stop_reason_rsp, "tstop") == 0)
  3010. {
  3011. stop_reason_rsp = (char *) alloca (strlen ("tstop:") + strlen (buf3) + 1);
  3012. strcpy (stop_reason_rsp, "tstop:");
  3013. strcat (stop_reason_rsp, buf3);
  3014. }
  3015. sprintf (packet,
  3016. "T%d;"
  3017. "%s:%x;"
  3018. "tframes:%x;tcreated:%x;"
  3019. "tfree:%x;tsize:%s;"
  3020. "circular:%d;"
  3021. "disconn:%d;"
  3022. "starttime:%s;stoptime:%s;"
  3023. "username:%s;notes:%s:",
  3024. tracing ? 1 : 0,
  3025. stop_reason_rsp, tracing_stop_tpnum,
  3026. traceframe_count, traceframes_created,
  3027. free_space (), phex_nz (trace_buffer_hi - trace_buffer_lo, 0),
  3028. circular_trace_buffer,
  3029. disconnected_tracing,
  3030. phex_nz (tracing_start_time, sizeof (tracing_start_time)),
  3031. phex_nz (tracing_stop_time, sizeof (tracing_stop_time)),
  3032. buf1, buf2);
  3033. }
  3034. static void
  3035. cmd_qtp (char *own_buf)
  3036. {
  3037. ULONGEST num, addr;
  3038. struct tracepoint *tpoint;
  3039. const char *packet = own_buf;
  3040. packet += strlen ("qTP:");
  3041. packet = unpack_varlen_hex (packet, &num);
  3042. ++packet; /* skip a colon */
  3043. packet = unpack_varlen_hex (packet, &addr);
  3044. /* See if we already have this tracepoint. */
  3045. tpoint = find_tracepoint (num, addr);
  3046. if (!tpoint)
  3047. {
  3048. trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
  3049. (int) num, paddress (addr));
  3050. write_enn (own_buf);
  3051. return;
  3052. }
  3053. sprintf (own_buf, "V%" PRIu64 ":%" PRIu64 "", tpoint->hit_count,
  3054. tpoint->traceframe_usage);
  3055. }
  3056. /* State variables to help return all the tracepoint bits. */
  3057. static struct tracepoint *cur_tpoint;
  3058. static unsigned int cur_action;
  3059. static unsigned int cur_step_action;
  3060. static struct source_string *cur_source_string;
  3061. static struct trace_state_variable *cur_tsv;
  3062. /* Compose a response that is an imitation of the syntax by which the
  3063. tracepoint was originally downloaded. */
  3064. static void
  3065. response_tracepoint (char *packet, struct tracepoint *tpoint)
  3066. {
  3067. char *buf;
  3068. sprintf (packet, "T%x:%s:%c:%" PRIx64 ":%" PRIx64, tpoint->number,
  3069. paddress (tpoint->address),
  3070. (tpoint->enabled ? 'E' : 'D'), tpoint->step_count,
  3071. tpoint->pass_count);
  3072. if (tpoint->type == fast_tracepoint)
  3073. sprintf (packet + strlen (packet), ":F%x", tpoint->orig_size);
  3074. else if (tpoint->type == static_tracepoint)
  3075. sprintf (packet + strlen (packet), ":S");
  3076. if (tpoint->cond)
  3077. {
  3078. buf = gdb_unparse_agent_expr (tpoint->cond);
  3079. sprintf (packet + strlen (packet), ":X%x,%s",
  3080. tpoint->cond->length, buf);
  3081. free (buf);
  3082. }
  3083. }
  3084. /* Compose a response that is an imitation of the syntax by which the
  3085. tracepoint action was originally downloaded (with the difference
  3086. that due to the way we store the actions, this will output a packet
  3087. per action, while GDB could have combined more than one action
  3088. per-packet. */
  3089. static void
  3090. response_action (char *packet, struct tracepoint *tpoint,
  3091. char *taction, int step)
  3092. {
  3093. sprintf (packet, "%c%x:%s:%s",
  3094. (step ? 'S' : 'A'), tpoint->number, paddress (tpoint->address),
  3095. taction);
  3096. }
  3097. /* Compose a response that is an imitation of the syntax by which the
  3098. tracepoint source piece was originally downloaded. */
  3099. static void
  3100. response_source (char *packet,
  3101. struct tracepoint *tpoint, struct source_string *src)
  3102. {
  3103. char *buf;
  3104. int len;
  3105. len = strlen (src->str);
  3106. buf = (char *) alloca (len * 2 + 1);
  3107. bin2hex ((gdb_byte *) src->str, buf, len);
  3108. sprintf (packet, "Z%x:%s:%s:%x:%x:%s",
  3109. tpoint->number, paddress (tpoint->address),
  3110. src->type, 0, len, buf);
  3111. }
  3112. /* Return the first piece of tracepoint definition, and initialize the
  3113. state machine that will iterate through all the tracepoint
  3114. bits. */
  3115. static void
  3116. cmd_qtfp (char *packet)
  3117. {
  3118. trace_debug ("Returning first tracepoint definition piece");
  3119. cur_tpoint = tracepoints;
  3120. cur_action = cur_step_action = 0;
  3121. cur_source_string = NULL;
  3122. if (cur_tpoint)
  3123. response_tracepoint (packet, cur_tpoint);
  3124. else
  3125. strcpy (packet, "l");
  3126. }
  3127. /* Return additional pieces of tracepoint definition. Each action and
  3128. stepping action must go into its own packet, because of packet size
  3129. limits, and so we use state variables to deliver one piece at a
  3130. time. */
  3131. static void
  3132. cmd_qtsp (char *packet)
  3133. {
  3134. trace_debug ("Returning subsequent tracepoint definition piece");
  3135. if (!cur_tpoint)
  3136. {
  3137. /* This case would normally never occur, but be prepared for
  3138. GDB misbehavior. */
  3139. strcpy (packet, "l");
  3140. }
  3141. else if (cur_action < cur_tpoint->numactions)
  3142. {
  3143. response_action (packet, cur_tpoint,
  3144. cur_tpoint->actions_str[cur_action], 0);
  3145. ++cur_action;
  3146. }
  3147. else if (cur_step_action < cur_tpoint->num_step_actions)
  3148. {
  3149. response_action (packet, cur_tpoint,
  3150. cur_tpoint->step_actions_str[cur_step_action], 1);
  3151. ++cur_step_action;
  3152. }
  3153. else if ((cur_source_string
  3154. ? cur_source_string->next
  3155. : cur_tpoint->source_strings))
  3156. {
  3157. if (cur_source_string)
  3158. cur_source_string = cur_source_string->next;
  3159. else
  3160. cur_source_string = cur_tpoint->source_strings;
  3161. response_source (packet, cur_tpoint, cur_source_string);
  3162. }
  3163. else
  3164. {
  3165. cur_tpoint = cur_tpoint->next;
  3166. cur_action = cur_step_action = 0;
  3167. cur_source_string = NULL;
  3168. if (cur_tpoint)
  3169. response_tracepoint (packet, cur_tpoint);
  3170. else
  3171. strcpy (packet, "l");
  3172. }
  3173. }
  3174. /* Compose a response that is an imitation of the syntax by which the
  3175. trace state variable was originally downloaded. */
  3176. static void
  3177. response_tsv (char *packet, struct trace_state_variable *tsv)
  3178. {
  3179. char *buf = (char *) "";
  3180. int namelen;
  3181. if (tsv->name)
  3182. {
  3183. namelen = strlen (tsv->name);
  3184. buf = (char *) alloca (namelen * 2 + 1);
  3185. bin2hex ((gdb_byte *) tsv->name, buf, namelen);
  3186. }
  3187. sprintf (packet, "%x:%s:%x:%s", tsv->number, phex_nz (tsv->initial_value, 0),
  3188. tsv->getter ? 1 : 0, buf);
  3189. }
  3190. /* Return the first trace state variable definition, and initialize
  3191. the state machine that will iterate through all the tsv bits. */
  3192. static void
  3193. cmd_qtfv (char *packet)
  3194. {
  3195. trace_debug ("Returning first trace state variable definition");
  3196. cur_tsv = trace_state_variables;
  3197. if (cur_tsv)
  3198. response_tsv (packet, cur_tsv);
  3199. else
  3200. strcpy (packet, "l");
  3201. }
  3202. /* Return additional trace state variable definitions. */
  3203. static void
  3204. cmd_qtsv (char *packet)
  3205. {
  3206. trace_debug ("Returning additional trace state variable definition");
  3207. if (cur_tsv)
  3208. {
  3209. cur_tsv = cur_tsv->next;
  3210. if (cur_tsv)
  3211. response_tsv (packet, cur_tsv);
  3212. else
  3213. strcpy (packet, "l");
  3214. }
  3215. else
  3216. strcpy (packet, "l");
  3217. }
  3218. /* Return the first static tracepoint marker, and initialize the state
  3219. machine that will iterate through all the static tracepoints
  3220. markers. */
  3221. static void
  3222. cmd_qtfstm (char *packet)
  3223. {
  3224. if (!maybe_write_ipa_ust_not_loaded (packet))
  3225. run_inferior_command (packet, strlen (packet) + 1);
  3226. }
  3227. /* Return additional static tracepoints markers. */
  3228. static void
  3229. cmd_qtsstm (char *packet)
  3230. {
  3231. if (!maybe_write_ipa_ust_not_loaded (packet))
  3232. run_inferior_command (packet, strlen (packet) + 1);
  3233. }
  3234. /* Return the definition of the static tracepoint at a given address.
  3235. Result packet is the same as qTsST's. */
  3236. static void
  3237. cmd_qtstmat (char *packet)
  3238. {
  3239. if (!maybe_write_ipa_ust_not_loaded (packet))
  3240. run_inferior_command (packet, strlen (packet) + 1);
  3241. }
  3242. /* Sent the agent a command to close it. */
  3243. void
  3244. gdb_agent_about_to_close (int pid)
  3245. {
  3246. char buf[IPA_CMD_BUF_SIZE];
  3247. if (!maybe_write_ipa_not_loaded (buf))
  3248. {
  3249. scoped_restore_current_thread restore_thread;
  3250. /* Find any thread which belongs to process PID. */
  3251. switch_to_thread (find_any_thread_of_pid (pid));
  3252. strcpy (buf, "close");
  3253. run_inferior_command (buf, strlen (buf) + 1);
  3254. }
  3255. }
  3256. /* Return the minimum instruction size needed for fast tracepoints as a
  3257. hexadecimal number. */
  3258. static void
  3259. cmd_qtminftpilen (char *packet)
  3260. {
  3261. if (current_thread == NULL)
  3262. {
  3263. /* Indicate that the minimum length is currently unknown. */
  3264. strcpy (packet, "0");
  3265. return;
  3266. }
  3267. sprintf (packet, "%x", target_get_min_fast_tracepoint_insn_len ());
  3268. }
  3269. /* Respond to qTBuffer packet with a block of raw data from the trace
  3270. buffer. GDB may ask for a lot, but we are allowed to reply with
  3271. only as much as will fit within packet limits or whatever. */
  3272. static void
  3273. cmd_qtbuffer (char *own_buf)
  3274. {
  3275. ULONGEST offset, num, tot;
  3276. unsigned char *tbp;
  3277. const char *packet = own_buf;
  3278. packet += strlen ("qTBuffer:");
  3279. packet = unpack_varlen_hex (packet, &offset);
  3280. ++packet; /* skip a comma */
  3281. unpack_varlen_hex (packet, &num);
  3282. trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
  3283. (int) num, phex_nz (offset, 0));
  3284. tot = (trace_buffer_hi - trace_buffer_lo) - free_space ();
  3285. /* If we're right at the end, reply specially that we're done. */
  3286. if (offset == tot)
  3287. {
  3288. strcpy (own_buf, "l");
  3289. return;
  3290. }
  3291. /* Object to any other out-of-bounds request. */
  3292. if (offset > tot)
  3293. {
  3294. write_enn (own_buf);
  3295. return;
  3296. }
  3297. /* Compute the pointer corresponding to the given offset, accounting
  3298. for wraparound. */
  3299. tbp = trace_buffer_start + offset;
  3300. if (tbp >= trace_buffer_wrap)
  3301. tbp -= (trace_buffer_wrap - trace_buffer_lo);
  3302. /* Trim to the remaining bytes if we're close to the end. */
  3303. if (num > tot - offset)
  3304. num = tot - offset;
  3305. /* Trim to available packet size. */
  3306. if (num >= (PBUFSIZ - 16) / 2 )
  3307. num = (PBUFSIZ - 16) / 2;
  3308. bin2hex (tbp, own_buf, num);
  3309. }
  3310. static void
  3311. cmd_bigqtbuffer_circular (char *own_buf)
  3312. {
  3313. ULONGEST val;
  3314. char *packet = own_buf;
  3315. packet += strlen ("QTBuffer:circular:");
  3316. unpack_varlen_hex (packet, &val);
  3317. circular_trace_buffer = val;
  3318. trace_debug ("Trace buffer is now %s",
  3319. circular_trace_buffer ? "circular" : "linear");
  3320. write_ok (own_buf);
  3321. }
  3322. static void
  3323. cmd_bigqtbuffer_size (char *own_buf)
  3324. {
  3325. ULONGEST val;
  3326. LONGEST sval;
  3327. char *packet = own_buf;
  3328. /* Can't change the size during a tracing run. */
  3329. if (tracing)
  3330. {
  3331. write_enn (own_buf);
  3332. return;
  3333. }
  3334. packet += strlen ("QTBuffer:size:");
  3335. /* -1 is sent as literal "-1". */
  3336. if (strcmp (packet, "-1") == 0)
  3337. sval = DEFAULT_TRACE_BUFFER_SIZE;
  3338. else
  3339. {
  3340. unpack_varlen_hex (packet, &val);
  3341. sval = (LONGEST) val;
  3342. }
  3343. init_trace_buffer (sval);
  3344. trace_debug ("Trace buffer is now %s bytes",
  3345. plongest (trace_buffer_size));
  3346. write_ok (own_buf);
  3347. }
  3348. static void
  3349. cmd_qtnotes (char *own_buf)
  3350. {
  3351. size_t nbytes;
  3352. char *saved, *user, *notes, *stopnote;
  3353. char *packet = own_buf;
  3354. packet += strlen ("QTNotes:");
  3355. while (*packet)
  3356. {
  3357. if (startswith (packet, "user:"))
  3358. {
  3359. packet += strlen ("user:");
  3360. saved = packet;
  3361. packet = strchr (packet, ';');
  3362. nbytes = (packet - saved) / 2;
  3363. user = (char *) xmalloc (nbytes + 1);
  3364. nbytes = hex2bin (saved, (gdb_byte *) user, nbytes);
  3365. user[nbytes] = '\0';
  3366. ++packet; /* skip the semicolon */
  3367. trace_debug ("User is '%s'", user);
  3368. xfree (tracing_user_name);
  3369. tracing_user_name = user;
  3370. }
  3371. else if (startswith (packet, "notes:"))
  3372. {
  3373. packet += strlen ("notes:");
  3374. saved = packet;
  3375. packet = strchr (packet, ';');
  3376. nbytes = (packet - saved) / 2;
  3377. notes = (char *) xmalloc (nbytes + 1);
  3378. nbytes = hex2bin (saved, (gdb_byte *) notes, nbytes);
  3379. notes[nbytes] = '\0';
  3380. ++packet; /* skip the semicolon */
  3381. trace_debug ("Notes is '%s'", notes);
  3382. xfree (tracing_notes);
  3383. tracing_notes = notes;
  3384. }
  3385. else if (startswith (packet, "tstop:"))
  3386. {
  3387. packet += strlen ("tstop:");
  3388. saved = packet;
  3389. packet = strchr (packet, ';');
  3390. nbytes = (packet - saved) / 2;
  3391. stopnote = (char *) xmalloc (nbytes + 1);
  3392. nbytes = hex2bin (saved, (gdb_byte *) stopnote, nbytes);
  3393. stopnote[nbytes] = '\0';
  3394. ++packet; /* skip the semicolon */
  3395. trace_debug ("tstop note is '%s'", stopnote);
  3396. xfree (tracing_stop_note);
  3397. tracing_stop_note = stopnote;
  3398. }
  3399. else
  3400. break;
  3401. }
  3402. write_ok (own_buf);
  3403. }
  3404. int
  3405. handle_tracepoint_general_set (char *packet)
  3406. {
  3407. if (strcmp ("QTinit", packet) == 0)
  3408. {
  3409. cmd_qtinit (packet);
  3410. return 1;
  3411. }
  3412. else if (startswith (packet, "QTDP:"))
  3413. {
  3414. cmd_qtdp (packet);
  3415. return 1;
  3416. }
  3417. else if (startswith (packet, "QTDPsrc:"))
  3418. {
  3419. cmd_qtdpsrc (packet);
  3420. return 1;
  3421. }
  3422. else if (startswith (packet, "QTEnable:"))
  3423. {
  3424. cmd_qtenable_disable (packet, 1);
  3425. return 1;
  3426. }
  3427. else if (startswith (packet, "QTDisable:"))
  3428. {
  3429. cmd_qtenable_disable (packet, 0);
  3430. return 1;
  3431. }
  3432. else if (startswith (packet, "QTDV:"))
  3433. {
  3434. cmd_qtdv (packet);
  3435. return 1;
  3436. }
  3437. else if (startswith (packet, "QTro:"))
  3438. {
  3439. cmd_qtro (packet);
  3440. return 1;
  3441. }
  3442. else if (strcmp ("QTStart", packet) == 0)
  3443. {
  3444. cmd_qtstart (packet);
  3445. return 1;
  3446. }
  3447. else if (strcmp ("QTStop", packet) == 0)
  3448. {
  3449. cmd_qtstop (packet);
  3450. return 1;
  3451. }
  3452. else if (startswith (packet, "QTDisconnected:"))
  3453. {
  3454. cmd_qtdisconnected (packet);
  3455. return 1;
  3456. }
  3457. else if (startswith (packet, "QTFrame:"))
  3458. {
  3459. cmd_qtframe (packet);
  3460. return 1;
  3461. }
  3462. else if (startswith (packet, "QTBuffer:circular:"))
  3463. {
  3464. cmd_bigqtbuffer_circular (packet);
  3465. return 1;
  3466. }
  3467. else if (startswith (packet, "QTBuffer:size:"))
  3468. {
  3469. cmd_bigqtbuffer_size (packet);
  3470. return 1;
  3471. }
  3472. else if (startswith (packet, "QTNotes:"))
  3473. {
  3474. cmd_qtnotes (packet);
  3475. return 1;
  3476. }
  3477. return 0;
  3478. }
  3479. int
  3480. handle_tracepoint_query (char *packet)
  3481. {
  3482. if (strcmp ("qTStatus", packet) == 0)
  3483. {
  3484. cmd_qtstatus (packet);
  3485. return 1;
  3486. }
  3487. else if (startswith (packet, "qTP:"))
  3488. {
  3489. cmd_qtp (packet);
  3490. return 1;
  3491. }
  3492. else if (strcmp ("qTfP", packet) == 0)
  3493. {
  3494. cmd_qtfp (packet);
  3495. return 1;
  3496. }
  3497. else if (strcmp ("qTsP", packet) == 0)
  3498. {
  3499. cmd_qtsp (packet);
  3500. return 1;
  3501. }
  3502. else if (strcmp ("qTfV", packet) == 0)
  3503. {
  3504. cmd_qtfv (packet);
  3505. return 1;
  3506. }
  3507. else if (strcmp ("qTsV", packet) == 0)
  3508. {
  3509. cmd_qtsv (packet);
  3510. return 1;
  3511. }
  3512. else if (startswith (packet, "qTV:"))
  3513. {
  3514. cmd_qtv (packet);
  3515. return 1;
  3516. }
  3517. else if (startswith (packet, "qTBuffer:"))
  3518. {
  3519. cmd_qtbuffer (packet);
  3520. return 1;
  3521. }
  3522. else if (strcmp ("qTfSTM", packet) == 0)
  3523. {
  3524. cmd_qtfstm (packet);
  3525. return 1;
  3526. }
  3527. else if (strcmp ("qTsSTM", packet) == 0)
  3528. {
  3529. cmd_qtsstm (packet);
  3530. return 1;
  3531. }
  3532. else if (startswith (packet, "qTSTMat:"))
  3533. {
  3534. cmd_qtstmat (packet);
  3535. return 1;
  3536. }
  3537. else if (strcmp ("qTMinFTPILen", packet) == 0)
  3538. {
  3539. cmd_qtminftpilen (packet);
  3540. return 1;
  3541. }
  3542. return 0;
  3543. }
  3544. #endif
  3545. #ifndef IN_PROCESS_AGENT
  3546. /* Call this when thread TINFO has hit the tracepoint defined by
  3547. TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
  3548. action. This adds a while-stepping collecting state item to the
  3549. threads' collecting state list, so that we can keep track of
  3550. multiple simultaneous while-stepping actions being collected by the
  3551. same thread. This can happen in cases like:
  3552. ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
  3553. ff0002 INSN2
  3554. ff0003 INSN3 <-- TP2, collect $regs
  3555. ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
  3556. ff0005 INSN5
  3557. Notice that when instruction INSN5 is reached, the while-stepping
  3558. actions of both TP1 and TP3 are still being collected, and that TP2
  3559. had been collected meanwhile. The whole range of ff0001-ff0005
  3560. should be single-stepped, due to at least TP1's while-stepping
  3561. action covering the whole range. */
  3562. static void
  3563. add_while_stepping_state (struct thread_info *tinfo,
  3564. int tp_number, CORE_ADDR tp_address)
  3565. {
  3566. struct wstep_state *wstep = XNEW (struct wstep_state);
  3567. wstep->next = tinfo->while_stepping;
  3568. wstep->tp_number = tp_number;
  3569. wstep->tp_address = tp_address;
  3570. wstep->current_step = 0;
  3571. tinfo->while_stepping = wstep;
  3572. }
  3573. /* Release the while-stepping collecting state WSTEP. */
  3574. static void
  3575. release_while_stepping_state (struct wstep_state *wstep)
  3576. {
  3577. free (wstep);
  3578. }
  3579. /* Release all while-stepping collecting states currently associated
  3580. with thread TINFO. */
  3581. void
  3582. release_while_stepping_state_list (struct thread_info *tinfo)
  3583. {
  3584. struct wstep_state *head;
  3585. while (tinfo->while_stepping)
  3586. {
  3587. head = tinfo->while_stepping;
  3588. tinfo->while_stepping = head->next;
  3589. release_while_stepping_state (head);
  3590. }
  3591. }
  3592. /* If TINFO was handling a 'while-stepping' action, the step has
  3593. finished, so collect any step data needed, and check if any more
  3594. steps are required. Return true if the thread was indeed
  3595. collecting tracepoint data, false otherwise. */
  3596. int
  3597. tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
  3598. {
  3599. struct tracepoint *tpoint;
  3600. struct wstep_state *wstep;
  3601. struct wstep_state **wstep_link;
  3602. struct trap_tracepoint_ctx ctx;
  3603. /* Pull in fast tracepoint trace frames from the inferior lib buffer into
  3604. our buffer. */
  3605. if (agent_loaded_p ())
  3606. upload_fast_traceframes ();
  3607. /* Check if we were indeed collecting data for one of more
  3608. tracepoints with a 'while-stepping' count. */
  3609. if (tinfo->while_stepping == NULL)
  3610. return 0;
  3611. if (!tracing)
  3612. {
  3613. /* We're not even tracing anymore. Stop this thread from
  3614. collecting. */
  3615. release_while_stepping_state_list (tinfo);
  3616. /* The thread had stopped due to a single-step request indeed
  3617. explained by a tracepoint. */
  3618. return 1;
  3619. }
  3620. wstep = tinfo->while_stepping;
  3621. wstep_link = &tinfo->while_stepping;
  3622. trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
  3623. target_pid_to_str (tinfo->id).c_str (),
  3624. wstep->tp_number, paddress (wstep->tp_address));
  3625. ctx.base.type = trap_tracepoint;
  3626. ctx.regcache = get_thread_regcache (tinfo, 1);
  3627. while (wstep != NULL)
  3628. {
  3629. tpoint = find_tracepoint (wstep->tp_number, wstep->tp_address);
  3630. if (tpoint == NULL)
  3631. {
  3632. trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
  3633. wstep->tp_number, paddress (wstep->tp_address),
  3634. target_pid_to_str (tinfo->id).c_str ());
  3635. /* Unlink. */
  3636. *wstep_link = wstep->next;
  3637. release_while_stepping_state (wstep);
  3638. wstep = *wstep_link;
  3639. continue;
  3640. }
  3641. /* We've just finished one step. */
  3642. ++wstep->current_step;
  3643. /* Collect data. */
  3644. collect_data_at_step ((struct tracepoint_hit_ctx *) &ctx,
  3645. stop_pc, tpoint, wstep->current_step);
  3646. if (wstep->current_step >= tpoint->step_count)
  3647. {
  3648. /* The requested numbers of steps have occurred. */
  3649. trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
  3650. target_pid_to_str (tinfo->id).c_str (),
  3651. wstep->tp_number, paddress (wstep->tp_address));
  3652. /* Unlink the wstep. */
  3653. *wstep_link = wstep->next;
  3654. release_while_stepping_state (wstep);
  3655. wstep = *wstep_link;
  3656. /* Only check the hit count now, which ensure that we do all
  3657. our stepping before stopping the run. */
  3658. if (tpoint->pass_count > 0
  3659. && tpoint->hit_count >= tpoint->pass_count
  3660. && stopping_tracepoint == NULL)
  3661. stopping_tracepoint = tpoint;
  3662. }
  3663. else
  3664. {
  3665. /* Keep single-stepping until the requested numbers of steps
  3666. have occurred. */
  3667. wstep_link = &wstep->next;
  3668. wstep = *wstep_link;
  3669. }
  3670. if (stopping_tracepoint
  3671. || trace_buffer_is_full
  3672. || expr_eval_result != expr_eval_no_error)
  3673. {
  3674. stop_tracing ();
  3675. break;
  3676. }
  3677. }
  3678. return 1;
  3679. }
  3680. /* Handle any internal tracing control breakpoint hits. That means,
  3681. pull traceframes from the IPA to our buffer, and syncing both
  3682. tracing agents when the IPA's tracing stops for some reason. */
  3683. int
  3684. handle_tracepoint_bkpts (struct thread_info *tinfo, CORE_ADDR stop_pc)
  3685. {
  3686. /* Pull in fast tracepoint trace frames from the inferior in-process
  3687. agent's buffer into our buffer. */
  3688. if (!agent_loaded_p ())
  3689. return 0;
  3690. upload_fast_traceframes ();
  3691. /* Check if the in-process agent had decided we should stop
  3692. tracing. */
  3693. if (stop_pc == ipa_sym_addrs.addr_stop_tracing)
  3694. {
  3695. int ipa_trace_buffer_is_full;
  3696. CORE_ADDR ipa_stopping_tracepoint;
  3697. int ipa_expr_eval_result;
  3698. CORE_ADDR ipa_error_tracepoint;
  3699. trace_debug ("lib stopped at stop_tracing");
  3700. read_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full,
  3701. &ipa_trace_buffer_is_full);
  3702. read_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
  3703. &ipa_stopping_tracepoint);
  3704. write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint, 0);
  3705. read_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint,
  3706. &ipa_error_tracepoint);
  3707. write_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint, 0);
  3708. read_inferior_integer (ipa_sym_addrs.addr_expr_eval_result,
  3709. &ipa_expr_eval_result);
  3710. write_inferior_integer (ipa_sym_addrs.addr_expr_eval_result, 0);
  3711. trace_debug ("lib: trace_buffer_is_full: %d, "
  3712. "stopping_tracepoint: %s, "
  3713. "ipa_expr_eval_result: %d, "
  3714. "error_tracepoint: %s, ",
  3715. ipa_trace_buffer_is_full,
  3716. paddress (ipa_stopping_tracepoint),
  3717. ipa_expr_eval_result,
  3718. paddress (ipa_error_tracepoint));
  3719. if (ipa_trace_buffer_is_full)
  3720. trace_debug ("lib stopped due to full buffer.");
  3721. if (ipa_stopping_tracepoint)
  3722. trace_debug ("lib stopped due to tpoint");
  3723. if (ipa_error_tracepoint)
  3724. trace_debug ("lib stopped due to error");
  3725. if (ipa_stopping_tracepoint != 0)
  3726. {
  3727. stopping_tracepoint
  3728. = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint);
  3729. }
  3730. else if (ipa_expr_eval_result != expr_eval_no_error)
  3731. {
  3732. expr_eval_result = ipa_expr_eval_result;
  3733. error_tracepoint
  3734. = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint);
  3735. }
  3736. stop_tracing ();
  3737. return 1;
  3738. }
  3739. else if (stop_pc == ipa_sym_addrs.addr_flush_trace_buffer)
  3740. {
  3741. trace_debug ("lib stopped at flush_trace_buffer");
  3742. return 1;
  3743. }
  3744. return 0;
  3745. }
  3746. /* Return true if TINFO just hit a tracepoint. Collect data if
  3747. so. */
  3748. int
  3749. tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc)
  3750. {
  3751. struct tracepoint *tpoint;
  3752. int ret = 0;
  3753. struct trap_tracepoint_ctx ctx;
  3754. /* Not tracing, don't handle. */
  3755. if (!tracing)
  3756. return 0;
  3757. ctx.base.type = trap_tracepoint;
  3758. ctx.regcache = get_thread_regcache (tinfo, 1);
  3759. for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
  3760. {
  3761. /* Note that we collect fast tracepoints here as well. We'll
  3762. step over the fast tracepoint jump later, which avoids the
  3763. double collect. However, we don't collect for static
  3764. tracepoints here, because UST markers are compiled in program,
  3765. and probes will be executed in program. So static tracepoints
  3766. are collected there. */
  3767. if (tpoint->enabled && stop_pc == tpoint->address
  3768. && tpoint->type != static_tracepoint)
  3769. {
  3770. trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
  3771. target_pid_to_str (tinfo->id).c_str (),
  3772. tpoint->number, paddress (tpoint->address));
  3773. /* Test the condition if present, and collect if true. */
  3774. if (!tpoint->cond
  3775. || (condition_true_at_tracepoint
  3776. ((struct tracepoint_hit_ctx *) &ctx, tpoint)))
  3777. collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
  3778. stop_pc, tpoint);
  3779. if (stopping_tracepoint
  3780. || trace_buffer_is_full
  3781. || expr_eval_result != expr_eval_no_error)
  3782. {
  3783. stop_tracing ();
  3784. }
  3785. /* If the tracepoint had a 'while-stepping' action, then set
  3786. the thread to collect this tracepoint on the following
  3787. single-steps. */
  3788. else if (tpoint->step_count > 0)
  3789. {
  3790. add_while_stepping_state (tinfo,
  3791. tpoint->number, tpoint->address);
  3792. }
  3793. ret = 1;
  3794. }
  3795. }
  3796. return ret;
  3797. }
  3798. #endif
  3799. #if defined IN_PROCESS_AGENT && defined HAVE_UST
  3800. struct ust_marker_data;
  3801. static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
  3802. struct traceframe *tframe);
  3803. #endif
  3804. /* Create a trace frame for the hit of the given tracepoint in the
  3805. given thread. */
  3806. static void
  3807. collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, CORE_ADDR stop_pc,
  3808. struct tracepoint *tpoint)
  3809. {
  3810. struct traceframe *tframe;
  3811. int acti;
  3812. /* Only count it as a hit when we actually collect data. */
  3813. tpoint->hit_count++;
  3814. /* If we've exceeded a defined pass count, record the event for
  3815. later, and finish the collection for this hit. This test is only
  3816. for nonstepping tracepoints, stepping tracepoints test at the end
  3817. of their while-stepping loop. */
  3818. if (tpoint->pass_count > 0
  3819. && tpoint->hit_count >= tpoint->pass_count
  3820. && tpoint->step_count == 0
  3821. && stopping_tracepoint == NULL)
  3822. stopping_tracepoint = tpoint;
  3823. trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64,
  3824. tpoint->number, paddress (tpoint->address), tpoint->hit_count);
  3825. tframe = add_traceframe (tpoint);
  3826. if (tframe)
  3827. {
  3828. for (acti = 0; acti < tpoint->numactions; ++acti)
  3829. {
  3830. #ifndef IN_PROCESS_AGENT
  3831. trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
  3832. tpoint->number, paddress (tpoint->address),
  3833. tpoint->actions_str[acti]);
  3834. #endif
  3835. do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
  3836. tpoint->actions[acti]);
  3837. }
  3838. finish_traceframe (tframe);
  3839. }
  3840. if (tframe == NULL && tracing)
  3841. trace_buffer_is_full = 1;
  3842. }
  3843. #ifndef IN_PROCESS_AGENT
  3844. static void
  3845. collect_data_at_step (struct tracepoint_hit_ctx *ctx,
  3846. CORE_ADDR stop_pc,
  3847. struct tracepoint *tpoint, int current_step)
  3848. {
  3849. struct traceframe *tframe;
  3850. int acti;
  3851. trace_debug ("Making new step traceframe for "
  3852. "tracepoint %d at 0x%s, step %d of %" PRIu64 ", hit %" PRIu64,
  3853. tpoint->number, paddress (tpoint->address),
  3854. current_step, tpoint->step_count,
  3855. tpoint->hit_count);
  3856. tframe = add_traceframe (tpoint);
  3857. if (tframe)
  3858. {
  3859. for (acti = 0; acti < tpoint->num_step_actions; ++acti)
  3860. {
  3861. trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
  3862. tpoint->number, paddress (tpoint->address),
  3863. tpoint->step_actions_str[acti]);
  3864. do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
  3865. tpoint->step_actions[acti]);
  3866. }
  3867. finish_traceframe (tframe);
  3868. }
  3869. if (tframe == NULL && tracing)
  3870. trace_buffer_is_full = 1;
  3871. }
  3872. #endif
  3873. #ifdef IN_PROCESS_AGENT
  3874. /* The target description index for IPA. Passed from gdbserver, used
  3875. to select ipa_tdesc. */
  3876. EXTERN_C_PUSH
  3877. IP_AGENT_EXPORT_VAR int ipa_tdesc_idx;
  3878. EXTERN_C_POP
  3879. #endif
  3880. static struct regcache *
  3881. get_context_regcache (struct tracepoint_hit_ctx *ctx)
  3882. {
  3883. struct regcache *regcache = NULL;
  3884. #ifdef IN_PROCESS_AGENT
  3885. const struct target_desc *ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx);
  3886. if (ctx->type == fast_tracepoint)
  3887. {
  3888. struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx;
  3889. if (!fctx->regcache_initted)
  3890. {
  3891. fctx->regcache_initted = 1;
  3892. init_register_cache (&fctx->regcache, ipa_tdesc, fctx->regspace);
  3893. supply_regblock (&fctx->regcache, NULL);
  3894. supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);
  3895. }
  3896. regcache = &fctx->regcache;
  3897. }
  3898. #ifdef HAVE_UST
  3899. if (ctx->type == static_tracepoint)
  3900. {
  3901. struct static_tracepoint_ctx *sctx
  3902. = (struct static_tracepoint_ctx *) ctx;
  3903. if (!sctx->regcache_initted)
  3904. {
  3905. sctx->regcache_initted = 1;
  3906. init_register_cache (&sctx->regcache, ipa_tdesc, sctx->regspace);
  3907. supply_regblock (&sctx->regcache, NULL);
  3908. /* Pass down the tracepoint address, because REGS doesn't
  3909. include the PC, but we know what it must have been. */
  3910. supply_static_tracepoint_registers (&sctx->regcache,
  3911. (const unsigned char *)
  3912. sctx->regs,
  3913. sctx->tpoint->address);
  3914. }
  3915. regcache = &sctx->regcache;
  3916. }
  3917. #endif
  3918. #else
  3919. if (ctx->type == trap_tracepoint)
  3920. {
  3921. struct trap_tracepoint_ctx *tctx = (struct trap_tracepoint_ctx *) ctx;
  3922. regcache = tctx->regcache;
  3923. }
  3924. #endif
  3925. gdb_assert (regcache != NULL);
  3926. return regcache;
  3927. }
  3928. static void
  3929. do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
  3930. CORE_ADDR stop_pc,
  3931. struct tracepoint *tpoint,
  3932. struct traceframe *tframe,
  3933. struct tracepoint_action *taction)
  3934. {
  3935. enum eval_result_type err;
  3936. switch (taction->type)
  3937. {
  3938. case 'M':
  3939. {
  3940. struct collect_memory_action *maction;
  3941. struct eval_agent_expr_context ax_ctx;
  3942. maction = (struct collect_memory_action *) taction;
  3943. ax_ctx.regcache = NULL;
  3944. ax_ctx.tframe = tframe;
  3945. ax_ctx.tpoint = tpoint;
  3946. trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
  3947. pulongest (maction->len),
  3948. paddress (maction->addr), maction->basereg);
  3949. /* (should use basereg) */
  3950. agent_mem_read (&ax_ctx, NULL, (CORE_ADDR) maction->addr,
  3951. maction->len);
  3952. break;
  3953. }
  3954. case 'R':
  3955. {
  3956. unsigned char *regspace;
  3957. struct regcache tregcache;
  3958. struct regcache *context_regcache;
  3959. int regcache_size;
  3960. trace_debug ("Want to collect registers");
  3961. context_regcache = get_context_regcache (ctx);
  3962. regcache_size = register_cache_size (context_regcache->tdesc);
  3963. /* Collect all registers for now. */
  3964. regspace = add_traceframe_block (tframe, tpoint, 1 + regcache_size);
  3965. if (regspace == NULL)
  3966. {
  3967. trace_debug ("Trace buffer block allocation failed, skipping");
  3968. break;
  3969. }
  3970. /* Identify a register block. */
  3971. *regspace = 'R';
  3972. /* Wrap the regblock in a register cache (in the stack, we
  3973. don't want to malloc here). */
  3974. init_register_cache (&tregcache, context_regcache->tdesc,
  3975. regspace + 1);
  3976. /* Copy the register data to the regblock. */
  3977. regcache_cpy (&tregcache, context_regcache);
  3978. #ifndef IN_PROCESS_AGENT
  3979. /* On some platforms, trap-based tracepoints will have the PC
  3980. pointing to the next instruction after the trap, but we
  3981. don't want the user or GDB trying to guess whether the
  3982. saved PC needs adjusting; so always record the adjusted
  3983. stop_pc. Note that we can't use tpoint->address instead,
  3984. since it will be wrong for while-stepping actions. This
  3985. adjustment is a nop for fast tracepoints collected from the
  3986. in-process lib (but not if GDBserver is collecting one
  3987. preemptively), since the PC had already been adjusted to
  3988. contain the tracepoint's address by the jump pad. */
  3989. trace_debug ("Storing stop pc (0x%s) in regblock",
  3990. paddress (stop_pc));
  3991. /* This changes the regblock, not the thread's
  3992. regcache. */
  3993. regcache_write_pc (&tregcache, stop_pc);
  3994. #endif
  3995. }
  3996. break;
  3997. case 'X':
  3998. {
  3999. struct eval_expr_action *eaction;
  4000. struct eval_agent_expr_context ax_ctx;
  4001. eaction = (struct eval_expr_action *) taction;
  4002. ax_ctx.regcache = get_context_regcache (ctx);
  4003. ax_ctx.tframe = tframe;
  4004. ax_ctx.tpoint = tpoint;
  4005. trace_debug ("Want to evaluate expression");
  4006. err = gdb_eval_agent_expr (&ax_ctx, eaction->expr, NULL);
  4007. if (err != expr_eval_no_error)
  4008. {
  4009. record_tracepoint_error (tpoint, "action expression", err);
  4010. return;
  4011. }
  4012. }
  4013. break;
  4014. case 'L':
  4015. {
  4016. #if defined IN_PROCESS_AGENT && defined HAVE_UST
  4017. trace_debug ("Want to collect static trace data");
  4018. collect_ust_data_at_tracepoint (ctx, tframe);
  4019. #else
  4020. trace_debug ("warning: collecting static trace data, "
  4021. "but static tracepoints are not supported");
  4022. #endif
  4023. }
  4024. break;
  4025. default:
  4026. trace_debug ("unknown trace action '%c', ignoring", taction->type);
  4027. break;
  4028. }
  4029. }
  4030. static int
  4031. condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
  4032. struct tracepoint *tpoint)
  4033. {
  4034. ULONGEST value = 0;
  4035. enum eval_result_type err;
  4036. /* Presently, gdbserver doesn't run compiled conditions, only the
  4037. IPA does. If the program stops at a fast tracepoint's address
  4038. (e.g., due to a breakpoint, trap tracepoint, or stepping),
  4039. gdbserver preemptively collect the fast tracepoint. Later, on
  4040. resume, gdbserver steps over the fast tracepoint like it steps
  4041. over breakpoints, so that the IPA doesn't see that fast
  4042. tracepoint. This avoids double collects of fast tracepoints in
  4043. that stopping scenario. Having gdbserver itself handle the fast
  4044. tracepoint gives the user a consistent view of when fast or trap
  4045. tracepoints are collected, compared to an alternative where only
  4046. trap tracepoints are collected on stop, and fast tracepoints on
  4047. resume. When a fast tracepoint is being processed by gdbserver,
  4048. it is always the non-compiled condition expression that is
  4049. used. */
  4050. #ifdef IN_PROCESS_AGENT
  4051. if (tpoint->compiled_cond)
  4052. {
  4053. struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx;
  4054. err = ((condfn) (uintptr_t) (tpoint->compiled_cond)) (fctx->regs, &value);
  4055. }
  4056. else
  4057. #endif
  4058. {
  4059. struct eval_agent_expr_context ax_ctx;
  4060. ax_ctx.regcache = get_context_regcache (ctx);
  4061. ax_ctx.tframe = NULL;
  4062. ax_ctx.tpoint = tpoint;
  4063. err = gdb_eval_agent_expr (&ax_ctx, tpoint->cond, &value);
  4064. }
  4065. if (err != expr_eval_no_error)
  4066. {
  4067. record_tracepoint_error (tpoint, "condition", err);
  4068. /* The error case must return false. */
  4069. return 0;
  4070. }
  4071. trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
  4072. tpoint->number, paddress (tpoint->address),
  4073. pulongest (value));
  4074. return (value ? 1 : 0);
  4075. }
  4076. /* Do memory copies for bytecodes. */
  4077. /* Do the recording of memory blocks for actions and bytecodes. */
  4078. int
  4079. agent_mem_read (struct eval_agent_expr_context *ctx,
  4080. unsigned char *to, CORE_ADDR from, ULONGEST len)
  4081. {
  4082. unsigned char *mspace;
  4083. ULONGEST remaining = len;
  4084. unsigned short blocklen;
  4085. /* If a 'to' buffer is specified, use it. */
  4086. if (to != NULL)
  4087. {
  4088. read_inferior_memory (from, to, len);
  4089. return 0;
  4090. }
  4091. /* Otherwise, create a new memory block in the trace buffer. */
  4092. while (remaining > 0)
  4093. {
  4094. size_t sp;
  4095. blocklen = (remaining > 65535 ? 65535 : remaining);
  4096. sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
  4097. mspace = add_traceframe_block (ctx->tframe, ctx->tpoint, sp);
  4098. if (mspace == NULL)
  4099. return 1;
  4100. /* Identify block as a memory block. */
  4101. *mspace = 'M';
  4102. ++mspace;
  4103. /* Record address and size. */
  4104. memcpy (mspace, &from, sizeof (from));
  4105. mspace += sizeof (from);
  4106. memcpy (mspace, &blocklen, sizeof (blocklen));
  4107. mspace += sizeof (blocklen);
  4108. /* Record the memory block proper. */
  4109. read_inferior_memory (from, mspace, blocklen);
  4110. trace_debug ("%d bytes recorded", blocklen);
  4111. remaining -= blocklen;
  4112. from += blocklen;
  4113. }
  4114. return 0;
  4115. }
  4116. int
  4117. agent_mem_read_string (struct eval_agent_expr_context *ctx,
  4118. unsigned char *to, CORE_ADDR from, ULONGEST len)
  4119. {
  4120. unsigned char *buf, *mspace;
  4121. ULONGEST remaining = len;
  4122. unsigned short blocklen, i;
  4123. /* To save a bit of space, block lengths are 16-bit, so break large
  4124. requests into multiple blocks. Bordering on overkill for strings,
  4125. but it could happen that someone specifies a large max length. */
  4126. while (remaining > 0)
  4127. {
  4128. size_t sp;
  4129. blocklen = (remaining > 65535 ? 65535 : remaining);
  4130. /* We want working space to accumulate nonzero bytes, since
  4131. traceframes must have a predecided size (otherwise it gets
  4132. harder to wrap correctly for the circular case, etc). */
  4133. buf = (unsigned char *) xmalloc (blocklen + 1);
  4134. for (i = 0; i < blocklen; ++i)
  4135. {
  4136. /* Read the string one byte at a time, in case the string is
  4137. at the end of a valid memory area - we don't want a
  4138. correctly-terminated string to engender segvio
  4139. complaints. */
  4140. read_inferior_memory (from + i, buf + i, 1);
  4141. if (buf[i] == '\0')
  4142. {
  4143. blocklen = i + 1;
  4144. /* Make sure outer loop stops now too. */
  4145. remaining = blocklen;
  4146. break;
  4147. }
  4148. }
  4149. sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
  4150. mspace = add_traceframe_block (ctx->tframe, ctx->tpoint, sp);
  4151. if (mspace == NULL)
  4152. {
  4153. xfree (buf);
  4154. return 1;
  4155. }
  4156. /* Identify block as a memory block. */
  4157. *mspace = 'M';
  4158. ++mspace;
  4159. /* Record address and size. */
  4160. memcpy ((void *) mspace, (void *) &from, sizeof (from));
  4161. mspace += sizeof (from);
  4162. memcpy ((void *) mspace, (void *) &blocklen, sizeof (blocklen));
  4163. mspace += sizeof (blocklen);
  4164. /* Copy the string contents. */
  4165. memcpy ((void *) mspace, (void *) buf, blocklen);
  4166. remaining -= blocklen;
  4167. from += blocklen;
  4168. xfree (buf);
  4169. }
  4170. return 0;
  4171. }
  4172. /* Record the value of a trace state variable. */
  4173. int
  4174. agent_tsv_read (struct eval_agent_expr_context *ctx, int n)
  4175. {
  4176. unsigned char *vspace;
  4177. LONGEST val;
  4178. vspace = add_traceframe_block (ctx->tframe, ctx->tpoint,
  4179. 1 + sizeof (n) + sizeof (LONGEST));
  4180. if (vspace == NULL)
  4181. return 1;
  4182. /* Identify block as a variable. */
  4183. *vspace = 'V';
  4184. /* Record variable's number and value. */
  4185. memcpy (vspace + 1, &n, sizeof (n));
  4186. val = get_trace_state_variable_value (n);
  4187. memcpy (vspace + 1 + sizeof (n), &val, sizeof (val));
  4188. trace_debug ("Variable %d recorded", n);
  4189. return 0;
  4190. }
  4191. #ifndef IN_PROCESS_AGENT
  4192. /* Callback for traceframe_walk_blocks, used to find a given block
  4193. type in a traceframe. */
  4194. static int
  4195. match_blocktype (char blocktype, unsigned char *dataptr, void *data)
  4196. {
  4197. char *wantedp = (char *) data;
  4198. if (*wantedp == blocktype)
  4199. return 1;
  4200. return 0;
  4201. }
  4202. /* Walk over all traceframe blocks of the traceframe buffer starting
  4203. at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
  4204. block found, passing in DATA unmodified. If CALLBACK returns true,
  4205. this returns a pointer to where the block is found. Returns NULL
  4206. if no callback call returned true, indicating that all blocks have
  4207. been walked. */
  4208. static unsigned char *
  4209. traceframe_walk_blocks (unsigned char *database, unsigned int datasize,
  4210. int tfnum,
  4211. int (*callback) (char blocktype,
  4212. unsigned char *dataptr,
  4213. void *data),
  4214. void *data)
  4215. {
  4216. unsigned char *dataptr;
  4217. if (datasize == 0)
  4218. {
  4219. trace_debug ("traceframe %d has no data", tfnum);
  4220. return NULL;
  4221. }
  4222. /* Iterate through a traceframe's blocks, looking for a block of the
  4223. requested type. */
  4224. for (dataptr = database;
  4225. dataptr < database + datasize;
  4226. /* nothing */)
  4227. {
  4228. char blocktype;
  4229. unsigned short mlen;
  4230. if (dataptr == trace_buffer_wrap)
  4231. {
  4232. /* Adjust to reflect wrapping part of the frame around to
  4233. the beginning. */
  4234. datasize = dataptr - database;
  4235. dataptr = database = trace_buffer_lo;
  4236. }
  4237. blocktype = *dataptr++;
  4238. if ((*callback) (blocktype, dataptr, data))
  4239. return dataptr;
  4240. switch (blocktype)
  4241. {
  4242. case 'R':
  4243. /* Skip over the registers block. */
  4244. dataptr += current_target_desc ()->registers_size;
  4245. break;
  4246. case 'M':
  4247. /* Skip over the memory block. */
  4248. dataptr += sizeof (CORE_ADDR);
  4249. memcpy (&mlen, dataptr, sizeof (mlen));
  4250. dataptr += (sizeof (mlen) + mlen);
  4251. break;
  4252. case 'V':
  4253. /* Skip over the TSV block. */
  4254. dataptr += (sizeof (int) + sizeof (LONGEST));
  4255. break;
  4256. case 'S':
  4257. /* Skip over the static trace data block. */
  4258. memcpy (&mlen, dataptr, sizeof (mlen));
  4259. dataptr += (sizeof (mlen) + mlen);
  4260. break;
  4261. default:
  4262. trace_debug ("traceframe %d has unknown block type 0x%x",
  4263. tfnum, blocktype);
  4264. return NULL;
  4265. }
  4266. }
  4267. return NULL;
  4268. }
  4269. /* Look for the block of type TYPE_WANTED in the traceframe starting
  4270. at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
  4271. number. */
  4272. static unsigned char *
  4273. traceframe_find_block_type (unsigned char *database, unsigned int datasize,
  4274. int tfnum, char type_wanted)
  4275. {
  4276. return traceframe_walk_blocks (database, datasize, tfnum,
  4277. match_blocktype, &type_wanted);
  4278. }
  4279. static unsigned char *
  4280. traceframe_find_regblock (struct traceframe *tframe, int tfnum)
  4281. {
  4282. unsigned char *regblock;
  4283. regblock = traceframe_find_block_type (tframe->data,
  4284. tframe->data_size,
  4285. tfnum, 'R');
  4286. if (regblock == NULL)
  4287. trace_debug ("traceframe %d has no register data", tfnum);
  4288. return regblock;
  4289. }
  4290. /* Get registers from a traceframe. */
  4291. int
  4292. fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
  4293. {
  4294. unsigned char *dataptr;
  4295. struct tracepoint *tpoint;
  4296. struct traceframe *tframe;
  4297. tframe = find_traceframe (tfnum);
  4298. if (tframe == NULL)
  4299. {
  4300. trace_debug ("traceframe %d not found", tfnum);
  4301. return 1;
  4302. }
  4303. dataptr = traceframe_find_regblock (tframe, tfnum);
  4304. if (dataptr == NULL)
  4305. {
  4306. /* Mark registers unavailable. */
  4307. supply_regblock (regcache, NULL);
  4308. /* We can generally guess at a PC, although this will be
  4309. misleading for while-stepping frames and multi-location
  4310. tracepoints. */
  4311. tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
  4312. if (tpoint != NULL)
  4313. regcache_write_pc (regcache, tpoint->address);
  4314. }
  4315. else
  4316. supply_regblock (regcache, dataptr);
  4317. return 0;
  4318. }
  4319. static CORE_ADDR
  4320. traceframe_get_pc (struct traceframe *tframe)
  4321. {
  4322. struct regcache regcache;
  4323. unsigned char *dataptr;
  4324. const struct target_desc *tdesc = current_target_desc ();
  4325. dataptr = traceframe_find_regblock (tframe, -1);
  4326. if (dataptr == NULL)
  4327. return 0;
  4328. init_register_cache (&regcache, tdesc, dataptr);
  4329. return regcache_read_pc (&regcache);
  4330. }
  4331. /* Read a requested block of memory from a trace frame. */
  4332. int
  4333. traceframe_read_mem (int tfnum, CORE_ADDR addr,
  4334. unsigned char *buf, ULONGEST length,
  4335. ULONGEST *nbytes)
  4336. {
  4337. struct traceframe *tframe;
  4338. unsigned char *database, *dataptr;
  4339. unsigned int datasize;
  4340. CORE_ADDR maddr;
  4341. unsigned short mlen;
  4342. trace_debug ("traceframe_read_mem");
  4343. tframe = find_traceframe (tfnum);
  4344. if (!tframe)
  4345. {
  4346. trace_debug ("traceframe %d not found", tfnum);
  4347. return 1;
  4348. }
  4349. datasize = tframe->data_size;
  4350. database = dataptr = &tframe->data[0];
  4351. /* Iterate through a traceframe's blocks, looking for memory. */
  4352. while ((dataptr = traceframe_find_block_type (dataptr,
  4353. datasize
  4354. - (dataptr - database),
  4355. tfnum, 'M')) != NULL)
  4356. {
  4357. memcpy (&maddr, dataptr, sizeof (maddr));
  4358. dataptr += sizeof (maddr);
  4359. memcpy (&mlen, dataptr, sizeof (mlen));
  4360. dataptr += sizeof (mlen);
  4361. trace_debug ("traceframe %d has %d bytes at %s",
  4362. tfnum, mlen, paddress (maddr));
  4363. /* If the block includes the first part of the desired range,
  4364. return as much it has; GDB will re-request the remainder,
  4365. which might be in a different block of this trace frame. */
  4366. if (maddr <= addr && addr < (maddr + mlen))
  4367. {
  4368. ULONGEST amt = (maddr + mlen) - addr;
  4369. if (amt > length)
  4370. amt = length;
  4371. memcpy (buf, dataptr + (addr - maddr), amt);
  4372. *nbytes = amt;
  4373. return 0;
  4374. }
  4375. /* Skip over this block. */
  4376. dataptr += mlen;
  4377. }
  4378. trace_debug ("traceframe %d has no memory data for the desired region",
  4379. tfnum);
  4380. *nbytes = 0;
  4381. return 0;
  4382. }
  4383. static int
  4384. traceframe_read_tsv (int tsvnum, LONGEST *val)
  4385. {
  4386. client_state &cs = get_client_state ();
  4387. int tfnum;
  4388. struct traceframe *tframe;
  4389. unsigned char *database, *dataptr;
  4390. unsigned int datasize;
  4391. int vnum;
  4392. int found = 0;
  4393. trace_debug ("traceframe_read_tsv");
  4394. tfnum = cs.current_traceframe;
  4395. if (tfnum < 0)
  4396. {
  4397. trace_debug ("no current traceframe");
  4398. return 1;
  4399. }
  4400. tframe = find_traceframe (tfnum);
  4401. if (tframe == NULL)
  4402. {
  4403. trace_debug ("traceframe %d not found", tfnum);
  4404. return 1;
  4405. }
  4406. datasize = tframe->data_size;
  4407. database = dataptr = &tframe->data[0];
  4408. /* Iterate through a traceframe's blocks, looking for the last
  4409. matched tsv. */
  4410. while ((dataptr = traceframe_find_block_type (dataptr,
  4411. datasize
  4412. - (dataptr - database),
  4413. tfnum, 'V')) != NULL)
  4414. {
  4415. memcpy (&vnum, dataptr, sizeof (vnum));
  4416. dataptr += sizeof (vnum);
  4417. trace_debug ("traceframe %d has variable %d", tfnum, vnum);
  4418. /* Check that this is the variable we want. */
  4419. if (tsvnum == vnum)
  4420. {
  4421. memcpy (val, dataptr, sizeof (*val));
  4422. found = 1;
  4423. }
  4424. /* Skip over this block. */
  4425. dataptr += sizeof (LONGEST);
  4426. }
  4427. if (!found)
  4428. trace_debug ("traceframe %d has no data for variable %d",
  4429. tfnum, tsvnum);
  4430. return !found;
  4431. }
  4432. /* Read a requested block of static tracepoint data from a trace
  4433. frame. */
  4434. int
  4435. traceframe_read_sdata (int tfnum, ULONGEST offset,
  4436. unsigned char *buf, ULONGEST length,
  4437. ULONGEST *nbytes)
  4438. {
  4439. struct traceframe *tframe;
  4440. unsigned char *database, *dataptr;
  4441. unsigned int datasize;
  4442. unsigned short mlen;
  4443. trace_debug ("traceframe_read_sdata");
  4444. tframe = find_traceframe (tfnum);
  4445. if (!tframe)
  4446. {
  4447. trace_debug ("traceframe %d not found", tfnum);
  4448. return 1;
  4449. }
  4450. datasize = tframe->data_size;
  4451. database = &tframe->data[0];
  4452. /* Iterate through a traceframe's blocks, looking for static
  4453. tracepoint data. */
  4454. dataptr = traceframe_find_block_type (database, datasize,
  4455. tfnum, 'S');
  4456. if (dataptr != NULL)
  4457. {
  4458. memcpy (&mlen, dataptr, sizeof (mlen));
  4459. dataptr += sizeof (mlen);
  4460. if (offset < mlen)
  4461. {
  4462. if (offset + length > mlen)
  4463. length = mlen - offset;
  4464. memcpy (buf, dataptr, length);
  4465. *nbytes = length;
  4466. }
  4467. else
  4468. *nbytes = 0;
  4469. return 0;
  4470. }
  4471. trace_debug ("traceframe %d has no static trace data", tfnum);
  4472. *nbytes = 0;
  4473. return 0;
  4474. }
  4475. /* Callback for traceframe_walk_blocks. Builds a traceframe-info
  4476. object. DATA is pointer to a struct buffer holding the
  4477. traceframe-info object being built. */
  4478. static int
  4479. build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
  4480. {
  4481. struct buffer *buffer = (struct buffer *) data;
  4482. switch (blocktype)
  4483. {
  4484. case 'M':
  4485. {
  4486. unsigned short mlen;
  4487. CORE_ADDR maddr;
  4488. memcpy (&maddr, dataptr, sizeof (maddr));
  4489. dataptr += sizeof (maddr);
  4490. memcpy (&mlen, dataptr, sizeof (mlen));
  4491. dataptr += sizeof (mlen);
  4492. buffer_xml_printf (buffer,
  4493. "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
  4494. paddress (maddr), phex_nz (mlen, sizeof (mlen)));
  4495. break;
  4496. }
  4497. case 'V':
  4498. {
  4499. int vnum;
  4500. memcpy (&vnum, dataptr, sizeof (vnum));
  4501. buffer_xml_printf (buffer, "<tvar id=\"%d\"/>\n", vnum);
  4502. break;
  4503. }
  4504. case 'R':
  4505. case 'S':
  4506. {
  4507. break;
  4508. }
  4509. default:
  4510. warning ("Unhandled trace block type (%d) '%c ' "
  4511. "while building trace frame info.",
  4512. blocktype, blocktype);
  4513. break;
  4514. }
  4515. return 0;
  4516. }
  4517. /* Build a traceframe-info object for traceframe number TFNUM into
  4518. BUFFER. */
  4519. int
  4520. traceframe_read_info (int tfnum, struct buffer *buffer)
  4521. {
  4522. struct traceframe *tframe;
  4523. trace_debug ("traceframe_read_info");
  4524. tframe = find_traceframe (tfnum);
  4525. if (!tframe)
  4526. {
  4527. trace_debug ("traceframe %d not found", tfnum);
  4528. return 1;
  4529. }
  4530. buffer_grow_str (buffer, "<traceframe-info>\n");
  4531. traceframe_walk_blocks (tframe->data, tframe->data_size,
  4532. tfnum, build_traceframe_info_xml, buffer);
  4533. buffer_grow_str0 (buffer, "</traceframe-info>\n");
  4534. return 0;
  4535. }
  4536. /* Return the first fast tracepoint whose jump pad contains PC. */
  4537. static struct tracepoint *
  4538. fast_tracepoint_from_jump_pad_address (CORE_ADDR pc)
  4539. {
  4540. struct tracepoint *tpoint;
  4541. for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
  4542. if (tpoint->type == fast_tracepoint)
  4543. if (tpoint->jump_pad <= pc && pc < tpoint->jump_pad_end)
  4544. return tpoint;
  4545. return NULL;
  4546. }
  4547. /* Return the first fast tracepoint whose trampoline contains PC. */
  4548. static struct tracepoint *
  4549. fast_tracepoint_from_trampoline_address (CORE_ADDR pc)
  4550. {
  4551. struct tracepoint *tpoint;
  4552. for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
  4553. {
  4554. if (tpoint->type == fast_tracepoint
  4555. && tpoint->trampoline <= pc && pc < tpoint->trampoline_end)
  4556. return tpoint;
  4557. }
  4558. return NULL;
  4559. }
  4560. /* Return GDBserver's tracepoint that matches the IP Agent's
  4561. tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
  4562. address space. */
  4563. static struct tracepoint *
  4564. fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj)
  4565. {
  4566. struct tracepoint *tpoint;
  4567. for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
  4568. if (tpoint->type == fast_tracepoint)
  4569. if (tpoint->obj_addr_on_target == ipa_tpoint_obj)
  4570. return tpoint;
  4571. return NULL;
  4572. }
  4573. #endif
  4574. /* The type of the object that is used to synchronize fast tracepoint
  4575. collection. */
  4576. typedef struct collecting_t
  4577. {
  4578. /* The fast tracepoint number currently collecting. */
  4579. uintptr_t tpoint;
  4580. /* A number that GDBserver can use to identify the thread that is
  4581. presently holding the collect lock. This need not (and usually
  4582. is not) the thread id, as getting the current thread ID usually
  4583. requires a system call, which we want to avoid like the plague.
  4584. Usually this is thread's TCB, found in the TLS (pseudo-)
  4585. register, which is readable with a single insn on several
  4586. architectures. */
  4587. uintptr_t thread_area;
  4588. } collecting_t;
  4589. #ifndef IN_PROCESS_AGENT
  4590. void
  4591. force_unlock_trace_buffer (void)
  4592. {
  4593. write_inferior_data_pointer (ipa_sym_addrs.addr_collecting, 0);
  4594. }
  4595. /* Check if the thread identified by THREAD_AREA which is stopped at
  4596. STOP_PC, is presently locking the fast tracepoint collection, and
  4597. if so, gather some status of said collection. Returns 0 if the
  4598. thread isn't collecting or in the jump pad at all. 1, if in the
  4599. jump pad (or within gdb_collect) and hasn't executed the adjusted
  4600. original insn yet (can set a breakpoint there and run to it). 2,
  4601. if presently executing the adjusted original insn --- in which
  4602. case, if we want to move the thread out of the jump pad, we need to
  4603. single-step it until this function returns 0. */
  4604. fast_tpoint_collect_result
  4605. fast_tracepoint_collecting (CORE_ADDR thread_area,
  4606. CORE_ADDR stop_pc,
  4607. struct fast_tpoint_collect_status *status)
  4608. {
  4609. CORE_ADDR ipa_collecting;
  4610. CORE_ADDR ipa_gdb_jump_pad_buffer, ipa_gdb_jump_pad_buffer_end;
  4611. CORE_ADDR ipa_gdb_trampoline_buffer;
  4612. CORE_ADDR ipa_gdb_trampoline_buffer_end;
  4613. struct tracepoint *tpoint;
  4614. int needs_breakpoint;
  4615. /* The thread THREAD_AREA is either:
  4616. 0. not collecting at all, not within the jump pad, or within
  4617. gdb_collect or one of its callees.
  4618. 1. in the jump pad and haven't reached gdb_collect
  4619. 2. within gdb_collect (out of the jump pad) (collect is set)
  4620. 3. we're in the jump pad, after gdb_collect having returned,
  4621. possibly executing the adjusted insns.
  4622. For cases 1 and 3, `collecting' may or not be set. The jump pad
  4623. doesn't have any complicated jump logic, so we can tell if the
  4624. thread is executing the adjust original insn or not by just
  4625. matching STOP_PC with known jump pad addresses. If we it isn't
  4626. yet executing the original insn, set a breakpoint there, and let
  4627. the thread run to it, so to quickly step over a possible (many
  4628. insns) gdb_collect call. Otherwise, or when the breakpoint is
  4629. hit, only a few (small number of) insns are left to be executed
  4630. in the jump pad. Single-step the thread until it leaves the
  4631. jump pad. */
  4632. again:
  4633. tpoint = NULL;
  4634. needs_breakpoint = 0;
  4635. trace_debug ("fast_tracepoint_collecting");
  4636. if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
  4637. &ipa_gdb_jump_pad_buffer))
  4638. {
  4639. internal_error (__FILE__, __LINE__,
  4640. "error extracting `gdb_jump_pad_buffer'");
  4641. }
  4642. if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer_end,
  4643. &ipa_gdb_jump_pad_buffer_end))
  4644. {
  4645. internal_error (__FILE__, __LINE__,
  4646. "error extracting `gdb_jump_pad_buffer_end'");
  4647. }
  4648. if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
  4649. &ipa_gdb_trampoline_buffer))
  4650. {
  4651. internal_error (__FILE__, __LINE__,
  4652. "error extracting `gdb_trampoline_buffer'");
  4653. }
  4654. if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
  4655. &ipa_gdb_trampoline_buffer_end))
  4656. {
  4657. internal_error (__FILE__, __LINE__,
  4658. "error extracting `gdb_trampoline_buffer_end'");
  4659. }
  4660. if (ipa_gdb_jump_pad_buffer <= stop_pc
  4661. && stop_pc < ipa_gdb_jump_pad_buffer_end)
  4662. {
  4663. /* We can tell which tracepoint(s) the thread is collecting by
  4664. matching the jump pad address back to the tracepoint. */
  4665. tpoint = fast_tracepoint_from_jump_pad_address (stop_pc);
  4666. if (tpoint == NULL)
  4667. {
  4668. warning ("in jump pad, but no matching tpoint?");
  4669. return fast_tpoint_collect_result::not_collecting;
  4670. }
  4671. else
  4672. {
  4673. trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
  4674. "adj_insn(%s, %s)",
  4675. tpoint->number, paddress (tpoint->address),
  4676. paddress (tpoint->jump_pad),
  4677. paddress (tpoint->jump_pad_end),
  4678. paddress (tpoint->adjusted_insn_addr),
  4679. paddress (tpoint->adjusted_insn_addr_end));
  4680. }
  4681. /* Definitely in the jump pad. May or may not need
  4682. fast-exit-jump-pad breakpoint. */
  4683. if (tpoint->jump_pad <= stop_pc
  4684. && stop_pc < tpoint->adjusted_insn_addr)
  4685. needs_breakpoint = 1;
  4686. }
  4687. else if (ipa_gdb_trampoline_buffer <= stop_pc
  4688. && stop_pc < ipa_gdb_trampoline_buffer_end)
  4689. {
  4690. /* We can tell which tracepoint(s) the thread is collecting by
  4691. matching the trampoline address back to the tracepoint. */
  4692. tpoint = fast_tracepoint_from_trampoline_address (stop_pc);
  4693. if (tpoint == NULL)
  4694. {
  4695. warning ("in trampoline, but no matching tpoint?");
  4696. return fast_tpoint_collect_result::not_collecting;
  4697. }
  4698. else
  4699. {
  4700. trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
  4701. tpoint->number, paddress (tpoint->address),
  4702. paddress (tpoint->trampoline),
  4703. paddress (tpoint->trampoline_end));
  4704. }
  4705. /* Have not reached jump pad yet, but treat the trampoline as a
  4706. part of the jump pad that is before the adjusted original
  4707. instruction. */
  4708. needs_breakpoint = 1;
  4709. }
  4710. else
  4711. {
  4712. collecting_t ipa_collecting_obj;
  4713. /* If `collecting' is set/locked, then the THREAD_AREA thread
  4714. may or not be the one holding the lock. We have to read the
  4715. lock to find out. */
  4716. if (read_inferior_data_pointer (ipa_sym_addrs.addr_collecting,
  4717. &ipa_collecting))
  4718. {
  4719. trace_debug ("fast_tracepoint_collecting:"
  4720. " failed reading 'collecting' in the inferior");
  4721. return fast_tpoint_collect_result::not_collecting;
  4722. }
  4723. if (!ipa_collecting)
  4724. {
  4725. trace_debug ("fast_tracepoint_collecting: not collecting"
  4726. " (and nobody is).");
  4727. return fast_tpoint_collect_result::not_collecting;
  4728. }
  4729. /* Some thread is collecting. Check which. */
  4730. if (read_inferior_memory (ipa_collecting,
  4731. (unsigned char *) &ipa_collecting_obj,
  4732. sizeof (ipa_collecting_obj)) != 0)
  4733. goto again;
  4734. if (ipa_collecting_obj.thread_area != thread_area)
  4735. {
  4736. trace_debug ("fast_tracepoint_collecting: not collecting "
  4737. "(another thread is)");
  4738. return fast_tpoint_collect_result::not_collecting;
  4739. }
  4740. tpoint
  4741. = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj.tpoint);
  4742. if (tpoint == NULL)
  4743. {
  4744. warning ("fast_tracepoint_collecting: collecting, "
  4745. "but tpoint %s not found?",
  4746. paddress ((CORE_ADDR) ipa_collecting_obj.tpoint));
  4747. return fast_tpoint_collect_result::not_collecting;
  4748. }
  4749. /* The thread is within `gdb_collect', skip over the rest of
  4750. fast tracepoint collection quickly using a breakpoint. */
  4751. needs_breakpoint = 1;
  4752. }
  4753. /* The caller wants a bit of status detail. */
  4754. if (status != NULL)
  4755. {
  4756. status->tpoint_num = tpoint->number;
  4757. status->tpoint_addr = tpoint->address;
  4758. status->adjusted_insn_addr = tpoint->adjusted_insn_addr;
  4759. status->adjusted_insn_addr_end = tpoint->adjusted_insn_addr_end;
  4760. }
  4761. if (needs_breakpoint)
  4762. {
  4763. /* Hasn't executed the original instruction yet. Set breakpoint
  4764. there, and wait till it's hit, then single-step until exiting
  4765. the jump pad. */
  4766. trace_debug ("\
  4767. fast_tracepoint_collecting, returning continue-until-break at %s",
  4768. paddress (tpoint->adjusted_insn_addr));
  4769. return fast_tpoint_collect_result::before_insn; /* continue */
  4770. }
  4771. else
  4772. {
  4773. /* Just single-step until exiting the jump pad. */
  4774. trace_debug ("fast_tracepoint_collecting, returning "
  4775. "need-single-step (%s-%s)",
  4776. paddress (tpoint->adjusted_insn_addr),
  4777. paddress (tpoint->adjusted_insn_addr_end));
  4778. return fast_tpoint_collect_result::at_insn; /* single-step */
  4779. }
  4780. }
  4781. #endif
  4782. #ifdef IN_PROCESS_AGENT
  4783. /* The global fast tracepoint collect lock. Points to a collecting_t
  4784. object built on the stack by the jump pad, if presently locked;
  4785. NULL if it isn't locked. Note that this lock *must* be set while
  4786. executing any *function other than the jump pad. See
  4787. fast_tracepoint_collecting. */
  4788. EXTERN_C_PUSH
  4789. IP_AGENT_EXPORT_VAR collecting_t *collecting;
  4790. EXTERN_C_POP
  4791. /* This is needed for -Wmissing-declarations. */
  4792. IP_AGENT_EXPORT_FUNC void gdb_collect (struct tracepoint *tpoint,
  4793. unsigned char *regs);
  4794. /* This routine, called from the jump pad (in asm) is designed to be
  4795. called from the jump pads of fast tracepoints, thus it is on the
  4796. critical path. */
  4797. IP_AGENT_EXPORT_FUNC void
  4798. gdb_collect (struct tracepoint *tpoint, unsigned char *regs)
  4799. {
  4800. struct fast_tracepoint_ctx ctx;
  4801. const struct target_desc *ipa_tdesc;
  4802. /* Don't do anything until the trace run is completely set up. */
  4803. if (!tracing)
  4804. return;
  4805. ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx);
  4806. ctx.base.type = fast_tracepoint;
  4807. ctx.regs = regs;
  4808. ctx.regcache_initted = 0;
  4809. /* Wrap the regblock in a register cache (in the stack, we don't
  4810. want to malloc here). */
  4811. ctx.regspace = (unsigned char *) alloca (ipa_tdesc->registers_size);
  4812. if (ctx.regspace == NULL)
  4813. {
  4814. trace_debug ("Trace buffer block allocation failed, skipping");
  4815. return;
  4816. }
  4817. for (ctx.tpoint = tpoint;
  4818. ctx.tpoint != NULL && ctx.tpoint->address == tpoint->address;
  4819. ctx.tpoint = ctx.tpoint->next)
  4820. {
  4821. if (!ctx.tpoint->enabled)
  4822. continue;
  4823. /* Multiple tracepoints of different types, such as fast tracepoint and
  4824. static tracepoint, can be set at the same address. */
  4825. if (ctx.tpoint->type != tpoint->type)
  4826. continue;
  4827. /* Test the condition if present, and collect if true. */
  4828. if (ctx.tpoint->cond == NULL
  4829. || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
  4830. ctx.tpoint))
  4831. {
  4832. collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
  4833. ctx.tpoint->address, ctx.tpoint);
  4834. /* Note that this will cause original insns to be written back
  4835. to where we jumped from, but that's OK because we're jumping
  4836. back to the next whole instruction. This will go badly if
  4837. instruction restoration is not atomic though. */
  4838. if (stopping_tracepoint
  4839. || trace_buffer_is_full
  4840. || expr_eval_result != expr_eval_no_error)
  4841. {
  4842. stop_tracing ();
  4843. break;
  4844. }
  4845. }
  4846. else
  4847. {
  4848. /* If there was a condition and it evaluated to false, the only
  4849. way we would stop tracing is if there was an error during
  4850. condition expression evaluation. */
  4851. if (expr_eval_result != expr_eval_no_error)
  4852. {
  4853. stop_tracing ();
  4854. break;
  4855. }
  4856. }
  4857. }
  4858. }
  4859. /* These global variables points to the corresponding functions. This is
  4860. necessary on powerpc64, where asking for function symbol address from gdb
  4861. results in returning the actual code pointer, instead of the descriptor
  4862. pointer. */
  4863. typedef void (*gdb_collect_ptr_type) (struct tracepoint *, unsigned char *);
  4864. typedef ULONGEST (*get_raw_reg_ptr_type) (const unsigned char *, int);
  4865. typedef LONGEST (*get_trace_state_variable_value_ptr_type) (int);
  4866. typedef void (*set_trace_state_variable_value_ptr_type) (int, LONGEST);
  4867. EXTERN_C_PUSH
  4868. IP_AGENT_EXPORT_VAR gdb_collect_ptr_type gdb_collect_ptr = gdb_collect;
  4869. IP_AGENT_EXPORT_VAR get_raw_reg_ptr_type get_raw_reg_ptr = get_raw_reg;
  4870. IP_AGENT_EXPORT_VAR get_trace_state_variable_value_ptr_type
  4871. get_trace_state_variable_value_ptr = get_trace_state_variable_value;
  4872. IP_AGENT_EXPORT_VAR set_trace_state_variable_value_ptr_type
  4873. set_trace_state_variable_value_ptr = set_trace_state_variable_value;
  4874. EXTERN_C_POP
  4875. #endif
  4876. #ifndef IN_PROCESS_AGENT
  4877. CORE_ADDR
  4878. get_raw_reg_func_addr (void)
  4879. {
  4880. CORE_ADDR res;
  4881. if (read_inferior_data_pointer (ipa_sym_addrs.addr_get_raw_reg_ptr, &res))
  4882. {
  4883. error ("error extracting get_raw_reg_ptr");
  4884. return 0;
  4885. }
  4886. return res;
  4887. }
  4888. CORE_ADDR
  4889. get_get_tsv_func_addr (void)
  4890. {
  4891. CORE_ADDR res;
  4892. if (read_inferior_data_pointer (
  4893. ipa_sym_addrs.addr_get_trace_state_variable_value_ptr, &res))
  4894. {
  4895. error ("error extracting get_trace_state_variable_value_ptr");
  4896. return 0;
  4897. }
  4898. return res;
  4899. }
  4900. CORE_ADDR
  4901. get_set_tsv_func_addr (void)
  4902. {
  4903. CORE_ADDR res;
  4904. if (read_inferior_data_pointer (
  4905. ipa_sym_addrs.addr_set_trace_state_variable_value_ptr, &res))
  4906. {
  4907. error ("error extracting set_trace_state_variable_value_ptr");
  4908. return 0;
  4909. }
  4910. return res;
  4911. }
  4912. static void
  4913. compile_tracepoint_condition (struct tracepoint *tpoint,
  4914. CORE_ADDR *jump_entry)
  4915. {
  4916. CORE_ADDR entry_point = *jump_entry;
  4917. enum eval_result_type err;
  4918. trace_debug ("Starting condition compilation for tracepoint %d\n",
  4919. tpoint->number);
  4920. /* Initialize the global pointer to the code being built. */
  4921. current_insn_ptr = *jump_entry;
  4922. emit_prologue ();
  4923. err = compile_bytecodes (tpoint->cond);
  4924. if (err == expr_eval_no_error)
  4925. {
  4926. emit_epilogue ();
  4927. /* Record the beginning of the compiled code. */
  4928. tpoint->compiled_cond = entry_point;
  4929. trace_debug ("Condition compilation for tracepoint %d complete\n",
  4930. tpoint->number);
  4931. }
  4932. else
  4933. {
  4934. /* Leave the unfinished code in situ, but don't point to it. */
  4935. tpoint->compiled_cond = 0;
  4936. trace_debug ("Condition compilation for tracepoint %d failed, "
  4937. "error code %d",
  4938. tpoint->number, err);
  4939. }
  4940. /* Update the code pointer passed in. Note that we do this even if
  4941. the compile fails, so that we can look at the partial results
  4942. instead of letting them be overwritten. */
  4943. *jump_entry = current_insn_ptr;
  4944. /* Leave a gap, to aid dump decipherment. */
  4945. *jump_entry += 16;
  4946. }
  4947. /* The base pointer of the IPA's heap. This is the only memory the
  4948. IPA is allowed to use. The IPA should _not_ call the inferior's
  4949. `malloc' during operation. That'd be slow, and, most importantly,
  4950. it may not be safe. We may be collecting a tracepoint in a signal
  4951. handler, for example. */
  4952. static CORE_ADDR target_tp_heap;
  4953. /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
  4954. to 8 bytes. */
  4955. static CORE_ADDR
  4956. target_malloc (ULONGEST size)
  4957. {
  4958. CORE_ADDR ptr;
  4959. if (target_tp_heap == 0)
  4960. {
  4961. /* We have the pointer *address*, need what it points to. */
  4962. if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_tp_heap_buffer,
  4963. &target_tp_heap))
  4964. {
  4965. internal_error (__FILE__, __LINE__,
  4966. "couldn't get target heap head pointer");
  4967. }
  4968. }
  4969. ptr = target_tp_heap;
  4970. target_tp_heap += size;
  4971. /* Pad to 8-byte alignment. */
  4972. target_tp_heap = ((target_tp_heap + 7) & ~0x7);
  4973. return ptr;
  4974. }
  4975. static CORE_ADDR
  4976. download_agent_expr (struct agent_expr *expr)
  4977. {
  4978. CORE_ADDR expr_addr;
  4979. CORE_ADDR expr_bytes;
  4980. expr_addr = target_malloc (sizeof (*expr));
  4981. target_write_memory (expr_addr, (unsigned char *) expr, sizeof (*expr));
  4982. expr_bytes = target_malloc (expr->length);
  4983. write_inferior_data_pointer (expr_addr + offsetof (struct agent_expr, bytes),
  4984. expr_bytes);
  4985. target_write_memory (expr_bytes, expr->bytes, expr->length);
  4986. return expr_addr;
  4987. }
  4988. /* Align V up to N bits. */
  4989. #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
  4990. /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
  4991. static void
  4992. download_tracepoint_1 (struct tracepoint *tpoint)
  4993. {
  4994. struct tracepoint target_tracepoint;
  4995. CORE_ADDR tpptr = 0;
  4996. gdb_assert (tpoint->type == fast_tracepoint
  4997. || tpoint->type == static_tracepoint);
  4998. if (tpoint->cond != NULL && target_emit_ops () != NULL)
  4999. {
  5000. CORE_ADDR jentry, jump_entry;
  5001. jentry = jump_entry = get_jump_space_head ();
  5002. if (tpoint->cond != NULL)
  5003. {
  5004. /* Pad to 8-byte alignment. (needed?) */
  5005. /* Actually this should be left for the target to
  5006. decide. */
  5007. jentry = UALIGN (jentry, 8);
  5008. compile_tracepoint_condition (tpoint, &jentry);
  5009. }
  5010. /* Pad to 8-byte alignment. */
  5011. jentry = UALIGN (jentry, 8);
  5012. claim_jump_space (jentry - jump_entry);
  5013. }
  5014. target_tracepoint = *tpoint;
  5015. tpptr = target_malloc (sizeof (*tpoint));
  5016. tpoint->obj_addr_on_target = tpptr;
  5017. /* Write the whole object. We'll fix up its pointers in a bit.
  5018. Assume no next for now. This is fixed up above on the next
  5019. iteration, if there's any. */
  5020. target_tracepoint.next = NULL;
  5021. /* Need to clear this here too, since we're downloading the
  5022. tracepoints before clearing our own copy. */
  5023. target_tracepoint.hit_count = 0;
  5024. target_write_memory (tpptr, (unsigned char *) &target_tracepoint,
  5025. sizeof (target_tracepoint));
  5026. if (tpoint->cond)
  5027. write_inferior_data_pointer (tpptr
  5028. + offsetof (struct tracepoint, cond),
  5029. download_agent_expr (tpoint->cond));
  5030. if (tpoint->numactions)
  5031. {
  5032. int i;
  5033. CORE_ADDR actions_array;
  5034. /* The pointers array. */
  5035. actions_array
  5036. = target_malloc (sizeof (*tpoint->actions) * tpoint->numactions);
  5037. write_inferior_data_pointer (tpptr + offsetof (struct tracepoint,
  5038. actions),
  5039. actions_array);
  5040. /* Now for each pointer, download the action. */
  5041. for (i = 0; i < tpoint->numactions; i++)
  5042. {
  5043. struct tracepoint_action *action = tpoint->actions[i];
  5044. CORE_ADDR ipa_action = tracepoint_action_download (action);
  5045. if (ipa_action != 0)
  5046. write_inferior_data_pointer (actions_array
  5047. + i * sizeof (*tpoint->actions),
  5048. ipa_action);
  5049. }
  5050. }
  5051. }
  5052. #define IPA_PROTO_FAST_TRACE_FLAG 0
  5053. #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
  5054. #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
  5055. #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
  5056. #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
  5057. /* Send a command to agent to download and install tracepoint TPOINT. */
  5058. static int
  5059. tracepoint_send_agent (struct tracepoint *tpoint)
  5060. {
  5061. char buf[IPA_CMD_BUF_SIZE];
  5062. char *p;
  5063. int i, ret;
  5064. p = buf;
  5065. strcpy (p, "FastTrace:");
  5066. p += 10;
  5067. COPY_FIELD_TO_BUF (p, tpoint, number);
  5068. COPY_FIELD_TO_BUF (p, tpoint, address);
  5069. COPY_FIELD_TO_BUF (p, tpoint, type);
  5070. COPY_FIELD_TO_BUF (p, tpoint, enabled);
  5071. COPY_FIELD_TO_BUF (p, tpoint, step_count);
  5072. COPY_FIELD_TO_BUF (p, tpoint, pass_count);
  5073. COPY_FIELD_TO_BUF (p, tpoint, numactions);
  5074. COPY_FIELD_TO_BUF (p, tpoint, hit_count);
  5075. COPY_FIELD_TO_BUF (p, tpoint, traceframe_usage);
  5076. COPY_FIELD_TO_BUF (p, tpoint, compiled_cond);
  5077. COPY_FIELD_TO_BUF (p, tpoint, orig_size);
  5078. /* condition */
  5079. p = agent_expr_send (p, tpoint->cond);
  5080. /* tracepoint_action */
  5081. for (i = 0; i < tpoint->numactions; i++)
  5082. {
  5083. struct tracepoint_action *action = tpoint->actions[i];
  5084. p[0] = action->type;
  5085. p = tracepoint_action_send (&p[1], action);
  5086. }
  5087. get_jump_space_head ();
  5088. /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
  5089. agent can use jump pad from it. */
  5090. if (tpoint->type == fast_tracepoint)
  5091. {
  5092. memcpy (p, &gdb_jump_pad_head, 8);
  5093. p += 8;
  5094. }
  5095. ret = run_inferior_command (buf, (int) (ptrdiff_t) (p - buf));
  5096. if (ret)
  5097. return ret;
  5098. if (!startswith (buf, "OK"))
  5099. return 1;
  5100. /* The value of tracepoint's target address is stored in BUF. */
  5101. memcpy (&tpoint->obj_addr_on_target,
  5102. &buf[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET], 8);
  5103. if (tpoint->type == fast_tracepoint)
  5104. {
  5105. unsigned char *insn
  5106. = (unsigned char *) &buf[IPA_PROTO_FAST_TRACE_FJUMP_INSN];
  5107. int fjump_size;
  5108. trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
  5109. (unsigned int) tpoint->obj_addr_on_target,
  5110. (unsigned int) gdb_jump_pad_head);
  5111. memcpy (&gdb_jump_pad_head, &buf[IPA_PROTO_FAST_TRACE_JUMP_PAD], 8);
  5112. /* This has been done in agent. We should also set up record for it. */
  5113. memcpy (&fjump_size, &buf[IPA_PROTO_FAST_TRACE_FJUMP_SIZE], 4);
  5114. /* Wire it in. */
  5115. tpoint->handle
  5116. = set_fast_tracepoint_jump (tpoint->address, insn, fjump_size);
  5117. }
  5118. return 0;
  5119. }
  5120. static void
  5121. download_tracepoint (struct tracepoint *tpoint)
  5122. {
  5123. struct tracepoint *tp, *tp_prev;
  5124. if (tpoint->type != fast_tracepoint
  5125. && tpoint->type != static_tracepoint)
  5126. return;
  5127. download_tracepoint_1 (tpoint);
  5128. /* Find the previous entry of TPOINT, which is fast tracepoint or
  5129. static tracepoint. */
  5130. tp_prev = NULL;
  5131. for (tp = tracepoints; tp != tpoint; tp = tp->next)
  5132. {
  5133. if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
  5134. tp_prev = tp;
  5135. }
  5136. if (tp_prev)
  5137. {
  5138. CORE_ADDR tp_prev_target_next_addr;
  5139. /* Insert TPOINT after TP_PREV in IPA. */
  5140. if (read_inferior_data_pointer (tp_prev->obj_addr_on_target
  5141. + offsetof (struct tracepoint, next),
  5142. &tp_prev_target_next_addr))
  5143. {
  5144. internal_error (__FILE__, __LINE__,
  5145. "error reading `tp_prev->next'");
  5146. }
  5147. /* tpoint->next = tp_prev->next */
  5148. write_inferior_data_pointer (tpoint->obj_addr_on_target
  5149. + offsetof (struct tracepoint, next),
  5150. tp_prev_target_next_addr);
  5151. /* tp_prev->next = tpoint */
  5152. write_inferior_data_pointer (tp_prev->obj_addr_on_target
  5153. + offsetof (struct tracepoint, next),
  5154. tpoint->obj_addr_on_target);
  5155. }
  5156. else
  5157. /* First object in list, set the head pointer in the
  5158. inferior. */
  5159. write_inferior_data_pointer (ipa_sym_addrs.addr_tracepoints,
  5160. tpoint->obj_addr_on_target);
  5161. }
  5162. static void
  5163. download_trace_state_variables (void)
  5164. {
  5165. CORE_ADDR ptr = 0, prev_ptr = 0;
  5166. struct trace_state_variable *tsv;
  5167. /* Start out empty. */
  5168. write_inferior_data_pointer (ipa_sym_addrs.addr_trace_state_variables, 0);
  5169. for (tsv = trace_state_variables; tsv != NULL; tsv = tsv->next)
  5170. {
  5171. struct trace_state_variable target_tsv;
  5172. /* TSV's with a getter have been initialized equally in both the
  5173. inferior and GDBserver. Skip them. */
  5174. if (tsv->getter != NULL)
  5175. continue;
  5176. target_tsv = *tsv;
  5177. prev_ptr = ptr;
  5178. ptr = target_malloc (sizeof (*tsv));
  5179. if (tsv == trace_state_variables)
  5180. {
  5181. /* First object in list, set the head pointer in the
  5182. inferior. */
  5183. write_inferior_data_pointer (ipa_sym_addrs.addr_trace_state_variables,
  5184. ptr);
  5185. }
  5186. else
  5187. {
  5188. write_inferior_data_pointer (prev_ptr
  5189. + offsetof (struct trace_state_variable,
  5190. next),
  5191. ptr);
  5192. }
  5193. /* Write the whole object. We'll fix up its pointers in a bit.
  5194. Assume no next, fixup when needed. */
  5195. target_tsv.next = NULL;
  5196. target_write_memory (ptr, (unsigned char *) &target_tsv,
  5197. sizeof (target_tsv));
  5198. if (tsv->name != NULL)
  5199. {
  5200. size_t size = strlen (tsv->name) + 1;
  5201. CORE_ADDR name_addr = target_malloc (size);
  5202. target_write_memory (name_addr,
  5203. (unsigned char *) tsv->name, size);
  5204. write_inferior_data_pointer (ptr
  5205. + offsetof (struct trace_state_variable,
  5206. name),
  5207. name_addr);
  5208. }
  5209. gdb_assert (tsv->getter == NULL);
  5210. }
  5211. if (prev_ptr != 0)
  5212. {
  5213. /* Fixup the next pointer in the last item in the list. */
  5214. write_inferior_data_pointer (prev_ptr
  5215. + offsetof (struct trace_state_variable,
  5216. next), 0);
  5217. }
  5218. }
  5219. /* Upload complete trace frames out of the IP Agent's trace buffer
  5220. into GDBserver's trace buffer. This always uploads either all or
  5221. no trace frames. This is the counter part of
  5222. `trace_alloc_trace_buffer'. See its description of the atomic
  5223. syncing mechanism. */
  5224. static void
  5225. upload_fast_traceframes (void)
  5226. {
  5227. unsigned int ipa_traceframe_read_count, ipa_traceframe_write_count;
  5228. unsigned int ipa_traceframe_read_count_racy, ipa_traceframe_write_count_racy;
  5229. CORE_ADDR tf;
  5230. struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
  5231. unsigned int curr_tbctrl_idx;
  5232. unsigned int ipa_trace_buffer_ctrl_curr;
  5233. unsigned int ipa_trace_buffer_ctrl_curr_old;
  5234. CORE_ADDR ipa_trace_buffer_ctrl_addr;
  5235. struct breakpoint *about_to_request_buffer_space_bkpt;
  5236. CORE_ADDR ipa_trace_buffer_lo;
  5237. CORE_ADDR ipa_trace_buffer_hi;
  5238. if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
  5239. &ipa_traceframe_read_count_racy))
  5240. {
  5241. /* This will happen in most targets if the current thread is
  5242. running. */
  5243. return;
  5244. }
  5245. if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
  5246. &ipa_traceframe_write_count_racy))
  5247. return;
  5248. trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
  5249. ipa_traceframe_write_count_racy
  5250. - ipa_traceframe_read_count_racy,
  5251. ipa_traceframe_write_count_racy,
  5252. ipa_traceframe_read_count_racy);
  5253. if (ipa_traceframe_write_count_racy == ipa_traceframe_read_count_racy)
  5254. return;
  5255. about_to_request_buffer_space_bkpt
  5256. = set_breakpoint_at (ipa_sym_addrs.addr_about_to_request_buffer_space,
  5257. NULL);
  5258. if (read_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
  5259. &ipa_trace_buffer_ctrl_curr))
  5260. return;
  5261. ipa_trace_buffer_ctrl_curr_old = ipa_trace_buffer_ctrl_curr;
  5262. curr_tbctrl_idx = ipa_trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK;
  5263. {
  5264. unsigned int prev, counter;
  5265. /* Update the token, with new counters, and the GDBserver stamp
  5266. bit. Alway reuse the current TBC index. */
  5267. prev = ipa_trace_buffer_ctrl_curr & GDBSERVER_FLUSH_COUNT_MASK_CURR;
  5268. counter = (prev + 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR;
  5269. ipa_trace_buffer_ctrl_curr = (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
  5270. | (prev << 12)
  5271. | counter
  5272. | curr_tbctrl_idx);
  5273. }
  5274. if (write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
  5275. ipa_trace_buffer_ctrl_curr))
  5276. return;
  5277. trace_debug ("Lib: Committed %08x -> %08x",
  5278. ipa_trace_buffer_ctrl_curr_old,
  5279. ipa_trace_buffer_ctrl_curr);
  5280. /* Re-read these, now that we've installed the
  5281. `about_to_request_buffer_space' breakpoint/lock. A thread could
  5282. have finished a traceframe between the last read of these
  5283. counters and setting the breakpoint above. If we start
  5284. uploading, we never want to leave this function with
  5285. traceframe_read_count != 0, otherwise, GDBserver could end up
  5286. incrementing the counter tokens more than once (due to event loop
  5287. nesting), which would break the IP agent's "effective" detection
  5288. (see trace_alloc_trace_buffer). */
  5289. if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
  5290. &ipa_traceframe_read_count))
  5291. return;
  5292. if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
  5293. &ipa_traceframe_write_count))
  5294. return;
  5295. if (debug_threads)
  5296. {
  5297. trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
  5298. ipa_traceframe_write_count - ipa_traceframe_read_count,
  5299. ipa_traceframe_write_count, ipa_traceframe_read_count);
  5300. if (ipa_traceframe_write_count != ipa_traceframe_write_count_racy
  5301. || ipa_traceframe_read_count != ipa_traceframe_read_count_racy)
  5302. trace_debug ("note that ipa_traceframe_count's parts changed");
  5303. }
  5304. /* Get the address of the current TBC object (the IP agent has an
  5305. array of 3 such objects). The index is stored in the TBC
  5306. token. */
  5307. ipa_trace_buffer_ctrl_addr = ipa_sym_addrs.addr_trace_buffer_ctrl;
  5308. ipa_trace_buffer_ctrl_addr
  5309. += sizeof (struct ipa_trace_buffer_control) * curr_tbctrl_idx;
  5310. if (read_inferior_memory (ipa_trace_buffer_ctrl_addr,
  5311. (unsigned char *) &ipa_trace_buffer_ctrl,
  5312. sizeof (struct ipa_trace_buffer_control)))
  5313. return;
  5314. if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
  5315. &ipa_trace_buffer_lo))
  5316. return;
  5317. if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
  5318. &ipa_trace_buffer_hi))
  5319. return;
  5320. /* Offsets are easier to grok for debugging than raw addresses,
  5321. especially for the small trace buffer sizes that are useful for
  5322. testing. */
  5323. trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
  5324. "endfree=%d wrap=%d hi=%d",
  5325. curr_tbctrl_idx,
  5326. (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
  5327. (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
  5328. (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo),
  5329. (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
  5330. (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
  5331. /* Note that the IPA's buffer is always circular. */
  5332. #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
  5333. #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
  5334. ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
  5335. #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
  5336. (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
  5337. - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
  5338. ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
  5339. : 0))
  5340. tf = IPA_FIRST_TRACEFRAME ();
  5341. while (ipa_traceframe_write_count - ipa_traceframe_read_count)
  5342. {
  5343. struct tracepoint *tpoint;
  5344. struct traceframe *tframe;
  5345. unsigned char *block;
  5346. struct traceframe ipa_tframe;
  5347. if (read_inferior_memory (tf, (unsigned char *) &ipa_tframe,
  5348. offsetof (struct traceframe, data)))
  5349. error ("Uploading: couldn't read traceframe at %s\n", paddress (tf));
  5350. if (ipa_tframe.tpnum == 0)
  5351. {
  5352. internal_error (__FILE__, __LINE__,
  5353. "Uploading: No (more) fast traceframes, but"
  5354. " ipa_traceframe_count == %u??\n",
  5355. ipa_traceframe_write_count
  5356. - ipa_traceframe_read_count);
  5357. }
  5358. /* Note that this will be incorrect for multi-location
  5359. tracepoints... */
  5360. tpoint = find_next_tracepoint_by_number (NULL, ipa_tframe.tpnum);
  5361. tframe = add_traceframe (tpoint);
  5362. if (tframe == NULL)
  5363. {
  5364. trace_buffer_is_full = 1;
  5365. trace_debug ("Uploading: trace buffer is full");
  5366. }
  5367. else
  5368. {
  5369. /* Copy the whole set of blocks in one go for now. FIXME:
  5370. split this in smaller blocks. */
  5371. block = add_traceframe_block (tframe, tpoint,
  5372. ipa_tframe.data_size);
  5373. if (block != NULL)
  5374. {
  5375. if (read_inferior_memory (tf
  5376. + offsetof (struct traceframe, data),
  5377. block, ipa_tframe.data_size))
  5378. error ("Uploading: Couldn't read traceframe data at %s\n",
  5379. paddress (tf + offsetof (struct traceframe, data)));
  5380. }
  5381. trace_debug ("Uploading: traceframe didn't fit");
  5382. finish_traceframe (tframe);
  5383. }
  5384. tf = IPA_NEXT_TRACEFRAME (tf, &ipa_tframe);
  5385. /* If we freed the traceframe that wrapped around, go back
  5386. to the non-wrap case. */
  5387. if (tf < ipa_trace_buffer_ctrl.start)
  5388. {
  5389. trace_debug ("Lib: Discarding past the wraparound");
  5390. ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
  5391. }
  5392. ipa_trace_buffer_ctrl.start = tf;
  5393. ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_ctrl.start;
  5394. ++ipa_traceframe_read_count;
  5395. if (ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.free
  5396. && ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.end_free)
  5397. {
  5398. trace_debug ("Lib: buffer is fully empty. "
  5399. "Trace buffer [%d] start=%d free=%d endfree=%d",
  5400. curr_tbctrl_idx,
  5401. (int) (ipa_trace_buffer_ctrl.start
  5402. - ipa_trace_buffer_lo),
  5403. (int) (ipa_trace_buffer_ctrl.free
  5404. - ipa_trace_buffer_lo),
  5405. (int) (ipa_trace_buffer_ctrl.end_free
  5406. - ipa_trace_buffer_lo));
  5407. ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
  5408. ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
  5409. ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
  5410. ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
  5411. }
  5412. trace_debug ("Uploaded a traceframe\n"
  5413. "Lib: Trace buffer [%d] start=%d free=%d "
  5414. "endfree=%d wrap=%d hi=%d",
  5415. curr_tbctrl_idx,
  5416. (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
  5417. (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
  5418. (int) (ipa_trace_buffer_ctrl.end_free
  5419. - ipa_trace_buffer_lo),
  5420. (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
  5421. (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
  5422. }
  5423. if (target_write_memory (ipa_trace_buffer_ctrl_addr,
  5424. (unsigned char *) &ipa_trace_buffer_ctrl,
  5425. sizeof (struct ipa_trace_buffer_control)))
  5426. return;
  5427. write_inferior_integer (ipa_sym_addrs.addr_traceframe_read_count,
  5428. ipa_traceframe_read_count);
  5429. trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx);
  5430. target_pause_all (true);
  5431. delete_breakpoint (about_to_request_buffer_space_bkpt);
  5432. about_to_request_buffer_space_bkpt = NULL;
  5433. target_unpause_all (true);
  5434. if (trace_buffer_is_full)
  5435. stop_tracing ();
  5436. }
  5437. #endif
  5438. #ifdef IN_PROCESS_AGENT
  5439. IP_AGENT_EXPORT_VAR int ust_loaded;
  5440. IP_AGENT_EXPORT_VAR char cmd_buf[IPA_CMD_BUF_SIZE];
  5441. #ifdef HAVE_UST
  5442. /* Static tracepoints. */
  5443. /* UST puts a "struct tracepoint" in the global namespace, which
  5444. conflicts with our tracepoint. Arguably, being a library, it
  5445. shouldn't take ownership of such a generic name. We work around it
  5446. here. */
  5447. #define tracepoint ust_tracepoint
  5448. #include <ust/ust.h>
  5449. #undef tracepoint
  5450. extern int serialize_to_text (char *outbuf, int bufsize,
  5451. const char *fmt, va_list ap);
  5452. #define GDB_PROBE_NAME "gdb"
  5453. /* We dynamically search for the UST symbols instead of linking them
  5454. in. This lets the user decide if the application uses static
  5455. tracepoints, instead of always pulling libust.so in. This vector
  5456. holds pointers to all functions we care about. */
  5457. static struct
  5458. {
  5459. int (*serialize_to_text) (char *outbuf, int bufsize,
  5460. const char *fmt, va_list ap);
  5461. int (*ltt_probe_register) (struct ltt_available_probe *pdata);
  5462. int (*ltt_probe_unregister) (struct ltt_available_probe *pdata);
  5463. int (*ltt_marker_connect) (const char *channel, const char *mname,
  5464. const char *pname);
  5465. int (*ltt_marker_disconnect) (const char *channel, const char *mname,
  5466. const char *pname);
  5467. void (*marker_iter_start) (struct marker_iter *iter);
  5468. void (*marker_iter_next) (struct marker_iter *iter);
  5469. void (*marker_iter_stop) (struct marker_iter *iter);
  5470. void (*marker_iter_reset) (struct marker_iter *iter);
  5471. } ust_ops;
  5472. #include <dlfcn.h>
  5473. /* Cast through typeof to catch incompatible API changes. Since UST
  5474. only builds with gcc, we can freely use gcc extensions here
  5475. too. */
  5476. #define GET_UST_SYM(SYM) \
  5477. do \
  5478. { \
  5479. if (ust_ops.SYM == NULL) \
  5480. ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
  5481. if (ust_ops.SYM == NULL) \
  5482. return 0; \
  5483. } while (0)
  5484. #define USTF(SYM) ust_ops.SYM
  5485. /* Get pointers to all libust.so functions we care about. */
  5486. static int
  5487. dlsym_ust (void)
  5488. {
  5489. GET_UST_SYM (serialize_to_text);
  5490. GET_UST_SYM (ltt_probe_register);
  5491. GET_UST_SYM (ltt_probe_unregister);
  5492. GET_UST_SYM (ltt_marker_connect);
  5493. GET_UST_SYM (ltt_marker_disconnect);
  5494. GET_UST_SYM (marker_iter_start);
  5495. GET_UST_SYM (marker_iter_next);
  5496. GET_UST_SYM (marker_iter_stop);
  5497. GET_UST_SYM (marker_iter_reset);
  5498. ust_loaded = 1;
  5499. return 1;
  5500. }
  5501. /* Given an UST marker, return the matching gdb static tracepoint.
  5502. The match is done by address. */
  5503. static struct tracepoint *
  5504. ust_marker_to_static_tracepoint (const struct marker *mdata)
  5505. {
  5506. struct tracepoint *tpoint;
  5507. for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
  5508. {
  5509. if (tpoint->type != static_tracepoint)
  5510. continue;
  5511. if (tpoint->address == (uintptr_t) mdata->location)
  5512. return tpoint;
  5513. }
  5514. return NULL;
  5515. }
  5516. /* The probe function we install on lttng/ust markers. Whenever a
  5517. probed ust marker is hit, this function is called. This is similar
  5518. to gdb_collect, only for static tracepoints, instead of fast
  5519. tracepoints. */
  5520. static void
  5521. gdb_probe (const struct marker *mdata, void *probe_private,
  5522. struct registers *regs, void *call_private,
  5523. const char *fmt, va_list *args)
  5524. {
  5525. struct tracepoint *tpoint;
  5526. struct static_tracepoint_ctx ctx;
  5527. const struct target_desc *ipa_tdesc;
  5528. /* Don't do anything until the trace run is completely set up. */
  5529. if (!tracing)
  5530. {
  5531. trace_debug ("gdb_probe: not tracing\n");
  5532. return;
  5533. }
  5534. ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx);
  5535. ctx.base.type = static_tracepoint;
  5536. ctx.regcache_initted = 0;
  5537. ctx.regs = regs;
  5538. ctx.fmt = fmt;
  5539. ctx.args = args;
  5540. /* Wrap the regblock in a register cache (in the stack, we don't
  5541. want to malloc here). */
  5542. ctx.regspace = alloca (ipa_tdesc->registers_size);
  5543. if (ctx.regspace == NULL)
  5544. {
  5545. trace_debug ("Trace buffer block allocation failed, skipping");
  5546. return;
  5547. }
  5548. tpoint = ust_marker_to_static_tracepoint (mdata);
  5549. if (tpoint == NULL)
  5550. {
  5551. trace_debug ("gdb_probe: marker not known: "
  5552. "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
  5553. mdata->location, mdata->channel,
  5554. mdata->name, mdata->format);
  5555. return;
  5556. }
  5557. if (!tpoint->enabled)
  5558. {
  5559. trace_debug ("gdb_probe: tracepoint disabled");
  5560. return;
  5561. }
  5562. ctx.tpoint = tpoint;
  5563. trace_debug ("gdb_probe: collecting marker: "
  5564. "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
  5565. mdata->location, mdata->channel,
  5566. mdata->name, mdata->format);
  5567. /* Test the condition if present, and collect if true. */
  5568. if (tpoint->cond == NULL
  5569. || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
  5570. tpoint))
  5571. {
  5572. collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
  5573. tpoint->address, tpoint);
  5574. if (stopping_tracepoint
  5575. || trace_buffer_is_full
  5576. || expr_eval_result != expr_eval_no_error)
  5577. stop_tracing ();
  5578. }
  5579. else
  5580. {
  5581. /* If there was a condition and it evaluated to false, the only
  5582. way we would stop tracing is if there was an error during
  5583. condition expression evaluation. */
  5584. if (expr_eval_result != expr_eval_no_error)
  5585. stop_tracing ();
  5586. }
  5587. }
  5588. /* Called if the gdb static tracepoint requested collecting "$_sdata",
  5589. static tracepoint string data. This is a string passed to the
  5590. tracing library by the user, at the time of the tracepoint marker
  5591. call. E.g., in the UST marker call:
  5592. trace_mark (ust, bar33, "str %s", "FOOBAZ");
  5593. the collected data is "str FOOBAZ".
  5594. */
  5595. static void
  5596. collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
  5597. struct traceframe *tframe)
  5598. {
  5599. struct static_tracepoint_ctx *umd = (struct static_tracepoint_ctx *) ctx;
  5600. unsigned char *bufspace;
  5601. int size;
  5602. va_list copy;
  5603. unsigned short blocklen;
  5604. if (umd == NULL)
  5605. {
  5606. trace_debug ("Wanted to collect static trace data, "
  5607. "but there's no static trace data");
  5608. return;
  5609. }
  5610. va_copy (copy, *umd->args);
  5611. size = USTF(serialize_to_text) (NULL, 0, umd->fmt, copy);
  5612. va_end (copy);
  5613. trace_debug ("Want to collect ust data");
  5614. /* 'S' + size + string */
  5615. bufspace = add_traceframe_block (tframe, umd->tpoint,
  5616. 1 + sizeof (blocklen) + size + 1);
  5617. if (bufspace == NULL)
  5618. {
  5619. trace_debug ("Trace buffer block allocation failed, skipping");
  5620. return;
  5621. }
  5622. /* Identify a static trace data block. */
  5623. *bufspace = 'S';
  5624. blocklen = size + 1;
  5625. memcpy (bufspace + 1, &blocklen, sizeof (blocklen));
  5626. va_copy (copy, *umd->args);
  5627. USTF(serialize_to_text) ((char *) bufspace + 1 + sizeof (blocklen),
  5628. size + 1, umd->fmt, copy);
  5629. va_end (copy);
  5630. trace_debug ("Storing static tracepoint data in regblock: %s",
  5631. bufspace + 1 + sizeof (blocklen));
  5632. }
  5633. /* The probe to register with lttng/ust. */
  5634. static struct ltt_available_probe gdb_ust_probe =
  5635. {
  5636. GDB_PROBE_NAME,
  5637. NULL,
  5638. gdb_probe,
  5639. };
  5640. #endif /* HAVE_UST */
  5641. #endif /* IN_PROCESS_AGENT */
  5642. #ifndef IN_PROCESS_AGENT
  5643. /* Ask the in-process agent to run a command. Since we don't want to
  5644. have to handle the IPA hitting breakpoints while running the
  5645. command, we pause all threads, remove all breakpoints, and then set
  5646. the helper thread re-running. We communicate with the helper
  5647. thread by means of direct memory xfering, and a socket for
  5648. synchronization. */
  5649. static int
  5650. run_inferior_command (char *cmd, int len)
  5651. {
  5652. int err = -1;
  5653. int pid = current_ptid.pid ();
  5654. trace_debug ("run_inferior_command: running: %s", cmd);
  5655. target_pause_all (false);
  5656. uninsert_all_breakpoints ();
  5657. err = agent_run_command (pid, (const char *) cmd, len);
  5658. reinsert_all_breakpoints ();
  5659. target_unpause_all (false);
  5660. return err;
  5661. }
  5662. #else /* !IN_PROCESS_AGENT */
  5663. #include <sys/socket.h>
  5664. #include <sys/un.h>
  5665. #ifndef UNIX_PATH_MAX
  5666. #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
  5667. #endif
  5668. /* Where we put the socked used for synchronization. */
  5669. #define SOCK_DIR P_tmpdir
  5670. /* Thread ID of the helper thread. GDBserver reads this to know which
  5671. is the help thread. This is an LWP id on Linux. */
  5672. EXTERN_C_PUSH
  5673. IP_AGENT_EXPORT_VAR int helper_thread_id;
  5674. EXTERN_C_POP
  5675. static int
  5676. init_named_socket (const char *name)
  5677. {
  5678. int result, fd;
  5679. struct sockaddr_un addr;
  5680. result = fd = socket (PF_UNIX, SOCK_STREAM, 0);
  5681. if (result == -1)
  5682. {
  5683. warning ("socket creation failed: %s", safe_strerror (errno));
  5684. return -1;
  5685. }
  5686. addr.sun_family = AF_UNIX;
  5687. if (strlen (name) >= ARRAY_SIZE (addr.sun_path))
  5688. {
  5689. warning ("socket name too long for sockaddr_un::sun_path field: %s", name);
  5690. return -1;
  5691. }
  5692. strcpy (addr.sun_path, name);
  5693. result = access (name, F_OK);
  5694. if (result == 0)
  5695. {
  5696. /* File exists. */
  5697. result = unlink (name);
  5698. if (result == -1)
  5699. {
  5700. warning ("unlink failed: %s", safe_strerror (errno));
  5701. close (fd);
  5702. return -1;
  5703. }
  5704. warning ("socket %s already exists; overwriting", name);
  5705. }
  5706. result = bind (fd, (struct sockaddr *) &addr, sizeof (addr));
  5707. if (result == -1)
  5708. {
  5709. warning ("bind failed: %s", safe_strerror (errno));
  5710. close (fd);
  5711. return -1;
  5712. }
  5713. result = listen (fd, 1);
  5714. if (result == -1)
  5715. {
  5716. warning ("listen: %s", safe_strerror (errno));
  5717. close (fd);
  5718. return -1;
  5719. }
  5720. return fd;
  5721. }
  5722. static char agent_socket_name[UNIX_PATH_MAX];
  5723. static int
  5724. gdb_agent_socket_init (void)
  5725. {
  5726. int result, fd;
  5727. result = snprintf (agent_socket_name, UNIX_PATH_MAX, "%s/gdb_ust%d",
  5728. SOCK_DIR, getpid ());
  5729. if (result >= UNIX_PATH_MAX)
  5730. {
  5731. trace_debug ("string overflow allocating socket name");
  5732. return -1;
  5733. }
  5734. fd = init_named_socket (agent_socket_name);
  5735. if (fd < 0)
  5736. warning ("Error initializing named socket (%s) for communication with the "
  5737. "ust helper thread. Check that directory exists and that it "
  5738. "is writable.", agent_socket_name);
  5739. return fd;
  5740. }
  5741. #ifdef HAVE_UST
  5742. /* The next marker to be returned on a qTsSTM command. */
  5743. static const struct marker *next_st;
  5744. /* Returns the first known marker. */
  5745. struct marker *
  5746. first_marker (void)
  5747. {
  5748. struct marker_iter iter;
  5749. USTF(marker_iter_reset) (&iter);
  5750. USTF(marker_iter_start) (&iter);
  5751. return iter.marker;
  5752. }
  5753. /* Returns the marker following M. */
  5754. const struct marker *
  5755. next_marker (const struct marker *m)
  5756. {
  5757. struct marker_iter iter;
  5758. USTF(marker_iter_reset) (&iter);
  5759. USTF(marker_iter_start) (&iter);
  5760. for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
  5761. {
  5762. if (iter.marker == m)
  5763. {
  5764. USTF(marker_iter_next) (&iter);
  5765. return iter.marker;
  5766. }
  5767. }
  5768. return NULL;
  5769. }
  5770. /* Return an hexstr version of the STR C string, fit for sending to
  5771. GDB. */
  5772. static char *
  5773. cstr_to_hexstr (const char *str)
  5774. {
  5775. int len = strlen (str);
  5776. char *hexstr = xmalloc (len * 2 + 1);
  5777. bin2hex ((gdb_byte *) str, hexstr, len);
  5778. return hexstr;
  5779. }
  5780. /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
  5781. packets. */
  5782. static void
  5783. response_ust_marker (char *packet, const struct marker *st)
  5784. {
  5785. char *strid, *format, *tmp;
  5786. next_st = next_marker (st);
  5787. tmp = xmalloc (strlen (st->channel) + 1 +
  5788. strlen (st->name) + 1);
  5789. sprintf (tmp, "%s/%s", st->channel, st->name);
  5790. strid = cstr_to_hexstr (tmp);
  5791. free (tmp);
  5792. format = cstr_to_hexstr (st->format);
  5793. sprintf (packet, "m%s:%s:%s",
  5794. paddress ((uintptr_t) st->location),
  5795. strid,
  5796. format);
  5797. free (strid);
  5798. free (format);
  5799. }
  5800. /* Return the first static tracepoint, and initialize the state
  5801. machine that will iterate through all the static tracepoints. */
  5802. static void
  5803. cmd_qtfstm (char *packet)
  5804. {
  5805. trace_debug ("Returning first trace state variable definition");
  5806. if (first_marker ())
  5807. response_ust_marker (packet, first_marker ());
  5808. else
  5809. strcpy (packet, "l");
  5810. }
  5811. /* Return additional trace state variable definitions. */
  5812. static void
  5813. cmd_qtsstm (char *packet)
  5814. {
  5815. trace_debug ("Returning static tracepoint");
  5816. if (next_st)
  5817. response_ust_marker (packet, next_st);
  5818. else
  5819. strcpy (packet, "l");
  5820. }
  5821. /* Disconnect the GDB probe from a marker at a given address. */
  5822. static void
  5823. unprobe_marker_at (char *packet)
  5824. {
  5825. char *p = packet;
  5826. ULONGEST address;
  5827. struct marker_iter iter;
  5828. p += sizeof ("unprobe_marker_at:") - 1;
  5829. p = unpack_varlen_hex (p, &address);
  5830. USTF(marker_iter_reset) (&iter);
  5831. USTF(marker_iter_start) (&iter);
  5832. for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
  5833. if ((uintptr_t ) iter.marker->location == address)
  5834. {
  5835. int result;
  5836. result = USTF(ltt_marker_disconnect) (iter.marker->channel,
  5837. iter.marker->name,
  5838. GDB_PROBE_NAME);
  5839. if (result < 0)
  5840. warning ("could not disable marker %s/%s",
  5841. iter.marker->channel, iter.marker->name);
  5842. break;
  5843. }
  5844. }
  5845. /* Connect the GDB probe to a marker at a given address. */
  5846. static int
  5847. probe_marker_at (char *packet)
  5848. {
  5849. char *p = packet;
  5850. ULONGEST address;
  5851. struct marker_iter iter;
  5852. struct marker *m;
  5853. p += sizeof ("probe_marker_at:") - 1;
  5854. p = unpack_varlen_hex (p, &address);
  5855. USTF(marker_iter_reset) (&iter);
  5856. for (USTF(marker_iter_start) (&iter), m = iter.marker;
  5857. m != NULL;
  5858. USTF(marker_iter_next) (&iter), m = iter.marker)
  5859. if ((uintptr_t ) m->location == address)
  5860. {
  5861. int result;
  5862. trace_debug ("found marker for address. "
  5863. "ltt_marker_connect (marker = %s/%s)",
  5864. m->channel, m->name);
  5865. result = USTF(ltt_marker_connect) (m->channel, m->name,
  5866. GDB_PROBE_NAME);
  5867. if (result && result != -EEXIST)
  5868. trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
  5869. m->channel, m->name, -result);
  5870. if (result < 0)
  5871. {
  5872. sprintf (packet, "E.could not connect marker: channel=%s, name=%s",
  5873. m->channel, m->name);
  5874. return -1;
  5875. }
  5876. strcpy (packet, "OK");
  5877. return 0;
  5878. }
  5879. sprintf (packet, "E.no marker found at 0x%s", paddress (address));
  5880. return -1;
  5881. }
  5882. static int
  5883. cmd_qtstmat (char *packet)
  5884. {
  5885. char *p = packet;
  5886. ULONGEST address;
  5887. struct marker_iter iter;
  5888. struct marker *m;
  5889. p += sizeof ("qTSTMat:") - 1;
  5890. p = unpack_varlen_hex (p, &address);
  5891. USTF(marker_iter_reset) (&iter);
  5892. for (USTF(marker_iter_start) (&iter), m = iter.marker;
  5893. m != NULL;
  5894. USTF(marker_iter_next) (&iter), m = iter.marker)
  5895. if ((uintptr_t ) m->location == address)
  5896. {
  5897. response_ust_marker (packet, m);
  5898. return 0;
  5899. }
  5900. strcpy (packet, "l");
  5901. return -1;
  5902. }
  5903. static void
  5904. gdb_ust_init (void)
  5905. {
  5906. if (!dlsym_ust ())
  5907. return;
  5908. USTF(ltt_probe_register) (&gdb_ust_probe);
  5909. }
  5910. #endif /* HAVE_UST */
  5911. #include <sys/syscall.h>
  5912. static void
  5913. gdb_agent_remove_socket (void)
  5914. {
  5915. unlink (agent_socket_name);
  5916. }
  5917. /* Helper thread of agent. */
  5918. static void *
  5919. gdb_agent_helper_thread (void *arg)
  5920. {
  5921. int listen_fd;
  5922. atexit (gdb_agent_remove_socket);
  5923. while (1)
  5924. {
  5925. listen_fd = gdb_agent_socket_init ();
  5926. if (helper_thread_id == 0)
  5927. helper_thread_id = syscall (SYS_gettid);
  5928. if (listen_fd == -1)
  5929. {
  5930. warning ("could not create sync socket");
  5931. break;
  5932. }
  5933. while (1)
  5934. {
  5935. socklen_t tmp;
  5936. struct sockaddr_un sockaddr;
  5937. int fd;
  5938. char buf[1];
  5939. int ret;
  5940. int stop_loop = 0;
  5941. tmp = sizeof (sockaddr);
  5942. do
  5943. {
  5944. fd = accept (listen_fd, (struct sockaddr *) &sockaddr, &tmp);
  5945. }
  5946. /* It seems an ERESTARTSYS can escape out of accept. */
  5947. while (fd == -512 || (fd == -1 && errno == EINTR));
  5948. if (fd < 0)
  5949. {
  5950. warning ("Accept returned %d, error: %s",
  5951. fd, safe_strerror (errno));
  5952. break;
  5953. }
  5954. do
  5955. {
  5956. ret = read (fd, buf, 1);
  5957. } while (ret == -1 && errno == EINTR);
  5958. if (ret == -1)
  5959. {
  5960. warning ("reading socket (fd=%d) failed with %s",
  5961. fd, safe_strerror (errno));
  5962. close (fd);
  5963. break;
  5964. }
  5965. if (cmd_buf[0])
  5966. {
  5967. if (startswith (cmd_buf, "close"))
  5968. {
  5969. stop_loop = 1;
  5970. }
  5971. #ifdef HAVE_UST
  5972. else if (strcmp ("qTfSTM", cmd_buf) == 0)
  5973. {
  5974. cmd_qtfstm (cmd_buf);
  5975. }
  5976. else if (strcmp ("qTsSTM", cmd_buf) == 0)
  5977. {
  5978. cmd_qtsstm (cmd_buf);
  5979. }
  5980. else if (startswith (cmd_buf, "unprobe_marker_at:"))
  5981. {
  5982. unprobe_marker_at (cmd_buf);
  5983. }
  5984. else if (startswith (cmd_buf, "probe_marker_at:"))
  5985. {
  5986. probe_marker_at (cmd_buf);
  5987. }
  5988. else if (startswith (cmd_buf, "qTSTMat:"))
  5989. {
  5990. cmd_qtstmat (cmd_buf);
  5991. }
  5992. #endif /* HAVE_UST */
  5993. }
  5994. /* Fix compiler's warning: ignoring return value of 'write'. */
  5995. ret = write (fd, buf, 1);
  5996. close (fd);
  5997. if (stop_loop)
  5998. {
  5999. close (listen_fd);
  6000. unlink (agent_socket_name);
  6001. /* Sleep endlessly to wait the whole inferior stops. This
  6002. thread can not exit because GDB or GDBserver may still need
  6003. 'current_thread' (representing this thread) to access
  6004. inferior memory. Otherwise, this thread exits earlier than
  6005. other threads, and 'current_thread' is set to NULL. */
  6006. while (1)
  6007. sleep (10);
  6008. }
  6009. }
  6010. }
  6011. return NULL;
  6012. }
  6013. #include <signal.h>
  6014. #include <pthread.h>
  6015. EXTERN_C_PUSH
  6016. IP_AGENT_EXPORT_VAR int gdb_agent_capability = AGENT_CAPA_STATIC_TRACE;
  6017. EXTERN_C_POP
  6018. static void
  6019. gdb_agent_init (void)
  6020. {
  6021. int res;
  6022. pthread_t thread;
  6023. sigset_t new_mask;
  6024. sigset_t orig_mask;
  6025. /* We want the helper thread to be as transparent as possible, so
  6026. have it inherit an all-signals-blocked mask. */
  6027. sigfillset (&new_mask);
  6028. res = pthread_sigmask (SIG_SETMASK, &new_mask, &orig_mask);
  6029. if (res)
  6030. perror_with_name ("pthread_sigmask (1)");
  6031. res = pthread_create (&thread,
  6032. NULL,
  6033. gdb_agent_helper_thread,
  6034. NULL);
  6035. res = pthread_sigmask (SIG_SETMASK, &orig_mask, NULL);
  6036. if (res)
  6037. perror_with_name ("pthread_sigmask (2)");
  6038. while (helper_thread_id == 0)
  6039. usleep (1);
  6040. #ifdef HAVE_UST
  6041. gdb_ust_init ();
  6042. #endif
  6043. }
  6044. #include <sys/mman.h>
  6045. IP_AGENT_EXPORT_VAR char *gdb_tp_heap_buffer;
  6046. IP_AGENT_EXPORT_VAR char *gdb_jump_pad_buffer;
  6047. IP_AGENT_EXPORT_VAR char *gdb_jump_pad_buffer_end;
  6048. IP_AGENT_EXPORT_VAR char *gdb_trampoline_buffer;
  6049. IP_AGENT_EXPORT_VAR char *gdb_trampoline_buffer_end;
  6050. IP_AGENT_EXPORT_VAR char *gdb_trampoline_buffer_error;
  6051. /* Record the result of getting buffer space for fast tracepoint
  6052. trampolines. Any error message is copied, since caller may not be
  6053. using persistent storage. */
  6054. void
  6055. set_trampoline_buffer_space (CORE_ADDR begin, CORE_ADDR end, char *errmsg)
  6056. {
  6057. gdb_trampoline_buffer = (char *) (uintptr_t) begin;
  6058. gdb_trampoline_buffer_end = (char *) (uintptr_t) end;
  6059. if (errmsg)
  6060. strncpy (gdb_trampoline_buffer_error, errmsg, 99);
  6061. else
  6062. strcpy (gdb_trampoline_buffer_error, "no buffer passed");
  6063. }
  6064. static void __attribute__ ((constructor))
  6065. initialize_tracepoint_ftlib (void)
  6066. {
  6067. initialize_tracepoint ();
  6068. gdb_agent_init ();
  6069. }
  6070. #ifndef HAVE_GETAUXVAL
  6071. /* Retrieve the value of TYPE from the auxiliary vector. If TYPE is not
  6072. found, 0 is returned. This function is provided if glibc is too old. */
  6073. unsigned long
  6074. getauxval (unsigned long type)
  6075. {
  6076. unsigned long data[2];
  6077. FILE *f = fopen ("/proc/self/auxv", "r");
  6078. unsigned long value = 0;
  6079. if (f == NULL)
  6080. return 0;
  6081. while (fread (data, sizeof (data), 1, f) > 0)
  6082. {
  6083. if (data[0] == type)
  6084. {
  6085. value = data[1];
  6086. break;
  6087. }
  6088. }
  6089. fclose (f);
  6090. return value;
  6091. }
  6092. #endif
  6093. #endif /* IN_PROCESS_AGENT */
  6094. /* Return a timestamp, expressed as microseconds of the usual Unix
  6095. time. (As the result is a 64-bit number, it will not overflow any
  6096. time soon.) */
  6097. static LONGEST
  6098. get_timestamp (void)
  6099. {
  6100. using namespace std::chrono;
  6101. steady_clock::time_point now = steady_clock::now ();
  6102. return duration_cast<microseconds> (now.time_since_epoch ()).count ();
  6103. }
  6104. void
  6105. initialize_tracepoint (void)
  6106. {
  6107. /* Start with the default size. */
  6108. init_trace_buffer (DEFAULT_TRACE_BUFFER_SIZE);
  6109. /* Wire trace state variable 1 to be the timestamp. This will be
  6110. uploaded to GDB upon connection and become one of its trace state
  6111. variables. (In case you're wondering, if GDB already has a trace
  6112. variable numbered 1, it will be renumbered.) */
  6113. create_trace_state_variable (1, 0);
  6114. set_trace_state_variable_name (1, "trace_timestamp");
  6115. set_trace_state_variable_getter (1, get_timestamp);
  6116. #ifdef IN_PROCESS_AGENT
  6117. {
  6118. int pagesize;
  6119. size_t jump_pad_size;
  6120. pagesize = sysconf (_SC_PAGE_SIZE);
  6121. if (pagesize == -1)
  6122. perror_with_name ("sysconf");
  6123. #define SCRATCH_BUFFER_NPAGES 20
  6124. jump_pad_size = pagesize * SCRATCH_BUFFER_NPAGES;
  6125. gdb_tp_heap_buffer = (char *) xmalloc (5 * 1024 * 1024);
  6126. gdb_jump_pad_buffer = (char *) alloc_jump_pad_buffer (jump_pad_size);
  6127. if (gdb_jump_pad_buffer == NULL)
  6128. perror_with_name ("mmap");
  6129. gdb_jump_pad_buffer_end = gdb_jump_pad_buffer + jump_pad_size;
  6130. }
  6131. gdb_trampoline_buffer = gdb_trampoline_buffer_end = 0;
  6132. /* It's not a fatal error for something to go wrong with trampoline
  6133. buffer setup, but it can be mysterious, so create a channel to
  6134. report back on what went wrong, using a fixed size since we may
  6135. not be able to allocate space later when the problem occurs. */
  6136. gdb_trampoline_buffer_error = (char *) xmalloc (IPA_BUFSIZ);
  6137. strcpy (gdb_trampoline_buffer_error, "No errors reported");
  6138. initialize_low_tracepoint ();
  6139. #endif
  6140. }