linux-low.cc 190 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053
  1. /* Low level interface to ptrace, for the remote server for GDB.
  2. Copyright (C) 1995-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 "linux-low.h"
  16. #include "nat/linux-osdata.h"
  17. #include "gdbsupport/agent.h"
  18. #include "tdesc.h"
  19. #include "gdbsupport/event-loop.h"
  20. #include "gdbsupport/event-pipe.h"
  21. #include "gdbsupport/rsp-low.h"
  22. #include "gdbsupport/signals-state-save-restore.h"
  23. #include "nat/linux-nat.h"
  24. #include "nat/linux-waitpid.h"
  25. #include "gdbsupport/gdb_wait.h"
  26. #include "nat/gdb_ptrace.h"
  27. #include "nat/linux-ptrace.h"
  28. #include "nat/linux-procfs.h"
  29. #include "nat/linux-personality.h"
  30. #include <signal.h>
  31. #include <sys/ioctl.h>
  32. #include <fcntl.h>
  33. #include <unistd.h>
  34. #include <sys/syscall.h>
  35. #include <sched.h>
  36. #include <ctype.h>
  37. #include <pwd.h>
  38. #include <sys/types.h>
  39. #include <dirent.h>
  40. #include <sys/stat.h>
  41. #include <sys/vfs.h>
  42. #include <sys/uio.h>
  43. #include "gdbsupport/filestuff.h"
  44. #include "tracepoint.h"
  45. #include <inttypes.h>
  46. #include "gdbsupport/common-inferior.h"
  47. #include "nat/fork-inferior.h"
  48. #include "gdbsupport/environ.h"
  49. #include "gdbsupport/gdb-sigmask.h"
  50. #include "gdbsupport/scoped_restore.h"
  51. #ifndef ELFMAG0
  52. /* Don't include <linux/elf.h> here. If it got included by gdb_proc_service.h
  53. then ELFMAG0 will have been defined. If it didn't get included by
  54. gdb_proc_service.h then including it will likely introduce a duplicate
  55. definition of elf_fpregset_t. */
  56. #include <elf.h>
  57. #endif
  58. #include "nat/linux-namespaces.h"
  59. #ifndef O_LARGEFILE
  60. #define O_LARGEFILE 0
  61. #endif
  62. #ifndef AT_HWCAP2
  63. #define AT_HWCAP2 26
  64. #endif
  65. /* Some targets did not define these ptrace constants from the start,
  66. so gdbserver defines them locally here. In the future, these may
  67. be removed after they are added to asm/ptrace.h. */
  68. #if !(defined(PT_TEXT_ADDR) \
  69. || defined(PT_DATA_ADDR) \
  70. || defined(PT_TEXT_END_ADDR))
  71. #if defined(__mcoldfire__)
  72. /* These are still undefined in 3.10 kernels. */
  73. #define PT_TEXT_ADDR 49*4
  74. #define PT_DATA_ADDR 50*4
  75. #define PT_TEXT_END_ADDR 51*4
  76. /* These are still undefined in 3.10 kernels. */
  77. #elif defined(__TMS320C6X__)
  78. #define PT_TEXT_ADDR (0x10000*4)
  79. #define PT_DATA_ADDR (0x10004*4)
  80. #define PT_TEXT_END_ADDR (0x10008*4)
  81. #endif
  82. #endif
  83. #if (defined(__UCLIBC__) \
  84. && defined(HAS_NOMMU) \
  85. && defined(PT_TEXT_ADDR) \
  86. && defined(PT_DATA_ADDR) \
  87. && defined(PT_TEXT_END_ADDR))
  88. #define SUPPORTS_READ_OFFSETS
  89. #endif
  90. #ifdef HAVE_LINUX_BTRACE
  91. # include "nat/linux-btrace.h"
  92. # include "gdbsupport/btrace-common.h"
  93. #endif
  94. #ifndef HAVE_ELF32_AUXV_T
  95. /* Copied from glibc's elf.h. */
  96. typedef struct
  97. {
  98. uint32_t a_type; /* Entry type */
  99. union
  100. {
  101. uint32_t a_val; /* Integer value */
  102. /* We use to have pointer elements added here. We cannot do that,
  103. though, since it does not work when using 32-bit definitions
  104. on 64-bit platforms and vice versa. */
  105. } a_un;
  106. } Elf32_auxv_t;
  107. #endif
  108. #ifndef HAVE_ELF64_AUXV_T
  109. /* Copied from glibc's elf.h. */
  110. typedef struct
  111. {
  112. uint64_t a_type; /* Entry type */
  113. union
  114. {
  115. uint64_t a_val; /* Integer value */
  116. /* We use to have pointer elements added here. We cannot do that,
  117. though, since it does not work when using 32-bit definitions
  118. on 64-bit platforms and vice versa. */
  119. } a_un;
  120. } Elf64_auxv_t;
  121. #endif
  122. /* Does the current host support PTRACE_GETREGSET? */
  123. int have_ptrace_getregset = -1;
  124. /* Return TRUE if THREAD is the leader thread of the process. */
  125. static bool
  126. is_leader (thread_info *thread)
  127. {
  128. ptid_t ptid = ptid_of (thread);
  129. return ptid.pid () == ptid.lwp ();
  130. }
  131. /* LWP accessors. */
  132. /* See nat/linux-nat.h. */
  133. ptid_t
  134. ptid_of_lwp (struct lwp_info *lwp)
  135. {
  136. return ptid_of (get_lwp_thread (lwp));
  137. }
  138. /* See nat/linux-nat.h. */
  139. void
  140. lwp_set_arch_private_info (struct lwp_info *lwp,
  141. struct arch_lwp_info *info)
  142. {
  143. lwp->arch_private = info;
  144. }
  145. /* See nat/linux-nat.h. */
  146. struct arch_lwp_info *
  147. lwp_arch_private_info (struct lwp_info *lwp)
  148. {
  149. return lwp->arch_private;
  150. }
  151. /* See nat/linux-nat.h. */
  152. int
  153. lwp_is_stopped (struct lwp_info *lwp)
  154. {
  155. return lwp->stopped;
  156. }
  157. /* See nat/linux-nat.h. */
  158. enum target_stop_reason
  159. lwp_stop_reason (struct lwp_info *lwp)
  160. {
  161. return lwp->stop_reason;
  162. }
  163. /* See nat/linux-nat.h. */
  164. int
  165. lwp_is_stepping (struct lwp_info *lwp)
  166. {
  167. return lwp->stepping;
  168. }
  169. /* A list of all unknown processes which receive stop signals. Some
  170. other process will presumably claim each of these as forked
  171. children momentarily. */
  172. struct simple_pid_list
  173. {
  174. /* The process ID. */
  175. int pid;
  176. /* The status as reported by waitpid. */
  177. int status;
  178. /* Next in chain. */
  179. struct simple_pid_list *next;
  180. };
  181. static struct simple_pid_list *stopped_pids;
  182. /* Trivial list manipulation functions to keep track of a list of new
  183. stopped processes. */
  184. static void
  185. add_to_pid_list (struct simple_pid_list **listp, int pid, int status)
  186. {
  187. struct simple_pid_list *new_pid = XNEW (struct simple_pid_list);
  188. new_pid->pid = pid;
  189. new_pid->status = status;
  190. new_pid->next = *listp;
  191. *listp = new_pid;
  192. }
  193. static int
  194. pull_pid_from_list (struct simple_pid_list **listp, int pid, int *statusp)
  195. {
  196. struct simple_pid_list **p;
  197. for (p = listp; *p != NULL; p = &(*p)->next)
  198. if ((*p)->pid == pid)
  199. {
  200. struct simple_pid_list *next = (*p)->next;
  201. *statusp = (*p)->status;
  202. xfree (*p);
  203. *p = next;
  204. return 1;
  205. }
  206. return 0;
  207. }
  208. enum stopping_threads_kind
  209. {
  210. /* Not stopping threads presently. */
  211. NOT_STOPPING_THREADS,
  212. /* Stopping threads. */
  213. STOPPING_THREADS,
  214. /* Stopping and suspending threads. */
  215. STOPPING_AND_SUSPENDING_THREADS
  216. };
  217. /* This is set while stop_all_lwps is in effect. */
  218. static stopping_threads_kind stopping_threads = NOT_STOPPING_THREADS;
  219. /* FIXME make into a target method? */
  220. int using_threads = 1;
  221. /* True if we're presently stabilizing threads (moving them out of
  222. jump pads). */
  223. static int stabilizing_threads;
  224. static void unsuspend_all_lwps (struct lwp_info *except);
  225. static void mark_lwp_dead (struct lwp_info *lwp, int wstat);
  226. static int lwp_is_marked_dead (struct lwp_info *lwp);
  227. static int kill_lwp (unsigned long lwpid, int signo);
  228. static void enqueue_pending_signal (struct lwp_info *lwp, int signal, siginfo_t *info);
  229. static int linux_low_ptrace_options (int attached);
  230. static int check_ptrace_stopped_lwp_gone (struct lwp_info *lp);
  231. /* When the event-loop is doing a step-over, this points at the thread
  232. being stepped. */
  233. static ptid_t step_over_bkpt;
  234. bool
  235. linux_process_target::low_supports_breakpoints ()
  236. {
  237. return false;
  238. }
  239. CORE_ADDR
  240. linux_process_target::low_get_pc (regcache *regcache)
  241. {
  242. return 0;
  243. }
  244. void
  245. linux_process_target::low_set_pc (regcache *regcache, CORE_ADDR newpc)
  246. {
  247. gdb_assert_not_reached ("linux target op low_set_pc is not implemented");
  248. }
  249. std::vector<CORE_ADDR>
  250. linux_process_target::low_get_next_pcs (regcache *regcache)
  251. {
  252. gdb_assert_not_reached ("linux target op low_get_next_pcs is not "
  253. "implemented");
  254. }
  255. int
  256. linux_process_target::low_decr_pc_after_break ()
  257. {
  258. return 0;
  259. }
  260. /* True if LWP is stopped in its stepping range. */
  261. static int
  262. lwp_in_step_range (struct lwp_info *lwp)
  263. {
  264. CORE_ADDR pc = lwp->stop_pc;
  265. return (pc >= lwp->step_range_start && pc < lwp->step_range_end);
  266. }
  267. /* The event pipe registered as a waitable file in the event loop. */
  268. static event_pipe linux_event_pipe;
  269. /* True if we're currently in async mode. */
  270. #define target_is_async_p() (linux_event_pipe.is_open ())
  271. static void send_sigstop (struct lwp_info *lwp);
  272. /* Return non-zero if HEADER is a 64-bit ELF file. */
  273. static int
  274. elf_64_header_p (const Elf64_Ehdr *header, unsigned int *machine)
  275. {
  276. if (header->e_ident[EI_MAG0] == ELFMAG0
  277. && header->e_ident[EI_MAG1] == ELFMAG1
  278. && header->e_ident[EI_MAG2] == ELFMAG2
  279. && header->e_ident[EI_MAG3] == ELFMAG3)
  280. {
  281. *machine = header->e_machine;
  282. return header->e_ident[EI_CLASS] == ELFCLASS64;
  283. }
  284. *machine = EM_NONE;
  285. return -1;
  286. }
  287. /* Return non-zero if FILE is a 64-bit ELF file,
  288. zero if the file is not a 64-bit ELF file,
  289. and -1 if the file is not accessible or doesn't exist. */
  290. static int
  291. elf_64_file_p (const char *file, unsigned int *machine)
  292. {
  293. Elf64_Ehdr header;
  294. int fd;
  295. fd = open (file, O_RDONLY);
  296. if (fd < 0)
  297. return -1;
  298. if (read (fd, &header, sizeof (header)) != sizeof (header))
  299. {
  300. close (fd);
  301. return 0;
  302. }
  303. close (fd);
  304. return elf_64_header_p (&header, machine);
  305. }
  306. /* Accepts an integer PID; Returns true if the executable PID is
  307. running is a 64-bit ELF file.. */
  308. int
  309. linux_pid_exe_is_elf_64_file (int pid, unsigned int *machine)
  310. {
  311. char file[PATH_MAX];
  312. sprintf (file, "/proc/%d/exe", pid);
  313. return elf_64_file_p (file, machine);
  314. }
  315. void
  316. linux_process_target::delete_lwp (lwp_info *lwp)
  317. {
  318. struct thread_info *thr = get_lwp_thread (lwp);
  319. threads_debug_printf ("deleting %ld", lwpid_of (thr));
  320. remove_thread (thr);
  321. low_delete_thread (lwp->arch_private);
  322. delete lwp;
  323. }
  324. void
  325. linux_process_target::low_delete_thread (arch_lwp_info *info)
  326. {
  327. /* Default implementation should be overridden if architecture-specific
  328. info is being used. */
  329. gdb_assert (info == nullptr);
  330. }
  331. process_info *
  332. linux_process_target::add_linux_process (int pid, int attached)
  333. {
  334. struct process_info *proc;
  335. proc = add_process (pid, attached);
  336. proc->priv = XCNEW (struct process_info_private);
  337. proc->priv->arch_private = low_new_process ();
  338. return proc;
  339. }
  340. arch_process_info *
  341. linux_process_target::low_new_process ()
  342. {
  343. return nullptr;
  344. }
  345. void
  346. linux_process_target::low_delete_process (arch_process_info *info)
  347. {
  348. /* Default implementation must be overridden if architecture-specific
  349. info exists. */
  350. gdb_assert (info == nullptr);
  351. }
  352. void
  353. linux_process_target::low_new_fork (process_info *parent, process_info *child)
  354. {
  355. /* Nop. */
  356. }
  357. void
  358. linux_process_target::arch_setup_thread (thread_info *thread)
  359. {
  360. scoped_restore_current_thread restore_thread;
  361. switch_to_thread (thread);
  362. low_arch_setup ();
  363. }
  364. int
  365. linux_process_target::handle_extended_wait (lwp_info **orig_event_lwp,
  366. int wstat)
  367. {
  368. client_state &cs = get_client_state ();
  369. struct lwp_info *event_lwp = *orig_event_lwp;
  370. int event = linux_ptrace_get_extended_event (wstat);
  371. struct thread_info *event_thr = get_lwp_thread (event_lwp);
  372. struct lwp_info *new_lwp;
  373. gdb_assert (event_lwp->waitstatus.kind () == TARGET_WAITKIND_IGNORE);
  374. /* All extended events we currently use are mid-syscall. Only
  375. PTRACE_EVENT_STOP is delivered more like a signal-stop, but
  376. you have to be using PTRACE_SEIZE to get that. */
  377. event_lwp->syscall_state = TARGET_WAITKIND_SYSCALL_ENTRY;
  378. if ((event == PTRACE_EVENT_FORK) || (event == PTRACE_EVENT_VFORK)
  379. || (event == PTRACE_EVENT_CLONE))
  380. {
  381. ptid_t ptid;
  382. unsigned long new_pid;
  383. int ret, status;
  384. /* Get the pid of the new lwp. */
  385. ptrace (PTRACE_GETEVENTMSG, lwpid_of (event_thr), (PTRACE_TYPE_ARG3) 0,
  386. &new_pid);
  387. /* If we haven't already seen the new PID stop, wait for it now. */
  388. if (!pull_pid_from_list (&stopped_pids, new_pid, &status))
  389. {
  390. /* The new child has a pending SIGSTOP. We can't affect it until it
  391. hits the SIGSTOP, but we're already attached. */
  392. ret = my_waitpid (new_pid, &status, __WALL);
  393. if (ret == -1)
  394. perror_with_name ("waiting for new child");
  395. else if (ret != new_pid)
  396. warning ("wait returned unexpected PID %d", ret);
  397. else if (!WIFSTOPPED (status))
  398. warning ("wait returned unexpected status 0x%x", status);
  399. }
  400. if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK)
  401. {
  402. struct process_info *parent_proc;
  403. struct process_info *child_proc;
  404. struct lwp_info *child_lwp;
  405. struct thread_info *child_thr;
  406. ptid = ptid_t (new_pid, new_pid);
  407. threads_debug_printf ("Got fork event from LWP %ld, "
  408. "new child is %d",
  409. ptid_of (event_thr).lwp (),
  410. ptid.pid ());
  411. /* Add the new process to the tables and clone the breakpoint
  412. lists of the parent. We need to do this even if the new process
  413. will be detached, since we will need the process object and the
  414. breakpoints to remove any breakpoints from memory when we
  415. detach, and the client side will access registers. */
  416. child_proc = add_linux_process (new_pid, 0);
  417. gdb_assert (child_proc != NULL);
  418. child_lwp = add_lwp (ptid);
  419. gdb_assert (child_lwp != NULL);
  420. child_lwp->stopped = 1;
  421. child_lwp->must_set_ptrace_flags = 1;
  422. child_lwp->status_pending_p = 0;
  423. child_thr = get_lwp_thread (child_lwp);
  424. child_thr->last_resume_kind = resume_stop;
  425. child_thr->last_status.set_stopped (GDB_SIGNAL_0);
  426. /* If we're suspending all threads, leave this one suspended
  427. too. If the fork/clone parent is stepping over a breakpoint,
  428. all other threads have been suspended already. Leave the
  429. child suspended too. */
  430. if (stopping_threads == STOPPING_AND_SUSPENDING_THREADS
  431. || event_lwp->bp_reinsert != 0)
  432. {
  433. threads_debug_printf ("leaving child suspended");
  434. child_lwp->suspended = 1;
  435. }
  436. parent_proc = get_thread_process (event_thr);
  437. child_proc->attached = parent_proc->attached;
  438. if (event_lwp->bp_reinsert != 0
  439. && supports_software_single_step ()
  440. && event == PTRACE_EVENT_VFORK)
  441. {
  442. /* If we leave single-step breakpoints there, child will
  443. hit it, so uninsert single-step breakpoints from parent
  444. (and child). Once vfork child is done, reinsert
  445. them back to parent. */
  446. uninsert_single_step_breakpoints (event_thr);
  447. }
  448. clone_all_breakpoints (child_thr, event_thr);
  449. target_desc_up tdesc = allocate_target_description ();
  450. copy_target_description (tdesc.get (), parent_proc->tdesc);
  451. child_proc->tdesc = tdesc.release ();
  452. /* Clone arch-specific process data. */
  453. low_new_fork (parent_proc, child_proc);
  454. /* Save fork info in the parent thread. */
  455. if (event == PTRACE_EVENT_FORK)
  456. event_lwp->waitstatus.set_forked (ptid);
  457. else if (event == PTRACE_EVENT_VFORK)
  458. event_lwp->waitstatus.set_vforked (ptid);
  459. /* The status_pending field contains bits denoting the
  460. extended event, so when the pending event is handled,
  461. the handler will look at lwp->waitstatus. */
  462. event_lwp->status_pending_p = 1;
  463. event_lwp->status_pending = wstat;
  464. /* Link the threads until the parent event is passed on to
  465. higher layers. */
  466. event_lwp->fork_relative = child_lwp;
  467. child_lwp->fork_relative = event_lwp;
  468. /* If the parent thread is doing step-over with single-step
  469. breakpoints, the list of single-step breakpoints are cloned
  470. from the parent's. Remove them from the child process.
  471. In case of vfork, we'll reinsert them back once vforked
  472. child is done. */
  473. if (event_lwp->bp_reinsert != 0
  474. && supports_software_single_step ())
  475. {
  476. /* The child process is forked and stopped, so it is safe
  477. to access its memory without stopping all other threads
  478. from other processes. */
  479. delete_single_step_breakpoints (child_thr);
  480. gdb_assert (has_single_step_breakpoints (event_thr));
  481. gdb_assert (!has_single_step_breakpoints (child_thr));
  482. }
  483. /* Report the event. */
  484. return 0;
  485. }
  486. threads_debug_printf
  487. ("Got clone event from LWP %ld, new child is LWP %ld",
  488. lwpid_of (event_thr), new_pid);
  489. ptid = ptid_t (pid_of (event_thr), new_pid);
  490. new_lwp = add_lwp (ptid);
  491. /* Either we're going to immediately resume the new thread
  492. or leave it stopped. resume_one_lwp is a nop if it
  493. thinks the thread is currently running, so set this first
  494. before calling resume_one_lwp. */
  495. new_lwp->stopped = 1;
  496. /* If we're suspending all threads, leave this one suspended
  497. too. If the fork/clone parent is stepping over a breakpoint,
  498. all other threads have been suspended already. Leave the
  499. child suspended too. */
  500. if (stopping_threads == STOPPING_AND_SUSPENDING_THREADS
  501. || event_lwp->bp_reinsert != 0)
  502. new_lwp->suspended = 1;
  503. /* Normally we will get the pending SIGSTOP. But in some cases
  504. we might get another signal delivered to the group first.
  505. If we do get another signal, be sure not to lose it. */
  506. if (WSTOPSIG (status) != SIGSTOP)
  507. {
  508. new_lwp->stop_expected = 1;
  509. new_lwp->status_pending_p = 1;
  510. new_lwp->status_pending = status;
  511. }
  512. else if (cs.report_thread_events)
  513. {
  514. new_lwp->waitstatus.set_thread_created ();
  515. new_lwp->status_pending_p = 1;
  516. new_lwp->status_pending = status;
  517. }
  518. #ifdef USE_THREAD_DB
  519. thread_db_notice_clone (event_thr, ptid);
  520. #endif
  521. /* Don't report the event. */
  522. return 1;
  523. }
  524. else if (event == PTRACE_EVENT_VFORK_DONE)
  525. {
  526. event_lwp->waitstatus.set_vfork_done ();
  527. if (event_lwp->bp_reinsert != 0 && supports_software_single_step ())
  528. {
  529. reinsert_single_step_breakpoints (event_thr);
  530. gdb_assert (has_single_step_breakpoints (event_thr));
  531. }
  532. /* Report the event. */
  533. return 0;
  534. }
  535. else if (event == PTRACE_EVENT_EXEC && cs.report_exec_events)
  536. {
  537. struct process_info *proc;
  538. std::vector<int> syscalls_to_catch;
  539. ptid_t event_ptid;
  540. pid_t event_pid;
  541. threads_debug_printf ("Got exec event from LWP %ld",
  542. lwpid_of (event_thr));
  543. /* Get the event ptid. */
  544. event_ptid = ptid_of (event_thr);
  545. event_pid = event_ptid.pid ();
  546. /* Save the syscall list from the execing process. */
  547. proc = get_thread_process (event_thr);
  548. syscalls_to_catch = std::move (proc->syscalls_to_catch);
  549. /* Delete the execing process and all its threads. */
  550. mourn (proc);
  551. switch_to_thread (nullptr);
  552. /* Create a new process/lwp/thread. */
  553. proc = add_linux_process (event_pid, 0);
  554. event_lwp = add_lwp (event_ptid);
  555. event_thr = get_lwp_thread (event_lwp);
  556. gdb_assert (current_thread == event_thr);
  557. arch_setup_thread (event_thr);
  558. /* Set the event status. */
  559. event_lwp->waitstatus.set_execd
  560. (make_unique_xstrdup
  561. (linux_proc_pid_to_exec_file (lwpid_of (event_thr))));
  562. /* Mark the exec status as pending. */
  563. event_lwp->stopped = 1;
  564. event_lwp->status_pending_p = 1;
  565. event_lwp->status_pending = wstat;
  566. event_thr->last_resume_kind = resume_continue;
  567. event_thr->last_status.set_ignore ();
  568. /* Update syscall state in the new lwp, effectively mid-syscall too. */
  569. event_lwp->syscall_state = TARGET_WAITKIND_SYSCALL_ENTRY;
  570. /* Restore the list to catch. Don't rely on the client, which is free
  571. to avoid sending a new list when the architecture doesn't change.
  572. Also, for ANY_SYSCALL, the architecture doesn't really matter. */
  573. proc->syscalls_to_catch = std::move (syscalls_to_catch);
  574. /* Report the event. */
  575. *orig_event_lwp = event_lwp;
  576. return 0;
  577. }
  578. internal_error (__FILE__, __LINE__, _("unknown ptrace event %d"), event);
  579. }
  580. CORE_ADDR
  581. linux_process_target::get_pc (lwp_info *lwp)
  582. {
  583. struct regcache *regcache;
  584. CORE_ADDR pc;
  585. if (!low_supports_breakpoints ())
  586. return 0;
  587. scoped_restore_current_thread restore_thread;
  588. switch_to_thread (get_lwp_thread (lwp));
  589. regcache = get_thread_regcache (current_thread, 1);
  590. pc = low_get_pc (regcache);
  591. threads_debug_printf ("pc is 0x%lx", (long) pc);
  592. return pc;
  593. }
  594. void
  595. linux_process_target::get_syscall_trapinfo (lwp_info *lwp, int *sysno)
  596. {
  597. struct regcache *regcache;
  598. scoped_restore_current_thread restore_thread;
  599. switch_to_thread (get_lwp_thread (lwp));
  600. regcache = get_thread_regcache (current_thread, 1);
  601. low_get_syscall_trapinfo (regcache, sysno);
  602. threads_debug_printf ("get_syscall_trapinfo sysno %d", *sysno);
  603. }
  604. void
  605. linux_process_target::low_get_syscall_trapinfo (regcache *regcache, int *sysno)
  606. {
  607. /* By default, report an unknown system call number. */
  608. *sysno = UNKNOWN_SYSCALL;
  609. }
  610. bool
  611. linux_process_target::save_stop_reason (lwp_info *lwp)
  612. {
  613. CORE_ADDR pc;
  614. CORE_ADDR sw_breakpoint_pc;
  615. #if USE_SIGTRAP_SIGINFO
  616. siginfo_t siginfo;
  617. #endif
  618. if (!low_supports_breakpoints ())
  619. return false;
  620. pc = get_pc (lwp);
  621. sw_breakpoint_pc = pc - low_decr_pc_after_break ();
  622. /* breakpoint_at reads from the current thread. */
  623. scoped_restore_current_thread restore_thread;
  624. switch_to_thread (get_lwp_thread (lwp));
  625. #if USE_SIGTRAP_SIGINFO
  626. if (ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread),
  627. (PTRACE_TYPE_ARG3) 0, &siginfo) == 0)
  628. {
  629. if (siginfo.si_signo == SIGTRAP)
  630. {
  631. if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code)
  632. && GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code))
  633. {
  634. /* The si_code is ambiguous on this arch -- check debug
  635. registers. */
  636. if (!check_stopped_by_watchpoint (lwp))
  637. lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
  638. }
  639. else if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code))
  640. {
  641. /* If we determine the LWP stopped for a SW breakpoint,
  642. trust it. Particularly don't check watchpoint
  643. registers, because at least on s390, we'd find
  644. stopped-by-watchpoint as long as there's a watchpoint
  645. set. */
  646. lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
  647. }
  648. else if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code))
  649. {
  650. /* This can indicate either a hardware breakpoint or
  651. hardware watchpoint. Check debug registers. */
  652. if (!check_stopped_by_watchpoint (lwp))
  653. lwp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
  654. }
  655. else if (siginfo.si_code == TRAP_TRACE)
  656. {
  657. /* We may have single stepped an instruction that
  658. triggered a watchpoint. In that case, on some
  659. architectures (such as x86), instead of TRAP_HWBKPT,
  660. si_code indicates TRAP_TRACE, and we need to check
  661. the debug registers separately. */
  662. if (!check_stopped_by_watchpoint (lwp))
  663. lwp->stop_reason = TARGET_STOPPED_BY_SINGLE_STEP;
  664. }
  665. }
  666. }
  667. #else
  668. /* We may have just stepped a breakpoint instruction. E.g., in
  669. non-stop mode, GDB first tells the thread A to step a range, and
  670. then the user inserts a breakpoint inside the range. In that
  671. case we need to report the breakpoint PC. */
  672. if ((!lwp->stepping || lwp->stop_pc == sw_breakpoint_pc)
  673. && low_breakpoint_at (sw_breakpoint_pc))
  674. lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
  675. if (hardware_breakpoint_inserted_here (pc))
  676. lwp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
  677. if (lwp->stop_reason == TARGET_STOPPED_BY_NO_REASON)
  678. check_stopped_by_watchpoint (lwp);
  679. #endif
  680. if (lwp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT)
  681. {
  682. threads_debug_printf
  683. ("%s stopped by software breakpoint",
  684. target_pid_to_str (ptid_of (get_lwp_thread (lwp))).c_str ());
  685. /* Back up the PC if necessary. */
  686. if (pc != sw_breakpoint_pc)
  687. {
  688. struct regcache *regcache
  689. = get_thread_regcache (current_thread, 1);
  690. low_set_pc (regcache, sw_breakpoint_pc);
  691. }
  692. /* Update this so we record the correct stop PC below. */
  693. pc = sw_breakpoint_pc;
  694. }
  695. else if (lwp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
  696. threads_debug_printf
  697. ("%s stopped by hardware breakpoint",
  698. target_pid_to_str (ptid_of (get_lwp_thread (lwp))).c_str ());
  699. else if (lwp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
  700. threads_debug_printf
  701. ("%s stopped by hardware watchpoint",
  702. target_pid_to_str (ptid_of (get_lwp_thread (lwp))).c_str ());
  703. else if (lwp->stop_reason == TARGET_STOPPED_BY_SINGLE_STEP)
  704. threads_debug_printf
  705. ("%s stopped by trace",
  706. target_pid_to_str (ptid_of (get_lwp_thread (lwp))).c_str ());
  707. lwp->stop_pc = pc;
  708. return true;
  709. }
  710. lwp_info *
  711. linux_process_target::add_lwp (ptid_t ptid)
  712. {
  713. lwp_info *lwp = new lwp_info;
  714. lwp->thread = add_thread (ptid, lwp);
  715. low_new_thread (lwp);
  716. return lwp;
  717. }
  718. void
  719. linux_process_target::low_new_thread (lwp_info *info)
  720. {
  721. /* Nop. */
  722. }
  723. /* Callback to be used when calling fork_inferior, responsible for
  724. actually initiating the tracing of the inferior. */
  725. static void
  726. linux_ptrace_fun ()
  727. {
  728. if (ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0,
  729. (PTRACE_TYPE_ARG4) 0) < 0)
  730. trace_start_error_with_name ("ptrace");
  731. if (setpgid (0, 0) < 0)
  732. trace_start_error_with_name ("setpgid");
  733. /* If GDBserver is connected to gdb via stdio, redirect the inferior's
  734. stdout to stderr so that inferior i/o doesn't corrupt the connection.
  735. Also, redirect stdin to /dev/null. */
  736. if (remote_connection_is_stdio ())
  737. {
  738. if (close (0) < 0)
  739. trace_start_error_with_name ("close");
  740. if (open ("/dev/null", O_RDONLY) < 0)
  741. trace_start_error_with_name ("open");
  742. if (dup2 (2, 1) < 0)
  743. trace_start_error_with_name ("dup2");
  744. if (write (2, "stdin/stdout redirected\n",
  745. sizeof ("stdin/stdout redirected\n") - 1) < 0)
  746. {
  747. /* Errors ignored. */;
  748. }
  749. }
  750. }
  751. /* Start an inferior process and returns its pid.
  752. PROGRAM is the name of the program to be started, and PROGRAM_ARGS
  753. are its arguments. */
  754. int
  755. linux_process_target::create_inferior (const char *program,
  756. const std::vector<char *> &program_args)
  757. {
  758. client_state &cs = get_client_state ();
  759. struct lwp_info *new_lwp;
  760. int pid;
  761. ptid_t ptid;
  762. {
  763. maybe_disable_address_space_randomization restore_personality
  764. (cs.disable_randomization);
  765. std::string str_program_args = construct_inferior_arguments (program_args);
  766. pid = fork_inferior (program,
  767. str_program_args.c_str (),
  768. get_environ ()->envp (), linux_ptrace_fun,
  769. NULL, NULL, NULL, NULL);
  770. }
  771. add_linux_process (pid, 0);
  772. ptid = ptid_t (pid, pid);
  773. new_lwp = add_lwp (ptid);
  774. new_lwp->must_set_ptrace_flags = 1;
  775. post_fork_inferior (pid, program);
  776. return pid;
  777. }
  778. /* Implement the post_create_inferior target_ops method. */
  779. void
  780. linux_process_target::post_create_inferior ()
  781. {
  782. struct lwp_info *lwp = get_thread_lwp (current_thread);
  783. low_arch_setup ();
  784. if (lwp->must_set_ptrace_flags)
  785. {
  786. struct process_info *proc = current_process ();
  787. int options = linux_low_ptrace_options (proc->attached);
  788. linux_enable_event_reporting (lwpid_of (current_thread), options);
  789. lwp->must_set_ptrace_flags = 0;
  790. }
  791. }
  792. int
  793. linux_process_target::attach_lwp (ptid_t ptid)
  794. {
  795. struct lwp_info *new_lwp;
  796. int lwpid = ptid.lwp ();
  797. if (ptrace (PTRACE_ATTACH, lwpid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0)
  798. != 0)
  799. return errno;
  800. new_lwp = add_lwp (ptid);
  801. /* We need to wait for SIGSTOP before being able to make the next
  802. ptrace call on this LWP. */
  803. new_lwp->must_set_ptrace_flags = 1;
  804. if (linux_proc_pid_is_stopped (lwpid))
  805. {
  806. threads_debug_printf ("Attached to a stopped process");
  807. /* The process is definitely stopped. It is in a job control
  808. stop, unless the kernel predates the TASK_STOPPED /
  809. TASK_TRACED distinction, in which case it might be in a
  810. ptrace stop. Make sure it is in a ptrace stop; from there we
  811. can kill it, signal it, et cetera.
  812. First make sure there is a pending SIGSTOP. Since we are
  813. already attached, the process can not transition from stopped
  814. to running without a PTRACE_CONT; so we know this signal will
  815. go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
  816. probably already in the queue (unless this kernel is old
  817. enough to use TASK_STOPPED for ptrace stops); but since
  818. SIGSTOP is not an RT signal, it can only be queued once. */
  819. kill_lwp (lwpid, SIGSTOP);
  820. /* Finally, resume the stopped process. This will deliver the
  821. SIGSTOP (or a higher priority signal, just like normal
  822. PTRACE_ATTACH), which we'll catch later on. */
  823. ptrace (PTRACE_CONT, lwpid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
  824. }
  825. /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
  826. brings it to a halt.
  827. There are several cases to consider here:
  828. 1) gdbserver has already attached to the process and is being notified
  829. of a new thread that is being created.
  830. In this case we should ignore that SIGSTOP and resume the
  831. process. This is handled below by setting stop_expected = 1,
  832. and the fact that add_thread sets last_resume_kind ==
  833. resume_continue.
  834. 2) This is the first thread (the process thread), and we're attaching
  835. to it via attach_inferior.
  836. In this case we want the process thread to stop.
  837. This is handled by having linux_attach set last_resume_kind ==
  838. resume_stop after we return.
  839. If the pid we are attaching to is also the tgid, we attach to and
  840. stop all the existing threads. Otherwise, we attach to pid and
  841. ignore any other threads in the same group as this pid.
  842. 3) GDB is connecting to gdbserver and is requesting an enumeration of all
  843. existing threads.
  844. In this case we want the thread to stop.
  845. FIXME: This case is currently not properly handled.
  846. We should wait for the SIGSTOP but don't. Things work apparently
  847. because enough time passes between when we ptrace (ATTACH) and when
  848. gdb makes the next ptrace call on the thread.
  849. On the other hand, if we are currently trying to stop all threads, we
  850. should treat the new thread as if we had sent it a SIGSTOP. This works
  851. because we are guaranteed that the add_lwp call above added us to the
  852. end of the list, and so the new thread has not yet reached
  853. wait_for_sigstop (but will). */
  854. new_lwp->stop_expected = 1;
  855. return 0;
  856. }
  857. /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
  858. already attached. Returns true if a new LWP is found, false
  859. otherwise. */
  860. static int
  861. attach_proc_task_lwp_callback (ptid_t ptid)
  862. {
  863. /* Is this a new thread? */
  864. if (find_thread_ptid (ptid) == NULL)
  865. {
  866. int lwpid = ptid.lwp ();
  867. int err;
  868. threads_debug_printf ("Found new lwp %d", lwpid);
  869. err = the_linux_target->attach_lwp (ptid);
  870. /* Be quiet if we simply raced with the thread exiting. EPERM
  871. is returned if the thread's task still exists, and is marked
  872. as exited or zombie, as well as other conditions, so in that
  873. case, confirm the status in /proc/PID/status. */
  874. if (err == ESRCH
  875. || (err == EPERM && linux_proc_pid_is_gone (lwpid)))
  876. threads_debug_printf
  877. ("Cannot attach to lwp %d: thread is gone (%d: %s)",
  878. lwpid, err, safe_strerror (err));
  879. else if (err != 0)
  880. {
  881. std::string reason
  882. = linux_ptrace_attach_fail_reason_string (ptid, err);
  883. warning (_("Cannot attach to lwp %d: %s"), lwpid, reason.c_str ());
  884. }
  885. return 1;
  886. }
  887. return 0;
  888. }
  889. static void async_file_mark (void);
  890. /* Attach to PID. If PID is the tgid, attach to it and all
  891. of its threads. */
  892. int
  893. linux_process_target::attach (unsigned long pid)
  894. {
  895. struct process_info *proc;
  896. struct thread_info *initial_thread;
  897. ptid_t ptid = ptid_t (pid, pid);
  898. int err;
  899. proc = add_linux_process (pid, 1);
  900. /* Attach to PID. We will check for other threads
  901. soon. */
  902. err = attach_lwp (ptid);
  903. if (err != 0)
  904. {
  905. remove_process (proc);
  906. std::string reason = linux_ptrace_attach_fail_reason_string (ptid, err);
  907. error ("Cannot attach to process %ld: %s", pid, reason.c_str ());
  908. }
  909. /* Don't ignore the initial SIGSTOP if we just attached to this
  910. process. It will be collected by wait shortly. */
  911. initial_thread = find_thread_ptid (ptid_t (pid, pid));
  912. initial_thread->last_resume_kind = resume_stop;
  913. /* We must attach to every LWP. If /proc is mounted, use that to
  914. find them now. On the one hand, the inferior may be using raw
  915. clone instead of using pthreads. On the other hand, even if it
  916. is using pthreads, GDB may not be connected yet (thread_db needs
  917. to do symbol lookups, through qSymbol). Also, thread_db walks
  918. structures in the inferior's address space to find the list of
  919. threads/LWPs, and those structures may well be corrupted. Note
  920. that once thread_db is loaded, we'll still use it to list threads
  921. and associate pthread info with each LWP. */
  922. linux_proc_attach_tgid_threads (pid, attach_proc_task_lwp_callback);
  923. /* GDB will shortly read the xml target description for this
  924. process, to figure out the process' architecture. But the target
  925. description is only filled in when the first process/thread in
  926. the thread group reports its initial PTRACE_ATTACH SIGSTOP. Do
  927. that now, otherwise, if GDB is fast enough, it could read the
  928. target description _before_ that initial stop. */
  929. if (non_stop)
  930. {
  931. struct lwp_info *lwp;
  932. int wstat, lwpid;
  933. ptid_t pid_ptid = ptid_t (pid);
  934. lwpid = wait_for_event_filtered (pid_ptid, pid_ptid, &wstat, __WALL);
  935. gdb_assert (lwpid > 0);
  936. lwp = find_lwp_pid (ptid_t (lwpid));
  937. if (!WIFSTOPPED (wstat) || WSTOPSIG (wstat) != SIGSTOP)
  938. {
  939. lwp->status_pending_p = 1;
  940. lwp->status_pending = wstat;
  941. }
  942. initial_thread->last_resume_kind = resume_continue;
  943. async_file_mark ();
  944. gdb_assert (proc->tdesc != NULL);
  945. }
  946. return 0;
  947. }
  948. static int
  949. last_thread_of_process_p (int pid)
  950. {
  951. bool seen_one = false;
  952. thread_info *thread = find_thread (pid, [&] (thread_info *thr_arg)
  953. {
  954. if (!seen_one)
  955. {
  956. /* This is the first thread of this process we see. */
  957. seen_one = true;
  958. return false;
  959. }
  960. else
  961. {
  962. /* This is the second thread of this process we see. */
  963. return true;
  964. }
  965. });
  966. return thread == NULL;
  967. }
  968. /* Kill LWP. */
  969. static void
  970. linux_kill_one_lwp (struct lwp_info *lwp)
  971. {
  972. struct thread_info *thr = get_lwp_thread (lwp);
  973. int pid = lwpid_of (thr);
  974. /* PTRACE_KILL is unreliable. After stepping into a signal handler,
  975. there is no signal context, and ptrace(PTRACE_KILL) (or
  976. ptrace(PTRACE_CONT, SIGKILL), pretty much the same) acts like
  977. ptrace(CONT, pid, 0,0) and just resumes the tracee. A better
  978. alternative is to kill with SIGKILL. We only need one SIGKILL
  979. per process, not one for each thread. But since we still support
  980. support debugging programs using raw clone without CLONE_THREAD,
  981. we send one for each thread. For years, we used PTRACE_KILL
  982. only, so we're being a bit paranoid about some old kernels where
  983. PTRACE_KILL might work better (dubious if there are any such, but
  984. that's why it's paranoia), so we try SIGKILL first, PTRACE_KILL
  985. second, and so we're fine everywhere. */
  986. errno = 0;
  987. kill_lwp (pid, SIGKILL);
  988. if (debug_threads)
  989. {
  990. int save_errno = errno;
  991. threads_debug_printf ("kill_lwp (SIGKILL) %s, 0, 0 (%s)",
  992. target_pid_to_str (ptid_of (thr)).c_str (),
  993. save_errno ? safe_strerror (save_errno) : "OK");
  994. }
  995. errno = 0;
  996. ptrace (PTRACE_KILL, pid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
  997. if (debug_threads)
  998. {
  999. int save_errno = errno;
  1000. threads_debug_printf ("PTRACE_KILL %s, 0, 0 (%s)",
  1001. target_pid_to_str (ptid_of (thr)).c_str (),
  1002. save_errno ? safe_strerror (save_errno) : "OK");
  1003. }
  1004. }
  1005. /* Kill LWP and wait for it to die. */
  1006. static void
  1007. kill_wait_lwp (struct lwp_info *lwp)
  1008. {
  1009. struct thread_info *thr = get_lwp_thread (lwp);
  1010. int pid = ptid_of (thr).pid ();
  1011. int lwpid = ptid_of (thr).lwp ();
  1012. int wstat;
  1013. int res;
  1014. threads_debug_printf ("killing lwp %d, for pid: %d", lwpid, pid);
  1015. do
  1016. {
  1017. linux_kill_one_lwp (lwp);
  1018. /* Make sure it died. Notes:
  1019. - The loop is most likely unnecessary.
  1020. - We don't use wait_for_event as that could delete lwps
  1021. while we're iterating over them. We're not interested in
  1022. any pending status at this point, only in making sure all
  1023. wait status on the kernel side are collected until the
  1024. process is reaped.
  1025. - We don't use __WALL here as the __WALL emulation relies on
  1026. SIGCHLD, and killing a stopped process doesn't generate
  1027. one, nor an exit status.
  1028. */
  1029. res = my_waitpid (lwpid, &wstat, 0);
  1030. if (res == -1 && errno == ECHILD)
  1031. res = my_waitpid (lwpid, &wstat, __WCLONE);
  1032. } while (res > 0 && WIFSTOPPED (wstat));
  1033. /* Even if it was stopped, the child may have already disappeared.
  1034. E.g., if it was killed by SIGKILL. */
  1035. if (res < 0 && errno != ECHILD)
  1036. perror_with_name ("kill_wait_lwp");
  1037. }
  1038. /* Callback for `for_each_thread'. Kills an lwp of a given process,
  1039. except the leader. */
  1040. static void
  1041. kill_one_lwp_callback (thread_info *thread, int pid)
  1042. {
  1043. struct lwp_info *lwp = get_thread_lwp (thread);
  1044. /* We avoid killing the first thread here, because of a Linux kernel (at
  1045. least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
  1046. the children get a chance to be reaped, it will remain a zombie
  1047. forever. */
  1048. if (lwpid_of (thread) == pid)
  1049. {
  1050. threads_debug_printf ("is last of process %s",
  1051. target_pid_to_str (thread->id).c_str ());
  1052. return;
  1053. }
  1054. kill_wait_lwp (lwp);
  1055. }
  1056. int
  1057. linux_process_target::kill (process_info *process)
  1058. {
  1059. int pid = process->pid;
  1060. /* If we're killing a running inferior, make sure it is stopped
  1061. first, as PTRACE_KILL will not work otherwise. */
  1062. stop_all_lwps (0, NULL);
  1063. for_each_thread (pid, [&] (thread_info *thread)
  1064. {
  1065. kill_one_lwp_callback (thread, pid);
  1066. });
  1067. /* See the comment in linux_kill_one_lwp. We did not kill the first
  1068. thread in the list, so do so now. */
  1069. lwp_info *lwp = find_lwp_pid (ptid_t (pid));
  1070. if (lwp == NULL)
  1071. threads_debug_printf ("cannot find lwp for pid: %d", pid);
  1072. else
  1073. kill_wait_lwp (lwp);
  1074. mourn (process);
  1075. /* Since we presently can only stop all lwps of all processes, we
  1076. need to unstop lwps of other processes. */
  1077. unstop_all_lwps (0, NULL);
  1078. return 0;
  1079. }
  1080. /* Get pending signal of THREAD, for detaching purposes. This is the
  1081. signal the thread last stopped for, which we need to deliver to the
  1082. thread when detaching, otherwise, it'd be suppressed/lost. */
  1083. static int
  1084. get_detach_signal (struct thread_info *thread)
  1085. {
  1086. client_state &cs = get_client_state ();
  1087. enum gdb_signal signo = GDB_SIGNAL_0;
  1088. int status;
  1089. struct lwp_info *lp = get_thread_lwp (thread);
  1090. if (lp->status_pending_p)
  1091. status = lp->status_pending;
  1092. else
  1093. {
  1094. /* If the thread had been suspended by gdbserver, and it stopped
  1095. cleanly, then it'll have stopped with SIGSTOP. But we don't
  1096. want to deliver that SIGSTOP. */
  1097. if (thread->last_status.kind () != TARGET_WAITKIND_STOPPED
  1098. || thread->last_status.sig () == GDB_SIGNAL_0)
  1099. return 0;
  1100. /* Otherwise, we may need to deliver the signal we
  1101. intercepted. */
  1102. status = lp->last_status;
  1103. }
  1104. if (!WIFSTOPPED (status))
  1105. {
  1106. threads_debug_printf ("lwp %s hasn't stopped: no pending signal",
  1107. target_pid_to_str (ptid_of (thread)).c_str ());
  1108. return 0;
  1109. }
  1110. /* Extended wait statuses aren't real SIGTRAPs. */
  1111. if (WSTOPSIG (status) == SIGTRAP && linux_is_extended_waitstatus (status))
  1112. {
  1113. threads_debug_printf ("lwp %s had stopped with extended "
  1114. "status: no pending signal",
  1115. target_pid_to_str (ptid_of (thread)).c_str ());
  1116. return 0;
  1117. }
  1118. signo = gdb_signal_from_host (WSTOPSIG (status));
  1119. if (cs.program_signals_p && !cs.program_signals[signo])
  1120. {
  1121. threads_debug_printf ("lwp %s had signal %s, but it is in nopass state",
  1122. target_pid_to_str (ptid_of (thread)).c_str (),
  1123. gdb_signal_to_string (signo));
  1124. return 0;
  1125. }
  1126. else if (!cs.program_signals_p
  1127. /* If we have no way to know which signals GDB does not
  1128. want to have passed to the program, assume
  1129. SIGTRAP/SIGINT, which is GDB's default. */
  1130. && (signo == GDB_SIGNAL_TRAP || signo == GDB_SIGNAL_INT))
  1131. {
  1132. threads_debug_printf ("lwp %s had signal %s, "
  1133. "but we don't know if we should pass it. "
  1134. "Default to not.",
  1135. target_pid_to_str (ptid_of (thread)).c_str (),
  1136. gdb_signal_to_string (signo));
  1137. return 0;
  1138. }
  1139. else
  1140. {
  1141. threads_debug_printf ("lwp %s has pending signal %s: delivering it",
  1142. target_pid_to_str (ptid_of (thread)).c_str (),
  1143. gdb_signal_to_string (signo));
  1144. return WSTOPSIG (status);
  1145. }
  1146. }
  1147. void
  1148. linux_process_target::detach_one_lwp (lwp_info *lwp)
  1149. {
  1150. struct thread_info *thread = get_lwp_thread (lwp);
  1151. int sig;
  1152. int lwpid;
  1153. /* If there is a pending SIGSTOP, get rid of it. */
  1154. if (lwp->stop_expected)
  1155. {
  1156. threads_debug_printf ("Sending SIGCONT to %s",
  1157. target_pid_to_str (ptid_of (thread)).c_str ());
  1158. kill_lwp (lwpid_of (thread), SIGCONT);
  1159. lwp->stop_expected = 0;
  1160. }
  1161. /* Pass on any pending signal for this thread. */
  1162. sig = get_detach_signal (thread);
  1163. /* Preparing to resume may try to write registers, and fail if the
  1164. lwp is zombie. If that happens, ignore the error. We'll handle
  1165. it below, when detach fails with ESRCH. */
  1166. try
  1167. {
  1168. /* Flush any pending changes to the process's registers. */
  1169. regcache_invalidate_thread (thread);
  1170. /* Finally, let it resume. */
  1171. low_prepare_to_resume (lwp);
  1172. }
  1173. catch (const gdb_exception_error &ex)
  1174. {
  1175. if (!check_ptrace_stopped_lwp_gone (lwp))
  1176. throw;
  1177. }
  1178. lwpid = lwpid_of (thread);
  1179. if (ptrace (PTRACE_DETACH, lwpid, (PTRACE_TYPE_ARG3) 0,
  1180. (PTRACE_TYPE_ARG4) (long) sig) < 0)
  1181. {
  1182. int save_errno = errno;
  1183. /* We know the thread exists, so ESRCH must mean the lwp is
  1184. zombie. This can happen if one of the already-detached
  1185. threads exits the whole thread group. In that case we're
  1186. still attached, and must reap the lwp. */
  1187. if (save_errno == ESRCH)
  1188. {
  1189. int ret, status;
  1190. ret = my_waitpid (lwpid, &status, __WALL);
  1191. if (ret == -1)
  1192. {
  1193. warning (_("Couldn't reap LWP %d while detaching: %s"),
  1194. lwpid, safe_strerror (errno));
  1195. }
  1196. else if (!WIFEXITED (status) && !WIFSIGNALED (status))
  1197. {
  1198. warning (_("Reaping LWP %d while detaching "
  1199. "returned unexpected status 0x%x"),
  1200. lwpid, status);
  1201. }
  1202. }
  1203. else
  1204. {
  1205. error (_("Can't detach %s: %s"),
  1206. target_pid_to_str (ptid_of (thread)).c_str (),
  1207. safe_strerror (save_errno));
  1208. }
  1209. }
  1210. else
  1211. threads_debug_printf ("PTRACE_DETACH (%s, %s, 0) (OK)",
  1212. target_pid_to_str (ptid_of (thread)).c_str (),
  1213. strsignal (sig));
  1214. delete_lwp (lwp);
  1215. }
  1216. int
  1217. linux_process_target::detach (process_info *process)
  1218. {
  1219. struct lwp_info *main_lwp;
  1220. /* As there's a step over already in progress, let it finish first,
  1221. otherwise nesting a stabilize_threads operation on top gets real
  1222. messy. */
  1223. complete_ongoing_step_over ();
  1224. /* Stop all threads before detaching. First, ptrace requires that
  1225. the thread is stopped to successfully detach. Second, thread_db
  1226. may need to uninstall thread event breakpoints from memory, which
  1227. only works with a stopped process anyway. */
  1228. stop_all_lwps (0, NULL);
  1229. #ifdef USE_THREAD_DB
  1230. thread_db_detach (process);
  1231. #endif
  1232. /* Stabilize threads (move out of jump pads). */
  1233. target_stabilize_threads ();
  1234. /* Detach from the clone lwps first. If the thread group exits just
  1235. while we're detaching, we must reap the clone lwps before we're
  1236. able to reap the leader. */
  1237. for_each_thread (process->pid, [this] (thread_info *thread)
  1238. {
  1239. /* We don't actually detach from the thread group leader just yet.
  1240. If the thread group exits, we must reap the zombie clone lwps
  1241. before we're able to reap the leader. */
  1242. if (thread->id.pid () == thread->id.lwp ())
  1243. return;
  1244. lwp_info *lwp = get_thread_lwp (thread);
  1245. detach_one_lwp (lwp);
  1246. });
  1247. main_lwp = find_lwp_pid (ptid_t (process->pid));
  1248. detach_one_lwp (main_lwp);
  1249. mourn (process);
  1250. /* Since we presently can only stop all lwps of all processes, we
  1251. need to unstop lwps of other processes. */
  1252. unstop_all_lwps (0, NULL);
  1253. return 0;
  1254. }
  1255. /* Remove all LWPs that belong to process PROC from the lwp list. */
  1256. void
  1257. linux_process_target::mourn (process_info *process)
  1258. {
  1259. struct process_info_private *priv;
  1260. #ifdef USE_THREAD_DB
  1261. thread_db_mourn (process);
  1262. #endif
  1263. for_each_thread (process->pid, [this] (thread_info *thread)
  1264. {
  1265. delete_lwp (get_thread_lwp (thread));
  1266. });
  1267. /* Freeing all private data. */
  1268. priv = process->priv;
  1269. low_delete_process (priv->arch_private);
  1270. free (priv);
  1271. process->priv = NULL;
  1272. remove_process (process);
  1273. }
  1274. void
  1275. linux_process_target::join (int pid)
  1276. {
  1277. int status, ret;
  1278. do {
  1279. ret = my_waitpid (pid, &status, 0);
  1280. if (WIFEXITED (status) || WIFSIGNALED (status))
  1281. break;
  1282. } while (ret != -1 || errno != ECHILD);
  1283. }
  1284. /* Return true if the given thread is still alive. */
  1285. bool
  1286. linux_process_target::thread_alive (ptid_t ptid)
  1287. {
  1288. struct lwp_info *lwp = find_lwp_pid (ptid);
  1289. /* We assume we always know if a thread exits. If a whole process
  1290. exited but we still haven't been able to report it to GDB, we'll
  1291. hold on to the last lwp of the dead process. */
  1292. if (lwp != NULL)
  1293. return !lwp_is_marked_dead (lwp);
  1294. else
  1295. return 0;
  1296. }
  1297. bool
  1298. linux_process_target::thread_still_has_status_pending (thread_info *thread)
  1299. {
  1300. struct lwp_info *lp = get_thread_lwp (thread);
  1301. if (!lp->status_pending_p)
  1302. return 0;
  1303. if (thread->last_resume_kind != resume_stop
  1304. && (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
  1305. || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT))
  1306. {
  1307. CORE_ADDR pc;
  1308. int discard = 0;
  1309. gdb_assert (lp->last_status != 0);
  1310. pc = get_pc (lp);
  1311. scoped_restore_current_thread restore_thread;
  1312. switch_to_thread (thread);
  1313. if (pc != lp->stop_pc)
  1314. {
  1315. threads_debug_printf ("PC of %ld changed",
  1316. lwpid_of (thread));
  1317. discard = 1;
  1318. }
  1319. #if !USE_SIGTRAP_SIGINFO
  1320. else if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
  1321. && !low_breakpoint_at (pc))
  1322. {
  1323. threads_debug_printf ("previous SW breakpoint of %ld gone",
  1324. lwpid_of (thread));
  1325. discard = 1;
  1326. }
  1327. else if (lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT
  1328. && !hardware_breakpoint_inserted_here (pc))
  1329. {
  1330. threads_debug_printf ("previous HW breakpoint of %ld gone",
  1331. lwpid_of (thread));
  1332. discard = 1;
  1333. }
  1334. #endif
  1335. if (discard)
  1336. {
  1337. threads_debug_printf ("discarding pending breakpoint status");
  1338. lp->status_pending_p = 0;
  1339. return 0;
  1340. }
  1341. }
  1342. return 1;
  1343. }
  1344. /* Returns true if LWP is resumed from the client's perspective. */
  1345. static int
  1346. lwp_resumed (struct lwp_info *lwp)
  1347. {
  1348. struct thread_info *thread = get_lwp_thread (lwp);
  1349. if (thread->last_resume_kind != resume_stop)
  1350. return 1;
  1351. /* Did gdb send us a `vCont;t', but we haven't reported the
  1352. corresponding stop to gdb yet? If so, the thread is still
  1353. resumed/running from gdb's perspective. */
  1354. if (thread->last_resume_kind == resume_stop
  1355. && thread->last_status.kind () == TARGET_WAITKIND_IGNORE)
  1356. return 1;
  1357. return 0;
  1358. }
  1359. bool
  1360. linux_process_target::status_pending_p_callback (thread_info *thread,
  1361. ptid_t ptid)
  1362. {
  1363. struct lwp_info *lp = get_thread_lwp (thread);
  1364. /* Check if we're only interested in events from a specific process
  1365. or a specific LWP. */
  1366. if (!thread->id.matches (ptid))
  1367. return 0;
  1368. if (!lwp_resumed (lp))
  1369. return 0;
  1370. if (lp->status_pending_p
  1371. && !thread_still_has_status_pending (thread))
  1372. {
  1373. resume_one_lwp (lp, lp->stepping, GDB_SIGNAL_0, NULL);
  1374. return 0;
  1375. }
  1376. return lp->status_pending_p;
  1377. }
  1378. struct lwp_info *
  1379. find_lwp_pid (ptid_t ptid)
  1380. {
  1381. thread_info *thread = find_thread ([&] (thread_info *thr_arg)
  1382. {
  1383. int lwp = ptid.lwp () != 0 ? ptid.lwp () : ptid.pid ();
  1384. return thr_arg->id.lwp () == lwp;
  1385. });
  1386. if (thread == NULL)
  1387. return NULL;
  1388. return get_thread_lwp (thread);
  1389. }
  1390. /* Return the number of known LWPs in the tgid given by PID. */
  1391. static int
  1392. num_lwps (int pid)
  1393. {
  1394. int count = 0;
  1395. for_each_thread (pid, [&] (thread_info *thread)
  1396. {
  1397. count++;
  1398. });
  1399. return count;
  1400. }
  1401. /* See nat/linux-nat.h. */
  1402. struct lwp_info *
  1403. iterate_over_lwps (ptid_t filter,
  1404. gdb::function_view<iterate_over_lwps_ftype> callback)
  1405. {
  1406. thread_info *thread = find_thread (filter, [&] (thread_info *thr_arg)
  1407. {
  1408. lwp_info *lwp = get_thread_lwp (thr_arg);
  1409. return callback (lwp);
  1410. });
  1411. if (thread == NULL)
  1412. return NULL;
  1413. return get_thread_lwp (thread);
  1414. }
  1415. void
  1416. linux_process_target::check_zombie_leaders ()
  1417. {
  1418. for_each_process ([this] (process_info *proc)
  1419. {
  1420. pid_t leader_pid = pid_of (proc);
  1421. lwp_info *leader_lp = find_lwp_pid (ptid_t (leader_pid));
  1422. threads_debug_printf ("leader_pid=%d, leader_lp!=NULL=%d, "
  1423. "num_lwps=%d, zombie=%d",
  1424. leader_pid, leader_lp!= NULL, num_lwps (leader_pid),
  1425. linux_proc_pid_is_zombie (leader_pid));
  1426. if (leader_lp != NULL && !leader_lp->stopped
  1427. /* Check if there are other threads in the group, as we may
  1428. have raced with the inferior simply exiting. Note this
  1429. isn't a watertight check. If the inferior is
  1430. multi-threaded and is exiting, it may be we see the
  1431. leader as zombie before we reap all the non-leader
  1432. threads. See comments below. */
  1433. && !last_thread_of_process_p (leader_pid)
  1434. && linux_proc_pid_is_zombie (leader_pid))
  1435. {
  1436. /* A zombie leader in a multi-threaded program can mean one
  1437. of three things:
  1438. #1 - Only the leader exited, not the whole program, e.g.,
  1439. with pthread_exit. Since we can't reap the leader's exit
  1440. status until all other threads are gone and reaped too,
  1441. we want to delete the zombie leader right away, as it
  1442. can't be debugged, we can't read its registers, etc.
  1443. This is the main reason we check for zombie leaders
  1444. disappearing.
  1445. #2 - The whole thread-group/process exited (a group exit,
  1446. via e.g. exit(3), and there is (or will be shortly) an
  1447. exit reported for each thread in the process, and then
  1448. finally an exit for the leader once the non-leaders are
  1449. reaped.
  1450. #3 - There are 3 or more threads in the group, and a
  1451. thread other than the leader exec'd. See comments on
  1452. exec events at the top of the file.
  1453. Ideally we would never delete the leader for case #2.
  1454. Instead, we want to collect the exit status of each
  1455. non-leader thread, and then finally collect the exit
  1456. status of the leader as normal and use its exit code as
  1457. whole-process exit code. Unfortunately, there's no
  1458. race-free way to distinguish cases #1 and #2. We can't
  1459. assume the exit events for the non-leaders threads are
  1460. already pending in the kernel, nor can we assume the
  1461. non-leader threads are in zombie state already. Between
  1462. the leader becoming zombie and the non-leaders exiting
  1463. and becoming zombie themselves, there's a small time
  1464. window, so such a check would be racy. Temporarily
  1465. pausing all threads and checking to see if all threads
  1466. exit or not before re-resuming them would work in the
  1467. case that all threads are running right now, but it
  1468. wouldn't work if some thread is currently already
  1469. ptrace-stopped, e.g., due to scheduler-locking.
  1470. So what we do is we delete the leader anyhow, and then
  1471. later on when we see its exit status, we re-add it back.
  1472. We also make sure that we only report a whole-process
  1473. exit when we see the leader exiting, as opposed to when
  1474. the last LWP in the LWP list exits, which can be a
  1475. non-leader if we deleted the leader here. */
  1476. threads_debug_printf ("Thread group leader %d zombie "
  1477. "(it exited, or another thread execd), "
  1478. "deleting it.",
  1479. leader_pid);
  1480. delete_lwp (leader_lp);
  1481. }
  1482. });
  1483. }
  1484. /* Callback for `find_thread'. Returns the first LWP that is not
  1485. stopped. */
  1486. static bool
  1487. not_stopped_callback (thread_info *thread, ptid_t filter)
  1488. {
  1489. if (!thread->id.matches (filter))
  1490. return false;
  1491. lwp_info *lwp = get_thread_lwp (thread);
  1492. return !lwp->stopped;
  1493. }
  1494. /* Increment LWP's suspend count. */
  1495. static void
  1496. lwp_suspended_inc (struct lwp_info *lwp)
  1497. {
  1498. lwp->suspended++;
  1499. if (lwp->suspended > 4)
  1500. threads_debug_printf
  1501. ("LWP %ld has a suspiciously high suspend count, suspended=%d",
  1502. lwpid_of (get_lwp_thread (lwp)), lwp->suspended);
  1503. }
  1504. /* Decrement LWP's suspend count. */
  1505. static void
  1506. lwp_suspended_decr (struct lwp_info *lwp)
  1507. {
  1508. lwp->suspended--;
  1509. if (lwp->suspended < 0)
  1510. {
  1511. struct thread_info *thread = get_lwp_thread (lwp);
  1512. internal_error (__FILE__, __LINE__,
  1513. "unsuspend LWP %ld, suspended=%d\n", lwpid_of (thread),
  1514. lwp->suspended);
  1515. }
  1516. }
  1517. /* This function should only be called if the LWP got a SIGTRAP.
  1518. Handle any tracepoint steps or hits. Return true if a tracepoint
  1519. event was handled, 0 otherwise. */
  1520. static int
  1521. handle_tracepoints (struct lwp_info *lwp)
  1522. {
  1523. struct thread_info *tinfo = get_lwp_thread (lwp);
  1524. int tpoint_related_event = 0;
  1525. gdb_assert (lwp->suspended == 0);
  1526. /* If this tracepoint hit causes a tracing stop, we'll immediately
  1527. uninsert tracepoints. To do this, we temporarily pause all
  1528. threads, unpatch away, and then unpause threads. We need to make
  1529. sure the unpausing doesn't resume LWP too. */
  1530. lwp_suspended_inc (lwp);
  1531. /* And we need to be sure that any all-threads-stopping doesn't try
  1532. to move threads out of the jump pads, as it could deadlock the
  1533. inferior (LWP could be in the jump pad, maybe even holding the
  1534. lock.) */
  1535. /* Do any necessary step collect actions. */
  1536. tpoint_related_event |= tracepoint_finished_step (tinfo, lwp->stop_pc);
  1537. tpoint_related_event |= handle_tracepoint_bkpts (tinfo, lwp->stop_pc);
  1538. /* See if we just hit a tracepoint and do its main collect
  1539. actions. */
  1540. tpoint_related_event |= tracepoint_was_hit (tinfo, lwp->stop_pc);
  1541. lwp_suspended_decr (lwp);
  1542. gdb_assert (lwp->suspended == 0);
  1543. gdb_assert (!stabilizing_threads
  1544. || (lwp->collecting_fast_tracepoint
  1545. != fast_tpoint_collect_result::not_collecting));
  1546. if (tpoint_related_event)
  1547. {
  1548. threads_debug_printf ("got a tracepoint event");
  1549. return 1;
  1550. }
  1551. return 0;
  1552. }
  1553. fast_tpoint_collect_result
  1554. linux_process_target::linux_fast_tracepoint_collecting
  1555. (lwp_info *lwp, fast_tpoint_collect_status *status)
  1556. {
  1557. CORE_ADDR thread_area;
  1558. struct thread_info *thread = get_lwp_thread (lwp);
  1559. /* Get the thread area address. This is used to recognize which
  1560. thread is which when tracing with the in-process agent library.
  1561. We don't read anything from the address, and treat it as opaque;
  1562. it's the address itself that we assume is unique per-thread. */
  1563. if (low_get_thread_area (lwpid_of (thread), &thread_area) == -1)
  1564. return fast_tpoint_collect_result::not_collecting;
  1565. return fast_tracepoint_collecting (thread_area, lwp->stop_pc, status);
  1566. }
  1567. int
  1568. linux_process_target::low_get_thread_area (int lwpid, CORE_ADDR *addrp)
  1569. {
  1570. return -1;
  1571. }
  1572. bool
  1573. linux_process_target::maybe_move_out_of_jump_pad (lwp_info *lwp, int *wstat)
  1574. {
  1575. scoped_restore_current_thread restore_thread;
  1576. switch_to_thread (get_lwp_thread (lwp));
  1577. if ((wstat == NULL
  1578. || (WIFSTOPPED (*wstat) && WSTOPSIG (*wstat) != SIGTRAP))
  1579. && supports_fast_tracepoints ()
  1580. && agent_loaded_p ())
  1581. {
  1582. struct fast_tpoint_collect_status status;
  1583. threads_debug_printf
  1584. ("Checking whether LWP %ld needs to move out of the jump pad.",
  1585. lwpid_of (current_thread));
  1586. fast_tpoint_collect_result r
  1587. = linux_fast_tracepoint_collecting (lwp, &status);
  1588. if (wstat == NULL
  1589. || (WSTOPSIG (*wstat) != SIGILL
  1590. && WSTOPSIG (*wstat) != SIGFPE
  1591. && WSTOPSIG (*wstat) != SIGSEGV
  1592. && WSTOPSIG (*wstat) != SIGBUS))
  1593. {
  1594. lwp->collecting_fast_tracepoint = r;
  1595. if (r != fast_tpoint_collect_result::not_collecting)
  1596. {
  1597. if (r == fast_tpoint_collect_result::before_insn
  1598. && lwp->exit_jump_pad_bkpt == NULL)
  1599. {
  1600. /* Haven't executed the original instruction yet.
  1601. Set breakpoint there, and wait till it's hit,
  1602. then single-step until exiting the jump pad. */
  1603. lwp->exit_jump_pad_bkpt
  1604. = set_breakpoint_at (status.adjusted_insn_addr, NULL);
  1605. }
  1606. threads_debug_printf
  1607. ("Checking whether LWP %ld needs to move out of the jump pad..."
  1608. " it does", lwpid_of (current_thread));
  1609. return true;
  1610. }
  1611. }
  1612. else
  1613. {
  1614. /* If we get a synchronous signal while collecting, *and*
  1615. while executing the (relocated) original instruction,
  1616. reset the PC to point at the tpoint address, before
  1617. reporting to GDB. Otherwise, it's an IPA lib bug: just
  1618. report the signal to GDB, and pray for the best. */
  1619. lwp->collecting_fast_tracepoint
  1620. = fast_tpoint_collect_result::not_collecting;
  1621. if (r != fast_tpoint_collect_result::not_collecting
  1622. && (status.adjusted_insn_addr <= lwp->stop_pc
  1623. && lwp->stop_pc < status.adjusted_insn_addr_end))
  1624. {
  1625. siginfo_t info;
  1626. struct regcache *regcache;
  1627. /* The si_addr on a few signals references the address
  1628. of the faulting instruction. Adjust that as
  1629. well. */
  1630. if ((WSTOPSIG (*wstat) == SIGILL
  1631. || WSTOPSIG (*wstat) == SIGFPE
  1632. || WSTOPSIG (*wstat) == SIGBUS
  1633. || WSTOPSIG (*wstat) == SIGSEGV)
  1634. && ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread),
  1635. (PTRACE_TYPE_ARG3) 0, &info) == 0
  1636. /* Final check just to make sure we don't clobber
  1637. the siginfo of non-kernel-sent signals. */
  1638. && (uintptr_t) info.si_addr == lwp->stop_pc)
  1639. {
  1640. info.si_addr = (void *) (uintptr_t) status.tpoint_addr;
  1641. ptrace (PTRACE_SETSIGINFO, lwpid_of (current_thread),
  1642. (PTRACE_TYPE_ARG3) 0, &info);
  1643. }
  1644. regcache = get_thread_regcache (current_thread, 1);
  1645. low_set_pc (regcache, status.tpoint_addr);
  1646. lwp->stop_pc = status.tpoint_addr;
  1647. /* Cancel any fast tracepoint lock this thread was
  1648. holding. */
  1649. force_unlock_trace_buffer ();
  1650. }
  1651. if (lwp->exit_jump_pad_bkpt != NULL)
  1652. {
  1653. threads_debug_printf
  1654. ("Cancelling fast exit-jump-pad: removing bkpt."
  1655. "stopping all threads momentarily.");
  1656. stop_all_lwps (1, lwp);
  1657. delete_breakpoint (lwp->exit_jump_pad_bkpt);
  1658. lwp->exit_jump_pad_bkpt = NULL;
  1659. unstop_all_lwps (1, lwp);
  1660. gdb_assert (lwp->suspended >= 0);
  1661. }
  1662. }
  1663. }
  1664. threads_debug_printf
  1665. ("Checking whether LWP %ld needs to move out of the jump pad... no",
  1666. lwpid_of (current_thread));
  1667. return false;
  1668. }
  1669. /* Enqueue one signal in the "signals to report later when out of the
  1670. jump pad" list. */
  1671. static void
  1672. enqueue_one_deferred_signal (struct lwp_info *lwp, int *wstat)
  1673. {
  1674. struct thread_info *thread = get_lwp_thread (lwp);
  1675. threads_debug_printf ("Deferring signal %d for LWP %ld.",
  1676. WSTOPSIG (*wstat), lwpid_of (thread));
  1677. if (debug_threads)
  1678. {
  1679. for (const auto &sig : lwp->pending_signals_to_report)
  1680. threads_debug_printf (" Already queued %d", sig.signal);
  1681. threads_debug_printf (" (no more currently queued signals)");
  1682. }
  1683. /* Don't enqueue non-RT signals if they are already in the deferred
  1684. queue. (SIGSTOP being the easiest signal to see ending up here
  1685. twice) */
  1686. if (WSTOPSIG (*wstat) < __SIGRTMIN)
  1687. {
  1688. for (const auto &sig : lwp->pending_signals_to_report)
  1689. {
  1690. if (sig.signal == WSTOPSIG (*wstat))
  1691. {
  1692. threads_debug_printf
  1693. ("Not requeuing already queued non-RT signal %d for LWP %ld",
  1694. sig.signal, lwpid_of (thread));
  1695. return;
  1696. }
  1697. }
  1698. }
  1699. lwp->pending_signals_to_report.emplace_back (WSTOPSIG (*wstat));
  1700. ptrace (PTRACE_GETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
  1701. &lwp->pending_signals_to_report.back ().info);
  1702. }
  1703. /* Dequeue one signal from the "signals to report later when out of
  1704. the jump pad" list. */
  1705. static int
  1706. dequeue_one_deferred_signal (struct lwp_info *lwp, int *wstat)
  1707. {
  1708. struct thread_info *thread = get_lwp_thread (lwp);
  1709. if (!lwp->pending_signals_to_report.empty ())
  1710. {
  1711. const pending_signal &p_sig = lwp->pending_signals_to_report.front ();
  1712. *wstat = W_STOPCODE (p_sig.signal);
  1713. if (p_sig.info.si_signo != 0)
  1714. ptrace (PTRACE_SETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
  1715. &p_sig.info);
  1716. lwp->pending_signals_to_report.pop_front ();
  1717. threads_debug_printf ("Reporting deferred signal %d for LWP %ld.",
  1718. WSTOPSIG (*wstat), lwpid_of (thread));
  1719. if (debug_threads)
  1720. {
  1721. for (const auto &sig : lwp->pending_signals_to_report)
  1722. threads_debug_printf (" Still queued %d", sig.signal);
  1723. threads_debug_printf (" (no more queued signals)");
  1724. }
  1725. return 1;
  1726. }
  1727. return 0;
  1728. }
  1729. bool
  1730. linux_process_target::check_stopped_by_watchpoint (lwp_info *child)
  1731. {
  1732. scoped_restore_current_thread restore_thread;
  1733. switch_to_thread (get_lwp_thread (child));
  1734. if (low_stopped_by_watchpoint ())
  1735. {
  1736. child->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
  1737. child->stopped_data_address = low_stopped_data_address ();
  1738. }
  1739. return child->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
  1740. }
  1741. bool
  1742. linux_process_target::low_stopped_by_watchpoint ()
  1743. {
  1744. return false;
  1745. }
  1746. CORE_ADDR
  1747. linux_process_target::low_stopped_data_address ()
  1748. {
  1749. return 0;
  1750. }
  1751. /* Return the ptrace options that we want to try to enable. */
  1752. static int
  1753. linux_low_ptrace_options (int attached)
  1754. {
  1755. client_state &cs = get_client_state ();
  1756. int options = 0;
  1757. if (!attached)
  1758. options |= PTRACE_O_EXITKILL;
  1759. if (cs.report_fork_events)
  1760. options |= PTRACE_O_TRACEFORK;
  1761. if (cs.report_vfork_events)
  1762. options |= (PTRACE_O_TRACEVFORK | PTRACE_O_TRACEVFORKDONE);
  1763. if (cs.report_exec_events)
  1764. options |= PTRACE_O_TRACEEXEC;
  1765. options |= PTRACE_O_TRACESYSGOOD;
  1766. return options;
  1767. }
  1768. void
  1769. linux_process_target::filter_event (int lwpid, int wstat)
  1770. {
  1771. client_state &cs = get_client_state ();
  1772. struct lwp_info *child;
  1773. struct thread_info *thread;
  1774. int have_stop_pc = 0;
  1775. child = find_lwp_pid (ptid_t (lwpid));
  1776. /* Check for events reported by anything not in our LWP list. */
  1777. if (child == nullptr)
  1778. {
  1779. if (WIFSTOPPED (wstat))
  1780. {
  1781. if (WSTOPSIG (wstat) == SIGTRAP
  1782. && linux_ptrace_get_extended_event (wstat) == PTRACE_EVENT_EXEC)
  1783. {
  1784. /* A non-leader thread exec'ed after we've seen the
  1785. leader zombie, and removed it from our lists (in
  1786. check_zombie_leaders). The non-leader thread changes
  1787. its tid to the tgid. */
  1788. threads_debug_printf
  1789. ("Re-adding thread group leader LWP %d after exec.",
  1790. lwpid);
  1791. child = add_lwp (ptid_t (lwpid, lwpid));
  1792. child->stopped = 1;
  1793. switch_to_thread (child->thread);
  1794. }
  1795. else
  1796. {
  1797. /* A process we are controlling has forked and the new
  1798. child's stop was reported to us by the kernel. Save
  1799. its PID and go back to waiting for the fork event to
  1800. be reported - the stopped process might be returned
  1801. from waitpid before or after the fork event is. */
  1802. threads_debug_printf
  1803. ("Saving LWP %d status %s in stopped_pids list",
  1804. lwpid, status_to_str (wstat).c_str ());
  1805. add_to_pid_list (&stopped_pids, lwpid, wstat);
  1806. }
  1807. }
  1808. else
  1809. {
  1810. /* Don't report an event for the exit of an LWP not in our
  1811. list, i.e. not part of any inferior we're debugging.
  1812. This can happen if we detach from a program we originally
  1813. forked and then it exits. However, note that we may have
  1814. earlier deleted a leader of an inferior we're debugging,
  1815. in check_zombie_leaders. Re-add it back here if so. */
  1816. find_process ([&] (process_info *proc)
  1817. {
  1818. if (proc->pid == lwpid)
  1819. {
  1820. threads_debug_printf
  1821. ("Re-adding thread group leader LWP %d after exit.",
  1822. lwpid);
  1823. child = add_lwp (ptid_t (lwpid, lwpid));
  1824. return true;
  1825. }
  1826. return false;
  1827. });
  1828. }
  1829. if (child == nullptr)
  1830. return;
  1831. }
  1832. thread = get_lwp_thread (child);
  1833. child->stopped = 1;
  1834. child->last_status = wstat;
  1835. /* Check if the thread has exited. */
  1836. if ((WIFEXITED (wstat) || WIFSIGNALED (wstat)))
  1837. {
  1838. threads_debug_printf ("%d exited", lwpid);
  1839. if (finish_step_over (child))
  1840. {
  1841. /* Unsuspend all other LWPs, and set them back running again. */
  1842. unsuspend_all_lwps (child);
  1843. }
  1844. /* If this is not the leader LWP, then the exit signal was not
  1845. the end of the debugged application and should be ignored,
  1846. unless GDB wants to hear about thread exits. */
  1847. if (cs.report_thread_events || is_leader (thread))
  1848. {
  1849. /* Since events are serialized to GDB core, and we can't
  1850. report this one right now. Leave the status pending for
  1851. the next time we're able to report it. */
  1852. mark_lwp_dead (child, wstat);
  1853. return;
  1854. }
  1855. else
  1856. {
  1857. delete_lwp (child);
  1858. return;
  1859. }
  1860. }
  1861. gdb_assert (WIFSTOPPED (wstat));
  1862. if (WIFSTOPPED (wstat))
  1863. {
  1864. struct process_info *proc;
  1865. /* Architecture-specific setup after inferior is running. */
  1866. proc = find_process_pid (pid_of (thread));
  1867. if (proc->tdesc == NULL)
  1868. {
  1869. if (proc->attached)
  1870. {
  1871. /* This needs to happen after we have attached to the
  1872. inferior and it is stopped for the first time, but
  1873. before we access any inferior registers. */
  1874. arch_setup_thread (thread);
  1875. }
  1876. else
  1877. {
  1878. /* The process is started, but GDBserver will do
  1879. architecture-specific setup after the program stops at
  1880. the first instruction. */
  1881. child->status_pending_p = 1;
  1882. child->status_pending = wstat;
  1883. return;
  1884. }
  1885. }
  1886. }
  1887. if (WIFSTOPPED (wstat) && child->must_set_ptrace_flags)
  1888. {
  1889. struct process_info *proc = find_process_pid (pid_of (thread));
  1890. int options = linux_low_ptrace_options (proc->attached);
  1891. linux_enable_event_reporting (lwpid, options);
  1892. child->must_set_ptrace_flags = 0;
  1893. }
  1894. /* Always update syscall_state, even if it will be filtered later. */
  1895. if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SYSCALL_SIGTRAP)
  1896. {
  1897. child->syscall_state
  1898. = (child->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
  1899. ? TARGET_WAITKIND_SYSCALL_RETURN
  1900. : TARGET_WAITKIND_SYSCALL_ENTRY);
  1901. }
  1902. else
  1903. {
  1904. /* Almost all other ptrace-stops are known to be outside of system
  1905. calls, with further exceptions in handle_extended_wait. */
  1906. child->syscall_state = TARGET_WAITKIND_IGNORE;
  1907. }
  1908. /* Be careful to not overwrite stop_pc until save_stop_reason is
  1909. called. */
  1910. if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SIGTRAP
  1911. && linux_is_extended_waitstatus (wstat))
  1912. {
  1913. child->stop_pc = get_pc (child);
  1914. if (handle_extended_wait (&child, wstat))
  1915. {
  1916. /* The event has been handled, so just return without
  1917. reporting it. */
  1918. return;
  1919. }
  1920. }
  1921. if (linux_wstatus_maybe_breakpoint (wstat))
  1922. {
  1923. if (save_stop_reason (child))
  1924. have_stop_pc = 1;
  1925. }
  1926. if (!have_stop_pc)
  1927. child->stop_pc = get_pc (child);
  1928. if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SIGSTOP
  1929. && child->stop_expected)
  1930. {
  1931. threads_debug_printf ("Expected stop.");
  1932. child->stop_expected = 0;
  1933. if (thread->last_resume_kind == resume_stop)
  1934. {
  1935. /* We want to report the stop to the core. Treat the
  1936. SIGSTOP as a normal event. */
  1937. threads_debug_printf ("resume_stop SIGSTOP caught for %s.",
  1938. target_pid_to_str (ptid_of (thread)).c_str ());
  1939. }
  1940. else if (stopping_threads != NOT_STOPPING_THREADS)
  1941. {
  1942. /* Stopping threads. We don't want this SIGSTOP to end up
  1943. pending. */
  1944. threads_debug_printf ("SIGSTOP caught for %s while stopping threads.",
  1945. target_pid_to_str (ptid_of (thread)).c_str ());
  1946. return;
  1947. }
  1948. else
  1949. {
  1950. /* This is a delayed SIGSTOP. Filter out the event. */
  1951. threads_debug_printf ("%s %s, 0, 0 (discard delayed SIGSTOP)",
  1952. child->stepping ? "step" : "continue",
  1953. target_pid_to_str (ptid_of (thread)).c_str ());
  1954. resume_one_lwp (child, child->stepping, 0, NULL);
  1955. return;
  1956. }
  1957. }
  1958. child->status_pending_p = 1;
  1959. child->status_pending = wstat;
  1960. return;
  1961. }
  1962. bool
  1963. linux_process_target::maybe_hw_step (thread_info *thread)
  1964. {
  1965. if (supports_hardware_single_step ())
  1966. return true;
  1967. else
  1968. {
  1969. /* GDBserver must insert single-step breakpoint for software
  1970. single step. */
  1971. gdb_assert (has_single_step_breakpoints (thread));
  1972. return false;
  1973. }
  1974. }
  1975. void
  1976. linux_process_target::resume_stopped_resumed_lwps (thread_info *thread)
  1977. {
  1978. struct lwp_info *lp = get_thread_lwp (thread);
  1979. if (lp->stopped
  1980. && !lp->suspended
  1981. && !lp->status_pending_p
  1982. && thread->last_status.kind () == TARGET_WAITKIND_IGNORE)
  1983. {
  1984. int step = 0;
  1985. if (thread->last_resume_kind == resume_step)
  1986. step = maybe_hw_step (thread);
  1987. threads_debug_printf ("resuming stopped-resumed LWP %s at %s: step=%d",
  1988. target_pid_to_str (ptid_of (thread)).c_str (),
  1989. paddress (lp->stop_pc), step);
  1990. resume_one_lwp (lp, step, GDB_SIGNAL_0, NULL);
  1991. }
  1992. }
  1993. int
  1994. linux_process_target::wait_for_event_filtered (ptid_t wait_ptid,
  1995. ptid_t filter_ptid,
  1996. int *wstatp, int options)
  1997. {
  1998. struct thread_info *event_thread;
  1999. struct lwp_info *event_child, *requested_child;
  2000. sigset_t block_mask, prev_mask;
  2001. retry:
  2002. /* N.B. event_thread points to the thread_info struct that contains
  2003. event_child. Keep them in sync. */
  2004. event_thread = NULL;
  2005. event_child = NULL;
  2006. requested_child = NULL;
  2007. /* Check for a lwp with a pending status. */
  2008. if (filter_ptid == minus_one_ptid || filter_ptid.is_pid ())
  2009. {
  2010. event_thread = find_thread_in_random ([&] (thread_info *thread)
  2011. {
  2012. return status_pending_p_callback (thread, filter_ptid);
  2013. });
  2014. if (event_thread != NULL)
  2015. {
  2016. event_child = get_thread_lwp (event_thread);
  2017. threads_debug_printf ("Got a pending child %ld", lwpid_of (event_thread));
  2018. }
  2019. }
  2020. else if (filter_ptid != null_ptid)
  2021. {
  2022. requested_child = find_lwp_pid (filter_ptid);
  2023. if (stopping_threads == NOT_STOPPING_THREADS
  2024. && requested_child->status_pending_p
  2025. && (requested_child->collecting_fast_tracepoint
  2026. != fast_tpoint_collect_result::not_collecting))
  2027. {
  2028. enqueue_one_deferred_signal (requested_child,
  2029. &requested_child->status_pending);
  2030. requested_child->status_pending_p = 0;
  2031. requested_child->status_pending = 0;
  2032. resume_one_lwp (requested_child, 0, 0, NULL);
  2033. }
  2034. if (requested_child->suspended
  2035. && requested_child->status_pending_p)
  2036. {
  2037. internal_error (__FILE__, __LINE__,
  2038. "requesting an event out of a"
  2039. " suspended child?");
  2040. }
  2041. if (requested_child->status_pending_p)
  2042. {
  2043. event_child = requested_child;
  2044. event_thread = get_lwp_thread (event_child);
  2045. }
  2046. }
  2047. if (event_child != NULL)
  2048. {
  2049. threads_debug_printf ("Got an event from pending child %ld (%04x)",
  2050. lwpid_of (event_thread),
  2051. event_child->status_pending);
  2052. *wstatp = event_child->status_pending;
  2053. event_child->status_pending_p = 0;
  2054. event_child->status_pending = 0;
  2055. switch_to_thread (event_thread);
  2056. return lwpid_of (event_thread);
  2057. }
  2058. /* But if we don't find a pending event, we'll have to wait.
  2059. We only enter this loop if no process has a pending wait status.
  2060. Thus any action taken in response to a wait status inside this
  2061. loop is responding as soon as we detect the status, not after any
  2062. pending events. */
  2063. /* Make sure SIGCHLD is blocked until the sigsuspend below. Block
  2064. all signals while here. */
  2065. sigfillset (&block_mask);
  2066. gdb_sigmask (SIG_BLOCK, &block_mask, &prev_mask);
  2067. /* Always pull all events out of the kernel. We'll randomly select
  2068. an event LWP out of all that have events, to prevent
  2069. starvation. */
  2070. while (event_child == NULL)
  2071. {
  2072. pid_t ret = 0;
  2073. /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
  2074. quirks:
  2075. - If the thread group leader exits while other threads in the
  2076. thread group still exist, waitpid(TGID, ...) hangs. That
  2077. waitpid won't return an exit status until the other threads
  2078. in the group are reaped.
  2079. - When a non-leader thread execs, that thread just vanishes
  2080. without reporting an exit (so we'd hang if we waited for it
  2081. explicitly in that case). The exec event is reported to
  2082. the TGID pid. */
  2083. errno = 0;
  2084. ret = my_waitpid (-1, wstatp, options | WNOHANG);
  2085. threads_debug_printf ("waitpid(-1, ...) returned %d, %s",
  2086. ret, errno ? safe_strerror (errno) : "ERRNO-OK");
  2087. if (ret > 0)
  2088. {
  2089. threads_debug_printf ("waitpid %ld received %s",
  2090. (long) ret, status_to_str (*wstatp).c_str ());
  2091. /* Filter all events. IOW, leave all events pending. We'll
  2092. randomly select an event LWP out of all that have events
  2093. below. */
  2094. filter_event (ret, *wstatp);
  2095. /* Retry until nothing comes out of waitpid. A single
  2096. SIGCHLD can indicate more than one child stopped. */
  2097. continue;
  2098. }
  2099. /* Now that we've pulled all events out of the kernel, resume
  2100. LWPs that don't have an interesting event to report. */
  2101. if (stopping_threads == NOT_STOPPING_THREADS)
  2102. for_each_thread ([this] (thread_info *thread)
  2103. {
  2104. resume_stopped_resumed_lwps (thread);
  2105. });
  2106. /* ... and find an LWP with a status to report to the core, if
  2107. any. */
  2108. event_thread = find_thread_in_random ([&] (thread_info *thread)
  2109. {
  2110. return status_pending_p_callback (thread, filter_ptid);
  2111. });
  2112. if (event_thread != NULL)
  2113. {
  2114. event_child = get_thread_lwp (event_thread);
  2115. *wstatp = event_child->status_pending;
  2116. event_child->status_pending_p = 0;
  2117. event_child->status_pending = 0;
  2118. break;
  2119. }
  2120. /* Check for zombie thread group leaders. Those can't be reaped
  2121. until all other threads in the thread group are. */
  2122. check_zombie_leaders ();
  2123. auto not_stopped = [&] (thread_info *thread)
  2124. {
  2125. return not_stopped_callback (thread, wait_ptid);
  2126. };
  2127. /* If there are no resumed children left in the set of LWPs we
  2128. want to wait for, bail. We can't just block in
  2129. waitpid/sigsuspend, because lwps might have been left stopped
  2130. in trace-stop state, and we'd be stuck forever waiting for
  2131. their status to change (which would only happen if we resumed
  2132. them). Even if WNOHANG is set, this return code is preferred
  2133. over 0 (below), as it is more detailed. */
  2134. if (find_thread (not_stopped) == NULL)
  2135. {
  2136. threads_debug_printf ("exit (no unwaited-for LWP)");
  2137. gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
  2138. return -1;
  2139. }
  2140. /* No interesting event to report to the caller. */
  2141. if ((options & WNOHANG))
  2142. {
  2143. threads_debug_printf ("WNOHANG set, no event found");
  2144. gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
  2145. return 0;
  2146. }
  2147. /* Block until we get an event reported with SIGCHLD. */
  2148. threads_debug_printf ("sigsuspend'ing");
  2149. sigsuspend (&prev_mask);
  2150. gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
  2151. goto retry;
  2152. }
  2153. gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
  2154. switch_to_thread (event_thread);
  2155. return lwpid_of (event_thread);
  2156. }
  2157. int
  2158. linux_process_target::wait_for_event (ptid_t ptid, int *wstatp, int options)
  2159. {
  2160. return wait_for_event_filtered (ptid, ptid, wstatp, options);
  2161. }
  2162. /* Select one LWP out of those that have events pending. */
  2163. static void
  2164. select_event_lwp (struct lwp_info **orig_lp)
  2165. {
  2166. struct thread_info *event_thread = NULL;
  2167. /* In all-stop, give preference to the LWP that is being
  2168. single-stepped. There will be at most one, and it's the LWP that
  2169. the core is most interested in. If we didn't do this, then we'd
  2170. have to handle pending step SIGTRAPs somehow in case the core
  2171. later continues the previously-stepped thread, otherwise we'd
  2172. report the pending SIGTRAP, and the core, not having stepped the
  2173. thread, wouldn't understand what the trap was for, and therefore
  2174. would report it to the user as a random signal. */
  2175. if (!non_stop)
  2176. {
  2177. event_thread = find_thread ([] (thread_info *thread)
  2178. {
  2179. lwp_info *lp = get_thread_lwp (thread);
  2180. return (thread->last_status.kind () == TARGET_WAITKIND_IGNORE
  2181. && thread->last_resume_kind == resume_step
  2182. && lp->status_pending_p);
  2183. });
  2184. if (event_thread != NULL)
  2185. threads_debug_printf
  2186. ("Select single-step %s",
  2187. target_pid_to_str (ptid_of (event_thread)).c_str ());
  2188. }
  2189. if (event_thread == NULL)
  2190. {
  2191. /* No single-stepping LWP. Select one at random, out of those
  2192. which have had events. */
  2193. event_thread = find_thread_in_random ([&] (thread_info *thread)
  2194. {
  2195. lwp_info *lp = get_thread_lwp (thread);
  2196. /* Only resumed LWPs that have an event pending. */
  2197. return (thread->last_status.kind () == TARGET_WAITKIND_IGNORE
  2198. && lp->status_pending_p);
  2199. });
  2200. }
  2201. if (event_thread != NULL)
  2202. {
  2203. struct lwp_info *event_lp = get_thread_lwp (event_thread);
  2204. /* Switch the event LWP. */
  2205. *orig_lp = event_lp;
  2206. }
  2207. }
  2208. /* Decrement the suspend count of all LWPs, except EXCEPT, if non
  2209. NULL. */
  2210. static void
  2211. unsuspend_all_lwps (struct lwp_info *except)
  2212. {
  2213. for_each_thread ([&] (thread_info *thread)
  2214. {
  2215. lwp_info *lwp = get_thread_lwp (thread);
  2216. if (lwp != except)
  2217. lwp_suspended_decr (lwp);
  2218. });
  2219. }
  2220. static bool lwp_running (thread_info *thread);
  2221. /* Stabilize threads (move out of jump pads).
  2222. If a thread is midway collecting a fast tracepoint, we need to
  2223. finish the collection and move it out of the jump pad before
  2224. reporting the signal.
  2225. This avoids recursion while collecting (when a signal arrives
  2226. midway, and the signal handler itself collects), which would trash
  2227. the trace buffer. In case the user set a breakpoint in a signal
  2228. handler, this avoids the backtrace showing the jump pad, etc..
  2229. Most importantly, there are certain things we can't do safely if
  2230. threads are stopped in a jump pad (or in its callee's). For
  2231. example:
  2232. - starting a new trace run. A thread still collecting the
  2233. previous run, could trash the trace buffer when resumed. The trace
  2234. buffer control structures would have been reset but the thread had
  2235. no way to tell. The thread could even midway memcpy'ing to the
  2236. buffer, which would mean that when resumed, it would clobber the
  2237. trace buffer that had been set for a new run.
  2238. - we can't rewrite/reuse the jump pads for new tracepoints
  2239. safely. Say you do tstart while a thread is stopped midway while
  2240. collecting. When the thread is later resumed, it finishes the
  2241. collection, and returns to the jump pad, to execute the original
  2242. instruction that was under the tracepoint jump at the time the
  2243. older run had been started. If the jump pad had been rewritten
  2244. since for something else in the new run, the thread would now
  2245. execute the wrong / random instructions. */
  2246. void
  2247. linux_process_target::stabilize_threads ()
  2248. {
  2249. thread_info *thread_stuck = find_thread ([this] (thread_info *thread)
  2250. {
  2251. return stuck_in_jump_pad (thread);
  2252. });
  2253. if (thread_stuck != NULL)
  2254. {
  2255. threads_debug_printf ("can't stabilize, LWP %ld is stuck in jump pad",
  2256. lwpid_of (thread_stuck));
  2257. return;
  2258. }
  2259. scoped_restore_current_thread restore_thread;
  2260. stabilizing_threads = 1;
  2261. /* Kick 'em all. */
  2262. for_each_thread ([this] (thread_info *thread)
  2263. {
  2264. move_out_of_jump_pad (thread);
  2265. });
  2266. /* Loop until all are stopped out of the jump pads. */
  2267. while (find_thread (lwp_running) != NULL)
  2268. {
  2269. struct target_waitstatus ourstatus;
  2270. struct lwp_info *lwp;
  2271. int wstat;
  2272. /* Note that we go through the full wait even loop. While
  2273. moving threads out of jump pad, we need to be able to step
  2274. over internal breakpoints and such. */
  2275. wait_1 (minus_one_ptid, &ourstatus, 0);
  2276. if (ourstatus.kind () == TARGET_WAITKIND_STOPPED)
  2277. {
  2278. lwp = get_thread_lwp (current_thread);
  2279. /* Lock it. */
  2280. lwp_suspended_inc (lwp);
  2281. if (ourstatus.sig () != GDB_SIGNAL_0
  2282. || current_thread->last_resume_kind == resume_stop)
  2283. {
  2284. wstat = W_STOPCODE (gdb_signal_to_host (ourstatus.sig ()));
  2285. enqueue_one_deferred_signal (lwp, &wstat);
  2286. }
  2287. }
  2288. }
  2289. unsuspend_all_lwps (NULL);
  2290. stabilizing_threads = 0;
  2291. if (debug_threads)
  2292. {
  2293. thread_stuck = find_thread ([this] (thread_info *thread)
  2294. {
  2295. return stuck_in_jump_pad (thread);
  2296. });
  2297. if (thread_stuck != NULL)
  2298. threads_debug_printf
  2299. ("couldn't stabilize, LWP %ld got stuck in jump pad",
  2300. lwpid_of (thread_stuck));
  2301. }
  2302. }
  2303. /* Convenience function that is called when the kernel reports an
  2304. event that is not passed out to GDB. */
  2305. static ptid_t
  2306. ignore_event (struct target_waitstatus *ourstatus)
  2307. {
  2308. /* If we got an event, there may still be others, as a single
  2309. SIGCHLD can indicate more than one child stopped. This forces
  2310. another target_wait call. */
  2311. async_file_mark ();
  2312. ourstatus->set_ignore ();
  2313. return null_ptid;
  2314. }
  2315. ptid_t
  2316. linux_process_target::filter_exit_event (lwp_info *event_child,
  2317. target_waitstatus *ourstatus)
  2318. {
  2319. client_state &cs = get_client_state ();
  2320. struct thread_info *thread = get_lwp_thread (event_child);
  2321. ptid_t ptid = ptid_of (thread);
  2322. if (!is_leader (thread))
  2323. {
  2324. if (cs.report_thread_events)
  2325. ourstatus->set_thread_exited (0);
  2326. else
  2327. ourstatus->set_ignore ();
  2328. delete_lwp (event_child);
  2329. }
  2330. return ptid;
  2331. }
  2332. /* Returns 1 if GDB is interested in any event_child syscalls. */
  2333. static int
  2334. gdb_catching_syscalls_p (struct lwp_info *event_child)
  2335. {
  2336. struct thread_info *thread = get_lwp_thread (event_child);
  2337. struct process_info *proc = get_thread_process (thread);
  2338. return !proc->syscalls_to_catch.empty ();
  2339. }
  2340. bool
  2341. linux_process_target::gdb_catch_this_syscall (lwp_info *event_child)
  2342. {
  2343. int sysno;
  2344. struct thread_info *thread = get_lwp_thread (event_child);
  2345. struct process_info *proc = get_thread_process (thread);
  2346. if (proc->syscalls_to_catch.empty ())
  2347. return false;
  2348. if (proc->syscalls_to_catch[0] == ANY_SYSCALL)
  2349. return true;
  2350. get_syscall_trapinfo (event_child, &sysno);
  2351. for (int iter : proc->syscalls_to_catch)
  2352. if (iter == sysno)
  2353. return true;
  2354. return false;
  2355. }
  2356. ptid_t
  2357. linux_process_target::wait_1 (ptid_t ptid, target_waitstatus *ourstatus,
  2358. target_wait_flags target_options)
  2359. {
  2360. THREADS_SCOPED_DEBUG_ENTER_EXIT;
  2361. client_state &cs = get_client_state ();
  2362. int w;
  2363. struct lwp_info *event_child;
  2364. int options;
  2365. int pid;
  2366. int step_over_finished;
  2367. int bp_explains_trap;
  2368. int maybe_internal_trap;
  2369. int report_to_gdb;
  2370. int trace_event;
  2371. int in_step_range;
  2372. int any_resumed;
  2373. threads_debug_printf ("[%s]", target_pid_to_str (ptid).c_str ());
  2374. /* Translate generic target options into linux options. */
  2375. options = __WALL;
  2376. if (target_options & TARGET_WNOHANG)
  2377. options |= WNOHANG;
  2378. bp_explains_trap = 0;
  2379. trace_event = 0;
  2380. in_step_range = 0;
  2381. ourstatus->set_ignore ();
  2382. auto status_pending_p_any = [&] (thread_info *thread)
  2383. {
  2384. return status_pending_p_callback (thread, minus_one_ptid);
  2385. };
  2386. auto not_stopped = [&] (thread_info *thread)
  2387. {
  2388. return not_stopped_callback (thread, minus_one_ptid);
  2389. };
  2390. /* Find a resumed LWP, if any. */
  2391. if (find_thread (status_pending_p_any) != NULL)
  2392. any_resumed = 1;
  2393. else if (find_thread (not_stopped) != NULL)
  2394. any_resumed = 1;
  2395. else
  2396. any_resumed = 0;
  2397. if (step_over_bkpt == null_ptid)
  2398. pid = wait_for_event (ptid, &w, options);
  2399. else
  2400. {
  2401. threads_debug_printf ("step_over_bkpt set [%s], doing a blocking wait",
  2402. target_pid_to_str (step_over_bkpt).c_str ());
  2403. pid = wait_for_event (step_over_bkpt, &w, options & ~WNOHANG);
  2404. }
  2405. if (pid == 0 || (pid == -1 && !any_resumed))
  2406. {
  2407. gdb_assert (target_options & TARGET_WNOHANG);
  2408. threads_debug_printf ("ret = null_ptid, TARGET_WAITKIND_IGNORE");
  2409. ourstatus->set_ignore ();
  2410. return null_ptid;
  2411. }
  2412. else if (pid == -1)
  2413. {
  2414. threads_debug_printf ("ret = null_ptid, TARGET_WAITKIND_NO_RESUMED");
  2415. ourstatus->set_no_resumed ();
  2416. return null_ptid;
  2417. }
  2418. event_child = get_thread_lwp (current_thread);
  2419. /* wait_for_event only returns an exit status for the last
  2420. child of a process. Report it. */
  2421. if (WIFEXITED (w) || WIFSIGNALED (w))
  2422. {
  2423. if (WIFEXITED (w))
  2424. {
  2425. ourstatus->set_exited (WEXITSTATUS (w));
  2426. threads_debug_printf
  2427. ("ret = %s, exited with retcode %d",
  2428. target_pid_to_str (ptid_of (current_thread)).c_str (),
  2429. WEXITSTATUS (w));
  2430. }
  2431. else
  2432. {
  2433. ourstatus->set_signalled (gdb_signal_from_host (WTERMSIG (w)));
  2434. threads_debug_printf
  2435. ("ret = %s, terminated with signal %d",
  2436. target_pid_to_str (ptid_of (current_thread)).c_str (),
  2437. WTERMSIG (w));
  2438. }
  2439. if (ourstatus->kind () == TARGET_WAITKIND_EXITED)
  2440. return filter_exit_event (event_child, ourstatus);
  2441. return ptid_of (current_thread);
  2442. }
  2443. /* If step-over executes a breakpoint instruction, in the case of a
  2444. hardware single step it means a gdb/gdbserver breakpoint had been
  2445. planted on top of a permanent breakpoint, in the case of a software
  2446. single step it may just mean that gdbserver hit the reinsert breakpoint.
  2447. The PC has been adjusted by save_stop_reason to point at
  2448. the breakpoint address.
  2449. So in the case of the hardware single step advance the PC manually
  2450. past the breakpoint and in the case of software single step advance only
  2451. if it's not the single_step_breakpoint we are hitting.
  2452. This avoids that a program would keep trapping a permanent breakpoint
  2453. forever. */
  2454. if (step_over_bkpt != null_ptid
  2455. && event_child->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
  2456. && (event_child->stepping
  2457. || !single_step_breakpoint_inserted_here (event_child->stop_pc)))
  2458. {
  2459. int increment_pc = 0;
  2460. int breakpoint_kind = 0;
  2461. CORE_ADDR stop_pc = event_child->stop_pc;
  2462. breakpoint_kind = breakpoint_kind_from_current_state (&stop_pc);
  2463. sw_breakpoint_from_kind (breakpoint_kind, &increment_pc);
  2464. threads_debug_printf
  2465. ("step-over for %s executed software breakpoint",
  2466. target_pid_to_str (ptid_of (current_thread)).c_str ());
  2467. if (increment_pc != 0)
  2468. {
  2469. struct regcache *regcache
  2470. = get_thread_regcache (current_thread, 1);
  2471. event_child->stop_pc += increment_pc;
  2472. low_set_pc (regcache, event_child->stop_pc);
  2473. if (!low_breakpoint_at (event_child->stop_pc))
  2474. event_child->stop_reason = TARGET_STOPPED_BY_NO_REASON;
  2475. }
  2476. }
  2477. /* If this event was not handled before, and is not a SIGTRAP, we
  2478. report it. SIGILL and SIGSEGV are also treated as traps in case
  2479. a breakpoint is inserted at the current PC. If this target does
  2480. not support internal breakpoints at all, we also report the
  2481. SIGTRAP without further processing; it's of no concern to us. */
  2482. maybe_internal_trap
  2483. = (low_supports_breakpoints ()
  2484. && (WSTOPSIG (w) == SIGTRAP
  2485. || ((WSTOPSIG (w) == SIGILL
  2486. || WSTOPSIG (w) == SIGSEGV)
  2487. && low_breakpoint_at (event_child->stop_pc))));
  2488. if (maybe_internal_trap)
  2489. {
  2490. /* Handle anything that requires bookkeeping before deciding to
  2491. report the event or continue waiting. */
  2492. /* First check if we can explain the SIGTRAP with an internal
  2493. breakpoint, or if we should possibly report the event to GDB.
  2494. Do this before anything that may remove or insert a
  2495. breakpoint. */
  2496. bp_explains_trap = breakpoint_inserted_here (event_child->stop_pc);
  2497. /* We have a SIGTRAP, possibly a step-over dance has just
  2498. finished. If so, tweak the state machine accordingly,
  2499. reinsert breakpoints and delete any single-step
  2500. breakpoints. */
  2501. step_over_finished = finish_step_over (event_child);
  2502. /* Now invoke the callbacks of any internal breakpoints there. */
  2503. check_breakpoints (event_child->stop_pc);
  2504. /* Handle tracepoint data collecting. This may overflow the
  2505. trace buffer, and cause a tracing stop, removing
  2506. breakpoints. */
  2507. trace_event = handle_tracepoints (event_child);
  2508. if (bp_explains_trap)
  2509. threads_debug_printf ("Hit a gdbserver breakpoint.");
  2510. }
  2511. else
  2512. {
  2513. /* We have some other signal, possibly a step-over dance was in
  2514. progress, and it should be cancelled too. */
  2515. step_over_finished = finish_step_over (event_child);
  2516. }
  2517. /* We have all the data we need. Either report the event to GDB, or
  2518. resume threads and keep waiting for more. */
  2519. /* If we're collecting a fast tracepoint, finish the collection and
  2520. move out of the jump pad before delivering a signal. See
  2521. linux_stabilize_threads. */
  2522. if (WIFSTOPPED (w)
  2523. && WSTOPSIG (w) != SIGTRAP
  2524. && supports_fast_tracepoints ()
  2525. && agent_loaded_p ())
  2526. {
  2527. threads_debug_printf ("Got signal %d for LWP %ld. Check if we need "
  2528. "to defer or adjust it.",
  2529. WSTOPSIG (w), lwpid_of (current_thread));
  2530. /* Allow debugging the jump pad itself. */
  2531. if (current_thread->last_resume_kind != resume_step
  2532. && maybe_move_out_of_jump_pad (event_child, &w))
  2533. {
  2534. enqueue_one_deferred_signal (event_child, &w);
  2535. threads_debug_printf ("Signal %d for LWP %ld deferred (in jump pad)",
  2536. WSTOPSIG (w), lwpid_of (current_thread));
  2537. resume_one_lwp (event_child, 0, 0, NULL);
  2538. return ignore_event (ourstatus);
  2539. }
  2540. }
  2541. if (event_child->collecting_fast_tracepoint
  2542. != fast_tpoint_collect_result::not_collecting)
  2543. {
  2544. threads_debug_printf
  2545. ("LWP %ld was trying to move out of the jump pad (%d). "
  2546. "Check if we're already there.",
  2547. lwpid_of (current_thread),
  2548. (int) event_child->collecting_fast_tracepoint);
  2549. trace_event = 1;
  2550. event_child->collecting_fast_tracepoint
  2551. = linux_fast_tracepoint_collecting (event_child, NULL);
  2552. if (event_child->collecting_fast_tracepoint
  2553. != fast_tpoint_collect_result::before_insn)
  2554. {
  2555. /* No longer need this breakpoint. */
  2556. if (event_child->exit_jump_pad_bkpt != NULL)
  2557. {
  2558. threads_debug_printf
  2559. ("No longer need exit-jump-pad bkpt; removing it."
  2560. "stopping all threads momentarily.");
  2561. /* Other running threads could hit this breakpoint.
  2562. We don't handle moribund locations like GDB does,
  2563. instead we always pause all threads when removing
  2564. breakpoints, so that any step-over or
  2565. decr_pc_after_break adjustment is always taken
  2566. care of while the breakpoint is still
  2567. inserted. */
  2568. stop_all_lwps (1, event_child);
  2569. delete_breakpoint (event_child->exit_jump_pad_bkpt);
  2570. event_child->exit_jump_pad_bkpt = NULL;
  2571. unstop_all_lwps (1, event_child);
  2572. gdb_assert (event_child->suspended >= 0);
  2573. }
  2574. }
  2575. if (event_child->collecting_fast_tracepoint
  2576. == fast_tpoint_collect_result::not_collecting)
  2577. {
  2578. threads_debug_printf
  2579. ("fast tracepoint finished collecting successfully.");
  2580. /* We may have a deferred signal to report. */
  2581. if (dequeue_one_deferred_signal (event_child, &w))
  2582. threads_debug_printf ("dequeued one signal.");
  2583. else
  2584. {
  2585. threads_debug_printf ("no deferred signals.");
  2586. if (stabilizing_threads)
  2587. {
  2588. ourstatus->set_stopped (GDB_SIGNAL_0);
  2589. threads_debug_printf
  2590. ("ret = %s, stopped while stabilizing threads",
  2591. target_pid_to_str (ptid_of (current_thread)).c_str ());
  2592. return ptid_of (current_thread);
  2593. }
  2594. }
  2595. }
  2596. }
  2597. /* Check whether GDB would be interested in this event. */
  2598. /* Check if GDB is interested in this syscall. */
  2599. if (WIFSTOPPED (w)
  2600. && WSTOPSIG (w) == SYSCALL_SIGTRAP
  2601. && !gdb_catch_this_syscall (event_child))
  2602. {
  2603. threads_debug_printf ("Ignored syscall for LWP %ld.",
  2604. lwpid_of (current_thread));
  2605. resume_one_lwp (event_child, event_child->stepping, 0, NULL);
  2606. return ignore_event (ourstatus);
  2607. }
  2608. /* If GDB is not interested in this signal, don't stop other
  2609. threads, and don't report it to GDB. Just resume the inferior
  2610. right away. We do this for threading-related signals as well as
  2611. any that GDB specifically requested we ignore. But never ignore
  2612. SIGSTOP if we sent it ourselves, and do not ignore signals when
  2613. stepping - they may require special handling to skip the signal
  2614. handler. Also never ignore signals that could be caused by a
  2615. breakpoint. */
  2616. if (WIFSTOPPED (w)
  2617. && current_thread->last_resume_kind != resume_step
  2618. && (
  2619. #if defined (USE_THREAD_DB) && !defined (__ANDROID__)
  2620. (current_process ()->priv->thread_db != NULL
  2621. && (WSTOPSIG (w) == __SIGRTMIN
  2622. || WSTOPSIG (w) == __SIGRTMIN + 1))
  2623. ||
  2624. #endif
  2625. (cs.pass_signals[gdb_signal_from_host (WSTOPSIG (w))]
  2626. && !(WSTOPSIG (w) == SIGSTOP
  2627. && current_thread->last_resume_kind == resume_stop)
  2628. && !linux_wstatus_maybe_breakpoint (w))))
  2629. {
  2630. siginfo_t info, *info_p;
  2631. threads_debug_printf ("Ignored signal %d for LWP %ld.",
  2632. WSTOPSIG (w), lwpid_of (current_thread));
  2633. if (ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread),
  2634. (PTRACE_TYPE_ARG3) 0, &info) == 0)
  2635. info_p = &info;
  2636. else
  2637. info_p = NULL;
  2638. if (step_over_finished)
  2639. {
  2640. /* We cancelled this thread's step-over above. We still
  2641. need to unsuspend all other LWPs, and set them back
  2642. running again while the signal handler runs. */
  2643. unsuspend_all_lwps (event_child);
  2644. /* Enqueue the pending signal info so that proceed_all_lwps
  2645. doesn't lose it. */
  2646. enqueue_pending_signal (event_child, WSTOPSIG (w), info_p);
  2647. proceed_all_lwps ();
  2648. }
  2649. else
  2650. {
  2651. resume_one_lwp (event_child, event_child->stepping,
  2652. WSTOPSIG (w), info_p);
  2653. }
  2654. return ignore_event (ourstatus);
  2655. }
  2656. /* Note that all addresses are always "out of the step range" when
  2657. there's no range to begin with. */
  2658. in_step_range = lwp_in_step_range (event_child);
  2659. /* If GDB wanted this thread to single step, and the thread is out
  2660. of the step range, we always want to report the SIGTRAP, and let
  2661. GDB handle it. Watchpoints should always be reported. So should
  2662. signals we can't explain. A SIGTRAP we can't explain could be a
  2663. GDB breakpoint --- we may or not support Z0 breakpoints. If we
  2664. do, we're be able to handle GDB breakpoints on top of internal
  2665. breakpoints, by handling the internal breakpoint and still
  2666. reporting the event to GDB. If we don't, we're out of luck, GDB
  2667. won't see the breakpoint hit. If we see a single-step event but
  2668. the thread should be continuing, don't pass the trap to gdb.
  2669. That indicates that we had previously finished a single-step but
  2670. left the single-step pending -- see
  2671. complete_ongoing_step_over. */
  2672. report_to_gdb = (!maybe_internal_trap
  2673. || (current_thread->last_resume_kind == resume_step
  2674. && !in_step_range)
  2675. || event_child->stop_reason == TARGET_STOPPED_BY_WATCHPOINT
  2676. || (!in_step_range
  2677. && !bp_explains_trap
  2678. && !trace_event
  2679. && !step_over_finished
  2680. && !(current_thread->last_resume_kind == resume_continue
  2681. && event_child->stop_reason == TARGET_STOPPED_BY_SINGLE_STEP))
  2682. || (gdb_breakpoint_here (event_child->stop_pc)
  2683. && gdb_condition_true_at_breakpoint (event_child->stop_pc)
  2684. && gdb_no_commands_at_breakpoint (event_child->stop_pc))
  2685. || event_child->waitstatus.kind () != TARGET_WAITKIND_IGNORE);
  2686. run_breakpoint_commands (event_child->stop_pc);
  2687. /* We found no reason GDB would want us to stop. We either hit one
  2688. of our own breakpoints, or finished an internal step GDB
  2689. shouldn't know about. */
  2690. if (!report_to_gdb)
  2691. {
  2692. if (bp_explains_trap)
  2693. threads_debug_printf ("Hit a gdbserver breakpoint.");
  2694. if (step_over_finished)
  2695. threads_debug_printf ("Step-over finished.");
  2696. if (trace_event)
  2697. threads_debug_printf ("Tracepoint event.");
  2698. if (lwp_in_step_range (event_child))
  2699. threads_debug_printf ("Range stepping pc 0x%s [0x%s, 0x%s).",
  2700. paddress (event_child->stop_pc),
  2701. paddress (event_child->step_range_start),
  2702. paddress (event_child->step_range_end));
  2703. /* We're not reporting this breakpoint to GDB, so apply the
  2704. decr_pc_after_break adjustment to the inferior's regcache
  2705. ourselves. */
  2706. if (low_supports_breakpoints ())
  2707. {
  2708. struct regcache *regcache
  2709. = get_thread_regcache (current_thread, 1);
  2710. low_set_pc (regcache, event_child->stop_pc);
  2711. }
  2712. if (step_over_finished)
  2713. {
  2714. /* If we have finished stepping over a breakpoint, we've
  2715. stopped and suspended all LWPs momentarily except the
  2716. stepping one. This is where we resume them all again.
  2717. We're going to keep waiting, so use proceed, which
  2718. handles stepping over the next breakpoint. */
  2719. unsuspend_all_lwps (event_child);
  2720. }
  2721. else
  2722. {
  2723. /* Remove the single-step breakpoints if any. Note that
  2724. there isn't single-step breakpoint if we finished stepping
  2725. over. */
  2726. if (supports_software_single_step ()
  2727. && has_single_step_breakpoints (current_thread))
  2728. {
  2729. stop_all_lwps (0, event_child);
  2730. delete_single_step_breakpoints (current_thread);
  2731. unstop_all_lwps (0, event_child);
  2732. }
  2733. }
  2734. threads_debug_printf ("proceeding all threads.");
  2735. proceed_all_lwps ();
  2736. return ignore_event (ourstatus);
  2737. }
  2738. if (debug_threads)
  2739. {
  2740. if (event_child->waitstatus.kind () != TARGET_WAITKIND_IGNORE)
  2741. threads_debug_printf ("LWP %ld: extended event with waitstatus %s",
  2742. lwpid_of (get_lwp_thread (event_child)),
  2743. event_child->waitstatus.to_string ().c_str ());
  2744. if (current_thread->last_resume_kind == resume_step)
  2745. {
  2746. if (event_child->step_range_start == event_child->step_range_end)
  2747. threads_debug_printf
  2748. ("GDB wanted to single-step, reporting event.");
  2749. else if (!lwp_in_step_range (event_child))
  2750. threads_debug_printf ("Out of step range, reporting event.");
  2751. }
  2752. if (event_child->stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
  2753. threads_debug_printf ("Stopped by watchpoint.");
  2754. else if (gdb_breakpoint_here (event_child->stop_pc))
  2755. threads_debug_printf ("Stopped by GDB breakpoint.");
  2756. }
  2757. threads_debug_printf ("Hit a non-gdbserver trap event.");
  2758. /* Alright, we're going to report a stop. */
  2759. /* Remove single-step breakpoints. */
  2760. if (supports_software_single_step ())
  2761. {
  2762. /* Remove single-step breakpoints or not. It it is true, stop all
  2763. lwps, so that other threads won't hit the breakpoint in the
  2764. staled memory. */
  2765. int remove_single_step_breakpoints_p = 0;
  2766. if (non_stop)
  2767. {
  2768. remove_single_step_breakpoints_p
  2769. = has_single_step_breakpoints (current_thread);
  2770. }
  2771. else
  2772. {
  2773. /* In all-stop, a stop reply cancels all previous resume
  2774. requests. Delete all single-step breakpoints. */
  2775. find_thread ([&] (thread_info *thread) {
  2776. if (has_single_step_breakpoints (thread))
  2777. {
  2778. remove_single_step_breakpoints_p = 1;
  2779. return true;
  2780. }
  2781. return false;
  2782. });
  2783. }
  2784. if (remove_single_step_breakpoints_p)
  2785. {
  2786. /* If we remove single-step breakpoints from memory, stop all lwps,
  2787. so that other threads won't hit the breakpoint in the staled
  2788. memory. */
  2789. stop_all_lwps (0, event_child);
  2790. if (non_stop)
  2791. {
  2792. gdb_assert (has_single_step_breakpoints (current_thread));
  2793. delete_single_step_breakpoints (current_thread);
  2794. }
  2795. else
  2796. {
  2797. for_each_thread ([] (thread_info *thread){
  2798. if (has_single_step_breakpoints (thread))
  2799. delete_single_step_breakpoints (thread);
  2800. });
  2801. }
  2802. unstop_all_lwps (0, event_child);
  2803. }
  2804. }
  2805. if (!stabilizing_threads)
  2806. {
  2807. /* In all-stop, stop all threads. */
  2808. if (!non_stop)
  2809. stop_all_lwps (0, NULL);
  2810. if (step_over_finished)
  2811. {
  2812. if (!non_stop)
  2813. {
  2814. /* If we were doing a step-over, all other threads but
  2815. the stepping one had been paused in start_step_over,
  2816. with their suspend counts incremented. We don't want
  2817. to do a full unstop/unpause, because we're in
  2818. all-stop mode (so we want threads stopped), but we
  2819. still need to unsuspend the other threads, to
  2820. decrement their `suspended' count back. */
  2821. unsuspend_all_lwps (event_child);
  2822. }
  2823. else
  2824. {
  2825. /* If we just finished a step-over, then all threads had
  2826. been momentarily paused. In all-stop, that's fine,
  2827. we want threads stopped by now anyway. In non-stop,
  2828. we need to re-resume threads that GDB wanted to be
  2829. running. */
  2830. unstop_all_lwps (1, event_child);
  2831. }
  2832. }
  2833. /* If we're not waiting for a specific LWP, choose an event LWP
  2834. from among those that have had events. Giving equal priority
  2835. to all LWPs that have had events helps prevent
  2836. starvation. */
  2837. if (ptid == minus_one_ptid)
  2838. {
  2839. event_child->status_pending_p = 1;
  2840. event_child->status_pending = w;
  2841. select_event_lwp (&event_child);
  2842. /* current_thread and event_child must stay in sync. */
  2843. switch_to_thread (get_lwp_thread (event_child));
  2844. event_child->status_pending_p = 0;
  2845. w = event_child->status_pending;
  2846. }
  2847. /* Stabilize threads (move out of jump pads). */
  2848. if (!non_stop)
  2849. target_stabilize_threads ();
  2850. }
  2851. else
  2852. {
  2853. /* If we just finished a step-over, then all threads had been
  2854. momentarily paused. In all-stop, that's fine, we want
  2855. threads stopped by now anyway. In non-stop, we need to
  2856. re-resume threads that GDB wanted to be running. */
  2857. if (step_over_finished)
  2858. unstop_all_lwps (1, event_child);
  2859. }
  2860. /* At this point, we haven't set OURSTATUS. This is where we do it. */
  2861. gdb_assert (ourstatus->kind () == TARGET_WAITKIND_IGNORE);
  2862. if (event_child->waitstatus.kind () != TARGET_WAITKIND_IGNORE)
  2863. {
  2864. /* If the reported event is an exit, fork, vfork or exec, let
  2865. GDB know. */
  2866. /* Break the unreported fork relationship chain. */
  2867. if (event_child->waitstatus.kind () == TARGET_WAITKIND_FORKED
  2868. || event_child->waitstatus.kind () == TARGET_WAITKIND_VFORKED)
  2869. {
  2870. event_child->fork_relative->fork_relative = NULL;
  2871. event_child->fork_relative = NULL;
  2872. }
  2873. *ourstatus = event_child->waitstatus;
  2874. /* Clear the event lwp's waitstatus since we handled it already. */
  2875. event_child->waitstatus.set_ignore ();
  2876. }
  2877. else
  2878. {
  2879. /* The LWP stopped due to a plain signal or a syscall signal. Either way,
  2880. event_chid->waitstatus wasn't filled in with the details, so look at
  2881. the wait status W. */
  2882. if (WSTOPSIG (w) == SYSCALL_SIGTRAP)
  2883. {
  2884. int syscall_number;
  2885. get_syscall_trapinfo (event_child, &syscall_number);
  2886. if (event_child->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY)
  2887. ourstatus->set_syscall_entry (syscall_number);
  2888. else if (event_child->syscall_state == TARGET_WAITKIND_SYSCALL_RETURN)
  2889. ourstatus->set_syscall_return (syscall_number);
  2890. else
  2891. gdb_assert_not_reached ("unexpected syscall state");
  2892. }
  2893. else if (current_thread->last_resume_kind == resume_stop
  2894. && WSTOPSIG (w) == SIGSTOP)
  2895. {
  2896. /* A thread that has been requested to stop by GDB with vCont;t,
  2897. and it stopped cleanly, so report as SIG0. The use of
  2898. SIGSTOP is an implementation detail. */
  2899. ourstatus->set_stopped (GDB_SIGNAL_0);
  2900. }
  2901. else
  2902. ourstatus->set_stopped (gdb_signal_from_host (WSTOPSIG (w)));
  2903. }
  2904. /* Now that we've selected our final event LWP, un-adjust its PC if
  2905. it was a software breakpoint, and the client doesn't know we can
  2906. adjust the breakpoint ourselves. */
  2907. if (event_child->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
  2908. && !cs.swbreak_feature)
  2909. {
  2910. int decr_pc = low_decr_pc_after_break ();
  2911. if (decr_pc != 0)
  2912. {
  2913. struct regcache *regcache
  2914. = get_thread_regcache (current_thread, 1);
  2915. low_set_pc (regcache, event_child->stop_pc + decr_pc);
  2916. }
  2917. }
  2918. gdb_assert (step_over_bkpt == null_ptid);
  2919. threads_debug_printf ("ret = %s, %s",
  2920. target_pid_to_str (ptid_of (current_thread)).c_str (),
  2921. ourstatus->to_string ().c_str ());
  2922. if (ourstatus->kind () == TARGET_WAITKIND_EXITED)
  2923. return filter_exit_event (event_child, ourstatus);
  2924. return ptid_of (current_thread);
  2925. }
  2926. /* Get rid of any pending event in the pipe. */
  2927. static void
  2928. async_file_flush (void)
  2929. {
  2930. linux_event_pipe.flush ();
  2931. }
  2932. /* Put something in the pipe, so the event loop wakes up. */
  2933. static void
  2934. async_file_mark (void)
  2935. {
  2936. linux_event_pipe.mark ();
  2937. }
  2938. ptid_t
  2939. linux_process_target::wait (ptid_t ptid,
  2940. target_waitstatus *ourstatus,
  2941. target_wait_flags target_options)
  2942. {
  2943. ptid_t event_ptid;
  2944. /* Flush the async file first. */
  2945. if (target_is_async_p ())
  2946. async_file_flush ();
  2947. do
  2948. {
  2949. event_ptid = wait_1 (ptid, ourstatus, target_options);
  2950. }
  2951. while ((target_options & TARGET_WNOHANG) == 0
  2952. && event_ptid == null_ptid
  2953. && ourstatus->kind () == TARGET_WAITKIND_IGNORE);
  2954. /* If at least one stop was reported, there may be more. A single
  2955. SIGCHLD can signal more than one child stop. */
  2956. if (target_is_async_p ()
  2957. && (target_options & TARGET_WNOHANG) != 0
  2958. && event_ptid != null_ptid)
  2959. async_file_mark ();
  2960. return event_ptid;
  2961. }
  2962. /* Send a signal to an LWP. */
  2963. static int
  2964. kill_lwp (unsigned long lwpid, int signo)
  2965. {
  2966. int ret;
  2967. errno = 0;
  2968. ret = syscall (__NR_tkill, lwpid, signo);
  2969. if (errno == ENOSYS)
  2970. {
  2971. /* If tkill fails, then we are not using nptl threads, a
  2972. configuration we no longer support. */
  2973. perror_with_name (("tkill"));
  2974. }
  2975. return ret;
  2976. }
  2977. void
  2978. linux_stop_lwp (struct lwp_info *lwp)
  2979. {
  2980. send_sigstop (lwp);
  2981. }
  2982. static void
  2983. send_sigstop (struct lwp_info *lwp)
  2984. {
  2985. int pid;
  2986. pid = lwpid_of (get_lwp_thread (lwp));
  2987. /* If we already have a pending stop signal for this process, don't
  2988. send another. */
  2989. if (lwp->stop_expected)
  2990. {
  2991. threads_debug_printf ("Have pending sigstop for lwp %d", pid);
  2992. return;
  2993. }
  2994. threads_debug_printf ("Sending sigstop to lwp %d", pid);
  2995. lwp->stop_expected = 1;
  2996. kill_lwp (pid, SIGSTOP);
  2997. }
  2998. static void
  2999. send_sigstop (thread_info *thread, lwp_info *except)
  3000. {
  3001. struct lwp_info *lwp = get_thread_lwp (thread);
  3002. /* Ignore EXCEPT. */
  3003. if (lwp == except)
  3004. return;
  3005. if (lwp->stopped)
  3006. return;
  3007. send_sigstop (lwp);
  3008. }
  3009. /* Increment the suspend count of an LWP, and stop it, if not stopped
  3010. yet. */
  3011. static void
  3012. suspend_and_send_sigstop (thread_info *thread, lwp_info *except)
  3013. {
  3014. struct lwp_info *lwp = get_thread_lwp (thread);
  3015. /* Ignore EXCEPT. */
  3016. if (lwp == except)
  3017. return;
  3018. lwp_suspended_inc (lwp);
  3019. send_sigstop (thread, except);
  3020. }
  3021. static void
  3022. mark_lwp_dead (struct lwp_info *lwp, int wstat)
  3023. {
  3024. /* Store the exit status for later. */
  3025. lwp->status_pending_p = 1;
  3026. lwp->status_pending = wstat;
  3027. /* Store in waitstatus as well, as there's nothing else to process
  3028. for this event. */
  3029. if (WIFEXITED (wstat))
  3030. lwp->waitstatus.set_exited (WEXITSTATUS (wstat));
  3031. else if (WIFSIGNALED (wstat))
  3032. lwp->waitstatus.set_signalled (gdb_signal_from_host (WTERMSIG (wstat)));
  3033. /* Prevent trying to stop it. */
  3034. lwp->stopped = 1;
  3035. /* No further stops are expected from a dead lwp. */
  3036. lwp->stop_expected = 0;
  3037. }
  3038. /* Return true if LWP has exited already, and has a pending exit event
  3039. to report to GDB. */
  3040. static int
  3041. lwp_is_marked_dead (struct lwp_info *lwp)
  3042. {
  3043. return (lwp->status_pending_p
  3044. && (WIFEXITED (lwp->status_pending)
  3045. || WIFSIGNALED (lwp->status_pending)));
  3046. }
  3047. void
  3048. linux_process_target::wait_for_sigstop ()
  3049. {
  3050. struct thread_info *saved_thread;
  3051. ptid_t saved_tid;
  3052. int wstat;
  3053. int ret;
  3054. saved_thread = current_thread;
  3055. if (saved_thread != NULL)
  3056. saved_tid = saved_thread->id;
  3057. else
  3058. saved_tid = null_ptid; /* avoid bogus unused warning */
  3059. scoped_restore_current_thread restore_thread;
  3060. threads_debug_printf ("pulling events");
  3061. /* Passing NULL_PTID as filter indicates we want all events to be
  3062. left pending. Eventually this returns when there are no
  3063. unwaited-for children left. */
  3064. ret = wait_for_event_filtered (minus_one_ptid, null_ptid, &wstat, __WALL);
  3065. gdb_assert (ret == -1);
  3066. if (saved_thread == NULL || mythread_alive (saved_tid))
  3067. return;
  3068. else
  3069. {
  3070. threads_debug_printf ("Previously current thread died.");
  3071. /* We can't change the current inferior behind GDB's back,
  3072. otherwise, a subsequent command may apply to the wrong
  3073. process. */
  3074. restore_thread.dont_restore ();
  3075. switch_to_thread (nullptr);
  3076. }
  3077. }
  3078. bool
  3079. linux_process_target::stuck_in_jump_pad (thread_info *thread)
  3080. {
  3081. struct lwp_info *lwp = get_thread_lwp (thread);
  3082. if (lwp->suspended != 0)
  3083. {
  3084. internal_error (__FILE__, __LINE__,
  3085. "LWP %ld is suspended, suspended=%d\n",
  3086. lwpid_of (thread), lwp->suspended);
  3087. }
  3088. gdb_assert (lwp->stopped);
  3089. /* Allow debugging the jump pad, gdb_collect, etc.. */
  3090. return (supports_fast_tracepoints ()
  3091. && agent_loaded_p ()
  3092. && (gdb_breakpoint_here (lwp->stop_pc)
  3093. || lwp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT
  3094. || thread->last_resume_kind == resume_step)
  3095. && (linux_fast_tracepoint_collecting (lwp, NULL)
  3096. != fast_tpoint_collect_result::not_collecting));
  3097. }
  3098. void
  3099. linux_process_target::move_out_of_jump_pad (thread_info *thread)
  3100. {
  3101. struct lwp_info *lwp = get_thread_lwp (thread);
  3102. int *wstat;
  3103. if (lwp->suspended != 0)
  3104. {
  3105. internal_error (__FILE__, __LINE__,
  3106. "LWP %ld is suspended, suspended=%d\n",
  3107. lwpid_of (thread), lwp->suspended);
  3108. }
  3109. gdb_assert (lwp->stopped);
  3110. /* For gdb_breakpoint_here. */
  3111. scoped_restore_current_thread restore_thread;
  3112. switch_to_thread (thread);
  3113. wstat = lwp->status_pending_p ? &lwp->status_pending : NULL;
  3114. /* Allow debugging the jump pad, gdb_collect, etc. */
  3115. if (!gdb_breakpoint_here (lwp->stop_pc)
  3116. && lwp->stop_reason != TARGET_STOPPED_BY_WATCHPOINT
  3117. && thread->last_resume_kind != resume_step
  3118. && maybe_move_out_of_jump_pad (lwp, wstat))
  3119. {
  3120. threads_debug_printf ("LWP %ld needs stabilizing (in jump pad)",
  3121. lwpid_of (thread));
  3122. if (wstat)
  3123. {
  3124. lwp->status_pending_p = 0;
  3125. enqueue_one_deferred_signal (lwp, wstat);
  3126. threads_debug_printf ("Signal %d for LWP %ld deferred (in jump pad",
  3127. WSTOPSIG (*wstat), lwpid_of (thread));
  3128. }
  3129. resume_one_lwp (lwp, 0, 0, NULL);
  3130. }
  3131. else
  3132. lwp_suspended_inc (lwp);
  3133. }
  3134. static bool
  3135. lwp_running (thread_info *thread)
  3136. {
  3137. struct lwp_info *lwp = get_thread_lwp (thread);
  3138. if (lwp_is_marked_dead (lwp))
  3139. return false;
  3140. return !lwp->stopped;
  3141. }
  3142. void
  3143. linux_process_target::stop_all_lwps (int suspend, lwp_info *except)
  3144. {
  3145. /* Should not be called recursively. */
  3146. gdb_assert (stopping_threads == NOT_STOPPING_THREADS);
  3147. THREADS_SCOPED_DEBUG_ENTER_EXIT;
  3148. threads_debug_printf
  3149. ("%s, except=%s", suspend ? "stop-and-suspend" : "stop",
  3150. (except != NULL
  3151. ? target_pid_to_str (ptid_of (get_lwp_thread (except))).c_str ()
  3152. : "none"));
  3153. stopping_threads = (suspend
  3154. ? STOPPING_AND_SUSPENDING_THREADS
  3155. : STOPPING_THREADS);
  3156. if (suspend)
  3157. for_each_thread ([&] (thread_info *thread)
  3158. {
  3159. suspend_and_send_sigstop (thread, except);
  3160. });
  3161. else
  3162. for_each_thread ([&] (thread_info *thread)
  3163. {
  3164. send_sigstop (thread, except);
  3165. });
  3166. wait_for_sigstop ();
  3167. stopping_threads = NOT_STOPPING_THREADS;
  3168. threads_debug_printf ("setting stopping_threads back to !stopping");
  3169. }
  3170. /* Enqueue one signal in the chain of signals which need to be
  3171. delivered to this process on next resume. */
  3172. static void
  3173. enqueue_pending_signal (struct lwp_info *lwp, int signal, siginfo_t *info)
  3174. {
  3175. lwp->pending_signals.emplace_back (signal);
  3176. if (info == nullptr)
  3177. memset (&lwp->pending_signals.back ().info, 0, sizeof (siginfo_t));
  3178. else
  3179. lwp->pending_signals.back ().info = *info;
  3180. }
  3181. void
  3182. linux_process_target::install_software_single_step_breakpoints (lwp_info *lwp)
  3183. {
  3184. struct thread_info *thread = get_lwp_thread (lwp);
  3185. struct regcache *regcache = get_thread_regcache (thread, 1);
  3186. scoped_restore_current_thread restore_thread;
  3187. switch_to_thread (thread);
  3188. std::vector<CORE_ADDR> next_pcs = low_get_next_pcs (regcache);
  3189. for (CORE_ADDR pc : next_pcs)
  3190. set_single_step_breakpoint (pc, current_ptid);
  3191. }
  3192. int
  3193. linux_process_target::single_step (lwp_info* lwp)
  3194. {
  3195. int step = 0;
  3196. if (supports_hardware_single_step ())
  3197. {
  3198. step = 1;
  3199. }
  3200. else if (supports_software_single_step ())
  3201. {
  3202. install_software_single_step_breakpoints (lwp);
  3203. step = 0;
  3204. }
  3205. else
  3206. threads_debug_printf ("stepping is not implemented on this target");
  3207. return step;
  3208. }
  3209. /* The signal can be delivered to the inferior if we are not trying to
  3210. finish a fast tracepoint collect. Since signal can be delivered in
  3211. the step-over, the program may go to signal handler and trap again
  3212. after return from the signal handler. We can live with the spurious
  3213. double traps. */
  3214. static int
  3215. lwp_signal_can_be_delivered (struct lwp_info *lwp)
  3216. {
  3217. return (lwp->collecting_fast_tracepoint
  3218. == fast_tpoint_collect_result::not_collecting);
  3219. }
  3220. void
  3221. linux_process_target::resume_one_lwp_throw (lwp_info *lwp, int step,
  3222. int signal, siginfo_t *info)
  3223. {
  3224. struct thread_info *thread = get_lwp_thread (lwp);
  3225. int ptrace_request;
  3226. struct process_info *proc = get_thread_process (thread);
  3227. /* Note that target description may not be initialised
  3228. (proc->tdesc == NULL) at this point because the program hasn't
  3229. stopped at the first instruction yet. It means GDBserver skips
  3230. the extra traps from the wrapper program (see option --wrapper).
  3231. Code in this function that requires register access should be
  3232. guarded by proc->tdesc == NULL or something else. */
  3233. if (lwp->stopped == 0)
  3234. return;
  3235. gdb_assert (lwp->waitstatus.kind () == TARGET_WAITKIND_IGNORE);
  3236. fast_tpoint_collect_result fast_tp_collecting
  3237. = lwp->collecting_fast_tracepoint;
  3238. gdb_assert (!stabilizing_threads
  3239. || (fast_tp_collecting
  3240. != fast_tpoint_collect_result::not_collecting));
  3241. /* Cancel actions that rely on GDB not changing the PC (e.g., the
  3242. user used the "jump" command, or "set $pc = foo"). */
  3243. if (thread->while_stepping != NULL && lwp->stop_pc != get_pc (lwp))
  3244. {
  3245. /* Collecting 'while-stepping' actions doesn't make sense
  3246. anymore. */
  3247. release_while_stepping_state_list (thread);
  3248. }
  3249. /* If we have pending signals or status, and a new signal, enqueue the
  3250. signal. Also enqueue the signal if it can't be delivered to the
  3251. inferior right now. */
  3252. if (signal != 0
  3253. && (lwp->status_pending_p
  3254. || !lwp->pending_signals.empty ()
  3255. || !lwp_signal_can_be_delivered (lwp)))
  3256. {
  3257. enqueue_pending_signal (lwp, signal, info);
  3258. /* Postpone any pending signal. It was enqueued above. */
  3259. signal = 0;
  3260. }
  3261. if (lwp->status_pending_p)
  3262. {
  3263. threads_debug_printf
  3264. ("Not resuming lwp %ld (%s, stop %s); has pending status",
  3265. lwpid_of (thread), step ? "step" : "continue",
  3266. lwp->stop_expected ? "expected" : "not expected");
  3267. return;
  3268. }
  3269. scoped_restore_current_thread restore_thread;
  3270. switch_to_thread (thread);
  3271. /* This bit needs some thinking about. If we get a signal that
  3272. we must report while a single-step reinsert is still pending,
  3273. we often end up resuming the thread. It might be better to
  3274. (ew) allow a stack of pending events; then we could be sure that
  3275. the reinsert happened right away and not lose any signals.
  3276. Making this stack would also shrink the window in which breakpoints are
  3277. uninserted (see comment in linux_wait_for_lwp) but not enough for
  3278. complete correctness, so it won't solve that problem. It may be
  3279. worthwhile just to solve this one, however. */
  3280. if (lwp->bp_reinsert != 0)
  3281. {
  3282. threads_debug_printf (" pending reinsert at 0x%s",
  3283. paddress (lwp->bp_reinsert));
  3284. if (supports_hardware_single_step ())
  3285. {
  3286. if (fast_tp_collecting == fast_tpoint_collect_result::not_collecting)
  3287. {
  3288. if (step == 0)
  3289. warning ("BAD - reinserting but not stepping.");
  3290. if (lwp->suspended)
  3291. warning ("BAD - reinserting and suspended(%d).",
  3292. lwp->suspended);
  3293. }
  3294. }
  3295. step = maybe_hw_step (thread);
  3296. }
  3297. if (fast_tp_collecting == fast_tpoint_collect_result::before_insn)
  3298. threads_debug_printf
  3299. ("lwp %ld wants to get out of fast tracepoint jump pad "
  3300. "(exit-jump-pad-bkpt)", lwpid_of (thread));
  3301. else if (fast_tp_collecting == fast_tpoint_collect_result::at_insn)
  3302. {
  3303. threads_debug_printf
  3304. ("lwp %ld wants to get out of fast tracepoint jump pad single-stepping",
  3305. lwpid_of (thread));
  3306. if (supports_hardware_single_step ())
  3307. step = 1;
  3308. else
  3309. {
  3310. internal_error (__FILE__, __LINE__,
  3311. "moving out of jump pad single-stepping"
  3312. " not implemented on this target");
  3313. }
  3314. }
  3315. /* If we have while-stepping actions in this thread set it stepping.
  3316. If we have a signal to deliver, it may or may not be set to
  3317. SIG_IGN, we don't know. Assume so, and allow collecting
  3318. while-stepping into a signal handler. A possible smart thing to
  3319. do would be to set an internal breakpoint at the signal return
  3320. address, continue, and carry on catching this while-stepping
  3321. action only when that breakpoint is hit. A future
  3322. enhancement. */
  3323. if (thread->while_stepping != NULL)
  3324. {
  3325. threads_debug_printf
  3326. ("lwp %ld has a while-stepping action -> forcing step.",
  3327. lwpid_of (thread));
  3328. step = single_step (lwp);
  3329. }
  3330. if (proc->tdesc != NULL && low_supports_breakpoints ())
  3331. {
  3332. struct regcache *regcache = get_thread_regcache (current_thread, 1);
  3333. lwp->stop_pc = low_get_pc (regcache);
  3334. threads_debug_printf (" %s from pc 0x%lx", step ? "step" : "continue",
  3335. (long) lwp->stop_pc);
  3336. }
  3337. /* If we have pending signals, consume one if it can be delivered to
  3338. the inferior. */
  3339. if (!lwp->pending_signals.empty () && lwp_signal_can_be_delivered (lwp))
  3340. {
  3341. const pending_signal &p_sig = lwp->pending_signals.front ();
  3342. signal = p_sig.signal;
  3343. if (p_sig.info.si_signo != 0)
  3344. ptrace (PTRACE_SETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
  3345. &p_sig.info);
  3346. lwp->pending_signals.pop_front ();
  3347. }
  3348. threads_debug_printf ("Resuming lwp %ld (%s, signal %d, stop %s)",
  3349. lwpid_of (thread), step ? "step" : "continue", signal,
  3350. lwp->stop_expected ? "expected" : "not expected");
  3351. low_prepare_to_resume (lwp);
  3352. regcache_invalidate_thread (thread);
  3353. errno = 0;
  3354. lwp->stepping = step;
  3355. if (step)
  3356. ptrace_request = PTRACE_SINGLESTEP;
  3357. else if (gdb_catching_syscalls_p (lwp))
  3358. ptrace_request = PTRACE_SYSCALL;
  3359. else
  3360. ptrace_request = PTRACE_CONT;
  3361. ptrace (ptrace_request,
  3362. lwpid_of (thread),
  3363. (PTRACE_TYPE_ARG3) 0,
  3364. /* Coerce to a uintptr_t first to avoid potential gcc warning
  3365. of coercing an 8 byte integer to a 4 byte pointer. */
  3366. (PTRACE_TYPE_ARG4) (uintptr_t) signal);
  3367. if (errno)
  3368. {
  3369. int saved_errno = errno;
  3370. threads_debug_printf ("ptrace errno = %d (%s)",
  3371. saved_errno, strerror (saved_errno));
  3372. errno = saved_errno;
  3373. perror_with_name ("resuming thread");
  3374. }
  3375. /* Successfully resumed. Clear state that no longer makes sense,
  3376. and mark the LWP as running. Must not do this before resuming
  3377. otherwise if that fails other code will be confused. E.g., we'd
  3378. later try to stop the LWP and hang forever waiting for a stop
  3379. status. Note that we must not throw after this is cleared,
  3380. otherwise handle_zombie_lwp_error would get confused. */
  3381. lwp->stopped = 0;
  3382. lwp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
  3383. }
  3384. void
  3385. linux_process_target::low_prepare_to_resume (lwp_info *lwp)
  3386. {
  3387. /* Nop. */
  3388. }
  3389. /* Called when we try to resume a stopped LWP and that errors out. If
  3390. the LWP is no longer in ptrace-stopped state (meaning it's zombie,
  3391. or about to become), discard the error, clear any pending status
  3392. the LWP may have, and return true (we'll collect the exit status
  3393. soon enough). Otherwise, return false. */
  3394. static int
  3395. check_ptrace_stopped_lwp_gone (struct lwp_info *lp)
  3396. {
  3397. struct thread_info *thread = get_lwp_thread (lp);
  3398. /* If we get an error after resuming the LWP successfully, we'd
  3399. confuse !T state for the LWP being gone. */
  3400. gdb_assert (lp->stopped);
  3401. /* We can't just check whether the LWP is in 'Z (Zombie)' state,
  3402. because even if ptrace failed with ESRCH, the tracee may be "not
  3403. yet fully dead", but already refusing ptrace requests. In that
  3404. case the tracee has 'R (Running)' state for a little bit
  3405. (observed in Linux 3.18). See also the note on ESRCH in the
  3406. ptrace(2) man page. Instead, check whether the LWP has any state
  3407. other than ptrace-stopped. */
  3408. /* Don't assume anything if /proc/PID/status can't be read. */
  3409. if (linux_proc_pid_is_trace_stopped_nowarn (lwpid_of (thread)) == 0)
  3410. {
  3411. lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
  3412. lp->status_pending_p = 0;
  3413. return 1;
  3414. }
  3415. return 0;
  3416. }
  3417. void
  3418. linux_process_target::resume_one_lwp (lwp_info *lwp, int step, int signal,
  3419. siginfo_t *info)
  3420. {
  3421. try
  3422. {
  3423. resume_one_lwp_throw (lwp, step, signal, info);
  3424. }
  3425. catch (const gdb_exception_error &ex)
  3426. {
  3427. if (check_ptrace_stopped_lwp_gone (lwp))
  3428. {
  3429. /* This could because we tried to resume an LWP after its leader
  3430. exited. Mark it as resumed, so we can collect an exit event
  3431. from it. */
  3432. lwp->stopped = 0;
  3433. lwp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
  3434. }
  3435. else
  3436. throw;
  3437. }
  3438. }
  3439. /* This function is called once per thread via for_each_thread.
  3440. We look up which resume request applies to THREAD and mark it with a
  3441. pointer to the appropriate resume request.
  3442. This algorithm is O(threads * resume elements), but resume elements
  3443. is small (and will remain small at least until GDB supports thread
  3444. suspension). */
  3445. static void
  3446. linux_set_resume_request (thread_info *thread, thread_resume *resume, size_t n)
  3447. {
  3448. struct lwp_info *lwp = get_thread_lwp (thread);
  3449. for (int ndx = 0; ndx < n; ndx++)
  3450. {
  3451. ptid_t ptid = resume[ndx].thread;
  3452. if (ptid == minus_one_ptid
  3453. || ptid == thread->id
  3454. /* Handle both 'pPID' and 'pPID.-1' as meaning 'all threads
  3455. of PID'. */
  3456. || (ptid.pid () == pid_of (thread)
  3457. && (ptid.is_pid ()
  3458. || ptid.lwp () == -1)))
  3459. {
  3460. if (resume[ndx].kind == resume_stop
  3461. && thread->last_resume_kind == resume_stop)
  3462. {
  3463. threads_debug_printf
  3464. ("already %s LWP %ld at GDB's request",
  3465. (thread->last_status.kind () == TARGET_WAITKIND_STOPPED
  3466. ? "stopped" : "stopping"),
  3467. lwpid_of (thread));
  3468. continue;
  3469. }
  3470. /* Ignore (wildcard) resume requests for already-resumed
  3471. threads. */
  3472. if (resume[ndx].kind != resume_stop
  3473. && thread->last_resume_kind != resume_stop)
  3474. {
  3475. threads_debug_printf
  3476. ("already %s LWP %ld at GDB's request",
  3477. (thread->last_resume_kind == resume_step
  3478. ? "stepping" : "continuing"),
  3479. lwpid_of (thread));
  3480. continue;
  3481. }
  3482. /* Don't let wildcard resumes resume fork children that GDB
  3483. does not yet know are new fork children. */
  3484. if (lwp->fork_relative != NULL)
  3485. {
  3486. struct lwp_info *rel = lwp->fork_relative;
  3487. if (rel->status_pending_p
  3488. && (rel->waitstatus.kind () == TARGET_WAITKIND_FORKED
  3489. || rel->waitstatus.kind () == TARGET_WAITKIND_VFORKED))
  3490. {
  3491. threads_debug_printf
  3492. ("not resuming LWP %ld: has queued stop reply",
  3493. lwpid_of (thread));
  3494. continue;
  3495. }
  3496. }
  3497. /* If the thread has a pending event that has already been
  3498. reported to GDBserver core, but GDB has not pulled the
  3499. event out of the vStopped queue yet, likewise, ignore the
  3500. (wildcard) resume request. */
  3501. if (in_queued_stop_replies (thread->id))
  3502. {
  3503. threads_debug_printf
  3504. ("not resuming LWP %ld: has queued stop reply",
  3505. lwpid_of (thread));
  3506. continue;
  3507. }
  3508. lwp->resume = &resume[ndx];
  3509. thread->last_resume_kind = lwp->resume->kind;
  3510. lwp->step_range_start = lwp->resume->step_range_start;
  3511. lwp->step_range_end = lwp->resume->step_range_end;
  3512. /* If we had a deferred signal to report, dequeue one now.
  3513. This can happen if LWP gets more than one signal while
  3514. trying to get out of a jump pad. */
  3515. if (lwp->stopped
  3516. && !lwp->status_pending_p
  3517. && dequeue_one_deferred_signal (lwp, &lwp->status_pending))
  3518. {
  3519. lwp->status_pending_p = 1;
  3520. threads_debug_printf
  3521. ("Dequeueing deferred signal %d for LWP %ld, "
  3522. "leaving status pending.",
  3523. WSTOPSIG (lwp->status_pending),
  3524. lwpid_of (thread));
  3525. }
  3526. return;
  3527. }
  3528. }
  3529. /* No resume action for this thread. */
  3530. lwp->resume = NULL;
  3531. }
  3532. bool
  3533. linux_process_target::resume_status_pending (thread_info *thread)
  3534. {
  3535. struct lwp_info *lwp = get_thread_lwp (thread);
  3536. /* LWPs which will not be resumed are not interesting, because
  3537. we might not wait for them next time through linux_wait. */
  3538. if (lwp->resume == NULL)
  3539. return false;
  3540. return thread_still_has_status_pending (thread);
  3541. }
  3542. bool
  3543. linux_process_target::thread_needs_step_over (thread_info *thread)
  3544. {
  3545. struct lwp_info *lwp = get_thread_lwp (thread);
  3546. CORE_ADDR pc;
  3547. struct process_info *proc = get_thread_process (thread);
  3548. /* GDBserver is skipping the extra traps from the wrapper program,
  3549. don't have to do step over. */
  3550. if (proc->tdesc == NULL)
  3551. return false;
  3552. /* LWPs which will not be resumed are not interesting, because we
  3553. might not wait for them next time through linux_wait. */
  3554. if (!lwp->stopped)
  3555. {
  3556. threads_debug_printf ("Need step over [LWP %ld]? Ignoring, not stopped",
  3557. lwpid_of (thread));
  3558. return false;
  3559. }
  3560. if (thread->last_resume_kind == resume_stop)
  3561. {
  3562. threads_debug_printf
  3563. ("Need step over [LWP %ld]? Ignoring, should remain stopped",
  3564. lwpid_of (thread));
  3565. return false;
  3566. }
  3567. gdb_assert (lwp->suspended >= 0);
  3568. if (lwp->suspended)
  3569. {
  3570. threads_debug_printf ("Need step over [LWP %ld]? Ignoring, suspended",
  3571. lwpid_of (thread));
  3572. return false;
  3573. }
  3574. if (lwp->status_pending_p)
  3575. {
  3576. threads_debug_printf
  3577. ("Need step over [LWP %ld]? Ignoring, has pending status.",
  3578. lwpid_of (thread));
  3579. return false;
  3580. }
  3581. /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
  3582. or we have. */
  3583. pc = get_pc (lwp);
  3584. /* If the PC has changed since we stopped, then don't do anything,
  3585. and let the breakpoint/tracepoint be hit. This happens if, for
  3586. instance, GDB handled the decr_pc_after_break subtraction itself,
  3587. GDB is OOL stepping this thread, or the user has issued a "jump"
  3588. command, or poked thread's registers herself. */
  3589. if (pc != lwp->stop_pc)
  3590. {
  3591. threads_debug_printf
  3592. ("Need step over [LWP %ld]? Cancelling, PC was changed. "
  3593. "Old stop_pc was 0x%s, PC is now 0x%s", lwpid_of (thread),
  3594. paddress (lwp->stop_pc), paddress (pc));
  3595. return false;
  3596. }
  3597. /* On software single step target, resume the inferior with signal
  3598. rather than stepping over. */
  3599. if (supports_software_single_step ()
  3600. && !lwp->pending_signals.empty ()
  3601. && lwp_signal_can_be_delivered (lwp))
  3602. {
  3603. threads_debug_printf
  3604. ("Need step over [LWP %ld]? Ignoring, has pending signals.",
  3605. lwpid_of (thread));
  3606. return false;
  3607. }
  3608. scoped_restore_current_thread restore_thread;
  3609. switch_to_thread (thread);
  3610. /* We can only step over breakpoints we know about. */
  3611. if (breakpoint_here (pc) || fast_tracepoint_jump_here (pc))
  3612. {
  3613. /* Don't step over a breakpoint that GDB expects to hit
  3614. though. If the condition is being evaluated on the target's side
  3615. and it evaluate to false, step over this breakpoint as well. */
  3616. if (gdb_breakpoint_here (pc)
  3617. && gdb_condition_true_at_breakpoint (pc)
  3618. && gdb_no_commands_at_breakpoint (pc))
  3619. {
  3620. threads_debug_printf ("Need step over [LWP %ld]? yes, but found"
  3621. " GDB breakpoint at 0x%s; skipping step over",
  3622. lwpid_of (thread), paddress (pc));
  3623. return false;
  3624. }
  3625. else
  3626. {
  3627. threads_debug_printf ("Need step over [LWP %ld]? yes, "
  3628. "found breakpoint at 0x%s",
  3629. lwpid_of (thread), paddress (pc));
  3630. /* We've found an lwp that needs stepping over --- return 1 so
  3631. that find_thread stops looking. */
  3632. return true;
  3633. }
  3634. }
  3635. threads_debug_printf
  3636. ("Need step over [LWP %ld]? No, no breakpoint found at 0x%s",
  3637. lwpid_of (thread), paddress (pc));
  3638. return false;
  3639. }
  3640. void
  3641. linux_process_target::start_step_over (lwp_info *lwp)
  3642. {
  3643. struct thread_info *thread = get_lwp_thread (lwp);
  3644. CORE_ADDR pc;
  3645. threads_debug_printf ("Starting step-over on LWP %ld. Stopping all threads",
  3646. lwpid_of (thread));
  3647. stop_all_lwps (1, lwp);
  3648. if (lwp->suspended != 0)
  3649. {
  3650. internal_error (__FILE__, __LINE__,
  3651. "LWP %ld suspended=%d\n", lwpid_of (thread),
  3652. lwp->suspended);
  3653. }
  3654. threads_debug_printf ("Done stopping all threads for step-over.");
  3655. /* Note, we should always reach here with an already adjusted PC,
  3656. either by GDB (if we're resuming due to GDB's request), or by our
  3657. caller, if we just finished handling an internal breakpoint GDB
  3658. shouldn't care about. */
  3659. pc = get_pc (lwp);
  3660. bool step = false;
  3661. {
  3662. scoped_restore_current_thread restore_thread;
  3663. switch_to_thread (thread);
  3664. lwp->bp_reinsert = pc;
  3665. uninsert_breakpoints_at (pc);
  3666. uninsert_fast_tracepoint_jumps_at (pc);
  3667. step = single_step (lwp);
  3668. }
  3669. resume_one_lwp (lwp, step, 0, NULL);
  3670. /* Require next event from this LWP. */
  3671. step_over_bkpt = thread->id;
  3672. }
  3673. bool
  3674. linux_process_target::finish_step_over (lwp_info *lwp)
  3675. {
  3676. if (lwp->bp_reinsert != 0)
  3677. {
  3678. scoped_restore_current_thread restore_thread;
  3679. threads_debug_printf ("Finished step over.");
  3680. switch_to_thread (get_lwp_thread (lwp));
  3681. /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
  3682. may be no breakpoint to reinsert there by now. */
  3683. reinsert_breakpoints_at (lwp->bp_reinsert);
  3684. reinsert_fast_tracepoint_jumps_at (lwp->bp_reinsert);
  3685. lwp->bp_reinsert = 0;
  3686. /* Delete any single-step breakpoints. No longer needed. We
  3687. don't have to worry about other threads hitting this trap,
  3688. and later not being able to explain it, because we were
  3689. stepping over a breakpoint, and we hold all threads but
  3690. LWP stopped while doing that. */
  3691. if (!supports_hardware_single_step ())
  3692. {
  3693. gdb_assert (has_single_step_breakpoints (current_thread));
  3694. delete_single_step_breakpoints (current_thread);
  3695. }
  3696. step_over_bkpt = null_ptid;
  3697. return true;
  3698. }
  3699. else
  3700. return false;
  3701. }
  3702. void
  3703. linux_process_target::complete_ongoing_step_over ()
  3704. {
  3705. if (step_over_bkpt != null_ptid)
  3706. {
  3707. struct lwp_info *lwp;
  3708. int wstat;
  3709. int ret;
  3710. threads_debug_printf ("detach: step over in progress, finish it first");
  3711. /* Passing NULL_PTID as filter indicates we want all events to
  3712. be left pending. Eventually this returns when there are no
  3713. unwaited-for children left. */
  3714. ret = wait_for_event_filtered (minus_one_ptid, null_ptid, &wstat,
  3715. __WALL);
  3716. gdb_assert (ret == -1);
  3717. lwp = find_lwp_pid (step_over_bkpt);
  3718. if (lwp != NULL)
  3719. {
  3720. finish_step_over (lwp);
  3721. /* If we got our step SIGTRAP, don't leave it pending,
  3722. otherwise we would report it to GDB as a spurious
  3723. SIGTRAP. */
  3724. gdb_assert (lwp->status_pending_p);
  3725. if (WIFSTOPPED (lwp->status_pending)
  3726. && WSTOPSIG (lwp->status_pending) == SIGTRAP)
  3727. {
  3728. thread_info *thread = get_lwp_thread (lwp);
  3729. if (thread->last_resume_kind != resume_step)
  3730. {
  3731. threads_debug_printf ("detach: discard step-over SIGTRAP");
  3732. lwp->status_pending_p = 0;
  3733. lwp->status_pending = 0;
  3734. resume_one_lwp (lwp, lwp->stepping, 0, NULL);
  3735. }
  3736. else
  3737. threads_debug_printf
  3738. ("detach: resume_step, not discarding step-over SIGTRAP");
  3739. }
  3740. }
  3741. step_over_bkpt = null_ptid;
  3742. unsuspend_all_lwps (lwp);
  3743. }
  3744. }
  3745. void
  3746. linux_process_target::resume_one_thread (thread_info *thread,
  3747. bool leave_all_stopped)
  3748. {
  3749. struct lwp_info *lwp = get_thread_lwp (thread);
  3750. int leave_pending;
  3751. if (lwp->resume == NULL)
  3752. return;
  3753. if (lwp->resume->kind == resume_stop)
  3754. {
  3755. threads_debug_printf ("resume_stop request for LWP %ld",
  3756. lwpid_of (thread));
  3757. if (!lwp->stopped)
  3758. {
  3759. threads_debug_printf ("stopping LWP %ld", lwpid_of (thread));
  3760. /* Stop the thread, and wait for the event asynchronously,
  3761. through the event loop. */
  3762. send_sigstop (lwp);
  3763. }
  3764. else
  3765. {
  3766. threads_debug_printf ("already stopped LWP %ld", lwpid_of (thread));
  3767. /* The LWP may have been stopped in an internal event that
  3768. was not meant to be notified back to GDB (e.g., gdbserver
  3769. breakpoint), so we should be reporting a stop event in
  3770. this case too. */
  3771. /* If the thread already has a pending SIGSTOP, this is a
  3772. no-op. Otherwise, something later will presumably resume
  3773. the thread and this will cause it to cancel any pending
  3774. operation, due to last_resume_kind == resume_stop. If
  3775. the thread already has a pending status to report, we
  3776. will still report it the next time we wait - see
  3777. status_pending_p_callback. */
  3778. /* If we already have a pending signal to report, then
  3779. there's no need to queue a SIGSTOP, as this means we're
  3780. midway through moving the LWP out of the jumppad, and we
  3781. will report the pending signal as soon as that is
  3782. finished. */
  3783. if (lwp->pending_signals_to_report.empty ())
  3784. send_sigstop (lwp);
  3785. }
  3786. /* For stop requests, we're done. */
  3787. lwp->resume = NULL;
  3788. thread->last_status.set_ignore ();
  3789. return;
  3790. }
  3791. /* If this thread which is about to be resumed has a pending status,
  3792. then don't resume it - we can just report the pending status.
  3793. Likewise if it is suspended, because e.g., another thread is
  3794. stepping past a breakpoint. Make sure to queue any signals that
  3795. would otherwise be sent. In all-stop mode, we do this decision
  3796. based on if *any* thread has a pending status. If there's a
  3797. thread that needs the step-over-breakpoint dance, then don't
  3798. resume any other thread but that particular one. */
  3799. leave_pending = (lwp->suspended
  3800. || lwp->status_pending_p
  3801. || leave_all_stopped);
  3802. /* If we have a new signal, enqueue the signal. */
  3803. if (lwp->resume->sig != 0)
  3804. {
  3805. siginfo_t info, *info_p;
  3806. /* If this is the same signal we were previously stopped by,
  3807. make sure to queue its siginfo. */
  3808. if (WIFSTOPPED (lwp->last_status)
  3809. && WSTOPSIG (lwp->last_status) == lwp->resume->sig
  3810. && ptrace (PTRACE_GETSIGINFO, lwpid_of (thread),
  3811. (PTRACE_TYPE_ARG3) 0, &info) == 0)
  3812. info_p = &info;
  3813. else
  3814. info_p = NULL;
  3815. enqueue_pending_signal (lwp, lwp->resume->sig, info_p);
  3816. }
  3817. if (!leave_pending)
  3818. {
  3819. threads_debug_printf ("resuming LWP %ld", lwpid_of (thread));
  3820. proceed_one_lwp (thread, NULL);
  3821. }
  3822. else
  3823. threads_debug_printf ("leaving LWP %ld stopped", lwpid_of (thread));
  3824. thread->last_status.set_ignore ();
  3825. lwp->resume = NULL;
  3826. }
  3827. void
  3828. linux_process_target::resume (thread_resume *resume_info, size_t n)
  3829. {
  3830. struct thread_info *need_step_over = NULL;
  3831. THREADS_SCOPED_DEBUG_ENTER_EXIT;
  3832. for_each_thread ([&] (thread_info *thread)
  3833. {
  3834. linux_set_resume_request (thread, resume_info, n);
  3835. });
  3836. /* If there is a thread which would otherwise be resumed, which has
  3837. a pending status, then don't resume any threads - we can just
  3838. report the pending status. Make sure to queue any signals that
  3839. would otherwise be sent. In non-stop mode, we'll apply this
  3840. logic to each thread individually. We consume all pending events
  3841. before considering to start a step-over (in all-stop). */
  3842. bool any_pending = false;
  3843. if (!non_stop)
  3844. any_pending = find_thread ([this] (thread_info *thread)
  3845. {
  3846. return resume_status_pending (thread);
  3847. }) != nullptr;
  3848. /* If there is a thread which would otherwise be resumed, which is
  3849. stopped at a breakpoint that needs stepping over, then don't
  3850. resume any threads - have it step over the breakpoint with all
  3851. other threads stopped, then resume all threads again. Make sure
  3852. to queue any signals that would otherwise be delivered or
  3853. queued. */
  3854. if (!any_pending && low_supports_breakpoints ())
  3855. need_step_over = find_thread ([this] (thread_info *thread)
  3856. {
  3857. return thread_needs_step_over (thread);
  3858. });
  3859. bool leave_all_stopped = (need_step_over != NULL || any_pending);
  3860. if (need_step_over != NULL)
  3861. threads_debug_printf ("Not resuming all, need step over");
  3862. else if (any_pending)
  3863. threads_debug_printf ("Not resuming, all-stop and found "
  3864. "an LWP with pending status");
  3865. else
  3866. threads_debug_printf ("Resuming, no pending status or step over needed");
  3867. /* Even if we're leaving threads stopped, queue all signals we'd
  3868. otherwise deliver. */
  3869. for_each_thread ([&] (thread_info *thread)
  3870. {
  3871. resume_one_thread (thread, leave_all_stopped);
  3872. });
  3873. if (need_step_over)
  3874. start_step_over (get_thread_lwp (need_step_over));
  3875. /* We may have events that were pending that can/should be sent to
  3876. the client now. Trigger a linux_wait call. */
  3877. if (target_is_async_p ())
  3878. async_file_mark ();
  3879. }
  3880. void
  3881. linux_process_target::proceed_one_lwp (thread_info *thread, lwp_info *except)
  3882. {
  3883. struct lwp_info *lwp = get_thread_lwp (thread);
  3884. int step;
  3885. if (lwp == except)
  3886. return;
  3887. threads_debug_printf ("lwp %ld", lwpid_of (thread));
  3888. if (!lwp->stopped)
  3889. {
  3890. threads_debug_printf (" LWP %ld already running", lwpid_of (thread));
  3891. return;
  3892. }
  3893. if (thread->last_resume_kind == resume_stop
  3894. && thread->last_status.kind () != TARGET_WAITKIND_IGNORE)
  3895. {
  3896. threads_debug_printf (" client wants LWP to remain %ld stopped",
  3897. lwpid_of (thread));
  3898. return;
  3899. }
  3900. if (lwp->status_pending_p)
  3901. {
  3902. threads_debug_printf (" LWP %ld has pending status, leaving stopped",
  3903. lwpid_of (thread));
  3904. return;
  3905. }
  3906. gdb_assert (lwp->suspended >= 0);
  3907. if (lwp->suspended)
  3908. {
  3909. threads_debug_printf (" LWP %ld is suspended", lwpid_of (thread));
  3910. return;
  3911. }
  3912. if (thread->last_resume_kind == resume_stop
  3913. && lwp->pending_signals_to_report.empty ()
  3914. && (lwp->collecting_fast_tracepoint
  3915. == fast_tpoint_collect_result::not_collecting))
  3916. {
  3917. /* We haven't reported this LWP as stopped yet (otherwise, the
  3918. last_status.kind check above would catch it, and we wouldn't
  3919. reach here. This LWP may have been momentarily paused by a
  3920. stop_all_lwps call while handling for example, another LWP's
  3921. step-over. In that case, the pending expected SIGSTOP signal
  3922. that was queued at vCont;t handling time will have already
  3923. been consumed by wait_for_sigstop, and so we need to requeue
  3924. another one here. Note that if the LWP already has a SIGSTOP
  3925. pending, this is a no-op. */
  3926. threads_debug_printf
  3927. ("Client wants LWP %ld to stop. Making sure it has a SIGSTOP pending",
  3928. lwpid_of (thread));
  3929. send_sigstop (lwp);
  3930. }
  3931. if (thread->last_resume_kind == resume_step)
  3932. {
  3933. threads_debug_printf (" stepping LWP %ld, client wants it stepping",
  3934. lwpid_of (thread));
  3935. /* If resume_step is requested by GDB, install single-step
  3936. breakpoints when the thread is about to be actually resumed if
  3937. the single-step breakpoints weren't removed. */
  3938. if (supports_software_single_step ()
  3939. && !has_single_step_breakpoints (thread))
  3940. install_software_single_step_breakpoints (lwp);
  3941. step = maybe_hw_step (thread);
  3942. }
  3943. else if (lwp->bp_reinsert != 0)
  3944. {
  3945. threads_debug_printf (" stepping LWP %ld, reinsert set",
  3946. lwpid_of (thread));
  3947. step = maybe_hw_step (thread);
  3948. }
  3949. else
  3950. step = 0;
  3951. resume_one_lwp (lwp, step, 0, NULL);
  3952. }
  3953. void
  3954. linux_process_target::unsuspend_and_proceed_one_lwp (thread_info *thread,
  3955. lwp_info *except)
  3956. {
  3957. struct lwp_info *lwp = get_thread_lwp (thread);
  3958. if (lwp == except)
  3959. return;
  3960. lwp_suspended_decr (lwp);
  3961. proceed_one_lwp (thread, except);
  3962. }
  3963. void
  3964. linux_process_target::proceed_all_lwps ()
  3965. {
  3966. struct thread_info *need_step_over;
  3967. /* If there is a thread which would otherwise be resumed, which is
  3968. stopped at a breakpoint that needs stepping over, then don't
  3969. resume any threads - have it step over the breakpoint with all
  3970. other threads stopped, then resume all threads again. */
  3971. if (low_supports_breakpoints ())
  3972. {
  3973. need_step_over = find_thread ([this] (thread_info *thread)
  3974. {
  3975. return thread_needs_step_over (thread);
  3976. });
  3977. if (need_step_over != NULL)
  3978. {
  3979. threads_debug_printf ("found thread %ld needing a step-over",
  3980. lwpid_of (need_step_over));
  3981. start_step_over (get_thread_lwp (need_step_over));
  3982. return;
  3983. }
  3984. }
  3985. threads_debug_printf ("Proceeding, no step-over needed");
  3986. for_each_thread ([this] (thread_info *thread)
  3987. {
  3988. proceed_one_lwp (thread, NULL);
  3989. });
  3990. }
  3991. void
  3992. linux_process_target::unstop_all_lwps (int unsuspend, lwp_info *except)
  3993. {
  3994. THREADS_SCOPED_DEBUG_ENTER_EXIT;
  3995. if (except)
  3996. threads_debug_printf ("except=(LWP %ld)",
  3997. lwpid_of (get_lwp_thread (except)));
  3998. else
  3999. threads_debug_printf ("except=nullptr");
  4000. if (unsuspend)
  4001. for_each_thread ([&] (thread_info *thread)
  4002. {
  4003. unsuspend_and_proceed_one_lwp (thread, except);
  4004. });
  4005. else
  4006. for_each_thread ([&] (thread_info *thread)
  4007. {
  4008. proceed_one_lwp (thread, except);
  4009. });
  4010. }
  4011. #ifdef HAVE_LINUX_REGSETS
  4012. #define use_linux_regsets 1
  4013. /* Returns true if REGSET has been disabled. */
  4014. static int
  4015. regset_disabled (struct regsets_info *info, struct regset_info *regset)
  4016. {
  4017. return (info->disabled_regsets != NULL
  4018. && info->disabled_regsets[regset - info->regsets]);
  4019. }
  4020. /* Disable REGSET. */
  4021. static void
  4022. disable_regset (struct regsets_info *info, struct regset_info *regset)
  4023. {
  4024. int dr_offset;
  4025. dr_offset = regset - info->regsets;
  4026. if (info->disabled_regsets == NULL)
  4027. info->disabled_regsets = (char *) xcalloc (1, info->num_regsets);
  4028. info->disabled_regsets[dr_offset] = 1;
  4029. }
  4030. static int
  4031. regsets_fetch_inferior_registers (struct regsets_info *regsets_info,
  4032. struct regcache *regcache)
  4033. {
  4034. struct regset_info *regset;
  4035. int saw_general_regs = 0;
  4036. int pid;
  4037. struct iovec iov;
  4038. pid = lwpid_of (current_thread);
  4039. for (regset = regsets_info->regsets; regset->size >= 0; regset++)
  4040. {
  4041. void *buf, *data;
  4042. int nt_type, res;
  4043. if (regset->size == 0 || regset_disabled (regsets_info, regset))
  4044. continue;
  4045. buf = xmalloc (regset->size);
  4046. nt_type = regset->nt_type;
  4047. if (nt_type)
  4048. {
  4049. iov.iov_base = buf;
  4050. iov.iov_len = regset->size;
  4051. data = (void *) &iov;
  4052. }
  4053. else
  4054. data = buf;
  4055. #ifndef __sparc__
  4056. res = ptrace (regset->get_request, pid,
  4057. (PTRACE_TYPE_ARG3) (long) nt_type, data);
  4058. #else
  4059. res = ptrace (regset->get_request, pid, data, nt_type);
  4060. #endif
  4061. if (res < 0)
  4062. {
  4063. if (errno == EIO
  4064. || (errno == EINVAL && regset->type == OPTIONAL_REGS))
  4065. {
  4066. /* If we get EIO on a regset, or an EINVAL and the regset is
  4067. optional, do not try it again for this process mode. */
  4068. disable_regset (regsets_info, regset);
  4069. }
  4070. else if (errno == ENODATA)
  4071. {
  4072. /* ENODATA may be returned if the regset is currently
  4073. not "active". This can happen in normal operation,
  4074. so suppress the warning in this case. */
  4075. }
  4076. else if (errno == ESRCH)
  4077. {
  4078. /* At this point, ESRCH should mean the process is
  4079. already gone, in which case we simply ignore attempts
  4080. to read its registers. */
  4081. }
  4082. else
  4083. {
  4084. char s[256];
  4085. sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d",
  4086. pid);
  4087. perror (s);
  4088. }
  4089. }
  4090. else
  4091. {
  4092. if (regset->type == GENERAL_REGS)
  4093. saw_general_regs = 1;
  4094. regset->store_function (regcache, buf);
  4095. }
  4096. free (buf);
  4097. }
  4098. if (saw_general_regs)
  4099. return 0;
  4100. else
  4101. return 1;
  4102. }
  4103. static int
  4104. regsets_store_inferior_registers (struct regsets_info *regsets_info,
  4105. struct regcache *regcache)
  4106. {
  4107. struct regset_info *regset;
  4108. int saw_general_regs = 0;
  4109. int pid;
  4110. struct iovec iov;
  4111. pid = lwpid_of (current_thread);
  4112. for (regset = regsets_info->regsets; regset->size >= 0; regset++)
  4113. {
  4114. void *buf, *data;
  4115. int nt_type, res;
  4116. if (regset->size == 0 || regset_disabled (regsets_info, regset)
  4117. || regset->fill_function == NULL)
  4118. continue;
  4119. buf = xmalloc (regset->size);
  4120. /* First fill the buffer with the current register set contents,
  4121. in case there are any items in the kernel's regset that are
  4122. not in gdbserver's regcache. */
  4123. nt_type = regset->nt_type;
  4124. if (nt_type)
  4125. {
  4126. iov.iov_base = buf;
  4127. iov.iov_len = regset->size;
  4128. data = (void *) &iov;
  4129. }
  4130. else
  4131. data = buf;
  4132. #ifndef __sparc__
  4133. res = ptrace (regset->get_request, pid,
  4134. (PTRACE_TYPE_ARG3) (long) nt_type, data);
  4135. #else
  4136. res = ptrace (regset->get_request, pid, data, nt_type);
  4137. #endif
  4138. if (res == 0)
  4139. {
  4140. /* Then overlay our cached registers on that. */
  4141. regset->fill_function (regcache, buf);
  4142. /* Only now do we write the register set. */
  4143. #ifndef __sparc__
  4144. res = ptrace (regset->set_request, pid,
  4145. (PTRACE_TYPE_ARG3) (long) nt_type, data);
  4146. #else
  4147. res = ptrace (regset->set_request, pid, data, nt_type);
  4148. #endif
  4149. }
  4150. if (res < 0)
  4151. {
  4152. if (errno == EIO
  4153. || (errno == EINVAL && regset->type == OPTIONAL_REGS))
  4154. {
  4155. /* If we get EIO on a regset, or an EINVAL and the regset is
  4156. optional, do not try it again for this process mode. */
  4157. disable_regset (regsets_info, regset);
  4158. }
  4159. else if (errno == ESRCH)
  4160. {
  4161. /* At this point, ESRCH should mean the process is
  4162. already gone, in which case we simply ignore attempts
  4163. to change its registers. See also the related
  4164. comment in resume_one_lwp. */
  4165. free (buf);
  4166. return 0;
  4167. }
  4168. else
  4169. {
  4170. perror ("Warning: ptrace(regsets_store_inferior_registers)");
  4171. }
  4172. }
  4173. else if (regset->type == GENERAL_REGS)
  4174. saw_general_regs = 1;
  4175. free (buf);
  4176. }
  4177. if (saw_general_regs)
  4178. return 0;
  4179. else
  4180. return 1;
  4181. }
  4182. #else /* !HAVE_LINUX_REGSETS */
  4183. #define use_linux_regsets 0
  4184. #define regsets_fetch_inferior_registers(regsets_info, regcache) 1
  4185. #define regsets_store_inferior_registers(regsets_info, regcache) 1
  4186. #endif
  4187. /* Return 1 if register REGNO is supported by one of the regset ptrace
  4188. calls or 0 if it has to be transferred individually. */
  4189. static int
  4190. linux_register_in_regsets (const struct regs_info *regs_info, int regno)
  4191. {
  4192. unsigned char mask = 1 << (regno % 8);
  4193. size_t index = regno / 8;
  4194. return (use_linux_regsets
  4195. && (regs_info->regset_bitmap == NULL
  4196. || (regs_info->regset_bitmap[index] & mask) != 0));
  4197. }
  4198. #ifdef HAVE_LINUX_USRREGS
  4199. static int
  4200. register_addr (const struct usrregs_info *usrregs, int regnum)
  4201. {
  4202. int addr;
  4203. if (regnum < 0 || regnum >= usrregs->num_regs)
  4204. error ("Invalid register number %d.", regnum);
  4205. addr = usrregs->regmap[regnum];
  4206. return addr;
  4207. }
  4208. void
  4209. linux_process_target::fetch_register (const usrregs_info *usrregs,
  4210. regcache *regcache, int regno)
  4211. {
  4212. CORE_ADDR regaddr;
  4213. int i, size;
  4214. char *buf;
  4215. int pid;
  4216. if (regno >= usrregs->num_regs)
  4217. return;
  4218. if (low_cannot_fetch_register (regno))
  4219. return;
  4220. regaddr = register_addr (usrregs, regno);
  4221. if (regaddr == -1)
  4222. return;
  4223. size = ((register_size (regcache->tdesc, regno)
  4224. + sizeof (PTRACE_XFER_TYPE) - 1)
  4225. & -sizeof (PTRACE_XFER_TYPE));
  4226. buf = (char *) alloca (size);
  4227. pid = lwpid_of (current_thread);
  4228. for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
  4229. {
  4230. errno = 0;
  4231. *(PTRACE_XFER_TYPE *) (buf + i) =
  4232. ptrace (PTRACE_PEEKUSER, pid,
  4233. /* Coerce to a uintptr_t first to avoid potential gcc warning
  4234. of coercing an 8 byte integer to a 4 byte pointer. */
  4235. (PTRACE_TYPE_ARG3) (uintptr_t) regaddr, (PTRACE_TYPE_ARG4) 0);
  4236. regaddr += sizeof (PTRACE_XFER_TYPE);
  4237. if (errno != 0)
  4238. {
  4239. /* Mark register REGNO unavailable. */
  4240. supply_register (regcache, regno, NULL);
  4241. return;
  4242. }
  4243. }
  4244. low_supply_ptrace_register (regcache, regno, buf);
  4245. }
  4246. void
  4247. linux_process_target::store_register (const usrregs_info *usrregs,
  4248. regcache *regcache, int regno)
  4249. {
  4250. CORE_ADDR regaddr;
  4251. int i, size;
  4252. char *buf;
  4253. int pid;
  4254. if (regno >= usrregs->num_regs)
  4255. return;
  4256. if (low_cannot_store_register (regno))
  4257. return;
  4258. regaddr = register_addr (usrregs, regno);
  4259. if (regaddr == -1)
  4260. return;
  4261. size = ((register_size (regcache->tdesc, regno)
  4262. + sizeof (PTRACE_XFER_TYPE) - 1)
  4263. & -sizeof (PTRACE_XFER_TYPE));
  4264. buf = (char *) alloca (size);
  4265. memset (buf, 0, size);
  4266. low_collect_ptrace_register (regcache, regno, buf);
  4267. pid = lwpid_of (current_thread);
  4268. for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
  4269. {
  4270. errno = 0;
  4271. ptrace (PTRACE_POKEUSER, pid,
  4272. /* Coerce to a uintptr_t first to avoid potential gcc warning
  4273. about coercing an 8 byte integer to a 4 byte pointer. */
  4274. (PTRACE_TYPE_ARG3) (uintptr_t) regaddr,
  4275. (PTRACE_TYPE_ARG4) *(PTRACE_XFER_TYPE *) (buf + i));
  4276. if (errno != 0)
  4277. {
  4278. /* At this point, ESRCH should mean the process is
  4279. already gone, in which case we simply ignore attempts
  4280. to change its registers. See also the related
  4281. comment in resume_one_lwp. */
  4282. if (errno == ESRCH)
  4283. return;
  4284. if (!low_cannot_store_register (regno))
  4285. error ("writing register %d: %s", regno, safe_strerror (errno));
  4286. }
  4287. regaddr += sizeof (PTRACE_XFER_TYPE);
  4288. }
  4289. }
  4290. #endif /* HAVE_LINUX_USRREGS */
  4291. void
  4292. linux_process_target::low_collect_ptrace_register (regcache *regcache,
  4293. int regno, char *buf)
  4294. {
  4295. collect_register (regcache, regno, buf);
  4296. }
  4297. void
  4298. linux_process_target::low_supply_ptrace_register (regcache *regcache,
  4299. int regno, const char *buf)
  4300. {
  4301. supply_register (regcache, regno, buf);
  4302. }
  4303. void
  4304. linux_process_target::usr_fetch_inferior_registers (const regs_info *regs_info,
  4305. regcache *regcache,
  4306. int regno, int all)
  4307. {
  4308. #ifdef HAVE_LINUX_USRREGS
  4309. struct usrregs_info *usr = regs_info->usrregs;
  4310. if (regno == -1)
  4311. {
  4312. for (regno = 0; regno < usr->num_regs; regno++)
  4313. if (all || !linux_register_in_regsets (regs_info, regno))
  4314. fetch_register (usr, regcache, regno);
  4315. }
  4316. else
  4317. fetch_register (usr, regcache, regno);
  4318. #endif
  4319. }
  4320. void
  4321. linux_process_target::usr_store_inferior_registers (const regs_info *regs_info,
  4322. regcache *regcache,
  4323. int regno, int all)
  4324. {
  4325. #ifdef HAVE_LINUX_USRREGS
  4326. struct usrregs_info *usr = regs_info->usrregs;
  4327. if (regno == -1)
  4328. {
  4329. for (regno = 0; regno < usr->num_regs; regno++)
  4330. if (all || !linux_register_in_regsets (regs_info, regno))
  4331. store_register (usr, regcache, regno);
  4332. }
  4333. else
  4334. store_register (usr, regcache, regno);
  4335. #endif
  4336. }
  4337. void
  4338. linux_process_target::fetch_registers (regcache *regcache, int regno)
  4339. {
  4340. int use_regsets;
  4341. int all = 0;
  4342. const regs_info *regs_info = get_regs_info ();
  4343. if (regno == -1)
  4344. {
  4345. if (regs_info->usrregs != NULL)
  4346. for (regno = 0; regno < regs_info->usrregs->num_regs; regno++)
  4347. low_fetch_register (regcache, regno);
  4348. all = regsets_fetch_inferior_registers (regs_info->regsets_info, regcache);
  4349. if (regs_info->usrregs != NULL)
  4350. usr_fetch_inferior_registers (regs_info, regcache, -1, all);
  4351. }
  4352. else
  4353. {
  4354. if (low_fetch_register (regcache, regno))
  4355. return;
  4356. use_regsets = linux_register_in_regsets (regs_info, regno);
  4357. if (use_regsets)
  4358. all = regsets_fetch_inferior_registers (regs_info->regsets_info,
  4359. regcache);
  4360. if ((!use_regsets || all) && regs_info->usrregs != NULL)
  4361. usr_fetch_inferior_registers (regs_info, regcache, regno, 1);
  4362. }
  4363. }
  4364. void
  4365. linux_process_target::store_registers (regcache *regcache, int regno)
  4366. {
  4367. int use_regsets;
  4368. int all = 0;
  4369. const regs_info *regs_info = get_regs_info ();
  4370. if (regno == -1)
  4371. {
  4372. all = regsets_store_inferior_registers (regs_info->regsets_info,
  4373. regcache);
  4374. if (regs_info->usrregs != NULL)
  4375. usr_store_inferior_registers (regs_info, regcache, regno, all);
  4376. }
  4377. else
  4378. {
  4379. use_regsets = linux_register_in_regsets (regs_info, regno);
  4380. if (use_regsets)
  4381. all = regsets_store_inferior_registers (regs_info->regsets_info,
  4382. regcache);
  4383. if ((!use_regsets || all) && regs_info->usrregs != NULL)
  4384. usr_store_inferior_registers (regs_info, regcache, regno, 1);
  4385. }
  4386. }
  4387. bool
  4388. linux_process_target::low_fetch_register (regcache *regcache, int regno)
  4389. {
  4390. return false;
  4391. }
  4392. /* A wrapper for the read_memory target op. */
  4393. static int
  4394. linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
  4395. {
  4396. return the_target->read_memory (memaddr, myaddr, len);
  4397. }
  4398. /* Copy LEN bytes from inferior's memory starting at MEMADDR
  4399. to debugger memory starting at MYADDR. */
  4400. int
  4401. linux_process_target::read_memory (CORE_ADDR memaddr,
  4402. unsigned char *myaddr, int len)
  4403. {
  4404. int pid = lwpid_of (current_thread);
  4405. PTRACE_XFER_TYPE *buffer;
  4406. CORE_ADDR addr;
  4407. int count;
  4408. char filename[64];
  4409. int i;
  4410. int ret;
  4411. int fd;
  4412. /* Try using /proc. Don't bother for one word. */
  4413. if (len >= 3 * sizeof (long))
  4414. {
  4415. int bytes;
  4416. /* We could keep this file open and cache it - possibly one per
  4417. thread. That requires some juggling, but is even faster. */
  4418. sprintf (filename, "/proc/%d/mem", pid);
  4419. fd = open (filename, O_RDONLY | O_LARGEFILE);
  4420. if (fd == -1)
  4421. goto no_proc;
  4422. /* If pread64 is available, use it. It's faster if the kernel
  4423. supports it (only one syscall), and it's 64-bit safe even on
  4424. 32-bit platforms (for instance, SPARC debugging a SPARC64
  4425. application). */
  4426. #ifdef HAVE_PREAD64
  4427. bytes = pread64 (fd, myaddr, len, memaddr);
  4428. #else
  4429. bytes = -1;
  4430. if (lseek (fd, memaddr, SEEK_SET) != -1)
  4431. bytes = read (fd, myaddr, len);
  4432. #endif
  4433. close (fd);
  4434. if (bytes == len)
  4435. return 0;
  4436. /* Some data was read, we'll try to get the rest with ptrace. */
  4437. if (bytes > 0)
  4438. {
  4439. memaddr += bytes;
  4440. myaddr += bytes;
  4441. len -= bytes;
  4442. }
  4443. }
  4444. no_proc:
  4445. /* Round starting address down to longword boundary. */
  4446. addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
  4447. /* Round ending address up; get number of longwords that makes. */
  4448. count = ((((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
  4449. / sizeof (PTRACE_XFER_TYPE));
  4450. /* Allocate buffer of that many longwords. */
  4451. buffer = XALLOCAVEC (PTRACE_XFER_TYPE, count);
  4452. /* Read all the longwords */
  4453. errno = 0;
  4454. for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
  4455. {
  4456. /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
  4457. about coercing an 8 byte integer to a 4 byte pointer. */
  4458. buffer[i] = ptrace (PTRACE_PEEKTEXT, pid,
  4459. (PTRACE_TYPE_ARG3) (uintptr_t) addr,
  4460. (PTRACE_TYPE_ARG4) 0);
  4461. if (errno)
  4462. break;
  4463. }
  4464. ret = errno;
  4465. /* Copy appropriate bytes out of the buffer. */
  4466. if (i > 0)
  4467. {
  4468. i *= sizeof (PTRACE_XFER_TYPE);
  4469. i -= memaddr & (sizeof (PTRACE_XFER_TYPE) - 1);
  4470. memcpy (myaddr,
  4471. (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
  4472. i < len ? i : len);
  4473. }
  4474. return ret;
  4475. }
  4476. /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
  4477. memory at MEMADDR. On failure (cannot write to the inferior)
  4478. returns the value of errno. Always succeeds if LEN is zero. */
  4479. int
  4480. linux_process_target::write_memory (CORE_ADDR memaddr,
  4481. const unsigned char *myaddr, int len)
  4482. {
  4483. int i;
  4484. /* Round starting address down to longword boundary. */
  4485. CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
  4486. /* Round ending address up; get number of longwords that makes. */
  4487. int count
  4488. = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
  4489. / sizeof (PTRACE_XFER_TYPE);
  4490. /* Allocate buffer of that many longwords. */
  4491. PTRACE_XFER_TYPE *buffer = XALLOCAVEC (PTRACE_XFER_TYPE, count);
  4492. int pid = lwpid_of (current_thread);
  4493. if (len == 0)
  4494. {
  4495. /* Zero length write always succeeds. */
  4496. return 0;
  4497. }
  4498. if (debug_threads)
  4499. {
  4500. /* Dump up to four bytes. */
  4501. char str[4 * 2 + 1];
  4502. char *p = str;
  4503. int dump = len < 4 ? len : 4;
  4504. for (i = 0; i < dump; i++)
  4505. {
  4506. sprintf (p, "%02x", myaddr[i]);
  4507. p += 2;
  4508. }
  4509. *p = '\0';
  4510. threads_debug_printf ("Writing %s to 0x%08lx in process %d",
  4511. str, (long) memaddr, pid);
  4512. }
  4513. /* Fill start and end extra bytes of buffer with existing memory data. */
  4514. errno = 0;
  4515. /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
  4516. about coercing an 8 byte integer to a 4 byte pointer. */
  4517. buffer[0] = ptrace (PTRACE_PEEKTEXT, pid,
  4518. (PTRACE_TYPE_ARG3) (uintptr_t) addr,
  4519. (PTRACE_TYPE_ARG4) 0);
  4520. if (errno)
  4521. return errno;
  4522. if (count > 1)
  4523. {
  4524. errno = 0;
  4525. buffer[count - 1]
  4526. = ptrace (PTRACE_PEEKTEXT, pid,
  4527. /* Coerce to a uintptr_t first to avoid potential gcc warning
  4528. about coercing an 8 byte integer to a 4 byte pointer. */
  4529. (PTRACE_TYPE_ARG3) (uintptr_t) (addr + (count - 1)
  4530. * sizeof (PTRACE_XFER_TYPE)),
  4531. (PTRACE_TYPE_ARG4) 0);
  4532. if (errno)
  4533. return errno;
  4534. }
  4535. /* Copy data to be written over corresponding part of buffer. */
  4536. memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
  4537. myaddr, len);
  4538. /* Write the entire buffer. */
  4539. for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
  4540. {
  4541. errno = 0;
  4542. ptrace (PTRACE_POKETEXT, pid,
  4543. /* Coerce to a uintptr_t first to avoid potential gcc warning
  4544. about coercing an 8 byte integer to a 4 byte pointer. */
  4545. (PTRACE_TYPE_ARG3) (uintptr_t) addr,
  4546. (PTRACE_TYPE_ARG4) buffer[i]);
  4547. if (errno)
  4548. return errno;
  4549. }
  4550. return 0;
  4551. }
  4552. void
  4553. linux_process_target::look_up_symbols ()
  4554. {
  4555. #ifdef USE_THREAD_DB
  4556. struct process_info *proc = current_process ();
  4557. if (proc->priv->thread_db != NULL)
  4558. return;
  4559. thread_db_init ();
  4560. #endif
  4561. }
  4562. void
  4563. linux_process_target::request_interrupt ()
  4564. {
  4565. /* Send a SIGINT to the process group. This acts just like the user
  4566. typed a ^C on the controlling terminal. */
  4567. ::kill (-signal_pid, SIGINT);
  4568. }
  4569. bool
  4570. linux_process_target::supports_read_auxv ()
  4571. {
  4572. return true;
  4573. }
  4574. /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
  4575. to debugger memory starting at MYADDR. */
  4576. int
  4577. linux_process_target::read_auxv (CORE_ADDR offset, unsigned char *myaddr,
  4578. unsigned int len)
  4579. {
  4580. char filename[PATH_MAX];
  4581. int fd, n;
  4582. int pid = lwpid_of (current_thread);
  4583. xsnprintf (filename, sizeof filename, "/proc/%d/auxv", pid);
  4584. fd = open (filename, O_RDONLY);
  4585. if (fd < 0)
  4586. return -1;
  4587. if (offset != (CORE_ADDR) 0
  4588. && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
  4589. n = -1;
  4590. else
  4591. n = read (fd, myaddr, len);
  4592. close (fd);
  4593. return n;
  4594. }
  4595. int
  4596. linux_process_target::insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
  4597. int size, raw_breakpoint *bp)
  4598. {
  4599. if (type == raw_bkpt_type_sw)
  4600. return insert_memory_breakpoint (bp);
  4601. else
  4602. return low_insert_point (type, addr, size, bp);
  4603. }
  4604. int
  4605. linux_process_target::low_insert_point (raw_bkpt_type type, CORE_ADDR addr,
  4606. int size, raw_breakpoint *bp)
  4607. {
  4608. /* Unsupported (see target.h). */
  4609. return 1;
  4610. }
  4611. int
  4612. linux_process_target::remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
  4613. int size, raw_breakpoint *bp)
  4614. {
  4615. if (type == raw_bkpt_type_sw)
  4616. return remove_memory_breakpoint (bp);
  4617. else
  4618. return low_remove_point (type, addr, size, bp);
  4619. }
  4620. int
  4621. linux_process_target::low_remove_point (raw_bkpt_type type, CORE_ADDR addr,
  4622. int size, raw_breakpoint *bp)
  4623. {
  4624. /* Unsupported (see target.h). */
  4625. return 1;
  4626. }
  4627. /* Implement the stopped_by_sw_breakpoint target_ops
  4628. method. */
  4629. bool
  4630. linux_process_target::stopped_by_sw_breakpoint ()
  4631. {
  4632. struct lwp_info *lwp = get_thread_lwp (current_thread);
  4633. return (lwp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT);
  4634. }
  4635. /* Implement the supports_stopped_by_sw_breakpoint target_ops
  4636. method. */
  4637. bool
  4638. linux_process_target::supports_stopped_by_sw_breakpoint ()
  4639. {
  4640. return USE_SIGTRAP_SIGINFO;
  4641. }
  4642. /* Implement the stopped_by_hw_breakpoint target_ops
  4643. method. */
  4644. bool
  4645. linux_process_target::stopped_by_hw_breakpoint ()
  4646. {
  4647. struct lwp_info *lwp = get_thread_lwp (current_thread);
  4648. return (lwp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT);
  4649. }
  4650. /* Implement the supports_stopped_by_hw_breakpoint target_ops
  4651. method. */
  4652. bool
  4653. linux_process_target::supports_stopped_by_hw_breakpoint ()
  4654. {
  4655. return USE_SIGTRAP_SIGINFO;
  4656. }
  4657. /* Implement the supports_hardware_single_step target_ops method. */
  4658. bool
  4659. linux_process_target::supports_hardware_single_step ()
  4660. {
  4661. return true;
  4662. }
  4663. bool
  4664. linux_process_target::stopped_by_watchpoint ()
  4665. {
  4666. struct lwp_info *lwp = get_thread_lwp (current_thread);
  4667. return lwp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
  4668. }
  4669. CORE_ADDR
  4670. linux_process_target::stopped_data_address ()
  4671. {
  4672. struct lwp_info *lwp = get_thread_lwp (current_thread);
  4673. return lwp->stopped_data_address;
  4674. }
  4675. /* This is only used for targets that define PT_TEXT_ADDR,
  4676. PT_DATA_ADDR and PT_TEXT_END_ADDR. If those are not defined, supposedly
  4677. the target has different ways of acquiring this information, like
  4678. loadmaps. */
  4679. bool
  4680. linux_process_target::supports_read_offsets ()
  4681. {
  4682. #ifdef SUPPORTS_READ_OFFSETS
  4683. return true;
  4684. #else
  4685. return false;
  4686. #endif
  4687. }
  4688. /* Under uClinux, programs are loaded at non-zero offsets, which we need
  4689. to tell gdb about. */
  4690. int
  4691. linux_process_target::read_offsets (CORE_ADDR *text_p, CORE_ADDR *data_p)
  4692. {
  4693. #ifdef SUPPORTS_READ_OFFSETS
  4694. unsigned long text, text_end, data;
  4695. int pid = lwpid_of (current_thread);
  4696. errno = 0;
  4697. text = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_TEXT_ADDR,
  4698. (PTRACE_TYPE_ARG4) 0);
  4699. text_end = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_TEXT_END_ADDR,
  4700. (PTRACE_TYPE_ARG4) 0);
  4701. data = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_DATA_ADDR,
  4702. (PTRACE_TYPE_ARG4) 0);
  4703. if (errno == 0)
  4704. {
  4705. /* Both text and data offsets produced at compile-time (and so
  4706. used by gdb) are relative to the beginning of the program,
  4707. with the data segment immediately following the text segment.
  4708. However, the actual runtime layout in memory may put the data
  4709. somewhere else, so when we send gdb a data base-address, we
  4710. use the real data base address and subtract the compile-time
  4711. data base-address from it (which is just the length of the
  4712. text segment). BSS immediately follows data in both
  4713. cases. */
  4714. *text_p = text;
  4715. *data_p = data - (text_end - text);
  4716. return 1;
  4717. }
  4718. return 0;
  4719. #else
  4720. gdb_assert_not_reached ("target op read_offsets not supported");
  4721. #endif
  4722. }
  4723. bool
  4724. linux_process_target::supports_get_tls_address ()
  4725. {
  4726. #ifdef USE_THREAD_DB
  4727. return true;
  4728. #else
  4729. return false;
  4730. #endif
  4731. }
  4732. int
  4733. linux_process_target::get_tls_address (thread_info *thread,
  4734. CORE_ADDR offset,
  4735. CORE_ADDR load_module,
  4736. CORE_ADDR *address)
  4737. {
  4738. #ifdef USE_THREAD_DB
  4739. return thread_db_get_tls_address (thread, offset, load_module, address);
  4740. #else
  4741. return -1;
  4742. #endif
  4743. }
  4744. bool
  4745. linux_process_target::supports_qxfer_osdata ()
  4746. {
  4747. return true;
  4748. }
  4749. int
  4750. linux_process_target::qxfer_osdata (const char *annex,
  4751. unsigned char *readbuf,
  4752. unsigned const char *writebuf,
  4753. CORE_ADDR offset, int len)
  4754. {
  4755. return linux_common_xfer_osdata (annex, readbuf, offset, len);
  4756. }
  4757. void
  4758. linux_process_target::siginfo_fixup (siginfo_t *siginfo,
  4759. gdb_byte *inf_siginfo, int direction)
  4760. {
  4761. bool done = low_siginfo_fixup (siginfo, inf_siginfo, direction);
  4762. /* If there was no callback, or the callback didn't do anything,
  4763. then just do a straight memcpy. */
  4764. if (!done)
  4765. {
  4766. if (direction == 1)
  4767. memcpy (siginfo, inf_siginfo, sizeof (siginfo_t));
  4768. else
  4769. memcpy (inf_siginfo, siginfo, sizeof (siginfo_t));
  4770. }
  4771. }
  4772. bool
  4773. linux_process_target::low_siginfo_fixup (siginfo_t *native, gdb_byte *inf,
  4774. int direction)
  4775. {
  4776. return false;
  4777. }
  4778. bool
  4779. linux_process_target::supports_qxfer_siginfo ()
  4780. {
  4781. return true;
  4782. }
  4783. int
  4784. linux_process_target::qxfer_siginfo (const char *annex,
  4785. unsigned char *readbuf,
  4786. unsigned const char *writebuf,
  4787. CORE_ADDR offset, int len)
  4788. {
  4789. int pid;
  4790. siginfo_t siginfo;
  4791. gdb_byte inf_siginfo[sizeof (siginfo_t)];
  4792. if (current_thread == NULL)
  4793. return -1;
  4794. pid = lwpid_of (current_thread);
  4795. threads_debug_printf ("%s siginfo for lwp %d.",
  4796. readbuf != NULL ? "Reading" : "Writing",
  4797. pid);
  4798. if (offset >= sizeof (siginfo))
  4799. return -1;
  4800. if (ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo) != 0)
  4801. return -1;
  4802. /* When GDBSERVER is built as a 64-bit application, ptrace writes into
  4803. SIGINFO an object with 64-bit layout. Since debugging a 32-bit
  4804. inferior with a 64-bit GDBSERVER should look the same as debugging it
  4805. with a 32-bit GDBSERVER, we need to convert it. */
  4806. siginfo_fixup (&siginfo, inf_siginfo, 0);
  4807. if (offset + len > sizeof (siginfo))
  4808. len = sizeof (siginfo) - offset;
  4809. if (readbuf != NULL)
  4810. memcpy (readbuf, inf_siginfo + offset, len);
  4811. else
  4812. {
  4813. memcpy (inf_siginfo + offset, writebuf, len);
  4814. /* Convert back to ptrace layout before flushing it out. */
  4815. siginfo_fixup (&siginfo, inf_siginfo, 1);
  4816. if (ptrace (PTRACE_SETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo) != 0)
  4817. return -1;
  4818. }
  4819. return len;
  4820. }
  4821. /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
  4822. so we notice when children change state; as the handler for the
  4823. sigsuspend in my_waitpid. */
  4824. static void
  4825. sigchld_handler (int signo)
  4826. {
  4827. int old_errno = errno;
  4828. if (debug_threads)
  4829. {
  4830. do
  4831. {
  4832. /* Use the async signal safe debug function. */
  4833. if (debug_write ("sigchld_handler\n",
  4834. sizeof ("sigchld_handler\n") - 1) < 0)
  4835. break; /* just ignore */
  4836. } while (0);
  4837. }
  4838. if (target_is_async_p ())
  4839. async_file_mark (); /* trigger a linux_wait */
  4840. errno = old_errno;
  4841. }
  4842. bool
  4843. linux_process_target::supports_non_stop ()
  4844. {
  4845. return true;
  4846. }
  4847. bool
  4848. linux_process_target::async (bool enable)
  4849. {
  4850. bool previous = target_is_async_p ();
  4851. threads_debug_printf ("async (%d), previous=%d",
  4852. enable, previous);
  4853. if (previous != enable)
  4854. {
  4855. sigset_t mask;
  4856. sigemptyset (&mask);
  4857. sigaddset (&mask, SIGCHLD);
  4858. gdb_sigmask (SIG_BLOCK, &mask, NULL);
  4859. if (enable)
  4860. {
  4861. if (!linux_event_pipe.open_pipe ())
  4862. {
  4863. gdb_sigmask (SIG_UNBLOCK, &mask, NULL);
  4864. warning ("creating event pipe failed.");
  4865. return previous;
  4866. }
  4867. /* Register the event loop handler. */
  4868. add_file_handler (linux_event_pipe.event_fd (),
  4869. handle_target_event, NULL,
  4870. "linux-low");
  4871. /* Always trigger a linux_wait. */
  4872. async_file_mark ();
  4873. }
  4874. else
  4875. {
  4876. delete_file_handler (linux_event_pipe.event_fd ());
  4877. linux_event_pipe.close_pipe ();
  4878. }
  4879. gdb_sigmask (SIG_UNBLOCK, &mask, NULL);
  4880. }
  4881. return previous;
  4882. }
  4883. int
  4884. linux_process_target::start_non_stop (bool nonstop)
  4885. {
  4886. /* Register or unregister from event-loop accordingly. */
  4887. target_async (nonstop);
  4888. if (target_is_async_p () != (nonstop != false))
  4889. return -1;
  4890. return 0;
  4891. }
  4892. bool
  4893. linux_process_target::supports_multi_process ()
  4894. {
  4895. return true;
  4896. }
  4897. /* Check if fork events are supported. */
  4898. bool
  4899. linux_process_target::supports_fork_events ()
  4900. {
  4901. return true;
  4902. }
  4903. /* Check if vfork events are supported. */
  4904. bool
  4905. linux_process_target::supports_vfork_events ()
  4906. {
  4907. return true;
  4908. }
  4909. /* Check if exec events are supported. */
  4910. bool
  4911. linux_process_target::supports_exec_events ()
  4912. {
  4913. return true;
  4914. }
  4915. /* Target hook for 'handle_new_gdb_connection'. Causes a reset of the
  4916. ptrace flags for all inferiors. This is in case the new GDB connection
  4917. doesn't support the same set of events that the previous one did. */
  4918. void
  4919. linux_process_target::handle_new_gdb_connection ()
  4920. {
  4921. /* Request that all the lwps reset their ptrace options. */
  4922. for_each_thread ([] (thread_info *thread)
  4923. {
  4924. struct lwp_info *lwp = get_thread_lwp (thread);
  4925. if (!lwp->stopped)
  4926. {
  4927. /* Stop the lwp so we can modify its ptrace options. */
  4928. lwp->must_set_ptrace_flags = 1;
  4929. linux_stop_lwp (lwp);
  4930. }
  4931. else
  4932. {
  4933. /* Already stopped; go ahead and set the ptrace options. */
  4934. struct process_info *proc = find_process_pid (pid_of (thread));
  4935. int options = linux_low_ptrace_options (proc->attached);
  4936. linux_enable_event_reporting (lwpid_of (thread), options);
  4937. lwp->must_set_ptrace_flags = 0;
  4938. }
  4939. });
  4940. }
  4941. int
  4942. linux_process_target::handle_monitor_command (char *mon)
  4943. {
  4944. #ifdef USE_THREAD_DB
  4945. return thread_db_handle_monitor_command (mon);
  4946. #else
  4947. return 0;
  4948. #endif
  4949. }
  4950. int
  4951. linux_process_target::core_of_thread (ptid_t ptid)
  4952. {
  4953. return linux_common_core_of_thread (ptid);
  4954. }
  4955. bool
  4956. linux_process_target::supports_disable_randomization ()
  4957. {
  4958. return true;
  4959. }
  4960. bool
  4961. linux_process_target::supports_agent ()
  4962. {
  4963. return true;
  4964. }
  4965. bool
  4966. linux_process_target::supports_range_stepping ()
  4967. {
  4968. if (supports_software_single_step ())
  4969. return true;
  4970. return low_supports_range_stepping ();
  4971. }
  4972. bool
  4973. linux_process_target::low_supports_range_stepping ()
  4974. {
  4975. return false;
  4976. }
  4977. bool
  4978. linux_process_target::supports_pid_to_exec_file ()
  4979. {
  4980. return true;
  4981. }
  4982. const char *
  4983. linux_process_target::pid_to_exec_file (int pid)
  4984. {
  4985. return linux_proc_pid_to_exec_file (pid);
  4986. }
  4987. bool
  4988. linux_process_target::supports_multifs ()
  4989. {
  4990. return true;
  4991. }
  4992. int
  4993. linux_process_target::multifs_open (int pid, const char *filename,
  4994. int flags, mode_t mode)
  4995. {
  4996. return linux_mntns_open_cloexec (pid, filename, flags, mode);
  4997. }
  4998. int
  4999. linux_process_target::multifs_unlink (int pid, const char *filename)
  5000. {
  5001. return linux_mntns_unlink (pid, filename);
  5002. }
  5003. ssize_t
  5004. linux_process_target::multifs_readlink (int pid, const char *filename,
  5005. char *buf, size_t bufsiz)
  5006. {
  5007. return linux_mntns_readlink (pid, filename, buf, bufsiz);
  5008. }
  5009. #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
  5010. struct target_loadseg
  5011. {
  5012. /* Core address to which the segment is mapped. */
  5013. Elf32_Addr addr;
  5014. /* VMA recorded in the program header. */
  5015. Elf32_Addr p_vaddr;
  5016. /* Size of this segment in memory. */
  5017. Elf32_Word p_memsz;
  5018. };
  5019. # if defined PT_GETDSBT
  5020. struct target_loadmap
  5021. {
  5022. /* Protocol version number, must be zero. */
  5023. Elf32_Word version;
  5024. /* Pointer to the DSBT table, its size, and the DSBT index. */
  5025. unsigned *dsbt_table;
  5026. unsigned dsbt_size, dsbt_index;
  5027. /* Number of segments in this map. */
  5028. Elf32_Word nsegs;
  5029. /* The actual memory map. */
  5030. struct target_loadseg segs[/*nsegs*/];
  5031. };
  5032. # define LINUX_LOADMAP PT_GETDSBT
  5033. # define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
  5034. # define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
  5035. # else
  5036. struct target_loadmap
  5037. {
  5038. /* Protocol version number, must be zero. */
  5039. Elf32_Half version;
  5040. /* Number of segments in this map. */
  5041. Elf32_Half nsegs;
  5042. /* The actual memory map. */
  5043. struct target_loadseg segs[/*nsegs*/];
  5044. };
  5045. # define LINUX_LOADMAP PTRACE_GETFDPIC
  5046. # define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
  5047. # define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
  5048. # endif
  5049. bool
  5050. linux_process_target::supports_read_loadmap ()
  5051. {
  5052. return true;
  5053. }
  5054. int
  5055. linux_process_target::read_loadmap (const char *annex, CORE_ADDR offset,
  5056. unsigned char *myaddr, unsigned int len)
  5057. {
  5058. int pid = lwpid_of (current_thread);
  5059. int addr = -1;
  5060. struct target_loadmap *data = NULL;
  5061. unsigned int actual_length, copy_length;
  5062. if (strcmp (annex, "exec") == 0)
  5063. addr = (int) LINUX_LOADMAP_EXEC;
  5064. else if (strcmp (annex, "interp") == 0)
  5065. addr = (int) LINUX_LOADMAP_INTERP;
  5066. else
  5067. return -1;
  5068. if (ptrace (LINUX_LOADMAP, pid, addr, &data) != 0)
  5069. return -1;
  5070. if (data == NULL)
  5071. return -1;
  5072. actual_length = sizeof (struct target_loadmap)
  5073. + sizeof (struct target_loadseg) * data->nsegs;
  5074. if (offset < 0 || offset > actual_length)
  5075. return -1;
  5076. copy_length = actual_length - offset < len ? actual_length - offset : len;
  5077. memcpy (myaddr, (char *) data + offset, copy_length);
  5078. return copy_length;
  5079. }
  5080. #endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
  5081. bool
  5082. linux_process_target::supports_catch_syscall ()
  5083. {
  5084. return low_supports_catch_syscall ();
  5085. }
  5086. bool
  5087. linux_process_target::low_supports_catch_syscall ()
  5088. {
  5089. return false;
  5090. }
  5091. CORE_ADDR
  5092. linux_process_target::read_pc (regcache *regcache)
  5093. {
  5094. if (!low_supports_breakpoints ())
  5095. return 0;
  5096. return low_get_pc (regcache);
  5097. }
  5098. void
  5099. linux_process_target::write_pc (regcache *regcache, CORE_ADDR pc)
  5100. {
  5101. gdb_assert (low_supports_breakpoints ());
  5102. low_set_pc (regcache, pc);
  5103. }
  5104. bool
  5105. linux_process_target::supports_thread_stopped ()
  5106. {
  5107. return true;
  5108. }
  5109. bool
  5110. linux_process_target::thread_stopped (thread_info *thread)
  5111. {
  5112. return get_thread_lwp (thread)->stopped;
  5113. }
  5114. /* This exposes stop-all-threads functionality to other modules. */
  5115. void
  5116. linux_process_target::pause_all (bool freeze)
  5117. {
  5118. stop_all_lwps (freeze, NULL);
  5119. }
  5120. /* This exposes unstop-all-threads functionality to other gdbserver
  5121. modules. */
  5122. void
  5123. linux_process_target::unpause_all (bool unfreeze)
  5124. {
  5125. unstop_all_lwps (unfreeze, NULL);
  5126. }
  5127. int
  5128. linux_process_target::prepare_to_access_memory ()
  5129. {
  5130. /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
  5131. running LWP. */
  5132. if (non_stop)
  5133. target_pause_all (true);
  5134. return 0;
  5135. }
  5136. void
  5137. linux_process_target::done_accessing_memory ()
  5138. {
  5139. /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
  5140. running LWP. */
  5141. if (non_stop)
  5142. target_unpause_all (true);
  5143. }
  5144. /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
  5145. static int
  5146. get_phdr_phnum_from_proc_auxv (const int pid, const int is_elf64,
  5147. CORE_ADDR *phdr_memaddr, int *num_phdr)
  5148. {
  5149. char filename[PATH_MAX];
  5150. int fd;
  5151. const int auxv_size = is_elf64
  5152. ? sizeof (Elf64_auxv_t) : sizeof (Elf32_auxv_t);
  5153. char buf[sizeof (Elf64_auxv_t)]; /* The larger of the two. */
  5154. xsnprintf (filename, sizeof filename, "/proc/%d/auxv", pid);
  5155. fd = open (filename, O_RDONLY);
  5156. if (fd < 0)
  5157. return 1;
  5158. *phdr_memaddr = 0;
  5159. *num_phdr = 0;
  5160. while (read (fd, buf, auxv_size) == auxv_size
  5161. && (*phdr_memaddr == 0 || *num_phdr == 0))
  5162. {
  5163. if (is_elf64)
  5164. {
  5165. Elf64_auxv_t *const aux = (Elf64_auxv_t *) buf;
  5166. switch (aux->a_type)
  5167. {
  5168. case AT_PHDR:
  5169. *phdr_memaddr = aux->a_un.a_val;
  5170. break;
  5171. case AT_PHNUM:
  5172. *num_phdr = aux->a_un.a_val;
  5173. break;
  5174. }
  5175. }
  5176. else
  5177. {
  5178. Elf32_auxv_t *const aux = (Elf32_auxv_t *) buf;
  5179. switch (aux->a_type)
  5180. {
  5181. case AT_PHDR:
  5182. *phdr_memaddr = aux->a_un.a_val;
  5183. break;
  5184. case AT_PHNUM:
  5185. *num_phdr = aux->a_un.a_val;
  5186. break;
  5187. }
  5188. }
  5189. }
  5190. close (fd);
  5191. if (*phdr_memaddr == 0 || *num_phdr == 0)
  5192. {
  5193. warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
  5194. "phdr_memaddr = %ld, phdr_num = %d",
  5195. (long) *phdr_memaddr, *num_phdr);
  5196. return 2;
  5197. }
  5198. return 0;
  5199. }
  5200. /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
  5201. static CORE_ADDR
  5202. get_dynamic (const int pid, const int is_elf64)
  5203. {
  5204. CORE_ADDR phdr_memaddr, relocation;
  5205. int num_phdr, i;
  5206. unsigned char *phdr_buf;
  5207. const int phdr_size = is_elf64 ? sizeof (Elf64_Phdr) : sizeof (Elf32_Phdr);
  5208. if (get_phdr_phnum_from_proc_auxv (pid, is_elf64, &phdr_memaddr, &num_phdr))
  5209. return 0;
  5210. gdb_assert (num_phdr < 100); /* Basic sanity check. */
  5211. phdr_buf = (unsigned char *) alloca (num_phdr * phdr_size);
  5212. if (linux_read_memory (phdr_memaddr, phdr_buf, num_phdr * phdr_size))
  5213. return 0;
  5214. /* Compute relocation: it is expected to be 0 for "regular" executables,
  5215. non-zero for PIE ones. */
  5216. relocation = -1;
  5217. for (i = 0; relocation == -1 && i < num_phdr; i++)
  5218. if (is_elf64)
  5219. {
  5220. Elf64_Phdr *const p = (Elf64_Phdr *) (phdr_buf + i * phdr_size);
  5221. if (p->p_type == PT_PHDR)
  5222. relocation = phdr_memaddr - p->p_vaddr;
  5223. }
  5224. else
  5225. {
  5226. Elf32_Phdr *const p = (Elf32_Phdr *) (phdr_buf + i * phdr_size);
  5227. if (p->p_type == PT_PHDR)
  5228. relocation = phdr_memaddr - p->p_vaddr;
  5229. }
  5230. if (relocation == -1)
  5231. {
  5232. /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
  5233. any real world executables, including PIE executables, have always
  5234. PT_PHDR present. PT_PHDR is not present in some shared libraries or
  5235. in fpc (Free Pascal 2.4) binaries but neither of those have a need for
  5236. or present DT_DEBUG anyway (fpc binaries are statically linked).
  5237. Therefore if there exists DT_DEBUG there is always also PT_PHDR.
  5238. GDB could find RELOCATION also from AT_ENTRY - e_entry. */
  5239. return 0;
  5240. }
  5241. for (i = 0; i < num_phdr; i++)
  5242. {
  5243. if (is_elf64)
  5244. {
  5245. Elf64_Phdr *const p = (Elf64_Phdr *) (phdr_buf + i * phdr_size);
  5246. if (p->p_type == PT_DYNAMIC)
  5247. return p->p_vaddr + relocation;
  5248. }
  5249. else
  5250. {
  5251. Elf32_Phdr *const p = (Elf32_Phdr *) (phdr_buf + i * phdr_size);
  5252. if (p->p_type == PT_DYNAMIC)
  5253. return p->p_vaddr + relocation;
  5254. }
  5255. }
  5256. return 0;
  5257. }
  5258. /* Return &_r_debug in the inferior, or -1 if not present. Return value
  5259. can be 0 if the inferior does not yet have the library list initialized.
  5260. We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
  5261. DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
  5262. static CORE_ADDR
  5263. get_r_debug (const int pid, const int is_elf64)
  5264. {
  5265. CORE_ADDR dynamic_memaddr;
  5266. const int dyn_size = is_elf64 ? sizeof (Elf64_Dyn) : sizeof (Elf32_Dyn);
  5267. unsigned char buf[sizeof (Elf64_Dyn)]; /* The larger of the two. */
  5268. CORE_ADDR map = -1;
  5269. dynamic_memaddr = get_dynamic (pid, is_elf64);
  5270. if (dynamic_memaddr == 0)
  5271. return map;
  5272. while (linux_read_memory (dynamic_memaddr, buf, dyn_size) == 0)
  5273. {
  5274. if (is_elf64)
  5275. {
  5276. Elf64_Dyn *const dyn = (Elf64_Dyn *) buf;
  5277. #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
  5278. union
  5279. {
  5280. Elf64_Xword map;
  5281. unsigned char buf[sizeof (Elf64_Xword)];
  5282. }
  5283. rld_map;
  5284. #endif
  5285. #ifdef DT_MIPS_RLD_MAP
  5286. if (dyn->d_tag == DT_MIPS_RLD_MAP)
  5287. {
  5288. if (linux_read_memory (dyn->d_un.d_val,
  5289. rld_map.buf, sizeof (rld_map.buf)) == 0)
  5290. return rld_map.map;
  5291. else
  5292. break;
  5293. }
  5294. #endif /* DT_MIPS_RLD_MAP */
  5295. #ifdef DT_MIPS_RLD_MAP_REL
  5296. if (dyn->d_tag == DT_MIPS_RLD_MAP_REL)
  5297. {
  5298. if (linux_read_memory (dyn->d_un.d_val + dynamic_memaddr,
  5299. rld_map.buf, sizeof (rld_map.buf)) == 0)
  5300. return rld_map.map;
  5301. else
  5302. break;
  5303. }
  5304. #endif /* DT_MIPS_RLD_MAP_REL */
  5305. if (dyn->d_tag == DT_DEBUG && map == -1)
  5306. map = dyn->d_un.d_val;
  5307. if (dyn->d_tag == DT_NULL)
  5308. break;
  5309. }
  5310. else
  5311. {
  5312. Elf32_Dyn *const dyn = (Elf32_Dyn *) buf;
  5313. #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
  5314. union
  5315. {
  5316. Elf32_Word map;
  5317. unsigned char buf[sizeof (Elf32_Word)];
  5318. }
  5319. rld_map;
  5320. #endif
  5321. #ifdef DT_MIPS_RLD_MAP
  5322. if (dyn->d_tag == DT_MIPS_RLD_MAP)
  5323. {
  5324. if (linux_read_memory (dyn->d_un.d_val,
  5325. rld_map.buf, sizeof (rld_map.buf)) == 0)
  5326. return rld_map.map;
  5327. else
  5328. break;
  5329. }
  5330. #endif /* DT_MIPS_RLD_MAP */
  5331. #ifdef DT_MIPS_RLD_MAP_REL
  5332. if (dyn->d_tag == DT_MIPS_RLD_MAP_REL)
  5333. {
  5334. if (linux_read_memory (dyn->d_un.d_val + dynamic_memaddr,
  5335. rld_map.buf, sizeof (rld_map.buf)) == 0)
  5336. return rld_map.map;
  5337. else
  5338. break;
  5339. }
  5340. #endif /* DT_MIPS_RLD_MAP_REL */
  5341. if (dyn->d_tag == DT_DEBUG && map == -1)
  5342. map = dyn->d_un.d_val;
  5343. if (dyn->d_tag == DT_NULL)
  5344. break;
  5345. }
  5346. dynamic_memaddr += dyn_size;
  5347. }
  5348. return map;
  5349. }
  5350. /* Read one pointer from MEMADDR in the inferior. */
  5351. static int
  5352. read_one_ptr (CORE_ADDR memaddr, CORE_ADDR *ptr, int ptr_size)
  5353. {
  5354. int ret;
  5355. /* Go through a union so this works on either big or little endian
  5356. hosts, when the inferior's pointer size is smaller than the size
  5357. of CORE_ADDR. It is assumed the inferior's endianness is the
  5358. same of the superior's. */
  5359. union
  5360. {
  5361. CORE_ADDR core_addr;
  5362. unsigned int ui;
  5363. unsigned char uc;
  5364. } addr;
  5365. ret = linux_read_memory (memaddr, &addr.uc, ptr_size);
  5366. if (ret == 0)
  5367. {
  5368. if (ptr_size == sizeof (CORE_ADDR))
  5369. *ptr = addr.core_addr;
  5370. else if (ptr_size == sizeof (unsigned int))
  5371. *ptr = addr.ui;
  5372. else
  5373. gdb_assert_not_reached ("unhandled pointer size");
  5374. }
  5375. return ret;
  5376. }
  5377. bool
  5378. linux_process_target::supports_qxfer_libraries_svr4 ()
  5379. {
  5380. return true;
  5381. }
  5382. struct link_map_offsets
  5383. {
  5384. /* Offset and size of r_debug.r_version. */
  5385. int r_version_offset;
  5386. /* Offset and size of r_debug.r_map. */
  5387. int r_map_offset;
  5388. /* Offset to l_addr field in struct link_map. */
  5389. int l_addr_offset;
  5390. /* Offset to l_name field in struct link_map. */
  5391. int l_name_offset;
  5392. /* Offset to l_ld field in struct link_map. */
  5393. int l_ld_offset;
  5394. /* Offset to l_next field in struct link_map. */
  5395. int l_next_offset;
  5396. /* Offset to l_prev field in struct link_map. */
  5397. int l_prev_offset;
  5398. };
  5399. /* Construct qXfer:libraries-svr4:read reply. */
  5400. int
  5401. linux_process_target::qxfer_libraries_svr4 (const char *annex,
  5402. unsigned char *readbuf,
  5403. unsigned const char *writebuf,
  5404. CORE_ADDR offset, int len)
  5405. {
  5406. struct process_info_private *const priv = current_process ()->priv;
  5407. char filename[PATH_MAX];
  5408. int pid, is_elf64;
  5409. static const struct link_map_offsets lmo_32bit_offsets =
  5410. {
  5411. 0, /* r_version offset. */
  5412. 4, /* r_debug.r_map offset. */
  5413. 0, /* l_addr offset in link_map. */
  5414. 4, /* l_name offset in link_map. */
  5415. 8, /* l_ld offset in link_map. */
  5416. 12, /* l_next offset in link_map. */
  5417. 16 /* l_prev offset in link_map. */
  5418. };
  5419. static const struct link_map_offsets lmo_64bit_offsets =
  5420. {
  5421. 0, /* r_version offset. */
  5422. 8, /* r_debug.r_map offset. */
  5423. 0, /* l_addr offset in link_map. */
  5424. 8, /* l_name offset in link_map. */
  5425. 16, /* l_ld offset in link_map. */
  5426. 24, /* l_next offset in link_map. */
  5427. 32 /* l_prev offset in link_map. */
  5428. };
  5429. const struct link_map_offsets *lmo;
  5430. unsigned int machine;
  5431. int ptr_size;
  5432. CORE_ADDR lm_addr = 0, lm_prev = 0;
  5433. CORE_ADDR l_name, l_addr, l_ld, l_next, l_prev;
  5434. int header_done = 0;
  5435. if (writebuf != NULL)
  5436. return -2;
  5437. if (readbuf == NULL)
  5438. return -1;
  5439. pid = lwpid_of (current_thread);
  5440. xsnprintf (filename, sizeof filename, "/proc/%d/exe", pid);
  5441. is_elf64 = elf_64_file_p (filename, &machine);
  5442. lmo = is_elf64 ? &lmo_64bit_offsets : &lmo_32bit_offsets;
  5443. ptr_size = is_elf64 ? 8 : 4;
  5444. while (annex[0] != '\0')
  5445. {
  5446. const char *sep;
  5447. CORE_ADDR *addrp;
  5448. int name_len;
  5449. sep = strchr (annex, '=');
  5450. if (sep == NULL)
  5451. break;
  5452. name_len = sep - annex;
  5453. if (name_len == 5 && startswith (annex, "start"))
  5454. addrp = &lm_addr;
  5455. else if (name_len == 4 && startswith (annex, "prev"))
  5456. addrp = &lm_prev;
  5457. else
  5458. {
  5459. annex = strchr (sep, ';');
  5460. if (annex == NULL)
  5461. break;
  5462. annex++;
  5463. continue;
  5464. }
  5465. annex = decode_address_to_semicolon (addrp, sep + 1);
  5466. }
  5467. if (lm_addr == 0)
  5468. {
  5469. int r_version = 0;
  5470. if (priv->r_debug == 0)
  5471. priv->r_debug = get_r_debug (pid, is_elf64);
  5472. /* We failed to find DT_DEBUG. Such situation will not change
  5473. for this inferior - do not retry it. Report it to GDB as
  5474. E01, see for the reasons at the GDB solib-svr4.c side. */
  5475. if (priv->r_debug == (CORE_ADDR) -1)
  5476. return -1;
  5477. if (priv->r_debug != 0)
  5478. {
  5479. if (linux_read_memory (priv->r_debug + lmo->r_version_offset,
  5480. (unsigned char *) &r_version,
  5481. sizeof (r_version)) != 0
  5482. || r_version < 1)
  5483. {
  5484. warning ("unexpected r_debug version %d", r_version);
  5485. }
  5486. else if (read_one_ptr (priv->r_debug + lmo->r_map_offset,
  5487. &lm_addr, ptr_size) != 0)
  5488. {
  5489. warning ("unable to read r_map from 0x%lx",
  5490. (long) priv->r_debug + lmo->r_map_offset);
  5491. }
  5492. }
  5493. }
  5494. std::string document = "<library-list-svr4 version=\"1.0\"";
  5495. while (lm_addr
  5496. && read_one_ptr (lm_addr + lmo->l_name_offset,
  5497. &l_name, ptr_size) == 0
  5498. && read_one_ptr (lm_addr + lmo->l_addr_offset,
  5499. &l_addr, ptr_size) == 0
  5500. && read_one_ptr (lm_addr + lmo->l_ld_offset,
  5501. &l_ld, ptr_size) == 0
  5502. && read_one_ptr (lm_addr + lmo->l_prev_offset,
  5503. &l_prev, ptr_size) == 0
  5504. && read_one_ptr (lm_addr + lmo->l_next_offset,
  5505. &l_next, ptr_size) == 0)
  5506. {
  5507. unsigned char libname[PATH_MAX];
  5508. if (lm_prev != l_prev)
  5509. {
  5510. warning ("Corrupted shared library list: 0x%lx != 0x%lx",
  5511. (long) lm_prev, (long) l_prev);
  5512. break;
  5513. }
  5514. /* Ignore the first entry even if it has valid name as the first entry
  5515. corresponds to the main executable. The first entry should not be
  5516. skipped if the dynamic loader was loaded late by a static executable
  5517. (see solib-svr4.c parameter ignore_first). But in such case the main
  5518. executable does not have PT_DYNAMIC present and this function already
  5519. exited above due to failed get_r_debug. */
  5520. if (lm_prev == 0)
  5521. string_appendf (document, " main-lm=\"0x%lx\"", (unsigned long) lm_addr);
  5522. else
  5523. {
  5524. /* Not checking for error because reading may stop before
  5525. we've got PATH_MAX worth of characters. */
  5526. libname[0] = '\0';
  5527. linux_read_memory (l_name, libname, sizeof (libname) - 1);
  5528. libname[sizeof (libname) - 1] = '\0';
  5529. if (libname[0] != '\0')
  5530. {
  5531. if (!header_done)
  5532. {
  5533. /* Terminate `<library-list-svr4'. */
  5534. document += '>';
  5535. header_done = 1;
  5536. }
  5537. string_appendf (document, "<library name=\"");
  5538. xml_escape_text_append (&document, (char *) libname);
  5539. string_appendf (document, "\" lm=\"0x%lx\" "
  5540. "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
  5541. (unsigned long) lm_addr, (unsigned long) l_addr,
  5542. (unsigned long) l_ld);
  5543. }
  5544. }
  5545. lm_prev = lm_addr;
  5546. lm_addr = l_next;
  5547. }
  5548. if (!header_done)
  5549. {
  5550. /* Empty list; terminate `<library-list-svr4'. */
  5551. document += "/>";
  5552. }
  5553. else
  5554. document += "</library-list-svr4>";
  5555. int document_len = document.length ();
  5556. if (offset < document_len)
  5557. document_len -= offset;
  5558. else
  5559. document_len = 0;
  5560. if (len > document_len)
  5561. len = document_len;
  5562. memcpy (readbuf, document.data () + offset, len);
  5563. return len;
  5564. }
  5565. #ifdef HAVE_LINUX_BTRACE
  5566. btrace_target_info *
  5567. linux_process_target::enable_btrace (thread_info *tp,
  5568. const btrace_config *conf)
  5569. {
  5570. return linux_enable_btrace (tp->id, conf);
  5571. }
  5572. /* See to_disable_btrace target method. */
  5573. int
  5574. linux_process_target::disable_btrace (btrace_target_info *tinfo)
  5575. {
  5576. enum btrace_error err;
  5577. err = linux_disable_btrace (tinfo);
  5578. return (err == BTRACE_ERR_NONE ? 0 : -1);
  5579. }
  5580. /* Encode an Intel Processor Trace configuration. */
  5581. static void
  5582. linux_low_encode_pt_config (struct buffer *buffer,
  5583. const struct btrace_data_pt_config *config)
  5584. {
  5585. buffer_grow_str (buffer, "<pt-config>\n");
  5586. switch (config->cpu.vendor)
  5587. {
  5588. case CV_INTEL:
  5589. buffer_xml_printf (buffer, "<cpu vendor=\"GenuineIntel\" family=\"%u\" "
  5590. "model=\"%u\" stepping=\"%u\"/>\n",
  5591. config->cpu.family, config->cpu.model,
  5592. config->cpu.stepping);
  5593. break;
  5594. default:
  5595. break;
  5596. }
  5597. buffer_grow_str (buffer, "</pt-config>\n");
  5598. }
  5599. /* Encode a raw buffer. */
  5600. static void
  5601. linux_low_encode_raw (struct buffer *buffer, const gdb_byte *data,
  5602. unsigned int size)
  5603. {
  5604. if (size == 0)
  5605. return;
  5606. /* We use hex encoding - see gdbsupport/rsp-low.h. */
  5607. buffer_grow_str (buffer, "<raw>\n");
  5608. while (size-- > 0)
  5609. {
  5610. char elem[2];
  5611. elem[0] = tohex ((*data >> 4) & 0xf);
  5612. elem[1] = tohex (*data++ & 0xf);
  5613. buffer_grow (buffer, elem, 2);
  5614. }
  5615. buffer_grow_str (buffer, "</raw>\n");
  5616. }
  5617. /* See to_read_btrace target method. */
  5618. int
  5619. linux_process_target::read_btrace (btrace_target_info *tinfo,
  5620. buffer *buffer,
  5621. enum btrace_read_type type)
  5622. {
  5623. struct btrace_data btrace;
  5624. enum btrace_error err;
  5625. err = linux_read_btrace (&btrace, tinfo, type);
  5626. if (err != BTRACE_ERR_NONE)
  5627. {
  5628. if (err == BTRACE_ERR_OVERFLOW)
  5629. buffer_grow_str0 (buffer, "E.Overflow.");
  5630. else
  5631. buffer_grow_str0 (buffer, "E.Generic Error.");
  5632. return -1;
  5633. }
  5634. switch (btrace.format)
  5635. {
  5636. case BTRACE_FORMAT_NONE:
  5637. buffer_grow_str0 (buffer, "E.No Trace.");
  5638. return -1;
  5639. case BTRACE_FORMAT_BTS:
  5640. buffer_grow_str (buffer, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
  5641. buffer_grow_str (buffer, "<btrace version=\"1.0\">\n");
  5642. for (const btrace_block &block : *btrace.variant.bts.blocks)
  5643. buffer_xml_printf (buffer, "<block begin=\"0x%s\" end=\"0x%s\"/>\n",
  5644. paddress (block.begin), paddress (block.end));
  5645. buffer_grow_str0 (buffer, "</btrace>\n");
  5646. break;
  5647. case BTRACE_FORMAT_PT:
  5648. buffer_grow_str (buffer, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
  5649. buffer_grow_str (buffer, "<btrace version=\"1.0\">\n");
  5650. buffer_grow_str (buffer, "<pt>\n");
  5651. linux_low_encode_pt_config (buffer, &btrace.variant.pt.config);
  5652. linux_low_encode_raw (buffer, btrace.variant.pt.data,
  5653. btrace.variant.pt.size);
  5654. buffer_grow_str (buffer, "</pt>\n");
  5655. buffer_grow_str0 (buffer, "</btrace>\n");
  5656. break;
  5657. default:
  5658. buffer_grow_str0 (buffer, "E.Unsupported Trace Format.");
  5659. return -1;
  5660. }
  5661. return 0;
  5662. }
  5663. /* See to_btrace_conf target method. */
  5664. int
  5665. linux_process_target::read_btrace_conf (const btrace_target_info *tinfo,
  5666. buffer *buffer)
  5667. {
  5668. const struct btrace_config *conf;
  5669. buffer_grow_str (buffer, "<!DOCTYPE btrace-conf SYSTEM \"btrace-conf.dtd\">\n");
  5670. buffer_grow_str (buffer, "<btrace-conf version=\"1.0\">\n");
  5671. conf = linux_btrace_conf (tinfo);
  5672. if (conf != NULL)
  5673. {
  5674. switch (conf->format)
  5675. {
  5676. case BTRACE_FORMAT_NONE:
  5677. break;
  5678. case BTRACE_FORMAT_BTS:
  5679. buffer_xml_printf (buffer, "<bts");
  5680. buffer_xml_printf (buffer, " size=\"0x%x\"", conf->bts.size);
  5681. buffer_xml_printf (buffer, " />\n");
  5682. break;
  5683. case BTRACE_FORMAT_PT:
  5684. buffer_xml_printf (buffer, "<pt");
  5685. buffer_xml_printf (buffer, " size=\"0x%x\"", conf->pt.size);
  5686. buffer_xml_printf (buffer, "/>\n");
  5687. break;
  5688. }
  5689. }
  5690. buffer_grow_str0 (buffer, "</btrace-conf>\n");
  5691. return 0;
  5692. }
  5693. #endif /* HAVE_LINUX_BTRACE */
  5694. /* See nat/linux-nat.h. */
  5695. ptid_t
  5696. current_lwp_ptid (void)
  5697. {
  5698. return ptid_of (current_thread);
  5699. }
  5700. const char *
  5701. linux_process_target::thread_name (ptid_t thread)
  5702. {
  5703. return linux_proc_tid_get_name (thread);
  5704. }
  5705. #if USE_THREAD_DB
  5706. bool
  5707. linux_process_target::thread_handle (ptid_t ptid, gdb_byte **handle,
  5708. int *handle_len)
  5709. {
  5710. return thread_db_thread_handle (ptid, handle, handle_len);
  5711. }
  5712. #endif
  5713. thread_info *
  5714. linux_process_target::thread_pending_parent (thread_info *thread)
  5715. {
  5716. lwp_info *parent = get_thread_lwp (thread)->pending_parent ();
  5717. if (parent == nullptr)
  5718. return nullptr;
  5719. return get_lwp_thread (parent);
  5720. }
  5721. thread_info *
  5722. linux_process_target::thread_pending_child (thread_info *thread)
  5723. {
  5724. lwp_info *child = get_thread_lwp (thread)->pending_child ();
  5725. if (child == nullptr)
  5726. return nullptr;
  5727. return get_lwp_thread (child);
  5728. }
  5729. /* Default implementation of linux_target_ops method "set_pc" for
  5730. 32-bit pc register which is literally named "pc". */
  5731. void
  5732. linux_set_pc_32bit (struct regcache *regcache, CORE_ADDR pc)
  5733. {
  5734. uint32_t newpc = pc;
  5735. supply_register_by_name (regcache, "pc", &newpc);
  5736. }
  5737. /* Default implementation of linux_target_ops method "get_pc" for
  5738. 32-bit pc register which is literally named "pc". */
  5739. CORE_ADDR
  5740. linux_get_pc_32bit (struct regcache *regcache)
  5741. {
  5742. uint32_t pc;
  5743. collect_register_by_name (regcache, "pc", &pc);
  5744. threads_debug_printf ("stop pc is 0x%" PRIx32, pc);
  5745. return pc;
  5746. }
  5747. /* Default implementation of linux_target_ops method "set_pc" for
  5748. 64-bit pc register which is literally named "pc". */
  5749. void
  5750. linux_set_pc_64bit (struct regcache *regcache, CORE_ADDR pc)
  5751. {
  5752. uint64_t newpc = pc;
  5753. supply_register_by_name (regcache, "pc", &newpc);
  5754. }
  5755. /* Default implementation of linux_target_ops method "get_pc" for
  5756. 64-bit pc register which is literally named "pc". */
  5757. CORE_ADDR
  5758. linux_get_pc_64bit (struct regcache *regcache)
  5759. {
  5760. uint64_t pc;
  5761. collect_register_by_name (regcache, "pc", &pc);
  5762. threads_debug_printf ("stop pc is 0x%" PRIx64, pc);
  5763. return pc;
  5764. }
  5765. /* See linux-low.h. */
  5766. int
  5767. linux_get_auxv (int wordsize, CORE_ADDR match, CORE_ADDR *valp)
  5768. {
  5769. gdb_byte *data = (gdb_byte *) alloca (2 * wordsize);
  5770. int offset = 0;
  5771. gdb_assert (wordsize == 4 || wordsize == 8);
  5772. while (the_target->read_auxv (offset, data, 2 * wordsize) == 2 * wordsize)
  5773. {
  5774. if (wordsize == 4)
  5775. {
  5776. uint32_t *data_p = (uint32_t *) data;
  5777. if (data_p[0] == match)
  5778. {
  5779. *valp = data_p[1];
  5780. return 1;
  5781. }
  5782. }
  5783. else
  5784. {
  5785. uint64_t *data_p = (uint64_t *) data;
  5786. if (data_p[0] == match)
  5787. {
  5788. *valp = data_p[1];
  5789. return 1;
  5790. }
  5791. }
  5792. offset += 2 * wordsize;
  5793. }
  5794. return 0;
  5795. }
  5796. /* See linux-low.h. */
  5797. CORE_ADDR
  5798. linux_get_hwcap (int wordsize)
  5799. {
  5800. CORE_ADDR hwcap = 0;
  5801. linux_get_auxv (wordsize, AT_HWCAP, &hwcap);
  5802. return hwcap;
  5803. }
  5804. /* See linux-low.h. */
  5805. CORE_ADDR
  5806. linux_get_hwcap2 (int wordsize)
  5807. {
  5808. CORE_ADDR hwcap2 = 0;
  5809. linux_get_auxv (wordsize, AT_HWCAP2, &hwcap2);
  5810. return hwcap2;
  5811. }
  5812. #ifdef HAVE_LINUX_REGSETS
  5813. void
  5814. initialize_regsets_info (struct regsets_info *info)
  5815. {
  5816. for (info->num_regsets = 0;
  5817. info->regsets[info->num_regsets].size >= 0;
  5818. info->num_regsets++)
  5819. ;
  5820. }
  5821. #endif
  5822. void
  5823. initialize_low (void)
  5824. {
  5825. struct sigaction sigchld_action;
  5826. memset (&sigchld_action, 0, sizeof (sigchld_action));
  5827. set_target_ops (the_linux_target);
  5828. linux_ptrace_init_warnings ();
  5829. linux_proc_init_warnings ();
  5830. sigchld_action.sa_handler = sigchld_handler;
  5831. sigemptyset (&sigchld_action.sa_mask);
  5832. sigchld_action.sa_flags = SA_RESTART;
  5833. sigaction (SIGCHLD, &sigchld_action, NULL);
  5834. initialize_low_arch ();
  5835. linux_check_ptrace_features ();
  5836. }