linux-nat.c 126 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472
  1. /* GNU/Linux native-dependent code common to multiple platforms.
  2. Copyright (C) 2001-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 "defs.h"
  15. #include "inferior.h"
  16. #include "infrun.h"
  17. #include "target.h"
  18. #include "nat/linux-nat.h"
  19. #include "nat/linux-waitpid.h"
  20. #include "gdbsupport/gdb_wait.h"
  21. #include <unistd.h>
  22. #include <sys/syscall.h>
  23. #include "nat/gdb_ptrace.h"
  24. #include "linux-nat.h"
  25. #include "nat/linux-ptrace.h"
  26. #include "nat/linux-procfs.h"
  27. #include "nat/linux-personality.h"
  28. #include "linux-fork.h"
  29. #include "gdbthread.h"
  30. #include "gdbcmd.h"
  31. #include "regcache.h"
  32. #include "regset.h"
  33. #include "inf-child.h"
  34. #include "inf-ptrace.h"
  35. #include "auxv.h"
  36. #include <sys/procfs.h> /* for elf_gregset etc. */
  37. #include "elf-bfd.h" /* for elfcore_write_* */
  38. #include "gregset.h" /* for gregset */
  39. #include "gdbcore.h" /* for get_exec_file */
  40. #include <ctype.h> /* for isdigit */
  41. #include <sys/stat.h> /* for struct stat */
  42. #include <fcntl.h> /* for O_RDONLY */
  43. #include "inf-loop.h"
  44. #include "gdbsupport/event-loop.h"
  45. #include "event-top.h"
  46. #include <pwd.h>
  47. #include <sys/types.h>
  48. #include <dirent.h>
  49. #include "xml-support.h"
  50. #include <sys/vfs.h>
  51. #include "solib.h"
  52. #include "nat/linux-osdata.h"
  53. #include "linux-tdep.h"
  54. #include "symfile.h"
  55. #include "gdbsupport/agent.h"
  56. #include "tracepoint.h"
  57. #include "gdbsupport/buffer.h"
  58. #include "target-descriptions.h"
  59. #include "gdbsupport/filestuff.h"
  60. #include "objfiles.h"
  61. #include "nat/linux-namespaces.h"
  62. #include "gdbsupport/block-signals.h"
  63. #include "gdbsupport/fileio.h"
  64. #include "gdbsupport/scope-exit.h"
  65. #include "gdbsupport/gdb-sigmask.h"
  66. #include "gdbsupport/common-debug.h"
  67. #include <unordered_map>
  68. /* This comment documents high-level logic of this file.
  69. Waiting for events in sync mode
  70. ===============================
  71. When waiting for an event in a specific thread, we just use waitpid,
  72. passing the specific pid, and not passing WNOHANG.
  73. When waiting for an event in all threads, waitpid is not quite good:
  74. - If the thread group leader exits while other threads in the thread
  75. group still exist, waitpid(TGID, ...) hangs. That waitpid won't
  76. return an exit status until the other threads in the group are
  77. reaped.
  78. - When a non-leader thread execs, that thread just vanishes without
  79. reporting an exit (so we'd hang if we waited for it explicitly in
  80. that case). The exec event is instead reported to the TGID pid.
  81. The solution is to always use -1 and WNOHANG, together with
  82. sigsuspend.
  83. First, we use non-blocking waitpid to check for events. If nothing is
  84. found, we use sigsuspend to wait for SIGCHLD. When SIGCHLD arrives,
  85. it means something happened to a child process. As soon as we know
  86. there's an event, we get back to calling nonblocking waitpid.
  87. Note that SIGCHLD should be blocked between waitpid and sigsuspend
  88. calls, so that we don't miss a signal. If SIGCHLD arrives in between,
  89. when it's blocked, the signal becomes pending and sigsuspend
  90. immediately notices it and returns.
  91. Waiting for events in async mode (TARGET_WNOHANG)
  92. =================================================
  93. In async mode, GDB should always be ready to handle both user input
  94. and target events, so neither blocking waitpid nor sigsuspend are
  95. viable options. Instead, we should asynchronously notify the GDB main
  96. event loop whenever there's an unprocessed event from the target. We
  97. detect asynchronous target events by handling SIGCHLD signals. To
  98. notify the event loop about target events, an event pipe is used
  99. --- the pipe is registered as waitable event source in the event loop,
  100. the event loop select/poll's on the read end of this pipe (as well on
  101. other event sources, e.g., stdin), and the SIGCHLD handler marks the
  102. event pipe to raise an event. This is more portable than relying on
  103. pselect/ppoll, since on kernels that lack those syscalls, libc
  104. emulates them with select/poll+sigprocmask, and that is racy
  105. (a.k.a. plain broken).
  106. Obviously, if we fail to notify the event loop if there's a target
  107. event, it's bad. OTOH, if we notify the event loop when there's no
  108. event from the target, linux_nat_wait will detect that there's no real
  109. event to report, and return event of type TARGET_WAITKIND_IGNORE.
  110. This is mostly harmless, but it will waste time and is better avoided.
  111. The main design point is that every time GDB is outside linux-nat.c,
  112. we have a SIGCHLD handler installed that is called when something
  113. happens to the target and notifies the GDB event loop. Whenever GDB
  114. core decides to handle the event, and calls into linux-nat.c, we
  115. process things as in sync mode, except that the we never block in
  116. sigsuspend.
  117. While processing an event, we may end up momentarily blocked in
  118. waitpid calls. Those waitpid calls, while blocking, are guarantied to
  119. return quickly. E.g., in all-stop mode, before reporting to the core
  120. that an LWP hit a breakpoint, all LWPs are stopped by sending them
  121. SIGSTOP, and synchronously waiting for the SIGSTOP to be reported.
  122. Note that this is different from blocking indefinitely waiting for the
  123. next event --- here, we're already handling an event.
  124. Use of signals
  125. ==============
  126. We stop threads by sending a SIGSTOP. The use of SIGSTOP instead of another
  127. signal is not entirely significant; we just need for a signal to be delivered,
  128. so that we can intercept it. SIGSTOP's advantage is that it can not be
  129. blocked. A disadvantage is that it is not a real-time signal, so it can only
  130. be queued once; we do not keep track of other sources of SIGSTOP.
  131. Two other signals that can't be blocked are SIGCONT and SIGKILL. But we can't
  132. use them, because they have special behavior when the signal is generated -
  133. not when it is delivered. SIGCONT resumes the entire thread group and SIGKILL
  134. kills the entire thread group.
  135. A delivered SIGSTOP would stop the entire thread group, not just the thread we
  136. tkill'd. But we never let the SIGSTOP be delivered; we always intercept and
  137. cancel it (by PTRACE_CONT without passing SIGSTOP).
  138. We could use a real-time signal instead. This would solve those problems; we
  139. could use PTRACE_GETSIGINFO to locate the specific stop signals sent by GDB.
  140. But we would still have to have some support for SIGSTOP, since PTRACE_ATTACH
  141. generates it, and there are races with trying to find a signal that is not
  142. blocked.
  143. Exec events
  144. ===========
  145. The case of a thread group (process) with 3 or more threads, and a
  146. thread other than the leader execs is worth detailing:
  147. On an exec, the Linux kernel destroys all threads except the execing
  148. one in the thread group, and resets the execing thread's tid to the
  149. tgid. No exit notification is sent for the execing thread -- from the
  150. ptracer's perspective, it appears as though the execing thread just
  151. vanishes. Until we reap all other threads except the leader and the
  152. execing thread, the leader will be zombie, and the execing thread will
  153. be in `D (disc sleep)' state. As soon as all other threads are
  154. reaped, the execing thread changes its tid to the tgid, and the
  155. previous (zombie) leader vanishes, giving place to the "new"
  156. leader. */
  157. #ifndef O_LARGEFILE
  158. #define O_LARGEFILE 0
  159. #endif
  160. struct linux_nat_target *linux_target;
  161. /* Does the current host support PTRACE_GETREGSET? */
  162. enum tribool have_ptrace_getregset = TRIBOOL_UNKNOWN;
  163. /* When true, print debug messages relating to the linux native target. */
  164. static bool debug_linux_nat;
  165. /* Implement 'show debug linux-nat'. */
  166. static void
  167. show_debug_linux_nat (struct ui_file *file, int from_tty,
  168. struct cmd_list_element *c, const char *value)
  169. {
  170. gdb_printf (file, _("Debugging of GNU/Linux native targets is %s.\n"),
  171. value);
  172. }
  173. /* Print a linux-nat debug statement. */
  174. #define linux_nat_debug_printf(fmt, ...) \
  175. debug_prefixed_printf_cond (debug_linux_nat, "linux-nat", fmt, ##__VA_ARGS__)
  176. /* Print "linux-nat" enter/exit debug statements. */
  177. #define LINUX_NAT_SCOPED_DEBUG_ENTER_EXIT \
  178. scoped_debug_enter_exit (debug_linux_nat, "linux-nat")
  179. struct simple_pid_list
  180. {
  181. int pid;
  182. int status;
  183. struct simple_pid_list *next;
  184. };
  185. static struct simple_pid_list *stopped_pids;
  186. /* Whether target_thread_events is in effect. */
  187. static int report_thread_events;
  188. static int kill_lwp (int lwpid, int signo);
  189. static int stop_callback (struct lwp_info *lp);
  190. static void block_child_signals (sigset_t *prev_mask);
  191. static void restore_child_signals_mask (sigset_t *prev_mask);
  192. struct lwp_info;
  193. static struct lwp_info *add_lwp (ptid_t ptid);
  194. static void purge_lwp_list (int pid);
  195. static void delete_lwp (ptid_t ptid);
  196. static struct lwp_info *find_lwp_pid (ptid_t ptid);
  197. static int lwp_status_pending_p (struct lwp_info *lp);
  198. static void save_stop_reason (struct lwp_info *lp);
  199. static void close_proc_mem_file (pid_t pid);
  200. static void open_proc_mem_file (ptid_t ptid);
  201. /* Return TRUE if LWP is the leader thread of the process. */
  202. static bool
  203. is_leader (lwp_info *lp)
  204. {
  205. return lp->ptid.pid () == lp->ptid.lwp ();
  206. }
  207. /* LWP accessors. */
  208. /* See nat/linux-nat.h. */
  209. ptid_t
  210. ptid_of_lwp (struct lwp_info *lwp)
  211. {
  212. return lwp->ptid;
  213. }
  214. /* See nat/linux-nat.h. */
  215. void
  216. lwp_set_arch_private_info (struct lwp_info *lwp,
  217. struct arch_lwp_info *info)
  218. {
  219. lwp->arch_private = info;
  220. }
  221. /* See nat/linux-nat.h. */
  222. struct arch_lwp_info *
  223. lwp_arch_private_info (struct lwp_info *lwp)
  224. {
  225. return lwp->arch_private;
  226. }
  227. /* See nat/linux-nat.h. */
  228. int
  229. lwp_is_stopped (struct lwp_info *lwp)
  230. {
  231. return lwp->stopped;
  232. }
  233. /* See nat/linux-nat.h. */
  234. enum target_stop_reason
  235. lwp_stop_reason (struct lwp_info *lwp)
  236. {
  237. return lwp->stop_reason;
  238. }
  239. /* See nat/linux-nat.h. */
  240. int
  241. lwp_is_stepping (struct lwp_info *lwp)
  242. {
  243. return lwp->step;
  244. }
  245. /* Trivial list manipulation functions to keep track of a list of
  246. new stopped processes. */
  247. static void
  248. add_to_pid_list (struct simple_pid_list **listp, int pid, int status)
  249. {
  250. struct simple_pid_list *new_pid = XNEW (struct simple_pid_list);
  251. new_pid->pid = pid;
  252. new_pid->status = status;
  253. new_pid->next = *listp;
  254. *listp = new_pid;
  255. }
  256. static int
  257. pull_pid_from_list (struct simple_pid_list **listp, int pid, int *statusp)
  258. {
  259. struct simple_pid_list **p;
  260. for (p = listp; *p != NULL; p = &(*p)->next)
  261. if ((*p)->pid == pid)
  262. {
  263. struct simple_pid_list *next = (*p)->next;
  264. *statusp = (*p)->status;
  265. xfree (*p);
  266. *p = next;
  267. return 1;
  268. }
  269. return 0;
  270. }
  271. /* Return the ptrace options that we want to try to enable. */
  272. static int
  273. linux_nat_ptrace_options (int attached)
  274. {
  275. int options = 0;
  276. if (!attached)
  277. options |= PTRACE_O_EXITKILL;
  278. options |= (PTRACE_O_TRACESYSGOOD
  279. | PTRACE_O_TRACEVFORKDONE
  280. | PTRACE_O_TRACEVFORK
  281. | PTRACE_O_TRACEFORK
  282. | PTRACE_O_TRACEEXEC);
  283. return options;
  284. }
  285. /* Initialize ptrace and procfs warnings and check for supported
  286. ptrace features given PID.
  287. ATTACHED should be nonzero iff we attached to the inferior. */
  288. static void
  289. linux_init_ptrace_procfs (pid_t pid, int attached)
  290. {
  291. int options = linux_nat_ptrace_options (attached);
  292. linux_enable_event_reporting (pid, options);
  293. linux_ptrace_init_warnings ();
  294. linux_proc_init_warnings ();
  295. }
  296. linux_nat_target::~linux_nat_target ()
  297. {}
  298. void
  299. linux_nat_target::post_attach (int pid)
  300. {
  301. linux_init_ptrace_procfs (pid, 1);
  302. }
  303. /* Implement the virtual inf_ptrace_target::post_startup_inferior method. */
  304. void
  305. linux_nat_target::post_startup_inferior (ptid_t ptid)
  306. {
  307. linux_init_ptrace_procfs (ptid.pid (), 0);
  308. }
  309. /* Return the number of known LWPs in the tgid given by PID. */
  310. static int
  311. num_lwps (int pid)
  312. {
  313. int count = 0;
  314. for (const lwp_info *lp ATTRIBUTE_UNUSED : all_lwps ())
  315. if (lp->ptid.pid () == pid)
  316. count++;
  317. return count;
  318. }
  319. /* Deleter for lwp_info unique_ptr specialisation. */
  320. struct lwp_deleter
  321. {
  322. void operator() (struct lwp_info *lwp) const
  323. {
  324. delete_lwp (lwp->ptid);
  325. }
  326. };
  327. /* A unique_ptr specialisation for lwp_info. */
  328. typedef std::unique_ptr<struct lwp_info, lwp_deleter> lwp_info_up;
  329. /* Target hook for follow_fork. */
  330. void
  331. linux_nat_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
  332. target_waitkind fork_kind, bool follow_child,
  333. bool detach_fork)
  334. {
  335. inf_ptrace_target::follow_fork (child_inf, child_ptid, fork_kind,
  336. follow_child, detach_fork);
  337. if (!follow_child)
  338. {
  339. bool has_vforked = fork_kind == TARGET_WAITKIND_VFORKED;
  340. ptid_t parent_ptid = inferior_ptid;
  341. int parent_pid = parent_ptid.lwp ();
  342. int child_pid = child_ptid.lwp ();
  343. /* We're already attached to the parent, by default. */
  344. lwp_info *child_lp = add_lwp (child_ptid);
  345. child_lp->stopped = 1;
  346. child_lp->last_resume_kind = resume_stop;
  347. /* Detach new forked process? */
  348. if (detach_fork)
  349. {
  350. int child_stop_signal = 0;
  351. bool detach_child = true;
  352. /* Move CHILD_LP into a unique_ptr and clear the source pointer
  353. to prevent us doing anything stupid with it. */
  354. lwp_info_up child_lp_ptr (child_lp);
  355. child_lp = nullptr;
  356. linux_target->low_prepare_to_resume (child_lp_ptr.get ());
  357. /* When debugging an inferior in an architecture that supports
  358. hardware single stepping on a kernel without commit
  359. 6580807da14c423f0d0a708108e6df6ebc8bc83d, the vfork child
  360. process starts with the TIF_SINGLESTEP/X86_EFLAGS_TF bits
  361. set if the parent process had them set.
  362. To work around this, single step the child process
  363. once before detaching to clear the flags. */
  364. /* Note that we consult the parent's architecture instead of
  365. the child's because there's no inferior for the child at
  366. this point. */
  367. if (!gdbarch_software_single_step_p (target_thread_architecture
  368. (parent_ptid)))
  369. {
  370. int status;
  371. linux_disable_event_reporting (child_pid);
  372. if (ptrace (PTRACE_SINGLESTEP, child_pid, 0, 0) < 0)
  373. perror_with_name (_("Couldn't do single step"));
  374. if (my_waitpid (child_pid, &status, 0) < 0)
  375. perror_with_name (_("Couldn't wait vfork process"));
  376. else
  377. {
  378. detach_child = WIFSTOPPED (status);
  379. child_stop_signal = WSTOPSIG (status);
  380. }
  381. }
  382. if (detach_child)
  383. {
  384. int signo = child_stop_signal;
  385. if (signo != 0
  386. && !signal_pass_state (gdb_signal_from_host (signo)))
  387. signo = 0;
  388. ptrace (PTRACE_DETACH, child_pid, 0, signo);
  389. close_proc_mem_file (child_pid);
  390. }
  391. }
  392. if (has_vforked)
  393. {
  394. lwp_info *parent_lp = find_lwp_pid (parent_ptid);
  395. linux_nat_debug_printf ("waiting for VFORK_DONE on %d", parent_pid);
  396. parent_lp->stopped = 1;
  397. /* We'll handle the VFORK_DONE event like any other
  398. event, in target_wait. */
  399. }
  400. }
  401. else
  402. {
  403. struct lwp_info *child_lp;
  404. child_lp = add_lwp (child_ptid);
  405. child_lp->stopped = 1;
  406. child_lp->last_resume_kind = resume_stop;
  407. }
  408. }
  409. int
  410. linux_nat_target::insert_fork_catchpoint (int pid)
  411. {
  412. return 0;
  413. }
  414. int
  415. linux_nat_target::remove_fork_catchpoint (int pid)
  416. {
  417. return 0;
  418. }
  419. int
  420. linux_nat_target::insert_vfork_catchpoint (int pid)
  421. {
  422. return 0;
  423. }
  424. int
  425. linux_nat_target::remove_vfork_catchpoint (int pid)
  426. {
  427. return 0;
  428. }
  429. int
  430. linux_nat_target::insert_exec_catchpoint (int pid)
  431. {
  432. return 0;
  433. }
  434. int
  435. linux_nat_target::remove_exec_catchpoint (int pid)
  436. {
  437. return 0;
  438. }
  439. int
  440. linux_nat_target::set_syscall_catchpoint (int pid, bool needed, int any_count,
  441. gdb::array_view<const int> syscall_counts)
  442. {
  443. /* On GNU/Linux, we ignore the arguments. It means that we only
  444. enable the syscall catchpoints, but do not disable them.
  445. Also, we do not use the `syscall_counts' information because we do not
  446. filter system calls here. We let GDB do the logic for us. */
  447. return 0;
  448. }
  449. /* List of known LWPs, keyed by LWP PID. This speeds up the common
  450. case of mapping a PID returned from the kernel to our corresponding
  451. lwp_info data structure. */
  452. static htab_t lwp_lwpid_htab;
  453. /* Calculate a hash from a lwp_info's LWP PID. */
  454. static hashval_t
  455. lwp_info_hash (const void *ap)
  456. {
  457. const struct lwp_info *lp = (struct lwp_info *) ap;
  458. pid_t pid = lp->ptid.lwp ();
  459. return iterative_hash_object (pid, 0);
  460. }
  461. /* Equality function for the lwp_info hash table. Compares the LWP's
  462. PID. */
  463. static int
  464. lwp_lwpid_htab_eq (const void *a, const void *b)
  465. {
  466. const struct lwp_info *entry = (const struct lwp_info *) a;
  467. const struct lwp_info *element = (const struct lwp_info *) b;
  468. return entry->ptid.lwp () == element->ptid.lwp ();
  469. }
  470. /* Create the lwp_lwpid_htab hash table. */
  471. static void
  472. lwp_lwpid_htab_create (void)
  473. {
  474. lwp_lwpid_htab = htab_create (100, lwp_info_hash, lwp_lwpid_htab_eq, NULL);
  475. }
  476. /* Add LP to the hash table. */
  477. static void
  478. lwp_lwpid_htab_add_lwp (struct lwp_info *lp)
  479. {
  480. void **slot;
  481. slot = htab_find_slot (lwp_lwpid_htab, lp, INSERT);
  482. gdb_assert (slot != NULL && *slot == NULL);
  483. *slot = lp;
  484. }
  485. /* Head of doubly-linked list of known LWPs. Sorted by reverse
  486. creation order. This order is assumed in some cases. E.g.,
  487. reaping status after killing alls lwps of a process: the leader LWP
  488. must be reaped last. */
  489. static intrusive_list<lwp_info> lwp_list;
  490. /* See linux-nat.h. */
  491. lwp_info_range
  492. all_lwps ()
  493. {
  494. return lwp_info_range (lwp_list.begin ());
  495. }
  496. /* See linux-nat.h. */
  497. lwp_info_safe_range
  498. all_lwps_safe ()
  499. {
  500. return lwp_info_safe_range (lwp_list.begin ());
  501. }
  502. /* Add LP to sorted-by-reverse-creation-order doubly-linked list. */
  503. static void
  504. lwp_list_add (struct lwp_info *lp)
  505. {
  506. lwp_list.push_front (*lp);
  507. }
  508. /* Remove LP from sorted-by-reverse-creation-order doubly-linked
  509. list. */
  510. static void
  511. lwp_list_remove (struct lwp_info *lp)
  512. {
  513. /* Remove from sorted-by-creation-order list. */
  514. lwp_list.erase (lwp_list.iterator_to (*lp));
  515. }
  516. /* Signal mask for use with sigsuspend in linux_nat_wait, initialized in
  517. _initialize_linux_nat. */
  518. static sigset_t suspend_mask;
  519. /* Signals to block to make that sigsuspend work. */
  520. static sigset_t blocked_mask;
  521. /* SIGCHLD action. */
  522. static struct sigaction sigchld_action;
  523. /* Block child signals (SIGCHLD and linux threads signals), and store
  524. the previous mask in PREV_MASK. */
  525. static void
  526. block_child_signals (sigset_t *prev_mask)
  527. {
  528. /* Make sure SIGCHLD is blocked. */
  529. if (!sigismember (&blocked_mask, SIGCHLD))
  530. sigaddset (&blocked_mask, SIGCHLD);
  531. gdb_sigmask (SIG_BLOCK, &blocked_mask, prev_mask);
  532. }
  533. /* Restore child signals mask, previously returned by
  534. block_child_signals. */
  535. static void
  536. restore_child_signals_mask (sigset_t *prev_mask)
  537. {
  538. gdb_sigmask (SIG_SETMASK, prev_mask, NULL);
  539. }
  540. /* Mask of signals to pass directly to the inferior. */
  541. static sigset_t pass_mask;
  542. /* Update signals to pass to the inferior. */
  543. void
  544. linux_nat_target::pass_signals
  545. (gdb::array_view<const unsigned char> pass_signals)
  546. {
  547. int signo;
  548. sigemptyset (&pass_mask);
  549. for (signo = 1; signo < NSIG; signo++)
  550. {
  551. int target_signo = gdb_signal_from_host (signo);
  552. if (target_signo < pass_signals.size () && pass_signals[target_signo])
  553. sigaddset (&pass_mask, signo);
  554. }
  555. }
  556. /* Prototypes for local functions. */
  557. static int stop_wait_callback (struct lwp_info *lp);
  558. static int resume_stopped_resumed_lwps (struct lwp_info *lp, const ptid_t wait_ptid);
  559. static int check_ptrace_stopped_lwp_gone (struct lwp_info *lp);
  560. /* Destroy and free LP. */
  561. lwp_info::~lwp_info ()
  562. {
  563. /* Let the arch specific bits release arch_lwp_info. */
  564. linux_target->low_delete_thread (this->arch_private);
  565. }
  566. /* Traversal function for purge_lwp_list. */
  567. static int
  568. lwp_lwpid_htab_remove_pid (void **slot, void *info)
  569. {
  570. struct lwp_info *lp = (struct lwp_info *) *slot;
  571. int pid = *(int *) info;
  572. if (lp->ptid.pid () == pid)
  573. {
  574. htab_clear_slot (lwp_lwpid_htab, slot);
  575. lwp_list_remove (lp);
  576. delete lp;
  577. }
  578. return 1;
  579. }
  580. /* Remove all LWPs belong to PID from the lwp list. */
  581. static void
  582. purge_lwp_list (int pid)
  583. {
  584. htab_traverse_noresize (lwp_lwpid_htab, lwp_lwpid_htab_remove_pid, &pid);
  585. }
  586. /* Add the LWP specified by PTID to the list. PTID is the first LWP
  587. in the process. Return a pointer to the structure describing the
  588. new LWP.
  589. This differs from add_lwp in that we don't let the arch specific
  590. bits know about this new thread. Current clients of this callback
  591. take the opportunity to install watchpoints in the new thread, and
  592. we shouldn't do that for the first thread. If we're spawning a
  593. child ("run"), the thread executes the shell wrapper first, and we
  594. shouldn't touch it until it execs the program we want to debug.
  595. For "attach", it'd be okay to call the callback, but it's not
  596. necessary, because watchpoints can't yet have been inserted into
  597. the inferior. */
  598. static struct lwp_info *
  599. add_initial_lwp (ptid_t ptid)
  600. {
  601. gdb_assert (ptid.lwp_p ());
  602. lwp_info *lp = new lwp_info (ptid);
  603. /* Add to sorted-by-reverse-creation-order list. */
  604. lwp_list_add (lp);
  605. /* Add to keyed-by-pid htab. */
  606. lwp_lwpid_htab_add_lwp (lp);
  607. return lp;
  608. }
  609. /* Add the LWP specified by PID to the list. Return a pointer to the
  610. structure describing the new LWP. The LWP should already be
  611. stopped. */
  612. static struct lwp_info *
  613. add_lwp (ptid_t ptid)
  614. {
  615. struct lwp_info *lp;
  616. lp = add_initial_lwp (ptid);
  617. /* Let the arch specific bits know about this new thread. Current
  618. clients of this callback take the opportunity to install
  619. watchpoints in the new thread. We don't do this for the first
  620. thread though. See add_initial_lwp. */
  621. linux_target->low_new_thread (lp);
  622. return lp;
  623. }
  624. /* Remove the LWP specified by PID from the list. */
  625. static void
  626. delete_lwp (ptid_t ptid)
  627. {
  628. lwp_info dummy (ptid);
  629. void **slot = htab_find_slot (lwp_lwpid_htab, &dummy, NO_INSERT);
  630. if (slot == NULL)
  631. return;
  632. lwp_info *lp = *(struct lwp_info **) slot;
  633. gdb_assert (lp != NULL);
  634. htab_clear_slot (lwp_lwpid_htab, slot);
  635. /* Remove from sorted-by-creation-order list. */
  636. lwp_list_remove (lp);
  637. /* Release. */
  638. delete lp;
  639. }
  640. /* Return a pointer to the structure describing the LWP corresponding
  641. to PID. If no corresponding LWP could be found, return NULL. */
  642. static struct lwp_info *
  643. find_lwp_pid (ptid_t ptid)
  644. {
  645. int lwp;
  646. if (ptid.lwp_p ())
  647. lwp = ptid.lwp ();
  648. else
  649. lwp = ptid.pid ();
  650. lwp_info dummy (ptid_t (0, lwp));
  651. return (struct lwp_info *) htab_find (lwp_lwpid_htab, &dummy);
  652. }
  653. /* See nat/linux-nat.h. */
  654. struct lwp_info *
  655. iterate_over_lwps (ptid_t filter,
  656. gdb::function_view<iterate_over_lwps_ftype> callback)
  657. {
  658. for (lwp_info *lp : all_lwps_safe ())
  659. {
  660. if (lp->ptid.matches (filter))
  661. {
  662. if (callback (lp) != 0)
  663. return lp;
  664. }
  665. }
  666. return NULL;
  667. }
  668. /* Update our internal state when changing from one checkpoint to
  669. another indicated by NEW_PTID. We can only switch single-threaded
  670. applications, so we only create one new LWP, and the previous list
  671. is discarded. */
  672. void
  673. linux_nat_switch_fork (ptid_t new_ptid)
  674. {
  675. struct lwp_info *lp;
  676. purge_lwp_list (inferior_ptid.pid ());
  677. lp = add_lwp (new_ptid);
  678. lp->stopped = 1;
  679. /* This changes the thread's ptid while preserving the gdb thread
  680. num. Also changes the inferior pid, while preserving the
  681. inferior num. */
  682. thread_change_ptid (linux_target, inferior_ptid, new_ptid);
  683. /* We've just told GDB core that the thread changed target id, but,
  684. in fact, it really is a different thread, with different register
  685. contents. */
  686. registers_changed ();
  687. }
  688. /* Handle the exit of a single thread LP. */
  689. static void
  690. exit_lwp (struct lwp_info *lp)
  691. {
  692. struct thread_info *th = find_thread_ptid (linux_target, lp->ptid);
  693. if (th)
  694. {
  695. if (print_thread_events)
  696. gdb_printf (_("[%s exited]\n"),
  697. target_pid_to_str (lp->ptid).c_str ());
  698. delete_thread (th);
  699. }
  700. delete_lwp (lp->ptid);
  701. }
  702. /* Wait for the LWP specified by LP, which we have just attached to.
  703. Returns a wait status for that LWP, to cache. */
  704. static int
  705. linux_nat_post_attach_wait (ptid_t ptid, int *signalled)
  706. {
  707. pid_t new_pid, pid = ptid.lwp ();
  708. int status;
  709. if (linux_proc_pid_is_stopped (pid))
  710. {
  711. linux_nat_debug_printf ("Attaching to a stopped process");
  712. /* The process is definitely stopped. It is in a job control
  713. stop, unless the kernel predates the TASK_STOPPED /
  714. TASK_TRACED distinction, in which case it might be in a
  715. ptrace stop. Make sure it is in a ptrace stop; from there we
  716. can kill it, signal it, et cetera.
  717. First make sure there is a pending SIGSTOP. Since we are
  718. already attached, the process can not transition from stopped
  719. to running without a PTRACE_CONT; so we know this signal will
  720. go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
  721. probably already in the queue (unless this kernel is old
  722. enough to use TASK_STOPPED for ptrace stops); but since SIGSTOP
  723. is not an RT signal, it can only be queued once. */
  724. kill_lwp (pid, SIGSTOP);
  725. /* Finally, resume the stopped process. This will deliver the SIGSTOP
  726. (or a higher priority signal, just like normal PTRACE_ATTACH). */
  727. ptrace (PTRACE_CONT, pid, 0, 0);
  728. }
  729. /* Make sure the initial process is stopped. The user-level threads
  730. layer might want to poke around in the inferior, and that won't
  731. work if things haven't stabilized yet. */
  732. new_pid = my_waitpid (pid, &status, __WALL);
  733. gdb_assert (pid == new_pid);
  734. if (!WIFSTOPPED (status))
  735. {
  736. /* The pid we tried to attach has apparently just exited. */
  737. linux_nat_debug_printf ("Failed to stop %d: %s", pid,
  738. status_to_str (status).c_str ());
  739. return status;
  740. }
  741. if (WSTOPSIG (status) != SIGSTOP)
  742. {
  743. *signalled = 1;
  744. linux_nat_debug_printf ("Received %s after attaching",
  745. status_to_str (status).c_str ());
  746. }
  747. return status;
  748. }
  749. void
  750. linux_nat_target::create_inferior (const char *exec_file,
  751. const std::string &allargs,
  752. char **env, int from_tty)
  753. {
  754. maybe_disable_address_space_randomization restore_personality
  755. (disable_randomization);
  756. /* The fork_child mechanism is synchronous and calls target_wait, so
  757. we have to mask the async mode. */
  758. /* Make sure we report all signals during startup. */
  759. pass_signals ({});
  760. inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty);
  761. open_proc_mem_file (inferior_ptid);
  762. }
  763. /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
  764. already attached. Returns true if a new LWP is found, false
  765. otherwise. */
  766. static int
  767. attach_proc_task_lwp_callback (ptid_t ptid)
  768. {
  769. struct lwp_info *lp;
  770. /* Ignore LWPs we're already attached to. */
  771. lp = find_lwp_pid (ptid);
  772. if (lp == NULL)
  773. {
  774. int lwpid = ptid.lwp ();
  775. if (ptrace (PTRACE_ATTACH, lwpid, 0, 0) < 0)
  776. {
  777. int err = errno;
  778. /* Be quiet if we simply raced with the thread exiting.
  779. EPERM is returned if the thread's task still exists, and
  780. is marked as exited or zombie, as well as other
  781. conditions, so in that case, confirm the status in
  782. /proc/PID/status. */
  783. if (err == ESRCH
  784. || (err == EPERM && linux_proc_pid_is_gone (lwpid)))
  785. {
  786. linux_nat_debug_printf
  787. ("Cannot attach to lwp %d: thread is gone (%d: %s)",
  788. lwpid, err, safe_strerror (err));
  789. }
  790. else
  791. {
  792. std::string reason
  793. = linux_ptrace_attach_fail_reason_string (ptid, err);
  794. warning (_("Cannot attach to lwp %d: %s"),
  795. lwpid, reason.c_str ());
  796. }
  797. }
  798. else
  799. {
  800. linux_nat_debug_printf ("PTRACE_ATTACH %s, 0, 0 (OK)",
  801. ptid.to_string ().c_str ());
  802. lp = add_lwp (ptid);
  803. /* The next time we wait for this LWP we'll see a SIGSTOP as
  804. PTRACE_ATTACH brings it to a halt. */
  805. lp->signalled = 1;
  806. /* We need to wait for a stop before being able to make the
  807. next ptrace call on this LWP. */
  808. lp->must_set_ptrace_flags = 1;
  809. /* So that wait collects the SIGSTOP. */
  810. lp->resumed = 1;
  811. /* Also add the LWP to gdb's thread list, in case a
  812. matching libthread_db is not found (or the process uses
  813. raw clone). */
  814. add_thread (linux_target, lp->ptid);
  815. set_running (linux_target, lp->ptid, true);
  816. set_executing (linux_target, lp->ptid, true);
  817. }
  818. return 1;
  819. }
  820. return 0;
  821. }
  822. void
  823. linux_nat_target::attach (const char *args, int from_tty)
  824. {
  825. struct lwp_info *lp;
  826. int status;
  827. ptid_t ptid;
  828. /* Make sure we report all signals during attach. */
  829. pass_signals ({});
  830. try
  831. {
  832. inf_ptrace_target::attach (args, from_tty);
  833. }
  834. catch (const gdb_exception_error &ex)
  835. {
  836. pid_t pid = parse_pid_to_attach (args);
  837. std::string reason = linux_ptrace_attach_fail_reason (pid);
  838. if (!reason.empty ())
  839. throw_error (ex.error, "warning: %s\n%s", reason.c_str (),
  840. ex.what ());
  841. else
  842. throw_error (ex.error, "%s", ex.what ());
  843. }
  844. /* The ptrace base target adds the main thread with (pid,0,0)
  845. format. Decorate it with lwp info. */
  846. ptid = ptid_t (inferior_ptid.pid (),
  847. inferior_ptid.pid ());
  848. thread_change_ptid (linux_target, inferior_ptid, ptid);
  849. /* Add the initial process as the first LWP to the list. */
  850. lp = add_initial_lwp (ptid);
  851. status = linux_nat_post_attach_wait (lp->ptid, &lp->signalled);
  852. if (!WIFSTOPPED (status))
  853. {
  854. if (WIFEXITED (status))
  855. {
  856. int exit_code = WEXITSTATUS (status);
  857. target_terminal::ours ();
  858. target_mourn_inferior (inferior_ptid);
  859. if (exit_code == 0)
  860. error (_("Unable to attach: program exited normally."));
  861. else
  862. error (_("Unable to attach: program exited with code %d."),
  863. exit_code);
  864. }
  865. else if (WIFSIGNALED (status))
  866. {
  867. enum gdb_signal signo;
  868. target_terminal::ours ();
  869. target_mourn_inferior (inferior_ptid);
  870. signo = gdb_signal_from_host (WTERMSIG (status));
  871. error (_("Unable to attach: program terminated with signal "
  872. "%s, %s."),
  873. gdb_signal_to_name (signo),
  874. gdb_signal_to_string (signo));
  875. }
  876. internal_error (__FILE__, __LINE__,
  877. _("unexpected status %d for PID %ld"),
  878. status, (long) ptid.lwp ());
  879. }
  880. lp->stopped = 1;
  881. open_proc_mem_file (lp->ptid);
  882. /* Save the wait status to report later. */
  883. lp->resumed = 1;
  884. linux_nat_debug_printf ("waitpid %ld, saving status %s",
  885. (long) lp->ptid.pid (),
  886. status_to_str (status).c_str ());
  887. lp->status = status;
  888. /* We must attach to every LWP. If /proc is mounted, use that to
  889. find them now. The inferior may be using raw clone instead of
  890. using pthreads. But even if it is using pthreads, thread_db
  891. walks structures in the inferior's address space to find the list
  892. of threads/LWPs, and those structures may well be corrupted.
  893. Note that once thread_db is loaded, we'll still use it to list
  894. threads and associate pthread info with each LWP. */
  895. linux_proc_attach_tgid_threads (lp->ptid.pid (),
  896. attach_proc_task_lwp_callback);
  897. }
  898. /* Ptrace-detach the thread with pid PID. */
  899. static void
  900. detach_one_pid (int pid, int signo)
  901. {
  902. if (ptrace (PTRACE_DETACH, pid, 0, signo) < 0)
  903. {
  904. int save_errno = errno;
  905. /* We know the thread exists, so ESRCH must mean the lwp is
  906. zombie. This can happen if one of the already-detached
  907. threads exits the whole thread group. In that case we're
  908. still attached, and must reap the lwp. */
  909. if (save_errno == ESRCH)
  910. {
  911. int ret, status;
  912. ret = my_waitpid (pid, &status, __WALL);
  913. if (ret == -1)
  914. {
  915. warning (_("Couldn't reap LWP %d while detaching: %s"),
  916. pid, safe_strerror (errno));
  917. }
  918. else if (!WIFEXITED (status) && !WIFSIGNALED (status))
  919. {
  920. warning (_("Reaping LWP %d while detaching "
  921. "returned unexpected status 0x%x"),
  922. pid, status);
  923. }
  924. }
  925. else
  926. error (_("Can't detach %d: %s"),
  927. pid, safe_strerror (save_errno));
  928. }
  929. else
  930. linux_nat_debug_printf ("PTRACE_DETACH (%d, %s, 0) (OK)",
  931. pid, strsignal (signo));
  932. }
  933. /* Get pending signal of THREAD as a host signal number, for detaching
  934. purposes. This is the signal the thread last stopped for, which we
  935. need to deliver to the thread when detaching, otherwise, it'd be
  936. suppressed/lost. */
  937. static int
  938. get_detach_signal (struct lwp_info *lp)
  939. {
  940. enum gdb_signal signo = GDB_SIGNAL_0;
  941. /* If we paused threads momentarily, we may have stored pending
  942. events in lp->status or lp->waitstatus (see stop_wait_callback),
  943. and GDB core hasn't seen any signal for those threads.
  944. Otherwise, the last signal reported to the core is found in the
  945. thread object's stop_signal.
  946. There's a corner case that isn't handled here at present. Only
  947. if the thread stopped with a TARGET_WAITKIND_STOPPED does
  948. stop_signal make sense as a real signal to pass to the inferior.
  949. Some catchpoint related events, like
  950. TARGET_WAITKIND_(V)FORK|EXEC|SYSCALL, have their stop_signal set
  951. to GDB_SIGNAL_SIGTRAP when the catchpoint triggers. But,
  952. those traps are debug API (ptrace in our case) related and
  953. induced; the inferior wouldn't see them if it wasn't being
  954. traced. Hence, we should never pass them to the inferior, even
  955. when set to pass state. Since this corner case isn't handled by
  956. infrun.c when proceeding with a signal, for consistency, neither
  957. do we handle it here (or elsewhere in the file we check for
  958. signal pass state). Normally SIGTRAP isn't set to pass state, so
  959. this is really a corner case. */
  960. if (lp->waitstatus.kind () != TARGET_WAITKIND_IGNORE)
  961. signo = GDB_SIGNAL_0; /* a pending ptrace event, not a real signal. */
  962. else if (lp->status)
  963. signo = gdb_signal_from_host (WSTOPSIG (lp->status));
  964. else
  965. {
  966. struct thread_info *tp = find_thread_ptid (linux_target, lp->ptid);
  967. if (target_is_non_stop_p () && !tp->executing ())
  968. {
  969. if (tp->has_pending_waitstatus ())
  970. {
  971. /* If the thread has a pending event, and it was stopped with a
  972. signal, use that signal to resume it. If it has a pending
  973. event of another kind, it was not stopped with a signal, so
  974. resume it without a signal. */
  975. if (tp->pending_waitstatus ().kind () == TARGET_WAITKIND_STOPPED)
  976. signo = tp->pending_waitstatus ().sig ();
  977. else
  978. signo = GDB_SIGNAL_0;
  979. }
  980. else
  981. signo = tp->stop_signal ();
  982. }
  983. else if (!target_is_non_stop_p ())
  984. {
  985. ptid_t last_ptid;
  986. process_stratum_target *last_target;
  987. get_last_target_status (&last_target, &last_ptid, nullptr);
  988. if (last_target == linux_target
  989. && lp->ptid.lwp () == last_ptid.lwp ())
  990. signo = tp->stop_signal ();
  991. }
  992. }
  993. if (signo == GDB_SIGNAL_0)
  994. {
  995. linux_nat_debug_printf ("lwp %s has no pending signal",
  996. lp->ptid.to_string ().c_str ());
  997. }
  998. else if (!signal_pass_state (signo))
  999. {
  1000. linux_nat_debug_printf
  1001. ("lwp %s had signal %s but it is in no pass state",
  1002. lp->ptid.to_string ().c_str (), gdb_signal_to_string (signo));
  1003. }
  1004. else
  1005. {
  1006. linux_nat_debug_printf ("lwp %s has pending signal %s",
  1007. lp->ptid.to_string ().c_str (),
  1008. gdb_signal_to_string (signo));
  1009. return gdb_signal_to_host (signo);
  1010. }
  1011. return 0;
  1012. }
  1013. /* Detach from LP. If SIGNO_P is non-NULL, then it points to the
  1014. signal number that should be passed to the LWP when detaching.
  1015. Otherwise pass any pending signal the LWP may have, if any. */
  1016. static void
  1017. detach_one_lwp (struct lwp_info *lp, int *signo_p)
  1018. {
  1019. int lwpid = lp->ptid.lwp ();
  1020. int signo;
  1021. gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
  1022. /* If the lwp/thread we are about to detach has a pending fork event,
  1023. there is a process GDB is attached to that the core of GDB doesn't know
  1024. about. Detach from it. */
  1025. /* Check in lwp_info::status. */
  1026. if (WIFSTOPPED (lp->status) && linux_is_extended_waitstatus (lp->status))
  1027. {
  1028. int event = linux_ptrace_get_extended_event (lp->status);
  1029. if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK)
  1030. {
  1031. unsigned long child_pid;
  1032. int ret = ptrace (PTRACE_GETEVENTMSG, lp->ptid.lwp (), 0, &child_pid);
  1033. if (ret == 0)
  1034. detach_one_pid (child_pid, 0);
  1035. else
  1036. perror_warning_with_name (_("Failed to detach fork child"));
  1037. }
  1038. }
  1039. /* Check in lwp_info::waitstatus. */
  1040. if (lp->waitstatus.kind () == TARGET_WAITKIND_VFORKED
  1041. || lp->waitstatus.kind () == TARGET_WAITKIND_FORKED)
  1042. detach_one_pid (lp->waitstatus.child_ptid ().pid (), 0);
  1043. /* Check in thread_info::pending_waitstatus. */
  1044. thread_info *tp = find_thread_ptid (linux_target, lp->ptid);
  1045. if (tp->has_pending_waitstatus ())
  1046. {
  1047. const target_waitstatus &ws = tp->pending_waitstatus ();
  1048. if (ws.kind () == TARGET_WAITKIND_VFORKED
  1049. || ws.kind () == TARGET_WAITKIND_FORKED)
  1050. detach_one_pid (ws.child_ptid ().pid (), 0);
  1051. }
  1052. /* Check in thread_info::pending_follow. */
  1053. if (tp->pending_follow.kind () == TARGET_WAITKIND_VFORKED
  1054. || tp->pending_follow.kind () == TARGET_WAITKIND_FORKED)
  1055. detach_one_pid (tp->pending_follow.child_ptid ().pid (), 0);
  1056. if (lp->status != 0)
  1057. linux_nat_debug_printf ("Pending %s for %s on detach.",
  1058. strsignal (WSTOPSIG (lp->status)),
  1059. lp->ptid.to_string ().c_str ());
  1060. /* If there is a pending SIGSTOP, get rid of it. */
  1061. if (lp->signalled)
  1062. {
  1063. linux_nat_debug_printf ("Sending SIGCONT to %s",
  1064. lp->ptid.to_string ().c_str ());
  1065. kill_lwp (lwpid, SIGCONT);
  1066. lp->signalled = 0;
  1067. }
  1068. if (signo_p == NULL)
  1069. {
  1070. /* Pass on any pending signal for this LWP. */
  1071. signo = get_detach_signal (lp);
  1072. }
  1073. else
  1074. signo = *signo_p;
  1075. /* Preparing to resume may try to write registers, and fail if the
  1076. lwp is zombie. If that happens, ignore the error. We'll handle
  1077. it below, when detach fails with ESRCH. */
  1078. try
  1079. {
  1080. linux_target->low_prepare_to_resume (lp);
  1081. }
  1082. catch (const gdb_exception_error &ex)
  1083. {
  1084. if (!check_ptrace_stopped_lwp_gone (lp))
  1085. throw;
  1086. }
  1087. detach_one_pid (lwpid, signo);
  1088. delete_lwp (lp->ptid);
  1089. }
  1090. static int
  1091. detach_callback (struct lwp_info *lp)
  1092. {
  1093. /* We don't actually detach from the thread group leader just yet.
  1094. If the thread group exits, we must reap the zombie clone lwps
  1095. before we're able to reap the leader. */
  1096. if (lp->ptid.lwp () != lp->ptid.pid ())
  1097. detach_one_lwp (lp, NULL);
  1098. return 0;
  1099. }
  1100. void
  1101. linux_nat_target::detach (inferior *inf, int from_tty)
  1102. {
  1103. struct lwp_info *main_lwp;
  1104. int pid = inf->pid;
  1105. /* Don't unregister from the event loop, as there may be other
  1106. inferiors running. */
  1107. /* Stop all threads before detaching. ptrace requires that the
  1108. thread is stopped to successfully detach. */
  1109. iterate_over_lwps (ptid_t (pid), stop_callback);
  1110. /* ... and wait until all of them have reported back that
  1111. they're no longer running. */
  1112. iterate_over_lwps (ptid_t (pid), stop_wait_callback);
  1113. /* We can now safely remove breakpoints. We don't this in earlier
  1114. in common code because this target doesn't currently support
  1115. writing memory while the inferior is running. */
  1116. remove_breakpoints_inf (current_inferior ());
  1117. iterate_over_lwps (ptid_t (pid), detach_callback);
  1118. /* Only the initial process should be left right now. */
  1119. gdb_assert (num_lwps (pid) == 1);
  1120. main_lwp = find_lwp_pid (ptid_t (pid));
  1121. if (forks_exist_p ())
  1122. {
  1123. /* Multi-fork case. The current inferior_ptid is being detached
  1124. from, but there are other viable forks to debug. Detach from
  1125. the current fork, and context-switch to the first
  1126. available. */
  1127. linux_fork_detach (from_tty);
  1128. }
  1129. else
  1130. {
  1131. target_announce_detach (from_tty);
  1132. /* Pass on any pending signal for the last LWP. */
  1133. int signo = get_detach_signal (main_lwp);
  1134. detach_one_lwp (main_lwp, &signo);
  1135. detach_success (inf);
  1136. }
  1137. close_proc_mem_file (pid);
  1138. }
  1139. /* Resume execution of the inferior process. If STEP is nonzero,
  1140. single-step it. If SIGNAL is nonzero, give it that signal. */
  1141. static void
  1142. linux_resume_one_lwp_throw (struct lwp_info *lp, int step,
  1143. enum gdb_signal signo)
  1144. {
  1145. lp->step = step;
  1146. /* stop_pc doubles as the PC the LWP had when it was last resumed.
  1147. We only presently need that if the LWP is stepped though (to
  1148. handle the case of stepping a breakpoint instruction). */
  1149. if (step)
  1150. {
  1151. struct regcache *regcache = get_thread_regcache (linux_target, lp->ptid);
  1152. lp->stop_pc = regcache_read_pc (regcache);
  1153. }
  1154. else
  1155. lp->stop_pc = 0;
  1156. linux_target->low_prepare_to_resume (lp);
  1157. linux_target->low_resume (lp->ptid, step, signo);
  1158. /* Successfully resumed. Clear state that no longer makes sense,
  1159. and mark the LWP as running. Must not do this before resuming
  1160. otherwise if that fails other code will be confused. E.g., we'd
  1161. later try to stop the LWP and hang forever waiting for a stop
  1162. status. Note that we must not throw after this is cleared,
  1163. otherwise handle_zombie_lwp_error would get confused. */
  1164. lp->stopped = 0;
  1165. lp->core = -1;
  1166. lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
  1167. registers_changed_ptid (linux_target, lp->ptid);
  1168. }
  1169. /* Called when we try to resume a stopped LWP and that errors out. If
  1170. the LWP is no longer in ptrace-stopped state (meaning it's zombie,
  1171. or about to become), discard the error, clear any pending status
  1172. the LWP may have, and return true (we'll collect the exit status
  1173. soon enough). Otherwise, return false. */
  1174. static int
  1175. check_ptrace_stopped_lwp_gone (struct lwp_info *lp)
  1176. {
  1177. /* If we get an error after resuming the LWP successfully, we'd
  1178. confuse !T state for the LWP being gone. */
  1179. gdb_assert (lp->stopped);
  1180. /* We can't just check whether the LWP is in 'Z (Zombie)' state,
  1181. because even if ptrace failed with ESRCH, the tracee may be "not
  1182. yet fully dead", but already refusing ptrace requests. In that
  1183. case the tracee has 'R (Running)' state for a little bit
  1184. (observed in Linux 3.18). See also the note on ESRCH in the
  1185. ptrace(2) man page. Instead, check whether the LWP has any state
  1186. other than ptrace-stopped. */
  1187. /* Don't assume anything if /proc/PID/status can't be read. */
  1188. if (linux_proc_pid_is_trace_stopped_nowarn (lp->ptid.lwp ()) == 0)
  1189. {
  1190. lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
  1191. lp->status = 0;
  1192. lp->waitstatus.set_ignore ();
  1193. return 1;
  1194. }
  1195. return 0;
  1196. }
  1197. /* Like linux_resume_one_lwp_throw, but no error is thrown if the LWP
  1198. disappears while we try to resume it. */
  1199. static void
  1200. linux_resume_one_lwp (struct lwp_info *lp, int step, enum gdb_signal signo)
  1201. {
  1202. try
  1203. {
  1204. linux_resume_one_lwp_throw (lp, step, signo);
  1205. }
  1206. catch (const gdb_exception_error &ex)
  1207. {
  1208. if (!check_ptrace_stopped_lwp_gone (lp))
  1209. throw;
  1210. }
  1211. }
  1212. /* Resume LP. */
  1213. static void
  1214. resume_lwp (struct lwp_info *lp, int step, enum gdb_signal signo)
  1215. {
  1216. if (lp->stopped)
  1217. {
  1218. struct inferior *inf = find_inferior_ptid (linux_target, lp->ptid);
  1219. if (inf->vfork_child != NULL)
  1220. {
  1221. linux_nat_debug_printf ("Not resuming %s (vfork parent)",
  1222. lp->ptid.to_string ().c_str ());
  1223. }
  1224. else if (!lwp_status_pending_p (lp))
  1225. {
  1226. linux_nat_debug_printf ("Resuming sibling %s, %s, %s",
  1227. lp->ptid.to_string ().c_str (),
  1228. (signo != GDB_SIGNAL_0
  1229. ? strsignal (gdb_signal_to_host (signo))
  1230. : "0"),
  1231. step ? "step" : "resume");
  1232. linux_resume_one_lwp (lp, step, signo);
  1233. }
  1234. else
  1235. {
  1236. linux_nat_debug_printf ("Not resuming sibling %s (has pending)",
  1237. lp->ptid.to_string ().c_str ());
  1238. }
  1239. }
  1240. else
  1241. linux_nat_debug_printf ("Not resuming sibling %s (not stopped)",
  1242. lp->ptid.to_string ().c_str ());
  1243. }
  1244. /* Callback for iterate_over_lwps. If LWP is EXCEPT, do nothing.
  1245. Resume LWP with the last stop signal, if it is in pass state. */
  1246. static int
  1247. linux_nat_resume_callback (struct lwp_info *lp, struct lwp_info *except)
  1248. {
  1249. enum gdb_signal signo = GDB_SIGNAL_0;
  1250. if (lp == except)
  1251. return 0;
  1252. if (lp->stopped)
  1253. {
  1254. struct thread_info *thread;
  1255. thread = find_thread_ptid (linux_target, lp->ptid);
  1256. if (thread != NULL)
  1257. {
  1258. signo = thread->stop_signal ();
  1259. thread->set_stop_signal (GDB_SIGNAL_0);
  1260. }
  1261. }
  1262. resume_lwp (lp, 0, signo);
  1263. return 0;
  1264. }
  1265. static int
  1266. resume_clear_callback (struct lwp_info *lp)
  1267. {
  1268. lp->resumed = 0;
  1269. lp->last_resume_kind = resume_stop;
  1270. return 0;
  1271. }
  1272. static int
  1273. resume_set_callback (struct lwp_info *lp)
  1274. {
  1275. lp->resumed = 1;
  1276. lp->last_resume_kind = resume_continue;
  1277. return 0;
  1278. }
  1279. void
  1280. linux_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
  1281. {
  1282. struct lwp_info *lp;
  1283. int resume_many;
  1284. linux_nat_debug_printf ("Preparing to %s %s, %s, inferior_ptid %s",
  1285. step ? "step" : "resume",
  1286. ptid.to_string ().c_str (),
  1287. (signo != GDB_SIGNAL_0
  1288. ? strsignal (gdb_signal_to_host (signo)) : "0"),
  1289. inferior_ptid.to_string ().c_str ());
  1290. /* A specific PTID means `step only this process id'. */
  1291. resume_many = (minus_one_ptid == ptid
  1292. || ptid.is_pid ());
  1293. /* Mark the lwps we're resuming as resumed and update their
  1294. last_resume_kind to resume_continue. */
  1295. iterate_over_lwps (ptid, resume_set_callback);
  1296. /* See if it's the current inferior that should be handled
  1297. specially. */
  1298. if (resume_many)
  1299. lp = find_lwp_pid (inferior_ptid);
  1300. else
  1301. lp = find_lwp_pid (ptid);
  1302. gdb_assert (lp != NULL);
  1303. /* Remember if we're stepping. */
  1304. lp->last_resume_kind = step ? resume_step : resume_continue;
  1305. /* If we have a pending wait status for this thread, there is no
  1306. point in resuming the process. But first make sure that
  1307. linux_nat_wait won't preemptively handle the event - we
  1308. should never take this short-circuit if we are going to
  1309. leave LP running, since we have skipped resuming all the
  1310. other threads. This bit of code needs to be synchronized
  1311. with linux_nat_wait. */
  1312. if (lp->status && WIFSTOPPED (lp->status))
  1313. {
  1314. if (!lp->step
  1315. && WSTOPSIG (lp->status)
  1316. && sigismember (&pass_mask, WSTOPSIG (lp->status)))
  1317. {
  1318. linux_nat_debug_printf
  1319. ("Not short circuiting for ignored status 0x%x", lp->status);
  1320. /* FIXME: What should we do if we are supposed to continue
  1321. this thread with a signal? */
  1322. gdb_assert (signo == GDB_SIGNAL_0);
  1323. signo = gdb_signal_from_host (WSTOPSIG (lp->status));
  1324. lp->status = 0;
  1325. }
  1326. }
  1327. if (lwp_status_pending_p (lp))
  1328. {
  1329. /* FIXME: What should we do if we are supposed to continue
  1330. this thread with a signal? */
  1331. gdb_assert (signo == GDB_SIGNAL_0);
  1332. linux_nat_debug_printf ("Short circuiting for status 0x%x",
  1333. lp->status);
  1334. if (target_can_async_p ())
  1335. {
  1336. target_async (1);
  1337. /* Tell the event loop we have something to process. */
  1338. async_file_mark ();
  1339. }
  1340. return;
  1341. }
  1342. if (resume_many)
  1343. iterate_over_lwps (ptid, [=] (struct lwp_info *info)
  1344. {
  1345. return linux_nat_resume_callback (info, lp);
  1346. });
  1347. linux_nat_debug_printf ("%s %s, %s (resume event thread)",
  1348. step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
  1349. lp->ptid.to_string ().c_str (),
  1350. (signo != GDB_SIGNAL_0
  1351. ? strsignal (gdb_signal_to_host (signo)) : "0"));
  1352. linux_resume_one_lwp (lp, step, signo);
  1353. }
  1354. /* Send a signal to an LWP. */
  1355. static int
  1356. kill_lwp (int lwpid, int signo)
  1357. {
  1358. int ret;
  1359. errno = 0;
  1360. ret = syscall (__NR_tkill, lwpid, signo);
  1361. if (errno == ENOSYS)
  1362. {
  1363. /* If tkill fails, then we are not using nptl threads, a
  1364. configuration we no longer support. */
  1365. perror_with_name (("tkill"));
  1366. }
  1367. return ret;
  1368. }
  1369. /* Handle a GNU/Linux syscall trap wait response. If we see a syscall
  1370. event, check if the core is interested in it: if not, ignore the
  1371. event, and keep waiting; otherwise, we need to toggle the LWP's
  1372. syscall entry/exit status, since the ptrace event itself doesn't
  1373. indicate it, and report the trap to higher layers. */
  1374. static int
  1375. linux_handle_syscall_trap (struct lwp_info *lp, int stopping)
  1376. {
  1377. struct target_waitstatus *ourstatus = &lp->waitstatus;
  1378. struct gdbarch *gdbarch = target_thread_architecture (lp->ptid);
  1379. thread_info *thread = find_thread_ptid (linux_target, lp->ptid);
  1380. int syscall_number = (int) gdbarch_get_syscall_number (gdbarch, thread);
  1381. if (stopping)
  1382. {
  1383. /* If we're stopping threads, there's a SIGSTOP pending, which
  1384. makes it so that the LWP reports an immediate syscall return,
  1385. followed by the SIGSTOP. Skip seeing that "return" using
  1386. PTRACE_CONT directly, and let stop_wait_callback collect the
  1387. SIGSTOP. Later when the thread is resumed, a new syscall
  1388. entry event. If we didn't do this (and returned 0), we'd
  1389. leave a syscall entry pending, and our caller, by using
  1390. PTRACE_CONT to collect the SIGSTOP, skips the syscall return
  1391. itself. Later, when the user re-resumes this LWP, we'd see
  1392. another syscall entry event and we'd mistake it for a return.
  1393. If stop_wait_callback didn't force the SIGSTOP out of the LWP
  1394. (leaving immediately with LWP->signalled set, without issuing
  1395. a PTRACE_CONT), it would still be problematic to leave this
  1396. syscall enter pending, as later when the thread is resumed,
  1397. it would then see the same syscall exit mentioned above,
  1398. followed by the delayed SIGSTOP, while the syscall didn't
  1399. actually get to execute. It seems it would be even more
  1400. confusing to the user. */
  1401. linux_nat_debug_printf
  1402. ("ignoring syscall %d for LWP %ld (stopping threads), resuming with "
  1403. "PTRACE_CONT for SIGSTOP", syscall_number, lp->ptid.lwp ());
  1404. lp->syscall_state = TARGET_WAITKIND_IGNORE;
  1405. ptrace (PTRACE_CONT, lp->ptid.lwp (), 0, 0);
  1406. lp->stopped = 0;
  1407. return 1;
  1408. }
  1409. /* Always update the entry/return state, even if this particular
  1410. syscall isn't interesting to the core now. In async mode,
  1411. the user could install a new catchpoint for this syscall
  1412. between syscall enter/return, and we'll need to know to
  1413. report a syscall return if that happens. */
  1414. lp->syscall_state = (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
  1415. ? TARGET_WAITKIND_SYSCALL_RETURN
  1416. : TARGET_WAITKIND_SYSCALL_ENTRY);
  1417. if (catch_syscall_enabled ())
  1418. {
  1419. if (catching_syscall_number (syscall_number))
  1420. {
  1421. /* Alright, an event to report. */
  1422. if (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY)
  1423. ourstatus->set_syscall_entry (syscall_number);
  1424. else if (lp->syscall_state == TARGET_WAITKIND_SYSCALL_RETURN)
  1425. ourstatus->set_syscall_return (syscall_number);
  1426. else
  1427. gdb_assert_not_reached ("unexpected syscall state");
  1428. linux_nat_debug_printf
  1429. ("stopping for %s of syscall %d for LWP %ld",
  1430. (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
  1431. ? "entry" : "return"), syscall_number, lp->ptid.lwp ());
  1432. return 0;
  1433. }
  1434. linux_nat_debug_printf
  1435. ("ignoring %s of syscall %d for LWP %ld",
  1436. (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
  1437. ? "entry" : "return"), syscall_number, lp->ptid.lwp ());
  1438. }
  1439. else
  1440. {
  1441. /* If we had been syscall tracing, and hence used PT_SYSCALL
  1442. before on this LWP, it could happen that the user removes all
  1443. syscall catchpoints before we get to process this event.
  1444. There are two noteworthy issues here:
  1445. - When stopped at a syscall entry event, resuming with
  1446. PT_STEP still resumes executing the syscall and reports a
  1447. syscall return.
  1448. - Only PT_SYSCALL catches syscall enters. If we last
  1449. single-stepped this thread, then this event can't be a
  1450. syscall enter. If we last single-stepped this thread, this
  1451. has to be a syscall exit.
  1452. The points above mean that the next resume, be it PT_STEP or
  1453. PT_CONTINUE, can not trigger a syscall trace event. */
  1454. linux_nat_debug_printf
  1455. ("caught syscall event with no syscall catchpoints. %d for LWP %ld, "
  1456. "ignoring", syscall_number, lp->ptid.lwp ());
  1457. lp->syscall_state = TARGET_WAITKIND_IGNORE;
  1458. }
  1459. /* The core isn't interested in this event. For efficiency, avoid
  1460. stopping all threads only to have the core resume them all again.
  1461. Since we're not stopping threads, if we're still syscall tracing
  1462. and not stepping, we can't use PTRACE_CONT here, as we'd miss any
  1463. subsequent syscall. Simply resume using the inf-ptrace layer,
  1464. which knows when to use PT_SYSCALL or PT_CONTINUE. */
  1465. linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
  1466. return 1;
  1467. }
  1468. /* Handle a GNU/Linux extended wait response. If we see a clone
  1469. event, we need to add the new LWP to our list (and not report the
  1470. trap to higher layers). This function returns non-zero if the
  1471. event should be ignored and we should wait again. If STOPPING is
  1472. true, the new LWP remains stopped, otherwise it is continued. */
  1473. static int
  1474. linux_handle_extended_wait (struct lwp_info *lp, int status)
  1475. {
  1476. int pid = lp->ptid.lwp ();
  1477. struct target_waitstatus *ourstatus = &lp->waitstatus;
  1478. int event = linux_ptrace_get_extended_event (status);
  1479. /* All extended events we currently use are mid-syscall. Only
  1480. PTRACE_EVENT_STOP is delivered more like a signal-stop, but
  1481. you have to be using PTRACE_SEIZE to get that. */
  1482. lp->syscall_state = TARGET_WAITKIND_SYSCALL_ENTRY;
  1483. if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK
  1484. || event == PTRACE_EVENT_CLONE)
  1485. {
  1486. unsigned long new_pid;
  1487. int ret;
  1488. ptrace (PTRACE_GETEVENTMSG, pid, 0, &new_pid);
  1489. /* If we haven't already seen the new PID stop, wait for it now. */
  1490. if (! pull_pid_from_list (&stopped_pids, new_pid, &status))
  1491. {
  1492. /* The new child has a pending SIGSTOP. We can't affect it until it
  1493. hits the SIGSTOP, but we're already attached. */
  1494. ret = my_waitpid (new_pid, &status, __WALL);
  1495. if (ret == -1)
  1496. perror_with_name (_("waiting for new child"));
  1497. else if (ret != new_pid)
  1498. internal_error (__FILE__, __LINE__,
  1499. _("wait returned unexpected PID %d"), ret);
  1500. else if (!WIFSTOPPED (status))
  1501. internal_error (__FILE__, __LINE__,
  1502. _("wait returned unexpected status 0x%x"), status);
  1503. }
  1504. ptid_t child_ptid (new_pid, new_pid);
  1505. if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK)
  1506. {
  1507. open_proc_mem_file (child_ptid);
  1508. /* The arch-specific native code may need to know about new
  1509. forks even if those end up never mapped to an
  1510. inferior. */
  1511. linux_target->low_new_fork (lp, new_pid);
  1512. }
  1513. else if (event == PTRACE_EVENT_CLONE)
  1514. {
  1515. linux_target->low_new_clone (lp, new_pid);
  1516. }
  1517. if (event == PTRACE_EVENT_FORK
  1518. && linux_fork_checkpointing_p (lp->ptid.pid ()))
  1519. {
  1520. /* Handle checkpointing by linux-fork.c here as a special
  1521. case. We don't want the follow-fork-mode or 'catch fork'
  1522. to interfere with this. */
  1523. /* This won't actually modify the breakpoint list, but will
  1524. physically remove the breakpoints from the child. */
  1525. detach_breakpoints (ptid_t (new_pid, new_pid));
  1526. /* Retain child fork in ptrace (stopped) state. */
  1527. if (!find_fork_pid (new_pid))
  1528. add_fork (new_pid);
  1529. /* Report as spurious, so that infrun doesn't want to follow
  1530. this fork. We're actually doing an infcall in
  1531. linux-fork.c. */
  1532. ourstatus->set_spurious ();
  1533. /* Report the stop to the core. */
  1534. return 0;
  1535. }
  1536. if (event == PTRACE_EVENT_FORK)
  1537. ourstatus->set_forked (child_ptid);
  1538. else if (event == PTRACE_EVENT_VFORK)
  1539. ourstatus->set_vforked (child_ptid);
  1540. else if (event == PTRACE_EVENT_CLONE)
  1541. {
  1542. struct lwp_info *new_lp;
  1543. ourstatus->set_ignore ();
  1544. linux_nat_debug_printf
  1545. ("Got clone event from LWP %d, new child is LWP %ld", pid, new_pid);
  1546. new_lp = add_lwp (ptid_t (lp->ptid.pid (), new_pid));
  1547. new_lp->stopped = 1;
  1548. new_lp->resumed = 1;
  1549. /* If the thread_db layer is active, let it record the user
  1550. level thread id and status, and add the thread to GDB's
  1551. list. */
  1552. if (!thread_db_notice_clone (lp->ptid, new_lp->ptid))
  1553. {
  1554. /* The process is not using thread_db. Add the LWP to
  1555. GDB's list. */
  1556. target_post_attach (new_lp->ptid.lwp ());
  1557. add_thread (linux_target, new_lp->ptid);
  1558. }
  1559. /* Even if we're stopping the thread for some reason
  1560. internal to this module, from the perspective of infrun
  1561. and the user/frontend, this new thread is running until
  1562. it next reports a stop. */
  1563. set_running (linux_target, new_lp->ptid, true);
  1564. set_executing (linux_target, new_lp->ptid, true);
  1565. if (WSTOPSIG (status) != SIGSTOP)
  1566. {
  1567. /* This can happen if someone starts sending signals to
  1568. the new thread before it gets a chance to run, which
  1569. have a lower number than SIGSTOP (e.g. SIGUSR1).
  1570. This is an unlikely case, and harder to handle for
  1571. fork / vfork than for clone, so we do not try - but
  1572. we handle it for clone events here. */
  1573. new_lp->signalled = 1;
  1574. /* We created NEW_LP so it cannot yet contain STATUS. */
  1575. gdb_assert (new_lp->status == 0);
  1576. /* Save the wait status to report later. */
  1577. linux_nat_debug_printf
  1578. ("waitpid of new LWP %ld, saving status %s",
  1579. (long) new_lp->ptid.lwp (), status_to_str (status).c_str ());
  1580. new_lp->status = status;
  1581. }
  1582. else if (report_thread_events)
  1583. {
  1584. new_lp->waitstatus.set_thread_created ();
  1585. new_lp->status = status;
  1586. }
  1587. return 1;
  1588. }
  1589. return 0;
  1590. }
  1591. if (event == PTRACE_EVENT_EXEC)
  1592. {
  1593. linux_nat_debug_printf ("Got exec event from LWP %ld", lp->ptid.lwp ());
  1594. /* Close the previous /proc/PID/mem file for this inferior,
  1595. which was using the address space which is now gone.
  1596. Reading/writing from this file would return 0/EOF. */
  1597. close_proc_mem_file (lp->ptid.pid ());
  1598. /* Open a new file for the new address space. */
  1599. open_proc_mem_file (lp->ptid);
  1600. ourstatus->set_execd
  1601. (make_unique_xstrdup (linux_proc_pid_to_exec_file (pid)));
  1602. /* The thread that execed must have been resumed, but, when a
  1603. thread execs, it changes its tid to the tgid, and the old
  1604. tgid thread might have not been resumed. */
  1605. lp->resumed = 1;
  1606. return 0;
  1607. }
  1608. if (event == PTRACE_EVENT_VFORK_DONE)
  1609. {
  1610. linux_nat_debug_printf
  1611. ("Got PTRACE_EVENT_VFORK_DONE from LWP %ld",
  1612. lp->ptid.lwp ());
  1613. ourstatus->set_vfork_done ();
  1614. return 0;
  1615. }
  1616. internal_error (__FILE__, __LINE__,
  1617. _("unknown ptrace event %d"), event);
  1618. }
  1619. /* Suspend waiting for a signal. We're mostly interested in
  1620. SIGCHLD/SIGINT. */
  1621. static void
  1622. wait_for_signal ()
  1623. {
  1624. linux_nat_debug_printf ("about to sigsuspend");
  1625. sigsuspend (&suspend_mask);
  1626. /* If the quit flag is set, it means that the user pressed Ctrl-C
  1627. and we're debugging a process that is running on a separate
  1628. terminal, so we must forward the Ctrl-C to the inferior. (If the
  1629. inferior is sharing GDB's terminal, then the Ctrl-C reaches the
  1630. inferior directly.) We must do this here because functions that
  1631. need to block waiting for a signal loop forever until there's an
  1632. event to report before returning back to the event loop. */
  1633. if (!target_terminal::is_ours ())
  1634. {
  1635. if (check_quit_flag ())
  1636. target_pass_ctrlc ();
  1637. }
  1638. }
  1639. /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
  1640. exited. */
  1641. static int
  1642. wait_lwp (struct lwp_info *lp)
  1643. {
  1644. pid_t pid;
  1645. int status = 0;
  1646. int thread_dead = 0;
  1647. sigset_t prev_mask;
  1648. gdb_assert (!lp->stopped);
  1649. gdb_assert (lp->status == 0);
  1650. /* Make sure SIGCHLD is blocked for sigsuspend avoiding a race below. */
  1651. block_child_signals (&prev_mask);
  1652. for (;;)
  1653. {
  1654. pid = my_waitpid (lp->ptid.lwp (), &status, __WALL | WNOHANG);
  1655. if (pid == -1 && errno == ECHILD)
  1656. {
  1657. /* The thread has previously exited. We need to delete it
  1658. now because if this was a non-leader thread execing, we
  1659. won't get an exit event. See comments on exec events at
  1660. the top of the file. */
  1661. thread_dead = 1;
  1662. linux_nat_debug_printf ("%s vanished.",
  1663. lp->ptid.to_string ().c_str ());
  1664. }
  1665. if (pid != 0)
  1666. break;
  1667. /* Bugs 10970, 12702.
  1668. Thread group leader may have exited in which case we'll lock up in
  1669. waitpid if there are other threads, even if they are all zombies too.
  1670. Basically, we're not supposed to use waitpid this way.
  1671. tkill(pid,0) cannot be used here as it gets ESRCH for both
  1672. for zombie and running processes.
  1673. As a workaround, check if we're waiting for the thread group leader and
  1674. if it's a zombie, and avoid calling waitpid if it is.
  1675. This is racy, what if the tgl becomes a zombie right after we check?
  1676. Therefore always use WNOHANG with sigsuspend - it is equivalent to
  1677. waiting waitpid but linux_proc_pid_is_zombie is safe this way. */
  1678. if (lp->ptid.pid () == lp->ptid.lwp ()
  1679. && linux_proc_pid_is_zombie (lp->ptid.lwp ()))
  1680. {
  1681. thread_dead = 1;
  1682. linux_nat_debug_printf ("Thread group leader %s vanished.",
  1683. lp->ptid.to_string ().c_str ());
  1684. break;
  1685. }
  1686. /* Wait for next SIGCHLD and try again. This may let SIGCHLD handlers
  1687. get invoked despite our caller had them intentionally blocked by
  1688. block_child_signals. This is sensitive only to the loop of
  1689. linux_nat_wait_1 and there if we get called my_waitpid gets called
  1690. again before it gets to sigsuspend so we can safely let the handlers
  1691. get executed here. */
  1692. wait_for_signal ();
  1693. }
  1694. restore_child_signals_mask (&prev_mask);
  1695. if (!thread_dead)
  1696. {
  1697. gdb_assert (pid == lp->ptid.lwp ());
  1698. linux_nat_debug_printf ("waitpid %s received %s",
  1699. lp->ptid.to_string ().c_str (),
  1700. status_to_str (status).c_str ());
  1701. /* Check if the thread has exited. */
  1702. if (WIFEXITED (status) || WIFSIGNALED (status))
  1703. {
  1704. if (report_thread_events
  1705. || lp->ptid.pid () == lp->ptid.lwp ())
  1706. {
  1707. linux_nat_debug_printf ("LWP %d exited.", lp->ptid.pid ());
  1708. /* If this is the leader exiting, it means the whole
  1709. process is gone. Store the status to report to the
  1710. core. Store it in lp->waitstatus, because lp->status
  1711. would be ambiguous (W_EXITCODE(0,0) == 0). */
  1712. lp->waitstatus = host_status_to_waitstatus (status);
  1713. return 0;
  1714. }
  1715. thread_dead = 1;
  1716. linux_nat_debug_printf ("%s exited.",
  1717. lp->ptid.to_string ().c_str ());
  1718. }
  1719. }
  1720. if (thread_dead)
  1721. {
  1722. exit_lwp (lp);
  1723. return 0;
  1724. }
  1725. gdb_assert (WIFSTOPPED (status));
  1726. lp->stopped = 1;
  1727. if (lp->must_set_ptrace_flags)
  1728. {
  1729. inferior *inf = find_inferior_pid (linux_target, lp->ptid.pid ());
  1730. int options = linux_nat_ptrace_options (inf->attach_flag);
  1731. linux_enable_event_reporting (lp->ptid.lwp (), options);
  1732. lp->must_set_ptrace_flags = 0;
  1733. }
  1734. /* Handle GNU/Linux's syscall SIGTRAPs. */
  1735. if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP)
  1736. {
  1737. /* No longer need the sysgood bit. The ptrace event ends up
  1738. recorded in lp->waitstatus if we care for it. We can carry
  1739. on handling the event like a regular SIGTRAP from here
  1740. on. */
  1741. status = W_STOPCODE (SIGTRAP);
  1742. if (linux_handle_syscall_trap (lp, 1))
  1743. return wait_lwp (lp);
  1744. }
  1745. else
  1746. {
  1747. /* Almost all other ptrace-stops are known to be outside of system
  1748. calls, with further exceptions in linux_handle_extended_wait. */
  1749. lp->syscall_state = TARGET_WAITKIND_IGNORE;
  1750. }
  1751. /* Handle GNU/Linux's extended waitstatus for trace events. */
  1752. if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP
  1753. && linux_is_extended_waitstatus (status))
  1754. {
  1755. linux_nat_debug_printf ("Handling extended status 0x%06x", status);
  1756. linux_handle_extended_wait (lp, status);
  1757. return 0;
  1758. }
  1759. return status;
  1760. }
  1761. /* Send a SIGSTOP to LP. */
  1762. static int
  1763. stop_callback (struct lwp_info *lp)
  1764. {
  1765. if (!lp->stopped && !lp->signalled)
  1766. {
  1767. int ret;
  1768. linux_nat_debug_printf ("kill %s **<SIGSTOP>**",
  1769. lp->ptid.to_string ().c_str ());
  1770. errno = 0;
  1771. ret = kill_lwp (lp->ptid.lwp (), SIGSTOP);
  1772. linux_nat_debug_printf ("lwp kill %d %s", ret,
  1773. errno ? safe_strerror (errno) : "ERRNO-OK");
  1774. lp->signalled = 1;
  1775. gdb_assert (lp->status == 0);
  1776. }
  1777. return 0;
  1778. }
  1779. /* Request a stop on LWP. */
  1780. void
  1781. linux_stop_lwp (struct lwp_info *lwp)
  1782. {
  1783. stop_callback (lwp);
  1784. }
  1785. /* See linux-nat.h */
  1786. void
  1787. linux_stop_and_wait_all_lwps (void)
  1788. {
  1789. /* Stop all LWP's ... */
  1790. iterate_over_lwps (minus_one_ptid, stop_callback);
  1791. /* ... and wait until all of them have reported back that
  1792. they're no longer running. */
  1793. iterate_over_lwps (minus_one_ptid, stop_wait_callback);
  1794. }
  1795. /* See linux-nat.h */
  1796. void
  1797. linux_unstop_all_lwps (void)
  1798. {
  1799. iterate_over_lwps (minus_one_ptid,
  1800. [] (struct lwp_info *info)
  1801. {
  1802. return resume_stopped_resumed_lwps (info, minus_one_ptid);
  1803. });
  1804. }
  1805. /* Return non-zero if LWP PID has a pending SIGINT. */
  1806. static int
  1807. linux_nat_has_pending_sigint (int pid)
  1808. {
  1809. sigset_t pending, blocked, ignored;
  1810. linux_proc_pending_signals (pid, &pending, &blocked, &ignored);
  1811. if (sigismember (&pending, SIGINT)
  1812. && !sigismember (&ignored, SIGINT))
  1813. return 1;
  1814. return 0;
  1815. }
  1816. /* Set a flag in LP indicating that we should ignore its next SIGINT. */
  1817. static int
  1818. set_ignore_sigint (struct lwp_info *lp)
  1819. {
  1820. /* If a thread has a pending SIGINT, consume it; otherwise, set a
  1821. flag to consume the next one. */
  1822. if (lp->stopped && lp->status != 0 && WIFSTOPPED (lp->status)
  1823. && WSTOPSIG (lp->status) == SIGINT)
  1824. lp->status = 0;
  1825. else
  1826. lp->ignore_sigint = 1;
  1827. return 0;
  1828. }
  1829. /* If LP does not have a SIGINT pending, then clear the ignore_sigint flag.
  1830. This function is called after we know the LWP has stopped; if the LWP
  1831. stopped before the expected SIGINT was delivered, then it will never have
  1832. arrived. Also, if the signal was delivered to a shared queue and consumed
  1833. by a different thread, it will never be delivered to this LWP. */
  1834. static void
  1835. maybe_clear_ignore_sigint (struct lwp_info *lp)
  1836. {
  1837. if (!lp->ignore_sigint)
  1838. return;
  1839. if (!linux_nat_has_pending_sigint (lp->ptid.lwp ()))
  1840. {
  1841. linux_nat_debug_printf ("Clearing bogus flag for %s",
  1842. lp->ptid.to_string ().c_str ());
  1843. lp->ignore_sigint = 0;
  1844. }
  1845. }
  1846. /* Fetch the possible triggered data watchpoint info and store it in
  1847. LP.
  1848. On some archs, like x86, that use debug registers to set
  1849. watchpoints, it's possible that the way to know which watched
  1850. address trapped, is to check the register that is used to select
  1851. which address to watch. Problem is, between setting the watchpoint
  1852. and reading back which data address trapped, the user may change
  1853. the set of watchpoints, and, as a consequence, GDB changes the
  1854. debug registers in the inferior. To avoid reading back a stale
  1855. stopped-data-address when that happens, we cache in LP the fact
  1856. that a watchpoint trapped, and the corresponding data address, as
  1857. soon as we see LP stop with a SIGTRAP. If GDB changes the debug
  1858. registers meanwhile, we have the cached data we can rely on. */
  1859. static int
  1860. check_stopped_by_watchpoint (struct lwp_info *lp)
  1861. {
  1862. scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
  1863. inferior_ptid = lp->ptid;
  1864. if (linux_target->low_stopped_by_watchpoint ())
  1865. {
  1866. lp->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
  1867. lp->stopped_data_address_p
  1868. = linux_target->low_stopped_data_address (&lp->stopped_data_address);
  1869. }
  1870. return lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
  1871. }
  1872. /* Returns true if the LWP had stopped for a watchpoint. */
  1873. bool
  1874. linux_nat_target::stopped_by_watchpoint ()
  1875. {
  1876. struct lwp_info *lp = find_lwp_pid (inferior_ptid);
  1877. gdb_assert (lp != NULL);
  1878. return lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
  1879. }
  1880. bool
  1881. linux_nat_target::stopped_data_address (CORE_ADDR *addr_p)
  1882. {
  1883. struct lwp_info *lp = find_lwp_pid (inferior_ptid);
  1884. gdb_assert (lp != NULL);
  1885. *addr_p = lp->stopped_data_address;
  1886. return lp->stopped_data_address_p;
  1887. }
  1888. /* Commonly any breakpoint / watchpoint generate only SIGTRAP. */
  1889. bool
  1890. linux_nat_target::low_status_is_event (int status)
  1891. {
  1892. return WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP;
  1893. }
  1894. /* Wait until LP is stopped. */
  1895. static int
  1896. stop_wait_callback (struct lwp_info *lp)
  1897. {
  1898. inferior *inf = find_inferior_ptid (linux_target, lp->ptid);
  1899. /* If this is a vfork parent, bail out, it is not going to report
  1900. any SIGSTOP until the vfork is done with. */
  1901. if (inf->vfork_child != NULL)
  1902. return 0;
  1903. if (!lp->stopped)
  1904. {
  1905. int status;
  1906. status = wait_lwp (lp);
  1907. if (status == 0)
  1908. return 0;
  1909. if (lp->ignore_sigint && WIFSTOPPED (status)
  1910. && WSTOPSIG (status) == SIGINT)
  1911. {
  1912. lp->ignore_sigint = 0;
  1913. errno = 0;
  1914. ptrace (PTRACE_CONT, lp->ptid.lwp (), 0, 0);
  1915. lp->stopped = 0;
  1916. linux_nat_debug_printf
  1917. ("PTRACE_CONT %s, 0, 0 (%s) (discarding SIGINT)",
  1918. lp->ptid.to_string ().c_str (),
  1919. errno ? safe_strerror (errno) : "OK");
  1920. return stop_wait_callback (lp);
  1921. }
  1922. maybe_clear_ignore_sigint (lp);
  1923. if (WSTOPSIG (status) != SIGSTOP)
  1924. {
  1925. /* The thread was stopped with a signal other than SIGSTOP. */
  1926. linux_nat_debug_printf ("Pending event %s in %s",
  1927. status_to_str ((int) status).c_str (),
  1928. lp->ptid.to_string ().c_str ());
  1929. /* Save the sigtrap event. */
  1930. lp->status = status;
  1931. gdb_assert (lp->signalled);
  1932. save_stop_reason (lp);
  1933. }
  1934. else
  1935. {
  1936. /* We caught the SIGSTOP that we intended to catch. */
  1937. linux_nat_debug_printf ("Expected SIGSTOP caught for %s.",
  1938. lp->ptid.to_string ().c_str ());
  1939. lp->signalled = 0;
  1940. /* If we are waiting for this stop so we can report the thread
  1941. stopped then we need to record this status. Otherwise, we can
  1942. now discard this stop event. */
  1943. if (lp->last_resume_kind == resume_stop)
  1944. {
  1945. lp->status = status;
  1946. save_stop_reason (lp);
  1947. }
  1948. }
  1949. }
  1950. return 0;
  1951. }
  1952. /* Return non-zero if LP has a wait status pending. Discard the
  1953. pending event and resume the LWP if the event that originally
  1954. caused the stop became uninteresting. */
  1955. static int
  1956. status_callback (struct lwp_info *lp)
  1957. {
  1958. /* Only report a pending wait status if we pretend that this has
  1959. indeed been resumed. */
  1960. if (!lp->resumed)
  1961. return 0;
  1962. if (!lwp_status_pending_p (lp))
  1963. return 0;
  1964. if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
  1965. || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
  1966. {
  1967. struct regcache *regcache = get_thread_regcache (linux_target, lp->ptid);
  1968. CORE_ADDR pc;
  1969. int discard = 0;
  1970. pc = regcache_read_pc (regcache);
  1971. if (pc != lp->stop_pc)
  1972. {
  1973. linux_nat_debug_printf ("PC of %s changed. was=%s, now=%s",
  1974. lp->ptid.to_string ().c_str (),
  1975. paddress (target_gdbarch (), lp->stop_pc),
  1976. paddress (target_gdbarch (), pc));
  1977. discard = 1;
  1978. }
  1979. #if !USE_SIGTRAP_SIGINFO
  1980. else if (!breakpoint_inserted_here_p (regcache->aspace (), pc))
  1981. {
  1982. linux_nat_debug_printf ("previous breakpoint of %s, at %s gone",
  1983. lp->ptid.to_string ().c_str (),
  1984. paddress (target_gdbarch (), lp->stop_pc));
  1985. discard = 1;
  1986. }
  1987. #endif
  1988. if (discard)
  1989. {
  1990. linux_nat_debug_printf ("pending event of %s cancelled.",
  1991. lp->ptid.to_string ().c_str ());
  1992. lp->status = 0;
  1993. linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
  1994. return 0;
  1995. }
  1996. }
  1997. return 1;
  1998. }
  1999. /* Count the LWP's that have had events. */
  2000. static int
  2001. count_events_callback (struct lwp_info *lp, int *count)
  2002. {
  2003. gdb_assert (count != NULL);
  2004. /* Select only resumed LWPs that have an event pending. */
  2005. if (lp->resumed && lwp_status_pending_p (lp))
  2006. (*count)++;
  2007. return 0;
  2008. }
  2009. /* Select the LWP (if any) that is currently being single-stepped. */
  2010. static int
  2011. select_singlestep_lwp_callback (struct lwp_info *lp)
  2012. {
  2013. if (lp->last_resume_kind == resume_step
  2014. && lp->status != 0)
  2015. return 1;
  2016. else
  2017. return 0;
  2018. }
  2019. /* Returns true if LP has a status pending. */
  2020. static int
  2021. lwp_status_pending_p (struct lwp_info *lp)
  2022. {
  2023. /* We check for lp->waitstatus in addition to lp->status, because we
  2024. can have pending process exits recorded in lp->status and
  2025. W_EXITCODE(0,0) happens to be 0. */
  2026. return lp->status != 0 || lp->waitstatus.kind () != TARGET_WAITKIND_IGNORE;
  2027. }
  2028. /* Select the Nth LWP that has had an event. */
  2029. static int
  2030. select_event_lwp_callback (struct lwp_info *lp, int *selector)
  2031. {
  2032. gdb_assert (selector != NULL);
  2033. /* Select only resumed LWPs that have an event pending. */
  2034. if (lp->resumed && lwp_status_pending_p (lp))
  2035. if ((*selector)-- == 0)
  2036. return 1;
  2037. return 0;
  2038. }
  2039. /* Called when the LWP stopped for a signal/trap. If it stopped for a
  2040. trap check what caused it (breakpoint, watchpoint, trace, etc.),
  2041. and save the result in the LWP's stop_reason field. If it stopped
  2042. for a breakpoint, decrement the PC if necessary on the lwp's
  2043. architecture. */
  2044. static void
  2045. save_stop_reason (struct lwp_info *lp)
  2046. {
  2047. struct regcache *regcache;
  2048. struct gdbarch *gdbarch;
  2049. CORE_ADDR pc;
  2050. CORE_ADDR sw_bp_pc;
  2051. #if USE_SIGTRAP_SIGINFO
  2052. siginfo_t siginfo;
  2053. #endif
  2054. gdb_assert (lp->stop_reason == TARGET_STOPPED_BY_NO_REASON);
  2055. gdb_assert (lp->status != 0);
  2056. if (!linux_target->low_status_is_event (lp->status))
  2057. return;
  2058. regcache = get_thread_regcache (linux_target, lp->ptid);
  2059. gdbarch = regcache->arch ();
  2060. pc = regcache_read_pc (regcache);
  2061. sw_bp_pc = pc - gdbarch_decr_pc_after_break (gdbarch);
  2062. #if USE_SIGTRAP_SIGINFO
  2063. if (linux_nat_get_siginfo (lp->ptid, &siginfo))
  2064. {
  2065. if (siginfo.si_signo == SIGTRAP)
  2066. {
  2067. if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code)
  2068. && GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code))
  2069. {
  2070. /* The si_code is ambiguous on this arch -- check debug
  2071. registers. */
  2072. if (!check_stopped_by_watchpoint (lp))
  2073. lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
  2074. }
  2075. else if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code))
  2076. {
  2077. /* If we determine the LWP stopped for a SW breakpoint,
  2078. trust it. Particularly don't check watchpoint
  2079. registers, because, at least on s390, we'd find
  2080. stopped-by-watchpoint as long as there's a watchpoint
  2081. set. */
  2082. lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
  2083. }
  2084. else if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code))
  2085. {
  2086. /* This can indicate either a hardware breakpoint or
  2087. hardware watchpoint. Check debug registers. */
  2088. if (!check_stopped_by_watchpoint (lp))
  2089. lp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
  2090. }
  2091. else if (siginfo.si_code == TRAP_TRACE)
  2092. {
  2093. linux_nat_debug_printf ("%s stopped by trace",
  2094. lp->ptid.to_string ().c_str ());
  2095. /* We may have single stepped an instruction that
  2096. triggered a watchpoint. In that case, on some
  2097. architectures (such as x86), instead of TRAP_HWBKPT,
  2098. si_code indicates TRAP_TRACE, and we need to check
  2099. the debug registers separately. */
  2100. check_stopped_by_watchpoint (lp);
  2101. }
  2102. }
  2103. }
  2104. #else
  2105. if ((!lp->step || lp->stop_pc == sw_bp_pc)
  2106. && software_breakpoint_inserted_here_p (regcache->aspace (),
  2107. sw_bp_pc))
  2108. {
  2109. /* The LWP was either continued, or stepped a software
  2110. breakpoint instruction. */
  2111. lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
  2112. }
  2113. if (hardware_breakpoint_inserted_here_p (regcache->aspace (), pc))
  2114. lp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
  2115. if (lp->stop_reason == TARGET_STOPPED_BY_NO_REASON)
  2116. check_stopped_by_watchpoint (lp);
  2117. #endif
  2118. if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT)
  2119. {
  2120. linux_nat_debug_printf ("%s stopped by software breakpoint",
  2121. lp->ptid.to_string ().c_str ());
  2122. /* Back up the PC if necessary. */
  2123. if (pc != sw_bp_pc)
  2124. regcache_write_pc (regcache, sw_bp_pc);
  2125. /* Update this so we record the correct stop PC below. */
  2126. pc = sw_bp_pc;
  2127. }
  2128. else if (lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
  2129. {
  2130. linux_nat_debug_printf ("%s stopped by hardware breakpoint",
  2131. lp->ptid.to_string ().c_str ());
  2132. }
  2133. else if (lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
  2134. {
  2135. linux_nat_debug_printf ("%s stopped by hardware watchpoint",
  2136. lp->ptid.to_string ().c_str ());
  2137. }
  2138. lp->stop_pc = pc;
  2139. }
  2140. /* Returns true if the LWP had stopped for a software breakpoint. */
  2141. bool
  2142. linux_nat_target::stopped_by_sw_breakpoint ()
  2143. {
  2144. struct lwp_info *lp = find_lwp_pid (inferior_ptid);
  2145. gdb_assert (lp != NULL);
  2146. return lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
  2147. }
  2148. /* Implement the supports_stopped_by_sw_breakpoint method. */
  2149. bool
  2150. linux_nat_target::supports_stopped_by_sw_breakpoint ()
  2151. {
  2152. return USE_SIGTRAP_SIGINFO;
  2153. }
  2154. /* Returns true if the LWP had stopped for a hardware
  2155. breakpoint/watchpoint. */
  2156. bool
  2157. linux_nat_target::stopped_by_hw_breakpoint ()
  2158. {
  2159. struct lwp_info *lp = find_lwp_pid (inferior_ptid);
  2160. gdb_assert (lp != NULL);
  2161. return lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
  2162. }
  2163. /* Implement the supports_stopped_by_hw_breakpoint method. */
  2164. bool
  2165. linux_nat_target::supports_stopped_by_hw_breakpoint ()
  2166. {
  2167. return USE_SIGTRAP_SIGINFO;
  2168. }
  2169. /* Select one LWP out of those that have events pending. */
  2170. static void
  2171. select_event_lwp (ptid_t filter, struct lwp_info **orig_lp, int *status)
  2172. {
  2173. int num_events = 0;
  2174. int random_selector;
  2175. struct lwp_info *event_lp = NULL;
  2176. /* Record the wait status for the original LWP. */
  2177. (*orig_lp)->status = *status;
  2178. /* In all-stop, give preference to the LWP that is being
  2179. single-stepped. There will be at most one, and it will be the
  2180. LWP that the core is most interested in. If we didn't do this,
  2181. then we'd have to handle pending step SIGTRAPs somehow in case
  2182. the core later continues the previously-stepped thread, as
  2183. otherwise we'd report the pending SIGTRAP then, and the core, not
  2184. having stepped the thread, wouldn't understand what the trap was
  2185. for, and therefore would report it to the user as a random
  2186. signal. */
  2187. if (!target_is_non_stop_p ())
  2188. {
  2189. event_lp = iterate_over_lwps (filter, select_singlestep_lwp_callback);
  2190. if (event_lp != NULL)
  2191. {
  2192. linux_nat_debug_printf ("Select single-step %s",
  2193. event_lp->ptid.to_string ().c_str ());
  2194. }
  2195. }
  2196. if (event_lp == NULL)
  2197. {
  2198. /* Pick one at random, out of those which have had events. */
  2199. /* First see how many events we have. */
  2200. iterate_over_lwps (filter,
  2201. [&] (struct lwp_info *info)
  2202. {
  2203. return count_events_callback (info, &num_events);
  2204. });
  2205. gdb_assert (num_events > 0);
  2206. /* Now randomly pick a LWP out of those that have had
  2207. events. */
  2208. random_selector = (int)
  2209. ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
  2210. if (num_events > 1)
  2211. linux_nat_debug_printf ("Found %d events, selecting #%d",
  2212. num_events, random_selector);
  2213. event_lp
  2214. = (iterate_over_lwps
  2215. (filter,
  2216. [&] (struct lwp_info *info)
  2217. {
  2218. return select_event_lwp_callback (info,
  2219. &random_selector);
  2220. }));
  2221. }
  2222. if (event_lp != NULL)
  2223. {
  2224. /* Switch the event LWP. */
  2225. *orig_lp = event_lp;
  2226. *status = event_lp->status;
  2227. }
  2228. /* Flush the wait status for the event LWP. */
  2229. (*orig_lp)->status = 0;
  2230. }
  2231. /* Return non-zero if LP has been resumed. */
  2232. static int
  2233. resumed_callback (struct lwp_info *lp)
  2234. {
  2235. return lp->resumed;
  2236. }
  2237. /* Check if we should go on and pass this event to common code.
  2238. If so, save the status to the lwp_info structure associated to LWPID. */
  2239. static void
  2240. linux_nat_filter_event (int lwpid, int status)
  2241. {
  2242. struct lwp_info *lp;
  2243. int event = linux_ptrace_get_extended_event (status);
  2244. lp = find_lwp_pid (ptid_t (lwpid));
  2245. /* Check for events reported by anything not in our LWP list. */
  2246. if (lp == nullptr)
  2247. {
  2248. if (WIFSTOPPED (status))
  2249. {
  2250. if (WSTOPSIG (status) == SIGTRAP && event == PTRACE_EVENT_EXEC)
  2251. {
  2252. /* A non-leader thread exec'ed after we've seen the
  2253. leader zombie, and removed it from our lists (in
  2254. check_zombie_leaders). The non-leader thread changes
  2255. its tid to the tgid. */
  2256. linux_nat_debug_printf
  2257. ("Re-adding thread group leader LWP %d after exec.",
  2258. lwpid);
  2259. lp = add_lwp (ptid_t (lwpid, lwpid));
  2260. lp->stopped = 1;
  2261. lp->resumed = 1;
  2262. add_thread (linux_target, lp->ptid);
  2263. }
  2264. else
  2265. {
  2266. /* A process we are controlling has forked and the new
  2267. child's stop was reported to us by the kernel. Save
  2268. its PID and go back to waiting for the fork event to
  2269. be reported - the stopped process might be returned
  2270. from waitpid before or after the fork event is. */
  2271. linux_nat_debug_printf
  2272. ("Saving LWP %d status %s in stopped_pids list",
  2273. lwpid, status_to_str (status).c_str ());
  2274. add_to_pid_list (&stopped_pids, lwpid, status);
  2275. }
  2276. }
  2277. else
  2278. {
  2279. /* Don't report an event for the exit of an LWP not in our
  2280. list, i.e. not part of any inferior we're debugging.
  2281. This can happen if we detach from a program we originally
  2282. forked and then it exits. However, note that we may have
  2283. earlier deleted a leader of an inferior we're debugging,
  2284. in check_zombie_leaders. Re-add it back here if so. */
  2285. for (inferior *inf : all_inferiors (linux_target))
  2286. {
  2287. if (inf->pid == lwpid)
  2288. {
  2289. linux_nat_debug_printf
  2290. ("Re-adding thread group leader LWP %d after exit.",
  2291. lwpid);
  2292. lp = add_lwp (ptid_t (lwpid, lwpid));
  2293. lp->resumed = 1;
  2294. add_thread (linux_target, lp->ptid);
  2295. break;
  2296. }
  2297. }
  2298. }
  2299. if (lp == nullptr)
  2300. return;
  2301. }
  2302. /* This LWP is stopped now. (And if dead, this prevents it from
  2303. ever being continued.) */
  2304. lp->stopped = 1;
  2305. if (WIFSTOPPED (status) && lp->must_set_ptrace_flags)
  2306. {
  2307. inferior *inf = find_inferior_pid (linux_target, lp->ptid.pid ());
  2308. int options = linux_nat_ptrace_options (inf->attach_flag);
  2309. linux_enable_event_reporting (lp->ptid.lwp (), options);
  2310. lp->must_set_ptrace_flags = 0;
  2311. }
  2312. /* Handle GNU/Linux's syscall SIGTRAPs. */
  2313. if (WIFSTOPPED (status) && WSTOPSIG (status) == SYSCALL_SIGTRAP)
  2314. {
  2315. /* No longer need the sysgood bit. The ptrace event ends up
  2316. recorded in lp->waitstatus if we care for it. We can carry
  2317. on handling the event like a regular SIGTRAP from here
  2318. on. */
  2319. status = W_STOPCODE (SIGTRAP);
  2320. if (linux_handle_syscall_trap (lp, 0))
  2321. return;
  2322. }
  2323. else
  2324. {
  2325. /* Almost all other ptrace-stops are known to be outside of system
  2326. calls, with further exceptions in linux_handle_extended_wait. */
  2327. lp->syscall_state = TARGET_WAITKIND_IGNORE;
  2328. }
  2329. /* Handle GNU/Linux's extended waitstatus for trace events. */
  2330. if (WIFSTOPPED (status) && WSTOPSIG (status) == SIGTRAP
  2331. && linux_is_extended_waitstatus (status))
  2332. {
  2333. linux_nat_debug_printf ("Handling extended status 0x%06x", status);
  2334. if (linux_handle_extended_wait (lp, status))
  2335. return;
  2336. }
  2337. /* Check if the thread has exited. */
  2338. if (WIFEXITED (status) || WIFSIGNALED (status))
  2339. {
  2340. if (!report_thread_events && !is_leader (lp))
  2341. {
  2342. linux_nat_debug_printf ("%s exited.",
  2343. lp->ptid.to_string ().c_str ());
  2344. /* If this was not the leader exiting, then the exit signal
  2345. was not the end of the debugged application and should be
  2346. ignored. */
  2347. exit_lwp (lp);
  2348. return;
  2349. }
  2350. /* Note that even if the leader was ptrace-stopped, it can still
  2351. exit, if e.g., some other thread brings down the whole
  2352. process (calls `exit'). So don't assert that the lwp is
  2353. resumed. */
  2354. linux_nat_debug_printf ("LWP %ld exited (resumed=%d)",
  2355. lp->ptid.lwp (), lp->resumed);
  2356. /* Dead LWP's aren't expected to reported a pending sigstop. */
  2357. lp->signalled = 0;
  2358. /* Store the pending event in the waitstatus, because
  2359. W_EXITCODE(0,0) == 0. */
  2360. lp->waitstatus = host_status_to_waitstatus (status);
  2361. return;
  2362. }
  2363. /* Make sure we don't report a SIGSTOP that we sent ourselves in
  2364. an attempt to stop an LWP. */
  2365. if (lp->signalled
  2366. && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
  2367. {
  2368. lp->signalled = 0;
  2369. if (lp->last_resume_kind == resume_stop)
  2370. {
  2371. linux_nat_debug_printf ("resume_stop SIGSTOP caught for %s.",
  2372. lp->ptid.to_string ().c_str ());
  2373. }
  2374. else
  2375. {
  2376. /* This is a delayed SIGSTOP. Filter out the event. */
  2377. linux_nat_debug_printf
  2378. ("%s %s, 0, 0 (discard delayed SIGSTOP)",
  2379. lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
  2380. lp->ptid.to_string ().c_str ());
  2381. linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
  2382. gdb_assert (lp->resumed);
  2383. return;
  2384. }
  2385. }
  2386. /* Make sure we don't report a SIGINT that we have already displayed
  2387. for another thread. */
  2388. if (lp->ignore_sigint
  2389. && WIFSTOPPED (status) && WSTOPSIG (status) == SIGINT)
  2390. {
  2391. linux_nat_debug_printf ("Delayed SIGINT caught for %s.",
  2392. lp->ptid.to_string ().c_str ());
  2393. /* This is a delayed SIGINT. */
  2394. lp->ignore_sigint = 0;
  2395. linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
  2396. linux_nat_debug_printf ("%s %s, 0, 0 (discard SIGINT)",
  2397. lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
  2398. lp->ptid.to_string ().c_str ());
  2399. gdb_assert (lp->resumed);
  2400. /* Discard the event. */
  2401. return;
  2402. }
  2403. /* Don't report signals that GDB isn't interested in, such as
  2404. signals that are neither printed nor stopped upon. Stopping all
  2405. threads can be a bit time-consuming, so if we want decent
  2406. performance with heavily multi-threaded programs, especially when
  2407. they're using a high frequency timer, we'd better avoid it if we
  2408. can. */
  2409. if (WIFSTOPPED (status))
  2410. {
  2411. enum gdb_signal signo = gdb_signal_from_host (WSTOPSIG (status));
  2412. if (!target_is_non_stop_p ())
  2413. {
  2414. /* Only do the below in all-stop, as we currently use SIGSTOP
  2415. to implement target_stop (see linux_nat_stop) in
  2416. non-stop. */
  2417. if (signo == GDB_SIGNAL_INT && signal_pass_state (signo) == 0)
  2418. {
  2419. /* If ^C/BREAK is typed at the tty/console, SIGINT gets
  2420. forwarded to the entire process group, that is, all LWPs
  2421. will receive it - unless they're using CLONE_THREAD to
  2422. share signals. Since we only want to report it once, we
  2423. mark it as ignored for all LWPs except this one. */
  2424. iterate_over_lwps (ptid_t (lp->ptid.pid ()), set_ignore_sigint);
  2425. lp->ignore_sigint = 0;
  2426. }
  2427. else
  2428. maybe_clear_ignore_sigint (lp);
  2429. }
  2430. /* When using hardware single-step, we need to report every signal.
  2431. Otherwise, signals in pass_mask may be short-circuited
  2432. except signals that might be caused by a breakpoint, or SIGSTOP
  2433. if we sent the SIGSTOP and are waiting for it to arrive. */
  2434. if (!lp->step
  2435. && WSTOPSIG (status) && sigismember (&pass_mask, WSTOPSIG (status))
  2436. && (WSTOPSIG (status) != SIGSTOP
  2437. || !find_thread_ptid (linux_target, lp->ptid)->stop_requested)
  2438. && !linux_wstatus_maybe_breakpoint (status))
  2439. {
  2440. linux_resume_one_lwp (lp, lp->step, signo);
  2441. linux_nat_debug_printf
  2442. ("%s %s, %s (preempt 'handle')",
  2443. lp->step ? "PTRACE_SINGLESTEP" : "PTRACE_CONT",
  2444. lp->ptid.to_string ().c_str (),
  2445. (signo != GDB_SIGNAL_0
  2446. ? strsignal (gdb_signal_to_host (signo)) : "0"));
  2447. return;
  2448. }
  2449. }
  2450. /* An interesting event. */
  2451. gdb_assert (lp);
  2452. lp->status = status;
  2453. save_stop_reason (lp);
  2454. }
  2455. /* Detect zombie thread group leaders, and "exit" them. We can't reap
  2456. their exits until all other threads in the group have exited. */
  2457. static void
  2458. check_zombie_leaders (void)
  2459. {
  2460. for (inferior *inf : all_inferiors ())
  2461. {
  2462. struct lwp_info *leader_lp;
  2463. if (inf->pid == 0)
  2464. continue;
  2465. leader_lp = find_lwp_pid (ptid_t (inf->pid));
  2466. if (leader_lp != NULL
  2467. /* Check if there are other threads in the group, as we may
  2468. have raced with the inferior simply exiting. Note this
  2469. isn't a watertight check. If the inferior is
  2470. multi-threaded and is exiting, it may be we see the
  2471. leader as zombie before we reap all the non-leader
  2472. threads. See comments below. */
  2473. && num_lwps (inf->pid) > 1
  2474. && linux_proc_pid_is_zombie (inf->pid))
  2475. {
  2476. /* A zombie leader in a multi-threaded program can mean one
  2477. of three things:
  2478. #1 - Only the leader exited, not the whole program, e.g.,
  2479. with pthread_exit. Since we can't reap the leader's exit
  2480. status until all other threads are gone and reaped too,
  2481. we want to delete the zombie leader right away, as it
  2482. can't be debugged, we can't read its registers, etc.
  2483. This is the main reason we check for zombie leaders
  2484. disappearing.
  2485. #2 - The whole thread-group/process exited (a group exit,
  2486. via e.g. exit(3), and there is (or will be shortly) an
  2487. exit reported for each thread in the process, and then
  2488. finally an exit for the leader once the non-leaders are
  2489. reaped.
  2490. #3 - There are 3 or more threads in the group, and a
  2491. thread other than the leader exec'd. See comments on
  2492. exec events at the top of the file.
  2493. Ideally we would never delete the leader for case #2.
  2494. Instead, we want to collect the exit status of each
  2495. non-leader thread, and then finally collect the exit
  2496. status of the leader as normal and use its exit code as
  2497. whole-process exit code. Unfortunately, there's no
  2498. race-free way to distinguish cases #1 and #2. We can't
  2499. assume the exit events for the non-leaders threads are
  2500. already pending in the kernel, nor can we assume the
  2501. non-leader threads are in zombie state already. Between
  2502. the leader becoming zombie and the non-leaders exiting
  2503. and becoming zombie themselves, there's a small time
  2504. window, so such a check would be racy. Temporarily
  2505. pausing all threads and checking to see if all threads
  2506. exit or not before re-resuming them would work in the
  2507. case that all threads are running right now, but it
  2508. wouldn't work if some thread is currently already
  2509. ptrace-stopped, e.g., due to scheduler-locking.
  2510. So what we do is we delete the leader anyhow, and then
  2511. later on when we see its exit status, we re-add it back.
  2512. We also make sure that we only report a whole-process
  2513. exit when we see the leader exiting, as opposed to when
  2514. the last LWP in the LWP list exits, which can be a
  2515. non-leader if we deleted the leader here. */
  2516. linux_nat_debug_printf ("Thread group leader %d zombie "
  2517. "(it exited, or another thread execd), "
  2518. "deleting it.",
  2519. inf->pid);
  2520. exit_lwp (leader_lp);
  2521. }
  2522. }
  2523. }
  2524. /* Convenience function that is called when the kernel reports an exit
  2525. event. This decides whether to report the event to GDB as a
  2526. process exit event, a thread exit event, or to suppress the
  2527. event. */
  2528. static ptid_t
  2529. filter_exit_event (struct lwp_info *event_child,
  2530. struct target_waitstatus *ourstatus)
  2531. {
  2532. ptid_t ptid = event_child->ptid;
  2533. if (!is_leader (event_child))
  2534. {
  2535. if (report_thread_events)
  2536. ourstatus->set_thread_exited (0);
  2537. else
  2538. ourstatus->set_ignore ();
  2539. exit_lwp (event_child);
  2540. }
  2541. return ptid;
  2542. }
  2543. static ptid_t
  2544. linux_nat_wait_1 (ptid_t ptid, struct target_waitstatus *ourstatus,
  2545. target_wait_flags target_options)
  2546. {
  2547. sigset_t prev_mask;
  2548. enum resume_kind last_resume_kind;
  2549. struct lwp_info *lp;
  2550. int status;
  2551. linux_nat_debug_printf ("enter");
  2552. /* The first time we get here after starting a new inferior, we may
  2553. not have added it to the LWP list yet - this is the earliest
  2554. moment at which we know its PID. */
  2555. if (ptid.is_pid () && find_lwp_pid (ptid) == nullptr)
  2556. {
  2557. ptid_t lwp_ptid (ptid.pid (), ptid.pid ());
  2558. /* Upgrade the main thread's ptid. */
  2559. thread_change_ptid (linux_target, ptid, lwp_ptid);
  2560. lp = add_initial_lwp (lwp_ptid);
  2561. lp->resumed = 1;
  2562. }
  2563. /* Make sure SIGCHLD is blocked until the sigsuspend below. */
  2564. block_child_signals (&prev_mask);
  2565. /* First check if there is a LWP with a wait status pending. */
  2566. lp = iterate_over_lwps (ptid, status_callback);
  2567. if (lp != NULL)
  2568. {
  2569. linux_nat_debug_printf ("Using pending wait status %s for %s.",
  2570. status_to_str (lp->status).c_str (),
  2571. lp->ptid.to_string ().c_str ());
  2572. }
  2573. /* But if we don't find a pending event, we'll have to wait. Always
  2574. pull all events out of the kernel. We'll randomly select an
  2575. event LWP out of all that have events, to prevent starvation. */
  2576. while (lp == NULL)
  2577. {
  2578. pid_t lwpid;
  2579. /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
  2580. quirks:
  2581. - If the thread group leader exits while other threads in the
  2582. thread group still exist, waitpid(TGID, ...) hangs. That
  2583. waitpid won't return an exit status until the other threads
  2584. in the group are reaped.
  2585. - When a non-leader thread execs, that thread just vanishes
  2586. without reporting an exit (so we'd hang if we waited for it
  2587. explicitly in that case). The exec event is reported to
  2588. the TGID pid. */
  2589. errno = 0;
  2590. lwpid = my_waitpid (-1, &status, __WALL | WNOHANG);
  2591. linux_nat_debug_printf ("waitpid(-1, ...) returned %d, %s",
  2592. lwpid,
  2593. errno ? safe_strerror (errno) : "ERRNO-OK");
  2594. if (lwpid > 0)
  2595. {
  2596. linux_nat_debug_printf ("waitpid %ld received %s",
  2597. (long) lwpid,
  2598. status_to_str (status).c_str ());
  2599. linux_nat_filter_event (lwpid, status);
  2600. /* Retry until nothing comes out of waitpid. A single
  2601. SIGCHLD can indicate more than one child stopped. */
  2602. continue;
  2603. }
  2604. /* Now that we've pulled all events out of the kernel, resume
  2605. LWPs that don't have an interesting event to report. */
  2606. iterate_over_lwps (minus_one_ptid,
  2607. [] (struct lwp_info *info)
  2608. {
  2609. return resume_stopped_resumed_lwps (info, minus_one_ptid);
  2610. });
  2611. /* ... and find an LWP with a status to report to the core, if
  2612. any. */
  2613. lp = iterate_over_lwps (ptid, status_callback);
  2614. if (lp != NULL)
  2615. break;
  2616. /* Check for zombie thread group leaders. Those can't be reaped
  2617. until all other threads in the thread group are. */
  2618. check_zombie_leaders ();
  2619. /* If there are no resumed children left, bail. We'd be stuck
  2620. forever in the sigsuspend call below otherwise. */
  2621. if (iterate_over_lwps (ptid, resumed_callback) == NULL)
  2622. {
  2623. linux_nat_debug_printf ("exit (no resumed LWP)");
  2624. ourstatus->set_no_resumed ();
  2625. restore_child_signals_mask (&prev_mask);
  2626. return minus_one_ptid;
  2627. }
  2628. /* No interesting event to report to the core. */
  2629. if (target_options & TARGET_WNOHANG)
  2630. {
  2631. linux_nat_debug_printf ("exit (ignore)");
  2632. ourstatus->set_ignore ();
  2633. restore_child_signals_mask (&prev_mask);
  2634. return minus_one_ptid;
  2635. }
  2636. /* We shouldn't end up here unless we want to try again. */
  2637. gdb_assert (lp == NULL);
  2638. /* Block until we get an event reported with SIGCHLD. */
  2639. wait_for_signal ();
  2640. }
  2641. gdb_assert (lp);
  2642. status = lp->status;
  2643. lp->status = 0;
  2644. if (!target_is_non_stop_p ())
  2645. {
  2646. /* Now stop all other LWP's ... */
  2647. iterate_over_lwps (minus_one_ptid, stop_callback);
  2648. /* ... and wait until all of them have reported back that
  2649. they're no longer running. */
  2650. iterate_over_lwps (minus_one_ptid, stop_wait_callback);
  2651. }
  2652. /* If we're not waiting for a specific LWP, choose an event LWP from
  2653. among those that have had events. Giving equal priority to all
  2654. LWPs that have had events helps prevent starvation. */
  2655. if (ptid == minus_one_ptid || ptid.is_pid ())
  2656. select_event_lwp (ptid, &lp, &status);
  2657. gdb_assert (lp != NULL);
  2658. /* Now that we've selected our final event LWP, un-adjust its PC if
  2659. it was a software breakpoint, and we can't reliably support the
  2660. "stopped by software breakpoint" stop reason. */
  2661. if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
  2662. && !USE_SIGTRAP_SIGINFO)
  2663. {
  2664. struct regcache *regcache = get_thread_regcache (linux_target, lp->ptid);
  2665. struct gdbarch *gdbarch = regcache->arch ();
  2666. int decr_pc = gdbarch_decr_pc_after_break (gdbarch);
  2667. if (decr_pc != 0)
  2668. {
  2669. CORE_ADDR pc;
  2670. pc = regcache_read_pc (regcache);
  2671. regcache_write_pc (regcache, pc + decr_pc);
  2672. }
  2673. }
  2674. /* We'll need this to determine whether to report a SIGSTOP as
  2675. GDB_SIGNAL_0. Need to take a copy because resume_clear_callback
  2676. clears it. */
  2677. last_resume_kind = lp->last_resume_kind;
  2678. if (!target_is_non_stop_p ())
  2679. {
  2680. /* In all-stop, from the core's perspective, all LWPs are now
  2681. stopped until a new resume action is sent over. */
  2682. iterate_over_lwps (minus_one_ptid, resume_clear_callback);
  2683. }
  2684. else
  2685. {
  2686. resume_clear_callback (lp);
  2687. }
  2688. if (linux_target->low_status_is_event (status))
  2689. {
  2690. linux_nat_debug_printf ("trap ptid is %s.",
  2691. lp->ptid.to_string ().c_str ());
  2692. }
  2693. if (lp->waitstatus.kind () != TARGET_WAITKIND_IGNORE)
  2694. {
  2695. *ourstatus = lp->waitstatus;
  2696. lp->waitstatus.set_ignore ();
  2697. }
  2698. else
  2699. *ourstatus = host_status_to_waitstatus (status);
  2700. linux_nat_debug_printf ("exit");
  2701. restore_child_signals_mask (&prev_mask);
  2702. if (last_resume_kind == resume_stop
  2703. && ourstatus->kind () == TARGET_WAITKIND_STOPPED
  2704. && WSTOPSIG (status) == SIGSTOP)
  2705. {
  2706. /* A thread that has been requested to stop by GDB with
  2707. target_stop, and it stopped cleanly, so report as SIG0. The
  2708. use of SIGSTOP is an implementation detail. */
  2709. ourstatus->set_stopped (GDB_SIGNAL_0);
  2710. }
  2711. if (ourstatus->kind () == TARGET_WAITKIND_EXITED
  2712. || ourstatus->kind () == TARGET_WAITKIND_SIGNALLED)
  2713. lp->core = -1;
  2714. else
  2715. lp->core = linux_common_core_of_thread (lp->ptid);
  2716. if (ourstatus->kind () == TARGET_WAITKIND_EXITED)
  2717. return filter_exit_event (lp, ourstatus);
  2718. return lp->ptid;
  2719. }
  2720. /* Resume LWPs that are currently stopped without any pending status
  2721. to report, but are resumed from the core's perspective. */
  2722. static int
  2723. resume_stopped_resumed_lwps (struct lwp_info *lp, const ptid_t wait_ptid)
  2724. {
  2725. if (!lp->stopped)
  2726. {
  2727. linux_nat_debug_printf ("NOT resuming LWP %s, not stopped",
  2728. lp->ptid.to_string ().c_str ());
  2729. }
  2730. else if (!lp->resumed)
  2731. {
  2732. linux_nat_debug_printf ("NOT resuming LWP %s, not resumed",
  2733. lp->ptid.to_string ().c_str ());
  2734. }
  2735. else if (lwp_status_pending_p (lp))
  2736. {
  2737. linux_nat_debug_printf ("NOT resuming LWP %s, has pending status",
  2738. lp->ptid.to_string ().c_str ());
  2739. }
  2740. else
  2741. {
  2742. struct regcache *regcache = get_thread_regcache (linux_target, lp->ptid);
  2743. struct gdbarch *gdbarch = regcache->arch ();
  2744. try
  2745. {
  2746. CORE_ADDR pc = regcache_read_pc (regcache);
  2747. int leave_stopped = 0;
  2748. /* Don't bother if there's a breakpoint at PC that we'd hit
  2749. immediately, and we're not waiting for this LWP. */
  2750. if (!lp->ptid.matches (wait_ptid))
  2751. {
  2752. if (breakpoint_inserted_here_p (regcache->aspace (), pc))
  2753. leave_stopped = 1;
  2754. }
  2755. if (!leave_stopped)
  2756. {
  2757. linux_nat_debug_printf
  2758. ("resuming stopped-resumed LWP %s at %s: step=%d",
  2759. lp->ptid.to_string ().c_str (), paddress (gdbarch, pc),
  2760. lp->step);
  2761. linux_resume_one_lwp_throw (lp, lp->step, GDB_SIGNAL_0);
  2762. }
  2763. }
  2764. catch (const gdb_exception_error &ex)
  2765. {
  2766. if (!check_ptrace_stopped_lwp_gone (lp))
  2767. throw;
  2768. }
  2769. }
  2770. return 0;
  2771. }
  2772. ptid_t
  2773. linux_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
  2774. target_wait_flags target_options)
  2775. {
  2776. ptid_t event_ptid;
  2777. linux_nat_debug_printf ("[%s], [%s]", ptid.to_string ().c_str (),
  2778. target_options_to_string (target_options).c_str ());
  2779. /* Flush the async file first. */
  2780. if (target_is_async_p ())
  2781. async_file_flush ();
  2782. /* Resume LWPs that are currently stopped without any pending status
  2783. to report, but are resumed from the core's perspective. LWPs get
  2784. in this state if we find them stopping at a time we're not
  2785. interested in reporting the event (target_wait on a
  2786. specific_process, for example, see linux_nat_wait_1), and
  2787. meanwhile the event became uninteresting. Don't bother resuming
  2788. LWPs we're not going to wait for if they'd stop immediately. */
  2789. if (target_is_non_stop_p ())
  2790. iterate_over_lwps (minus_one_ptid,
  2791. [=] (struct lwp_info *info)
  2792. {
  2793. return resume_stopped_resumed_lwps (info, ptid);
  2794. });
  2795. event_ptid = linux_nat_wait_1 (ptid, ourstatus, target_options);
  2796. /* If we requested any event, and something came out, assume there
  2797. may be more. If we requested a specific lwp or process, also
  2798. assume there may be more. */
  2799. if (target_is_async_p ()
  2800. && ((ourstatus->kind () != TARGET_WAITKIND_IGNORE
  2801. && ourstatus->kind () != TARGET_WAITKIND_NO_RESUMED)
  2802. || ptid != minus_one_ptid))
  2803. async_file_mark ();
  2804. return event_ptid;
  2805. }
  2806. /* Kill one LWP. */
  2807. static void
  2808. kill_one_lwp (pid_t pid)
  2809. {
  2810. /* PTRACE_KILL may resume the inferior. Send SIGKILL first. */
  2811. errno = 0;
  2812. kill_lwp (pid, SIGKILL);
  2813. if (debug_linux_nat)
  2814. {
  2815. int save_errno = errno;
  2816. linux_nat_debug_printf
  2817. ("kill (SIGKILL) %ld, 0, 0 (%s)", (long) pid,
  2818. save_errno != 0 ? safe_strerror (save_errno) : "OK");
  2819. }
  2820. /* Some kernels ignore even SIGKILL for processes under ptrace. */
  2821. errno = 0;
  2822. ptrace (PTRACE_KILL, pid, 0, 0);
  2823. if (debug_linux_nat)
  2824. {
  2825. int save_errno = errno;
  2826. linux_nat_debug_printf
  2827. ("PTRACE_KILL %ld, 0, 0 (%s)", (long) pid,
  2828. save_errno ? safe_strerror (save_errno) : "OK");
  2829. }
  2830. }
  2831. /* Wait for an LWP to die. */
  2832. static void
  2833. kill_wait_one_lwp (pid_t pid)
  2834. {
  2835. pid_t res;
  2836. /* We must make sure that there are no pending events (delayed
  2837. SIGSTOPs, pending SIGTRAPs, etc.) to make sure the current
  2838. program doesn't interfere with any following debugging session. */
  2839. do
  2840. {
  2841. res = my_waitpid (pid, NULL, __WALL);
  2842. if (res != (pid_t) -1)
  2843. {
  2844. linux_nat_debug_printf ("wait %ld received unknown.", (long) pid);
  2845. /* The Linux kernel sometimes fails to kill a thread
  2846. completely after PTRACE_KILL; that goes from the stop
  2847. point in do_fork out to the one in get_signal_to_deliver
  2848. and waits again. So kill it again. */
  2849. kill_one_lwp (pid);
  2850. }
  2851. }
  2852. while (res == pid);
  2853. gdb_assert (res == -1 && errno == ECHILD);
  2854. }
  2855. /* Callback for iterate_over_lwps. */
  2856. static int
  2857. kill_callback (struct lwp_info *lp)
  2858. {
  2859. kill_one_lwp (lp->ptid.lwp ());
  2860. return 0;
  2861. }
  2862. /* Callback for iterate_over_lwps. */
  2863. static int
  2864. kill_wait_callback (struct lwp_info *lp)
  2865. {
  2866. kill_wait_one_lwp (lp->ptid.lwp ());
  2867. return 0;
  2868. }
  2869. /* Kill the fork children of any threads of inferior INF that are
  2870. stopped at a fork event. */
  2871. static void
  2872. kill_unfollowed_fork_children (struct inferior *inf)
  2873. {
  2874. for (thread_info *thread : inf->non_exited_threads ())
  2875. {
  2876. struct target_waitstatus *ws = &thread->pending_follow;
  2877. if (ws->kind () == TARGET_WAITKIND_FORKED
  2878. || ws->kind () == TARGET_WAITKIND_VFORKED)
  2879. {
  2880. ptid_t child_ptid = ws->child_ptid ();
  2881. int child_pid = child_ptid.pid ();
  2882. int child_lwp = child_ptid.lwp ();
  2883. kill_one_lwp (child_lwp);
  2884. kill_wait_one_lwp (child_lwp);
  2885. /* Let the arch-specific native code know this process is
  2886. gone. */
  2887. linux_target->low_forget_process (child_pid);
  2888. }
  2889. }
  2890. }
  2891. void
  2892. linux_nat_target::kill ()
  2893. {
  2894. /* If we're stopped while forking and we haven't followed yet,
  2895. kill the other task. We need to do this first because the
  2896. parent will be sleeping if this is a vfork. */
  2897. kill_unfollowed_fork_children (current_inferior ());
  2898. if (forks_exist_p ())
  2899. linux_fork_killall ();
  2900. else
  2901. {
  2902. ptid_t ptid = ptid_t (inferior_ptid.pid ());
  2903. /* Stop all threads before killing them, since ptrace requires
  2904. that the thread is stopped to successfully PTRACE_KILL. */
  2905. iterate_over_lwps (ptid, stop_callback);
  2906. /* ... and wait until all of them have reported back that
  2907. they're no longer running. */
  2908. iterate_over_lwps (ptid, stop_wait_callback);
  2909. /* Kill all LWP's ... */
  2910. iterate_over_lwps (ptid, kill_callback);
  2911. /* ... and wait until we've flushed all events. */
  2912. iterate_over_lwps (ptid, kill_wait_callback);
  2913. }
  2914. target_mourn_inferior (inferior_ptid);
  2915. }
  2916. void
  2917. linux_nat_target::mourn_inferior ()
  2918. {
  2919. int pid = inferior_ptid.pid ();
  2920. purge_lwp_list (pid);
  2921. close_proc_mem_file (pid);
  2922. if (! forks_exist_p ())
  2923. /* Normal case, no other forks available. */
  2924. inf_ptrace_target::mourn_inferior ();
  2925. else
  2926. /* Multi-fork case. The current inferior_ptid has exited, but
  2927. there are other viable forks to debug. Delete the exiting
  2928. one and context-switch to the first available. */
  2929. linux_fork_mourn_inferior ();
  2930. /* Let the arch-specific native code know this process is gone. */
  2931. linux_target->low_forget_process (pid);
  2932. }
  2933. /* Convert a native/host siginfo object, into/from the siginfo in the
  2934. layout of the inferiors' architecture. */
  2935. static void
  2936. siginfo_fixup (siginfo_t *siginfo, gdb_byte *inf_siginfo, int direction)
  2937. {
  2938. /* If the low target didn't do anything, then just do a straight
  2939. memcpy. */
  2940. if (!linux_target->low_siginfo_fixup (siginfo, inf_siginfo, direction))
  2941. {
  2942. if (direction == 1)
  2943. memcpy (siginfo, inf_siginfo, sizeof (siginfo_t));
  2944. else
  2945. memcpy (inf_siginfo, siginfo, sizeof (siginfo_t));
  2946. }
  2947. }
  2948. static enum target_xfer_status
  2949. linux_xfer_siginfo (enum target_object object,
  2950. const char *annex, gdb_byte *readbuf,
  2951. const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
  2952. ULONGEST *xfered_len)
  2953. {
  2954. int pid;
  2955. siginfo_t siginfo;
  2956. gdb_byte inf_siginfo[sizeof (siginfo_t)];
  2957. gdb_assert (object == TARGET_OBJECT_SIGNAL_INFO);
  2958. gdb_assert (readbuf || writebuf);
  2959. pid = inferior_ptid.lwp ();
  2960. if (pid == 0)
  2961. pid = inferior_ptid.pid ();
  2962. if (offset > sizeof (siginfo))
  2963. return TARGET_XFER_E_IO;
  2964. errno = 0;
  2965. ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
  2966. if (errno != 0)
  2967. return TARGET_XFER_E_IO;
  2968. /* When GDB is built as a 64-bit application, ptrace writes into
  2969. SIGINFO an object with 64-bit layout. Since debugging a 32-bit
  2970. inferior with a 64-bit GDB should look the same as debugging it
  2971. with a 32-bit GDB, we need to convert it. GDB core always sees
  2972. the converted layout, so any read/write will have to be done
  2973. post-conversion. */
  2974. siginfo_fixup (&siginfo, inf_siginfo, 0);
  2975. if (offset + len > sizeof (siginfo))
  2976. len = sizeof (siginfo) - offset;
  2977. if (readbuf != NULL)
  2978. memcpy (readbuf, inf_siginfo + offset, len);
  2979. else
  2980. {
  2981. memcpy (inf_siginfo + offset, writebuf, len);
  2982. /* Convert back to ptrace layout before flushing it out. */
  2983. siginfo_fixup (&siginfo, inf_siginfo, 1);
  2984. errno = 0;
  2985. ptrace (PTRACE_SETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo);
  2986. if (errno != 0)
  2987. return TARGET_XFER_E_IO;
  2988. }
  2989. *xfered_len = len;
  2990. return TARGET_XFER_OK;
  2991. }
  2992. static enum target_xfer_status
  2993. linux_nat_xfer_osdata (enum target_object object,
  2994. const char *annex, gdb_byte *readbuf,
  2995. const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
  2996. ULONGEST *xfered_len);
  2997. static enum target_xfer_status
  2998. linux_proc_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
  2999. ULONGEST offset, LONGEST len, ULONGEST *xfered_len);
  3000. enum target_xfer_status
  3001. linux_nat_target::xfer_partial (enum target_object object,
  3002. const char *annex, gdb_byte *readbuf,
  3003. const gdb_byte *writebuf,
  3004. ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
  3005. {
  3006. if (object == TARGET_OBJECT_SIGNAL_INFO)
  3007. return linux_xfer_siginfo (object, annex, readbuf, writebuf,
  3008. offset, len, xfered_len);
  3009. /* The target is connected but no live inferior is selected. Pass
  3010. this request down to a lower stratum (e.g., the executable
  3011. file). */
  3012. if (object == TARGET_OBJECT_MEMORY && inferior_ptid == null_ptid)
  3013. return TARGET_XFER_EOF;
  3014. if (object == TARGET_OBJECT_AUXV)
  3015. return memory_xfer_auxv (this, object, annex, readbuf, writebuf,
  3016. offset, len, xfered_len);
  3017. if (object == TARGET_OBJECT_OSDATA)
  3018. return linux_nat_xfer_osdata (object, annex, readbuf, writebuf,
  3019. offset, len, xfered_len);
  3020. if (object == TARGET_OBJECT_MEMORY)
  3021. {
  3022. /* GDB calculates all addresses in the largest possible address
  3023. width. The address width must be masked before its final use
  3024. by linux_proc_xfer_partial.
  3025. Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
  3026. int addr_bit = gdbarch_addr_bit (target_gdbarch ());
  3027. if (addr_bit < (sizeof (ULONGEST) * HOST_CHAR_BIT))
  3028. offset &= ((ULONGEST) 1 << addr_bit) - 1;
  3029. return linux_proc_xfer_memory_partial (readbuf, writebuf,
  3030. offset, len, xfered_len);
  3031. }
  3032. return inf_ptrace_target::xfer_partial (object, annex, readbuf, writebuf,
  3033. offset, len, xfered_len);
  3034. }
  3035. bool
  3036. linux_nat_target::thread_alive (ptid_t ptid)
  3037. {
  3038. /* As long as a PTID is in lwp list, consider it alive. */
  3039. return find_lwp_pid (ptid) != NULL;
  3040. }
  3041. /* Implement the to_update_thread_list target method for this
  3042. target. */
  3043. void
  3044. linux_nat_target::update_thread_list ()
  3045. {
  3046. /* We add/delete threads from the list as clone/exit events are
  3047. processed, so just try deleting exited threads still in the
  3048. thread list. */
  3049. delete_exited_threads ();
  3050. /* Update the processor core that each lwp/thread was last seen
  3051. running on. */
  3052. for (lwp_info *lwp : all_lwps ())
  3053. {
  3054. /* Avoid accessing /proc if the thread hasn't run since we last
  3055. time we fetched the thread's core. Accessing /proc becomes
  3056. noticeably expensive when we have thousands of LWPs. */
  3057. if (lwp->core == -1)
  3058. lwp->core = linux_common_core_of_thread (lwp->ptid);
  3059. }
  3060. }
  3061. std::string
  3062. linux_nat_target::pid_to_str (ptid_t ptid)
  3063. {
  3064. if (ptid.lwp_p ()
  3065. && (ptid.pid () != ptid.lwp ()
  3066. || num_lwps (ptid.pid ()) > 1))
  3067. return string_printf ("LWP %ld", ptid.lwp ());
  3068. return normal_pid_to_str (ptid);
  3069. }
  3070. const char *
  3071. linux_nat_target::thread_name (struct thread_info *thr)
  3072. {
  3073. return linux_proc_tid_get_name (thr->ptid);
  3074. }
  3075. /* Accepts an integer PID; Returns a string representing a file that
  3076. can be opened to get the symbols for the child process. */
  3077. char *
  3078. linux_nat_target::pid_to_exec_file (int pid)
  3079. {
  3080. return linux_proc_pid_to_exec_file (pid);
  3081. }
  3082. /* Object representing an /proc/PID/mem open file. We keep one such
  3083. file open per inferior.
  3084. It might be tempting to think about only ever opening one file at
  3085. most for all inferiors, closing/reopening the file as we access
  3086. memory of different inferiors, to minimize number of file
  3087. descriptors open, which can otherwise run into resource limits.
  3088. However, that does not work correctly -- if the inferior execs and
  3089. we haven't processed the exec event yet, and, we opened a
  3090. /proc/PID/mem file, we will get a mem file accessing the post-exec
  3091. address space, thinking we're opening it for the pre-exec address
  3092. space. That is dangerous as we can poke memory (e.g. clearing
  3093. breakpoints) in the post-exec memory by mistake, corrupting the
  3094. inferior. For that reason, we open the mem file as early as
  3095. possible, right after spawning, forking or attaching to the
  3096. inferior, when the inferior is stopped and thus before it has a
  3097. chance of execing.
  3098. Note that after opening the file, even if the thread we opened it
  3099. for subsequently exits, the open file is still usable for accessing
  3100. memory. It's only when the whole process exits or execs that the
  3101. file becomes invalid, at which point reads/writes return EOF. */
  3102. class proc_mem_file
  3103. {
  3104. public:
  3105. proc_mem_file (ptid_t ptid, int fd)
  3106. : m_ptid (ptid), m_fd (fd)
  3107. {
  3108. gdb_assert (m_fd != -1);
  3109. }
  3110. ~proc_mem_file ()
  3111. {
  3112. linux_nat_debug_printf ("closing fd %d for /proc/%d/task/%ld/mem",
  3113. m_fd, m_ptid.pid (), m_ptid.lwp ());
  3114. close (m_fd);
  3115. }
  3116. DISABLE_COPY_AND_ASSIGN (proc_mem_file);
  3117. int fd ()
  3118. {
  3119. return m_fd;
  3120. }
  3121. private:
  3122. /* The LWP this file was opened for. Just for debugging
  3123. purposes. */
  3124. ptid_t m_ptid;
  3125. /* The file descriptor. */
  3126. int m_fd = -1;
  3127. };
  3128. /* The map between an inferior process id, and the open /proc/PID/mem
  3129. file. This is stored in a map instead of in a per-inferior
  3130. structure because we need to be able to access memory of processes
  3131. which don't have a corresponding struct inferior object. E.g.,
  3132. with "detach-on-fork on" (the default), and "follow-fork parent"
  3133. (also default), we don't create an inferior for the fork child, but
  3134. we still need to remove breakpoints from the fork child's
  3135. memory. */
  3136. static std::unordered_map<int, proc_mem_file> proc_mem_file_map;
  3137. /* Close the /proc/PID/mem file for PID. */
  3138. static void
  3139. close_proc_mem_file (pid_t pid)
  3140. {
  3141. proc_mem_file_map.erase (pid);
  3142. }
  3143. /* Open the /proc/PID/mem file for the process (thread group) of PTID.
  3144. We actually open /proc/PID/task/LWP/mem, as that's the LWP we know
  3145. exists and is stopped right now. We prefer the
  3146. /proc/PID/task/LWP/mem form over /proc/LWP/mem to avoid tid-reuse
  3147. races, just in case this is ever called on an already-waited
  3148. LWP. */
  3149. static void
  3150. open_proc_mem_file (ptid_t ptid)
  3151. {
  3152. auto iter = proc_mem_file_map.find (ptid.pid ());
  3153. gdb_assert (iter == proc_mem_file_map.end ());
  3154. char filename[64];
  3155. xsnprintf (filename, sizeof filename,
  3156. "/proc/%d/task/%ld/mem", ptid.pid (), ptid.lwp ());
  3157. int fd = gdb_open_cloexec (filename, O_RDWR | O_LARGEFILE, 0).release ();
  3158. if (fd == -1)
  3159. {
  3160. warning (_("opening /proc/PID/mem file for lwp %d.%ld failed: %s (%d)"),
  3161. ptid.pid (), ptid.lwp (),
  3162. safe_strerror (errno), errno);
  3163. return;
  3164. }
  3165. proc_mem_file_map.emplace (std::piecewise_construct,
  3166. std::forward_as_tuple (ptid.pid ()),
  3167. std::forward_as_tuple (ptid, fd));
  3168. linux_nat_debug_printf ("opened fd %d for lwp %d.%ld",
  3169. fd, ptid.pid (), ptid.lwp ());
  3170. }
  3171. /* Implement the to_xfer_partial target method using /proc/PID/mem.
  3172. Because we can use a single read/write call, this can be much more
  3173. efficient than banging away at PTRACE_PEEKTEXT. Also, unlike
  3174. PTRACE_PEEKTEXT/PTRACE_POKETEXT, this works with running
  3175. threads. */
  3176. static enum target_xfer_status
  3177. linux_proc_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
  3178. ULONGEST offset, LONGEST len,
  3179. ULONGEST *xfered_len)
  3180. {
  3181. ssize_t ret;
  3182. auto iter = proc_mem_file_map.find (inferior_ptid.pid ());
  3183. if (iter == proc_mem_file_map.end ())
  3184. return TARGET_XFER_EOF;
  3185. int fd = iter->second.fd ();
  3186. gdb_assert (fd != -1);
  3187. /* Use pread64/pwrite64 if available, since they save a syscall and can
  3188. handle 64-bit offsets even on 32-bit platforms (for instance, SPARC
  3189. debugging a SPARC64 application). */
  3190. #ifdef HAVE_PREAD64
  3191. ret = (readbuf ? pread64 (fd, readbuf, len, offset)
  3192. : pwrite64 (fd, writebuf, len, offset));
  3193. #else
  3194. ret = lseek (fd, offset, SEEK_SET);
  3195. if (ret != -1)
  3196. ret = (readbuf ? read (fd, readbuf, len)
  3197. : write (fd, writebuf, len));
  3198. #endif
  3199. if (ret == -1)
  3200. {
  3201. linux_nat_debug_printf ("accessing fd %d for pid %d failed: %s (%d)",
  3202. fd, inferior_ptid.pid (),
  3203. safe_strerror (errno), errno);
  3204. return TARGET_XFER_EOF;
  3205. }
  3206. else if (ret == 0)
  3207. {
  3208. /* EOF means the address space is gone, the whole process exited
  3209. or execed. */
  3210. linux_nat_debug_printf ("accessing fd %d for pid %d got EOF",
  3211. fd, inferior_ptid.pid ());
  3212. return TARGET_XFER_EOF;
  3213. }
  3214. else
  3215. {
  3216. *xfered_len = ret;
  3217. return TARGET_XFER_OK;
  3218. }
  3219. }
  3220. /* Parse LINE as a signal set and add its set bits to SIGS. */
  3221. static void
  3222. add_line_to_sigset (const char *line, sigset_t *sigs)
  3223. {
  3224. int len = strlen (line) - 1;
  3225. const char *p;
  3226. int signum;
  3227. if (line[len] != '\n')
  3228. error (_("Could not parse signal set: %s"), line);
  3229. p = line;
  3230. signum = len * 4;
  3231. while (len-- > 0)
  3232. {
  3233. int digit;
  3234. if (*p >= '0' && *p <= '9')
  3235. digit = *p - '0';
  3236. else if (*p >= 'a' && *p <= 'f')
  3237. digit = *p - 'a' + 10;
  3238. else
  3239. error (_("Could not parse signal set: %s"), line);
  3240. signum -= 4;
  3241. if (digit & 1)
  3242. sigaddset (sigs, signum + 1);
  3243. if (digit & 2)
  3244. sigaddset (sigs, signum + 2);
  3245. if (digit & 4)
  3246. sigaddset (sigs, signum + 3);
  3247. if (digit & 8)
  3248. sigaddset (sigs, signum + 4);
  3249. p++;
  3250. }
  3251. }
  3252. /* Find process PID's pending signals from /proc/pid/status and set
  3253. SIGS to match. */
  3254. void
  3255. linux_proc_pending_signals (int pid, sigset_t *pending,
  3256. sigset_t *blocked, sigset_t *ignored)
  3257. {
  3258. char buffer[PATH_MAX], fname[PATH_MAX];
  3259. sigemptyset (pending);
  3260. sigemptyset (blocked);
  3261. sigemptyset (ignored);
  3262. xsnprintf (fname, sizeof fname, "/proc/%d/status", pid);
  3263. gdb_file_up procfile = gdb_fopen_cloexec (fname, "r");
  3264. if (procfile == NULL)
  3265. error (_("Could not open %s"), fname);
  3266. while (fgets (buffer, PATH_MAX, procfile.get ()) != NULL)
  3267. {
  3268. /* Normal queued signals are on the SigPnd line in the status
  3269. file. However, 2.6 kernels also have a "shared" pending
  3270. queue for delivering signals to a thread group, so check for
  3271. a ShdPnd line also.
  3272. Unfortunately some Red Hat kernels include the shared pending
  3273. queue but not the ShdPnd status field. */
  3274. if (startswith (buffer, "SigPnd:\t"))
  3275. add_line_to_sigset (buffer + 8, pending);
  3276. else if (startswith (buffer, "ShdPnd:\t"))
  3277. add_line_to_sigset (buffer + 8, pending);
  3278. else if (startswith (buffer, "SigBlk:\t"))
  3279. add_line_to_sigset (buffer + 8, blocked);
  3280. else if (startswith (buffer, "SigIgn:\t"))
  3281. add_line_to_sigset (buffer + 8, ignored);
  3282. }
  3283. }
  3284. static enum target_xfer_status
  3285. linux_nat_xfer_osdata (enum target_object object,
  3286. const char *annex, gdb_byte *readbuf,
  3287. const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
  3288. ULONGEST *xfered_len)
  3289. {
  3290. gdb_assert (object == TARGET_OBJECT_OSDATA);
  3291. *xfered_len = linux_common_xfer_osdata (annex, readbuf, offset, len);
  3292. if (*xfered_len == 0)
  3293. return TARGET_XFER_EOF;
  3294. else
  3295. return TARGET_XFER_OK;
  3296. }
  3297. std::vector<static_tracepoint_marker>
  3298. linux_nat_target::static_tracepoint_markers_by_strid (const char *strid)
  3299. {
  3300. char s[IPA_CMD_BUF_SIZE];
  3301. int pid = inferior_ptid.pid ();
  3302. std::vector<static_tracepoint_marker> markers;
  3303. const char *p = s;
  3304. ptid_t ptid = ptid_t (pid, 0);
  3305. static_tracepoint_marker marker;
  3306. /* Pause all */
  3307. target_stop (ptid);
  3308. memcpy (s, "qTfSTM", sizeof ("qTfSTM"));
  3309. s[sizeof ("qTfSTM")] = 0;
  3310. agent_run_command (pid, s, strlen (s) + 1);
  3311. /* Unpause all. */
  3312. SCOPE_EXIT { target_continue_no_signal (ptid); };
  3313. while (*p++ == 'm')
  3314. {
  3315. do
  3316. {
  3317. parse_static_tracepoint_marker_definition (p, &p, &marker);
  3318. if (strid == NULL || marker.str_id == strid)
  3319. markers.push_back (std::move (marker));
  3320. }
  3321. while (*p++ == ','); /* comma-separated list */
  3322. memcpy (s, "qTsSTM", sizeof ("qTsSTM"));
  3323. s[sizeof ("qTsSTM")] = 0;
  3324. agent_run_command (pid, s, strlen (s) + 1);
  3325. p = s;
  3326. }
  3327. return markers;
  3328. }
  3329. /* target_can_async_p implementation. */
  3330. bool
  3331. linux_nat_target::can_async_p ()
  3332. {
  3333. /* This flag should be checked in the common target.c code. */
  3334. gdb_assert (target_async_permitted);
  3335. /* Otherwise, this targets is always able to support async mode. */
  3336. return true;
  3337. }
  3338. bool
  3339. linux_nat_target::supports_non_stop ()
  3340. {
  3341. return true;
  3342. }
  3343. /* to_always_non_stop_p implementation. */
  3344. bool
  3345. linux_nat_target::always_non_stop_p ()
  3346. {
  3347. return true;
  3348. }
  3349. bool
  3350. linux_nat_target::supports_multi_process ()
  3351. {
  3352. return true;
  3353. }
  3354. bool
  3355. linux_nat_target::supports_disable_randomization ()
  3356. {
  3357. return true;
  3358. }
  3359. /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
  3360. so we notice when any child changes state, and notify the
  3361. event-loop; it allows us to use sigsuspend in linux_nat_wait_1
  3362. above to wait for the arrival of a SIGCHLD. */
  3363. static void
  3364. sigchld_handler (int signo)
  3365. {
  3366. int old_errno = errno;
  3367. if (debug_linux_nat)
  3368. gdb_stdlog->write_async_safe ("sigchld\n", sizeof ("sigchld\n") - 1);
  3369. if (signo == SIGCHLD)
  3370. {
  3371. /* Let the event loop know that there are events to handle. */
  3372. linux_nat_target::async_file_mark_if_open ();
  3373. }
  3374. errno = old_errno;
  3375. }
  3376. /* Callback registered with the target events file descriptor. */
  3377. static void
  3378. handle_target_event (int error, gdb_client_data client_data)
  3379. {
  3380. inferior_event_handler (INF_REG_EVENT);
  3381. }
  3382. /* target_async implementation. */
  3383. void
  3384. linux_nat_target::async (int enable)
  3385. {
  3386. if ((enable != 0) == is_async_p ())
  3387. return;
  3388. /* Block child signals while we create/destroy the pipe, as their
  3389. handler writes to it. */
  3390. gdb::block_signals blocker;
  3391. if (enable)
  3392. {
  3393. if (!async_file_open ())
  3394. internal_error (__FILE__, __LINE__, "creating event pipe failed.");
  3395. add_file_handler (async_wait_fd (), handle_target_event, NULL,
  3396. "linux-nat");
  3397. /* There may be pending events to handle. Tell the event loop
  3398. to poll them. */
  3399. async_file_mark ();
  3400. }
  3401. else
  3402. {
  3403. delete_file_handler (async_wait_fd ());
  3404. async_file_close ();
  3405. }
  3406. }
  3407. /* Stop an LWP, and push a GDB_SIGNAL_0 stop status if no other
  3408. event came out. */
  3409. static int
  3410. linux_nat_stop_lwp (struct lwp_info *lwp)
  3411. {
  3412. if (!lwp->stopped)
  3413. {
  3414. linux_nat_debug_printf ("running -> suspending %s",
  3415. lwp->ptid.to_string ().c_str ());
  3416. if (lwp->last_resume_kind == resume_stop)
  3417. {
  3418. linux_nat_debug_printf ("already stopping LWP %ld at GDB's request",
  3419. lwp->ptid.lwp ());
  3420. return 0;
  3421. }
  3422. stop_callback (lwp);
  3423. lwp->last_resume_kind = resume_stop;
  3424. }
  3425. else
  3426. {
  3427. /* Already known to be stopped; do nothing. */
  3428. if (debug_linux_nat)
  3429. {
  3430. if (find_thread_ptid (linux_target, lwp->ptid)->stop_requested)
  3431. linux_nat_debug_printf ("already stopped/stop_requested %s",
  3432. lwp->ptid.to_string ().c_str ());
  3433. else
  3434. linux_nat_debug_printf ("already stopped/no stop_requested yet %s",
  3435. lwp->ptid.to_string ().c_str ());
  3436. }
  3437. }
  3438. return 0;
  3439. }
  3440. void
  3441. linux_nat_target::stop (ptid_t ptid)
  3442. {
  3443. LINUX_NAT_SCOPED_DEBUG_ENTER_EXIT;
  3444. iterate_over_lwps (ptid, linux_nat_stop_lwp);
  3445. }
  3446. /* When requests are passed down from the linux-nat layer to the
  3447. single threaded inf-ptrace layer, ptids of (lwpid,0,0) form are
  3448. used. The address space pointer is stored in the inferior object,
  3449. but the common code that is passed such ptid can't tell whether
  3450. lwpid is a "main" process id or not (it assumes so). We reverse
  3451. look up the "main" process id from the lwp here. */
  3452. struct address_space *
  3453. linux_nat_target::thread_address_space (ptid_t ptid)
  3454. {
  3455. struct lwp_info *lwp;
  3456. struct inferior *inf;
  3457. int pid;
  3458. if (ptid.lwp () == 0)
  3459. {
  3460. /* An (lwpid,0,0) ptid. Look up the lwp object to get at the
  3461. tgid. */
  3462. lwp = find_lwp_pid (ptid);
  3463. pid = lwp->ptid.pid ();
  3464. }
  3465. else
  3466. {
  3467. /* A (pid,lwpid,0) ptid. */
  3468. pid = ptid.pid ();
  3469. }
  3470. inf = find_inferior_pid (this, pid);
  3471. gdb_assert (inf != NULL);
  3472. return inf->aspace;
  3473. }
  3474. /* Return the cached value of the processor core for thread PTID. */
  3475. int
  3476. linux_nat_target::core_of_thread (ptid_t ptid)
  3477. {
  3478. struct lwp_info *info = find_lwp_pid (ptid);
  3479. if (info)
  3480. return info->core;
  3481. return -1;
  3482. }
  3483. /* Implementation of to_filesystem_is_local. */
  3484. bool
  3485. linux_nat_target::filesystem_is_local ()
  3486. {
  3487. struct inferior *inf = current_inferior ();
  3488. if (inf->fake_pid_p || inf->pid == 0)
  3489. return true;
  3490. return linux_ns_same (inf->pid, LINUX_NS_MNT);
  3491. }
  3492. /* Convert the INF argument passed to a to_fileio_* method
  3493. to a process ID suitable for passing to its corresponding
  3494. linux_mntns_* function. If INF is non-NULL then the
  3495. caller is requesting the filesystem seen by INF. If INF
  3496. is NULL then the caller is requesting the filesystem seen
  3497. by the GDB. We fall back to GDB's filesystem in the case
  3498. that INF is non-NULL but its PID is unknown. */
  3499. static pid_t
  3500. linux_nat_fileio_pid_of (struct inferior *inf)
  3501. {
  3502. if (inf == NULL || inf->fake_pid_p || inf->pid == 0)
  3503. return getpid ();
  3504. else
  3505. return inf->pid;
  3506. }
  3507. /* Implementation of to_fileio_open. */
  3508. int
  3509. linux_nat_target::fileio_open (struct inferior *inf, const char *filename,
  3510. int flags, int mode, int warn_if_slow,
  3511. int *target_errno)
  3512. {
  3513. int nat_flags;
  3514. mode_t nat_mode;
  3515. int fd;
  3516. if (fileio_to_host_openflags (flags, &nat_flags) == -1
  3517. || fileio_to_host_mode (mode, &nat_mode) == -1)
  3518. {
  3519. *target_errno = FILEIO_EINVAL;
  3520. return -1;
  3521. }
  3522. fd = linux_mntns_open_cloexec (linux_nat_fileio_pid_of (inf),
  3523. filename, nat_flags, nat_mode);
  3524. if (fd == -1)
  3525. *target_errno = host_to_fileio_error (errno);
  3526. return fd;
  3527. }
  3528. /* Implementation of to_fileio_readlink. */
  3529. gdb::optional<std::string>
  3530. linux_nat_target::fileio_readlink (struct inferior *inf, const char *filename,
  3531. int *target_errno)
  3532. {
  3533. char buf[PATH_MAX];
  3534. int len;
  3535. len = linux_mntns_readlink (linux_nat_fileio_pid_of (inf),
  3536. filename, buf, sizeof (buf));
  3537. if (len < 0)
  3538. {
  3539. *target_errno = host_to_fileio_error (errno);
  3540. return {};
  3541. }
  3542. return std::string (buf, len);
  3543. }
  3544. /* Implementation of to_fileio_unlink. */
  3545. int
  3546. linux_nat_target::fileio_unlink (struct inferior *inf, const char *filename,
  3547. int *target_errno)
  3548. {
  3549. int ret;
  3550. ret = linux_mntns_unlink (linux_nat_fileio_pid_of (inf),
  3551. filename);
  3552. if (ret == -1)
  3553. *target_errno = host_to_fileio_error (errno);
  3554. return ret;
  3555. }
  3556. /* Implementation of the to_thread_events method. */
  3557. void
  3558. linux_nat_target::thread_events (int enable)
  3559. {
  3560. report_thread_events = enable;
  3561. }
  3562. linux_nat_target::linux_nat_target ()
  3563. {
  3564. /* We don't change the stratum; this target will sit at
  3565. process_stratum and thread_db will set at thread_stratum. This
  3566. is a little strange, since this is a multi-threaded-capable
  3567. target, but we want to be on the stack below thread_db, and we
  3568. also want to be used for single-threaded processes. */
  3569. }
  3570. /* See linux-nat.h. */
  3571. int
  3572. linux_nat_get_siginfo (ptid_t ptid, siginfo_t *siginfo)
  3573. {
  3574. int pid;
  3575. pid = ptid.lwp ();
  3576. if (pid == 0)
  3577. pid = ptid.pid ();
  3578. errno = 0;
  3579. ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, siginfo);
  3580. if (errno != 0)
  3581. {
  3582. memset (siginfo, 0, sizeof (*siginfo));
  3583. return 0;
  3584. }
  3585. return 1;
  3586. }
  3587. /* See nat/linux-nat.h. */
  3588. ptid_t
  3589. current_lwp_ptid (void)
  3590. {
  3591. gdb_assert (inferior_ptid.lwp_p ());
  3592. return inferior_ptid;
  3593. }
  3594. void _initialize_linux_nat ();
  3595. void
  3596. _initialize_linux_nat ()
  3597. {
  3598. add_setshow_boolean_cmd ("linux-nat", class_maintenance,
  3599. &debug_linux_nat, _("\
  3600. Set debugging of GNU/Linux native target."), _(" \
  3601. Show debugging of GNU/Linux native target."), _(" \
  3602. When on, print debug messages relating to the GNU/Linux native target."),
  3603. nullptr,
  3604. show_debug_linux_nat,
  3605. &setdebuglist, &showdebuglist);
  3606. add_setshow_boolean_cmd ("linux-namespaces", class_maintenance,
  3607. &debug_linux_namespaces, _("\
  3608. Set debugging of GNU/Linux namespaces module."), _("\
  3609. Show debugging of GNU/Linux namespaces module."), _("\
  3610. Enables printf debugging output."),
  3611. NULL,
  3612. NULL,
  3613. &setdebuglist, &showdebuglist);
  3614. /* Install a SIGCHLD handler. */
  3615. sigchld_action.sa_handler = sigchld_handler;
  3616. sigemptyset (&sigchld_action.sa_mask);
  3617. sigchld_action.sa_flags = SA_RESTART;
  3618. /* Make it the default. */
  3619. sigaction (SIGCHLD, &sigchld_action, NULL);
  3620. /* Make sure we don't block SIGCHLD during a sigsuspend. */
  3621. gdb_sigmask (SIG_SETMASK, NULL, &suspend_mask);
  3622. sigdelset (&suspend_mask, SIGCHLD);
  3623. sigemptyset (&blocked_mask);
  3624. lwp_lwpid_htab_create ();
  3625. }
  3626. /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
  3627. the GNU/Linux Threads library and therefore doesn't really belong
  3628. here. */
  3629. /* NPTL reserves the first two RT signals, but does not provide any
  3630. way for the debugger to query the signal numbers - fortunately
  3631. they don't change. */
  3632. static int lin_thread_signals[] = { __SIGRTMIN, __SIGRTMIN + 1 };
  3633. /* See linux-nat.h. */
  3634. unsigned int
  3635. lin_thread_get_thread_signal_num (void)
  3636. {
  3637. return sizeof (lin_thread_signals) / sizeof (lin_thread_signals[0]);
  3638. }
  3639. /* See linux-nat.h. */
  3640. int
  3641. lin_thread_get_thread_signal (unsigned int i)
  3642. {
  3643. gdb_assert (i < lin_thread_get_thread_signal_num ());
  3644. return lin_thread_signals[i];
  3645. }