gnu-nat.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475
  1. /* Interface GDB to the GNU Hurd.
  2. Copyright (C) 1992-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. Written by Miles Bader <miles@gnu.ai.mit.edu>
  5. Some code and ideas from m3-nat.c by Jukka Virtanen <jtv@hut.fi>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. /* Include this first, to pick up the <mach.h> 'thread_info' diversion. */
  17. #include "gnu-nat.h"
  18. /* Mach/Hurd headers are not yet ready for C++ compilation. */
  19. extern "C"
  20. {
  21. #include <mach.h>
  22. #include <mach_error.h>
  23. #include <mach/exception.h>
  24. #include <mach/message.h>
  25. #include <mach/notify.h>
  26. #include <mach/vm_attributes.h>
  27. #include <mach/vm_param.h>
  28. #include <hurd.h>
  29. #include <hurd/interrupt.h>
  30. #include <hurd/msg.h>
  31. #include <hurd/msg_request.h>
  32. #include <hurd/process.h>
  33. /* Defined in <hurd/process.h>, but we need forward declarations from
  34. <hurd/process_request.h> as well. */
  35. #undef _process_user_
  36. #include <hurd/process_request.h>
  37. #include <hurd/signal.h>
  38. #include <hurd/sigpreempt.h>
  39. #include <portinfo.h>
  40. }
  41. #include "defs.h"
  42. #include <ctype.h>
  43. #include <limits.h>
  44. #include <setjmp.h>
  45. #include <signal.h>
  46. #include <sys/ptrace.h>
  47. #include <elf.h>
  48. #include <link.h>
  49. #include "inferior.h"
  50. #include "symtab.h"
  51. #include "value.h"
  52. #include "language.h"
  53. #include "target.h"
  54. #include "gdbsupport/gdb_wait.h"
  55. #include "gdbarch.h"
  56. #include "gdbcmd.h"
  57. #include "gdbcore.h"
  58. #include "gdbthread.h"
  59. #include "gdbsupport/gdb_obstack.h"
  60. #include "tid-parse.h"
  61. #include "nat/fork-inferior.h"
  62. #include "inf-child.h"
  63. /* MIG stubs are not yet ready for C++ compilation. */
  64. extern "C"
  65. {
  66. #include "exc_request_S.h"
  67. #include "notify_S.h"
  68. #include "process_reply_S.h"
  69. #include "msg_reply_S.h"
  70. #include "exc_request_U.h"
  71. #include "msg_U.h"
  72. #include "gnu-nat-mig.h"
  73. }
  74. struct gnu_nat_target *gnu_target;
  75. static process_t proc_server = MACH_PORT_NULL;
  76. /* If we've sent a proc_wait_request to the proc server, the pid of the
  77. process we asked about. We can only ever have one outstanding. */
  78. int proc_wait_pid = 0;
  79. /* The number of wait requests we've sent, and expect replies from. */
  80. int proc_waits_pending = 0;
  81. bool gnu_debug_flag = false;
  82. /* Forward decls */
  83. static struct inf *make_inf ();
  84. #define inf_debug(_inf, msg, args...) \
  85. do { struct inf *__inf = (_inf); \
  86. debug ("{inf %d %s}: " msg, __inf->pid, \
  87. host_address_to_string (__inf) , ##args); } while (0)
  88. /* Evaluate RPC_EXPR in a scope with the variables MSGPORT and REFPORT bound
  89. to INF's msg port and task port respectively. If it has no msg port,
  90. EIEIO is returned. INF must refer to a running process! */
  91. #define INF_MSGPORT_RPC(inf, rpc_expr) \
  92. HURD_MSGPORT_RPC (proc_getmsgport (proc_server, inf->pid, &msgport), \
  93. (refport = inf->task->port, 0), 0, \
  94. msgport ? (rpc_expr) : EIEIO)
  95. /* Like INF_MSGPORT_RPC, but will also resume the signal thread to ensure
  96. there's someone around to deal with the RPC (and resuspend things
  97. afterwards). This effects INF's threads' resume_sc count. */
  98. #define INF_RESUME_MSGPORT_RPC(inf, rpc_expr) \
  99. (inf_set_threads_resume_sc_for_signal_thread (inf) \
  100. ? ({ kern_return_t __e; \
  101. inf_resume (inf); \
  102. __e = INF_MSGPORT_RPC (inf, rpc_expr); \
  103. inf_suspend (inf); \
  104. __e; }) \
  105. : EIEIO)
  106. /* The state passed by an exception message. */
  107. struct exc_state
  108. {
  109. int exception; /* The exception code. */
  110. int code, subcode;
  111. mach_port_t handler; /* The real exception port to handle this. */
  112. mach_port_t reply; /* The reply port from the exception call. */
  113. };
  114. /* The results of the last wait an inf did. */
  115. struct inf_wait
  116. {
  117. struct target_waitstatus status; /* The status returned to gdb. */
  118. struct exc_state exc; /* The exception that caused us to return. */
  119. struct proc *thread; /* The thread in question. */
  120. int suppress; /* Something trivial happened. */
  121. };
  122. /* Further Hurd-specific state of an inferior. */
  123. struct inf
  124. {
  125. /* Fields describing the current inferior. */
  126. struct proc *task; /* The mach task. */
  127. struct proc *threads; /* A linked list of all threads in TASK. */
  128. /* True if THREADS needn't be validated by querying the task. We
  129. assume that we and the task in question are the only ones
  130. frobbing the thread list, so as long as we don't let any code
  131. run, we don't have to worry about THREADS changing. */
  132. int threads_up_to_date;
  133. pid_t pid; /* The real system PID. */
  134. struct inf_wait wait; /* What to return from target_wait. */
  135. /* One thread proc in INF may be in `single-stepping mode'. This
  136. is it. */
  137. struct proc *step_thread;
  138. /* The thread we think is the signal thread. */
  139. struct proc *signal_thread;
  140. mach_port_t event_port; /* Where we receive various msgs. */
  141. /* True if we think at least one thread in the inferior could currently be
  142. running. */
  143. unsigned int running:1;
  144. /* True if the process has stopped (in the proc server sense). Note that
  145. since a proc server `stop' leaves the signal thread running, the inf can
  146. be RUNNING && STOPPED... */
  147. unsigned int stopped:1;
  148. /* True if the inferior has no message port. */
  149. unsigned int nomsg:1;
  150. /* True if the inferior is traced. */
  151. unsigned int traced:1;
  152. /* True if we shouldn't try waiting for the inferior, usually because we
  153. can't for some reason. */
  154. unsigned int no_wait:1;
  155. /* When starting a new inferior, we don't try to validate threads until all
  156. the proper execs have been done, which this flag states we still
  157. expect to happen. */
  158. unsigned int pending_execs:1;
  159. /* Fields describing global state. */
  160. /* The task suspend count used when gdb has control. This is normally 1 to
  161. make things easier for us, but sometimes (like when attaching to vital
  162. system servers) it may be desirable to let the task continue to run
  163. (pausing individual threads as necessary). */
  164. int pause_sc;
  165. /* The task suspend count left when detaching from a task. */
  166. int detach_sc;
  167. /* The initial values used for the run_sc and pause_sc of newly discovered
  168. threads -- see the definition of those fields in struct proc. */
  169. int default_thread_run_sc;
  170. int default_thread_pause_sc;
  171. int default_thread_detach_sc;
  172. /* True if the process should be traced when started/attached. Newly
  173. started processes *must* be traced at first to exec them properly, but
  174. if this is false, tracing is turned off as soon it has done so. */
  175. int want_signals;
  176. /* True if exceptions from the inferior process should be trapped. This
  177. must be on to use breakpoints. */
  178. int want_exceptions;
  179. };
  180. int
  181. __proc_pid (struct proc *proc)
  182. {
  183. return proc->inf->pid;
  184. }
  185. /* Update PROC's real suspend count to match it's desired one. Returns true
  186. if we think PROC is now in a runnable state. */
  187. int
  188. gnu_nat_target::proc_update_sc (struct proc *proc)
  189. {
  190. int running;
  191. int err = 0;
  192. int delta = proc->sc - proc->cur_sc;
  193. if (delta)
  194. proc_debug (proc, "sc: %d --> %d", proc->cur_sc, proc->sc);
  195. if (proc->sc == 0 && proc->state_changed)
  196. /* Since PROC may start running, we must write back any state changes. */
  197. {
  198. gdb_assert (proc_is_thread (proc));
  199. proc_debug (proc, "storing back changed thread state");
  200. err = thread_set_state (proc->port, THREAD_STATE_FLAVOR,
  201. (thread_state_t) &proc->state, THREAD_STATE_SIZE);
  202. if (!err)
  203. proc->state_changed = 0;
  204. }
  205. if (delta > 0)
  206. {
  207. while (delta-- > 0 && !err)
  208. {
  209. if (proc_is_task (proc))
  210. err = task_suspend (proc->port);
  211. else
  212. err = thread_suspend (proc->port);
  213. }
  214. }
  215. else
  216. {
  217. while (delta++ < 0 && !err)
  218. {
  219. if (proc_is_task (proc))
  220. err = task_resume (proc->port);
  221. else
  222. err = thread_resume (proc->port);
  223. }
  224. }
  225. if (!err)
  226. proc->cur_sc = proc->sc;
  227. /* If we got an error, then the task/thread has disappeared. */
  228. running = !err && proc->sc == 0;
  229. proc_debug (proc, "is %s", err ? "dead" : running ? "running" : "suspended");
  230. if (err)
  231. proc_debug (proc, "err = %s", safe_strerror (err));
  232. if (running)
  233. {
  234. proc->aborted = 0;
  235. proc->state_valid = proc->state_changed = 0;
  236. proc->fetched_regs = 0;
  237. }
  238. return running;
  239. }
  240. /* Thread_abort is called on PROC if needed. PROC must be a thread proc.
  241. If PROC is deemed `precious', then nothing is done unless FORCE is true.
  242. In particular, a thread is precious if it's running (in which case forcing
  243. it includes suspending it first), or if it has an exception pending. */
  244. void
  245. gnu_nat_target::proc_abort (struct proc *proc, int force)
  246. {
  247. gdb_assert (proc_is_thread (proc));
  248. if (!proc->aborted)
  249. {
  250. struct inf *inf = proc->inf;
  251. int running = (proc->cur_sc == 0 && inf->task->cur_sc == 0);
  252. if (running && force)
  253. {
  254. proc->sc = 1;
  255. inf_update_suspends (proc->inf);
  256. running = 0;
  257. warning (_("Stopped %s."), proc_string (proc));
  258. }
  259. else if (proc == inf->wait.thread && inf->wait.exc.reply && !force)
  260. /* An exception is pending on PROC, which don't mess with. */
  261. running = 1;
  262. if (!running)
  263. /* We only abort the thread if it's not actually running. */
  264. {
  265. thread_abort (proc->port);
  266. proc_debug (proc, "aborted");
  267. proc->aborted = 1;
  268. }
  269. else
  270. proc_debug (proc, "not aborting");
  271. }
  272. }
  273. /* Make sure that the state field in PROC is up to date, and return a pointer
  274. to it, or 0 if something is wrong. If WILL_MODIFY is true, makes sure
  275. that the thread is stopped and aborted first, and sets the state_changed
  276. field in PROC to true. */
  277. thread_state_t
  278. gnu_nat_target::proc_get_state (struct proc *proc, int will_modify)
  279. {
  280. int was_aborted = proc->aborted;
  281. proc_debug (proc, "updating state info%s",
  282. will_modify ? " (with intention to modify)" : "");
  283. proc_abort (proc, will_modify);
  284. if (!was_aborted && proc->aborted)
  285. /* PROC's state may have changed since we last fetched it. */
  286. proc->state_valid = 0;
  287. if (!proc->state_valid)
  288. {
  289. mach_msg_type_number_t state_size = THREAD_STATE_SIZE;
  290. kern_return_t err =
  291. thread_get_state (proc->port, THREAD_STATE_FLAVOR,
  292. (thread_state_t) &proc->state, &state_size);
  293. proc_debug (proc, "getting thread state");
  294. proc->state_valid = !err;
  295. }
  296. if (proc->state_valid)
  297. {
  298. if (will_modify)
  299. proc->state_changed = 1;
  300. return (thread_state_t) &proc->state;
  301. }
  302. else
  303. return 0;
  304. }
  305. /* Set PORT to PROC's exception port. */
  306. kern_return_t
  307. gnu_nat_target::proc_get_exception_port (struct proc * proc, mach_port_t * port)
  308. {
  309. if (proc_is_task (proc))
  310. return task_get_exception_port (proc->port, port);
  311. else
  312. return thread_get_exception_port (proc->port, port);
  313. }
  314. /* Set PROC's exception port to PORT. */
  315. kern_return_t
  316. gnu_nat_target::proc_set_exception_port (struct proc * proc, mach_port_t port)
  317. {
  318. proc_debug (proc, "setting exception port: %lu", port);
  319. if (proc_is_task (proc))
  320. return task_set_exception_port (proc->port, port);
  321. else
  322. return thread_set_exception_port (proc->port, port);
  323. }
  324. /* Get PROC's exception port, cleaning up a bit if proc has died. */
  325. mach_port_t
  326. gnu_nat_target::_proc_get_exc_port (struct proc *proc)
  327. {
  328. mach_port_t exc_port;
  329. kern_return_t err = proc_get_exception_port (proc, &exc_port);
  330. if (err)
  331. /* PROC must be dead. */
  332. {
  333. if (proc->exc_port)
  334. mach_port_deallocate (mach_task_self (), proc->exc_port);
  335. proc->exc_port = MACH_PORT_NULL;
  336. if (proc->saved_exc_port)
  337. mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
  338. proc->saved_exc_port = MACH_PORT_NULL;
  339. }
  340. return exc_port;
  341. }
  342. /* Replace PROC's exception port with EXC_PORT, unless it's already
  343. been done. Stash away any existing exception port so we can
  344. restore it later. */
  345. void
  346. gnu_nat_target::proc_steal_exc_port (struct proc *proc, mach_port_t exc_port)
  347. {
  348. mach_port_t cur_exc_port = _proc_get_exc_port (proc);
  349. if (cur_exc_port)
  350. {
  351. kern_return_t err = 0;
  352. proc_debug (proc, "inserting exception port: %lu", exc_port);
  353. if (cur_exc_port != exc_port)
  354. /* Put in our exception port. */
  355. err = proc_set_exception_port (proc, exc_port);
  356. if (err || cur_exc_port == proc->exc_port)
  357. /* We previously set the exception port, and it's still set. So we
  358. just keep the old saved port which is what the proc set. */
  359. {
  360. if (cur_exc_port)
  361. mach_port_deallocate (mach_task_self (), cur_exc_port);
  362. }
  363. else
  364. /* Keep a copy of PROC's old exception port so it can be restored. */
  365. {
  366. if (proc->saved_exc_port)
  367. mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
  368. proc->saved_exc_port = cur_exc_port;
  369. }
  370. proc_debug (proc, "saved exception port: %lu", proc->saved_exc_port);
  371. if (!err)
  372. proc->exc_port = exc_port;
  373. else
  374. warning (_("Error setting exception port for %s: %s"),
  375. proc_string (proc), safe_strerror (err));
  376. }
  377. }
  378. /* If we previously replaced PROC's exception port, put back what we
  379. found there at the time, unless *our* exception port has since been
  380. overwritten, in which case who knows what's going on. */
  381. void
  382. gnu_nat_target::proc_restore_exc_port (struct proc *proc)
  383. {
  384. mach_port_t cur_exc_port = _proc_get_exc_port (proc);
  385. if (cur_exc_port)
  386. {
  387. kern_return_t err = 0;
  388. proc_debug (proc, "restoring real exception port");
  389. if (proc->exc_port == cur_exc_port)
  390. /* Our's is still there. */
  391. err = proc_set_exception_port (proc, proc->saved_exc_port);
  392. if (proc->saved_exc_port)
  393. mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
  394. proc->saved_exc_port = MACH_PORT_NULL;
  395. if (!err)
  396. proc->exc_port = MACH_PORT_NULL;
  397. else
  398. warning (_("Error setting exception port for %s: %s"),
  399. proc_string (proc), safe_strerror (err));
  400. }
  401. }
  402. /* Turns hardware tracing in PROC on or off when SET is true or false,
  403. respectively. Returns true on success. */
  404. int
  405. gnu_nat_target::proc_trace (struct proc *proc, int set)
  406. {
  407. thread_state_t state = proc_get_state (proc, 1);
  408. if (!state)
  409. return 0; /* The thread must be dead. */
  410. proc_debug (proc, "tracing %s", set ? "on" : "off");
  411. if (set)
  412. {
  413. /* XXX We don't get the exception unless the thread has its own
  414. exception port???? */
  415. if (proc->exc_port == MACH_PORT_NULL)
  416. proc_steal_exc_port (proc, proc->inf->event_port);
  417. THREAD_STATE_SET_TRACED (state);
  418. }
  419. else
  420. THREAD_STATE_CLEAR_TRACED (state);
  421. return 1;
  422. }
  423. /* A variable from which to assign new TIDs. */
  424. static int next_thread_id = 1;
  425. /* Returns a new proc structure with the given fields. Also adds a
  426. notification for PORT becoming dead to be sent to INF's notify port. */
  427. struct proc *
  428. gnu_nat_target::make_proc (struct inf *inf, mach_port_t port, int tid)
  429. {
  430. kern_return_t err;
  431. mach_port_t prev_port = MACH_PORT_NULL;
  432. struct proc *proc = XNEW (struct proc);
  433. proc->port = port;
  434. proc->tid = tid;
  435. proc->inf = inf;
  436. proc->next = 0;
  437. proc->saved_exc_port = MACH_PORT_NULL;
  438. proc->exc_port = MACH_PORT_NULL;
  439. proc->sc = 0;
  440. proc->cur_sc = 0;
  441. /* Note that these are all the values for threads; the task simply uses the
  442. corresponding field in INF directly. */
  443. proc->run_sc = inf->default_thread_run_sc;
  444. proc->pause_sc = inf->default_thread_pause_sc;
  445. proc->detach_sc = inf->default_thread_detach_sc;
  446. proc->resume_sc = proc->run_sc;
  447. proc->aborted = 0;
  448. proc->dead = 0;
  449. proc->state_valid = 0;
  450. proc->state_changed = 0;
  451. proc_debug (proc, "is new");
  452. /* Get notified when things die. */
  453. err =
  454. mach_port_request_notification (mach_task_self (), port,
  455. MACH_NOTIFY_DEAD_NAME, 1,
  456. inf->event_port,
  457. MACH_MSG_TYPE_MAKE_SEND_ONCE,
  458. &prev_port);
  459. if (err)
  460. warning (_("Couldn't request notification for port %lu: %s"),
  461. port, safe_strerror (err));
  462. else
  463. {
  464. proc_debug (proc, "notifications to: %lu", inf->event_port);
  465. if (prev_port != MACH_PORT_NULL)
  466. mach_port_deallocate (mach_task_self (), prev_port);
  467. }
  468. if (inf->want_exceptions)
  469. {
  470. if (proc_is_task (proc))
  471. /* Make the task exception port point to us. */
  472. proc_steal_exc_port (proc, inf->event_port);
  473. else
  474. /* Just clear thread exception ports -- they default to the
  475. task one. */
  476. proc_steal_exc_port (proc, MACH_PORT_NULL);
  477. }
  478. return proc;
  479. }
  480. /* Frees PROC and any resources it uses, and returns the value of PROC's
  481. next field. */
  482. struct proc *
  483. gnu_nat_target::_proc_free (struct proc *proc)
  484. {
  485. struct inf *inf = proc->inf;
  486. struct proc *next = proc->next;
  487. proc_debug (proc, "freeing...");
  488. if (proc == inf->step_thread)
  489. /* Turn off single stepping. */
  490. inf_set_step_thread (inf, 0);
  491. if (proc == inf->wait.thread)
  492. inf_clear_wait (inf);
  493. if (proc == inf->signal_thread)
  494. inf->signal_thread = 0;
  495. if (proc->port != MACH_PORT_NULL)
  496. {
  497. if (proc->exc_port != MACH_PORT_NULL)
  498. /* Restore the original exception port. */
  499. proc_restore_exc_port (proc);
  500. if (proc->cur_sc != 0)
  501. /* Resume the thread/task. */
  502. {
  503. proc->sc = 0;
  504. proc_update_sc (proc);
  505. }
  506. mach_port_deallocate (mach_task_self (), proc->port);
  507. }
  508. xfree (proc);
  509. return next;
  510. }
  511. static struct inf *
  512. make_inf (void)
  513. {
  514. struct inf *inf = new struct inf;
  515. inf->task = 0;
  516. inf->threads = 0;
  517. inf->threads_up_to_date = 0;
  518. inf->pid = 0;
  519. inf->wait.status.set_spurious ();
  520. inf->wait.thread = 0;
  521. inf->wait.exc.handler = MACH_PORT_NULL;
  522. inf->wait.exc.reply = MACH_PORT_NULL;
  523. inf->step_thread = 0;
  524. inf->signal_thread = 0;
  525. inf->event_port = MACH_PORT_NULL;
  526. inf->running = 0;
  527. inf->stopped = 0;
  528. inf->nomsg = 1;
  529. inf->traced = 0;
  530. inf->no_wait = 0;
  531. inf->pending_execs = 0;
  532. inf->pause_sc = 1;
  533. inf->detach_sc = 0;
  534. inf->default_thread_run_sc = 0;
  535. inf->default_thread_pause_sc = 0;
  536. inf->default_thread_detach_sc = 0;
  537. inf->want_signals = 1; /* By default */
  538. inf->want_exceptions = 1; /* By default */
  539. return inf;
  540. }
  541. /* Clear INF's target wait status. */
  542. void
  543. gnu_nat_target::inf_clear_wait (struct inf *inf)
  544. {
  545. inf_debug (inf, "clearing wait");
  546. inf->wait.status.set_spurious ();
  547. inf->wait.thread = 0;
  548. inf->wait.suppress = 0;
  549. if (inf->wait.exc.handler != MACH_PORT_NULL)
  550. {
  551. mach_port_deallocate (mach_task_self (), inf->wait.exc.handler);
  552. inf->wait.exc.handler = MACH_PORT_NULL;
  553. }
  554. if (inf->wait.exc.reply != MACH_PORT_NULL)
  555. {
  556. mach_port_deallocate (mach_task_self (), inf->wait.exc.reply);
  557. inf->wait.exc.reply = MACH_PORT_NULL;
  558. }
  559. }
  560. void
  561. gnu_nat_target::inf_cleanup (struct inf *inf)
  562. {
  563. inf_debug (inf, "cleanup");
  564. inf_clear_wait (inf);
  565. inf_set_pid (inf, -1);
  566. inf->pid = 0;
  567. inf->running = 0;
  568. inf->stopped = 0;
  569. inf->nomsg = 1;
  570. inf->traced = 0;
  571. inf->no_wait = 0;
  572. inf->pending_execs = 0;
  573. if (inf->event_port)
  574. {
  575. mach_port_destroy (mach_task_self (), inf->event_port);
  576. inf->event_port = MACH_PORT_NULL;
  577. }
  578. }
  579. void
  580. gnu_nat_target::inf_startup (struct inf *inf, int pid)
  581. {
  582. kern_return_t err;
  583. inf_debug (inf, "startup: pid = %d", pid);
  584. inf_cleanup (inf);
  585. /* Make the port on which we receive all events. */
  586. err = mach_port_allocate (mach_task_self (),
  587. MACH_PORT_RIGHT_RECEIVE, &inf->event_port);
  588. if (err)
  589. error (_("Error allocating event port: %s"), safe_strerror (err));
  590. /* Make a send right for it, so we can easily copy it for other people. */
  591. mach_port_insert_right (mach_task_self (), inf->event_port,
  592. inf->event_port, MACH_MSG_TYPE_MAKE_SEND);
  593. inf_set_pid (inf, pid);
  594. }
  595. /* Close current process, if any, and attach INF to process PORT. */
  596. void
  597. gnu_nat_target::inf_set_pid (struct inf *inf, pid_t pid)
  598. {
  599. task_t task_port;
  600. struct proc *task = inf->task;
  601. inf_debug (inf, "setting pid: %d", pid);
  602. if (pid < 0)
  603. task_port = MACH_PORT_NULL;
  604. else
  605. {
  606. kern_return_t err = proc_pid2task (proc_server, pid, &task_port);
  607. if (err)
  608. error (_("Error getting task for pid %d: %s"),
  609. pid, safe_strerror (err));
  610. }
  611. inf_debug (inf, "setting task: %lu", task_port);
  612. if (inf->pause_sc)
  613. task_suspend (task_port);
  614. if (task && task->port != task_port)
  615. {
  616. inf->task = 0;
  617. inf_validate_procs (inf); /* Trash all the threads. */
  618. _proc_free (task); /* And the task. */
  619. }
  620. if (task_port != MACH_PORT_NULL)
  621. {
  622. inf->task = make_proc (inf, task_port, PROC_TID_TASK);
  623. inf->threads_up_to_date = 0;
  624. }
  625. if (inf->task)
  626. {
  627. inf->pid = pid;
  628. if (inf->pause_sc)
  629. /* Reflect task_suspend above. */
  630. inf->task->sc = inf->task->cur_sc = 1;
  631. }
  632. else
  633. inf->pid = -1;
  634. }
  635. /* Validates INF's stopped, nomsg and traced field from the actual
  636. proc server state. Note that the traced field is only updated from
  637. the proc server state if we do not have a message port. If we do
  638. have a message port we'd better look at the tracemask itself. */
  639. void
  640. gnu_nat_target::inf_validate_procinfo (struct inf *inf)
  641. {
  642. char *noise;
  643. mach_msg_type_number_t noise_len = 0;
  644. struct procinfo *pi;
  645. mach_msg_type_number_t pi_len = 0;
  646. int info_flags = 0;
  647. kern_return_t err =
  648. proc_getprocinfo (proc_server, inf->pid, &info_flags,
  649. (procinfo_t *) &pi, &pi_len, &noise, &noise_len);
  650. if (!err)
  651. {
  652. inf->stopped = !!(pi->state & PI_STOPPED);
  653. inf->nomsg = !!(pi->state & PI_NOMSG);
  654. if (inf->nomsg)
  655. inf->traced = !!(pi->state & PI_TRACED);
  656. vm_deallocate (mach_task_self (), (vm_address_t) pi,
  657. pi_len * sizeof (*(procinfo_t) 0));
  658. if (noise_len > 0)
  659. vm_deallocate (mach_task_self (), (vm_address_t) noise, noise_len);
  660. }
  661. }
  662. /* Validates INF's task suspend count. If it's higher than we expect,
  663. verify with the user before `stealing' the extra count. */
  664. void
  665. gnu_nat_target::inf_validate_task_sc (struct inf *inf)
  666. {
  667. char *noise;
  668. mach_msg_type_number_t noise_len = 0;
  669. struct procinfo *pi;
  670. mach_msg_type_number_t pi_len = 0;
  671. int info_flags = PI_FETCH_TASKINFO;
  672. int suspend_count = -1;
  673. kern_return_t err;
  674. retry:
  675. err = proc_getprocinfo (proc_server, inf->pid, &info_flags,
  676. (procinfo_t *) &pi, &pi_len, &noise, &noise_len);
  677. if (err)
  678. {
  679. inf->task->dead = 1; /* oh well */
  680. return;
  681. }
  682. if (inf->task->cur_sc < pi->taskinfo.suspend_count && suspend_count == -1)
  683. {
  684. /* The proc server might have suspended the task while stopping
  685. it. This happens when the task is handling a traced signal.
  686. Refetch the suspend count. The proc server should be
  687. finished stopping the task by now. */
  688. suspend_count = pi->taskinfo.suspend_count;
  689. goto retry;
  690. }
  691. suspend_count = pi->taskinfo.suspend_count;
  692. vm_deallocate (mach_task_self (), (vm_address_t) pi,
  693. pi_len * sizeof (*(procinfo_t) 0));
  694. if (noise_len > 0)
  695. vm_deallocate (mach_task_self (), (vm_address_t) noise, noise_len);
  696. if (inf->task->cur_sc < suspend_count)
  697. {
  698. if (!query (_("Pid %d has an additional task suspend count of %d;"
  699. " clear it? "), inf->pid,
  700. suspend_count - inf->task->cur_sc))
  701. error (_("Additional task suspend count left untouched."));
  702. inf->task->cur_sc = suspend_count;
  703. }
  704. }
  705. /* Turns tracing for INF on or off, depending on ON, unless it already
  706. is. If INF is running, the resume_sc count of INF's threads will
  707. be modified, and the signal thread will briefly be run to change
  708. the trace state. */
  709. void
  710. gnu_nat_target::inf_set_traced (struct inf *inf, int on)
  711. {
  712. if (on == inf->traced)
  713. return;
  714. if (inf->task && !inf->task->dead)
  715. /* Make it take effect immediately. */
  716. {
  717. sigset_t mask = on ? ~(sigset_t) 0 : 0;
  718. kern_return_t err =
  719. INF_RESUME_MSGPORT_RPC (inf, msg_set_init_int (msgport, refport,
  720. INIT_TRACEMASK, mask));
  721. if (err == EIEIO)
  722. {
  723. if (on)
  724. warning (_("Can't modify tracing state for pid %d: %s"),
  725. inf->pid, "No signal thread");
  726. inf->traced = on;
  727. }
  728. else if (err)
  729. warning (_("Can't modify tracing state for pid %d: %s"),
  730. inf->pid, safe_strerror (err));
  731. else
  732. inf->traced = on;
  733. }
  734. else
  735. inf->traced = on;
  736. }
  737. /* Makes all the real suspend count deltas of all the procs in INF
  738. match the desired values. Careful to always do thread/task suspend
  739. counts in the safe order. Returns true if at least one thread is
  740. thought to be running. */
  741. int
  742. gnu_nat_target::inf_update_suspends (struct inf *inf)
  743. {
  744. struct proc *task = inf->task;
  745. /* We don't have to update INF->threads even though we're iterating over it
  746. because we'll change a thread only if it already has an existing proc
  747. entry. */
  748. inf_debug (inf, "updating suspend counts");
  749. if (task)
  750. {
  751. struct proc *thread;
  752. int task_running = (task->sc == 0), thread_running = 0;
  753. if (task->sc > task->cur_sc)
  754. /* The task is becoming _more_ suspended; do before any threads. */
  755. task_running = proc_update_sc (task);
  756. if (inf->pending_execs)
  757. /* When we're waiting for an exec, things may be happening behind our
  758. back, so be conservative. */
  759. thread_running = 1;
  760. /* Do all the thread suspend counts. */
  761. for (thread = inf->threads; thread; thread = thread->next)
  762. thread_running |= proc_update_sc (thread);
  763. if (task->sc != task->cur_sc)
  764. /* We didn't do the task first, because we wanted to wait for the
  765. threads; do it now. */
  766. task_running = proc_update_sc (task);
  767. inf_debug (inf, "%srunning...",
  768. (thread_running && task_running) ? "" : "not ");
  769. inf->running = thread_running && task_running;
  770. /* Once any thread has executed some code, we can't depend on the
  771. threads list any more. */
  772. if (inf->running)
  773. inf->threads_up_to_date = 0;
  774. return inf->running;
  775. }
  776. return 0;
  777. }
  778. /* Converts a GDB pid to a struct proc. */
  779. struct proc *
  780. inf_tid_to_thread (struct inf *inf, int tid)
  781. {
  782. struct proc *thread = inf->threads;
  783. while (thread)
  784. if (thread->tid == tid)
  785. return thread;
  786. else
  787. thread = thread->next;
  788. return 0;
  789. }
  790. /* Converts a thread port to a struct proc. */
  791. static struct proc *
  792. inf_port_to_thread (struct inf *inf, mach_port_t port)
  793. {
  794. struct proc *thread = inf->threads;
  795. while (thread)
  796. if (thread->port == port)
  797. return thread;
  798. else
  799. thread = thread->next;
  800. return 0;
  801. }
  802. /* See gnu-nat.h. */
  803. void
  804. inf_threads (struct inf *inf, inf_threads_ftype *f, void *arg)
  805. {
  806. struct proc *thread;
  807. for (thread = inf->threads; thread; thread = thread->next)
  808. f (thread, arg);
  809. }
  810. /* Make INF's list of threads be consistent with reality of TASK. */
  811. void
  812. gnu_nat_target::inf_validate_procs (struct inf *inf)
  813. {
  814. thread_array_t threads;
  815. mach_msg_type_number_t num_threads, i;
  816. struct proc *task = inf->task;
  817. /* If no threads are currently running, this function will guarantee that
  818. things are up to date. The exception is if there are zero threads --
  819. then it is almost certainly in an odd state, and probably some outside
  820. agent will create threads. */
  821. inf->threads_up_to_date = inf->threads ? !inf->running : 0;
  822. if (task)
  823. {
  824. kern_return_t err = task_threads (task->port, &threads, &num_threads);
  825. inf_debug (inf, "fetching threads");
  826. if (err)
  827. /* TASK must be dead. */
  828. {
  829. task->dead = 1;
  830. task = 0;
  831. }
  832. }
  833. if (!task)
  834. {
  835. num_threads = 0;
  836. inf_debug (inf, "no task");
  837. }
  838. {
  839. /* Make things normally linear. */
  840. mach_msg_type_number_t search_start = 0;
  841. /* Which thread in PROCS corresponds to each task thread, & the task. */
  842. struct proc *matched[num_threads + 1];
  843. /* The last thread in INF->threads, so we can add to the end. */
  844. struct proc *last = 0;
  845. /* The current thread we're considering. */
  846. struct proc *thread = inf->threads;
  847. memset (matched, 0, sizeof (matched));
  848. while (thread)
  849. {
  850. mach_msg_type_number_t left;
  851. for (i = search_start, left = num_threads; left; i++, left--)
  852. {
  853. if (i >= num_threads)
  854. i -= num_threads; /* I wrapped around. */
  855. if (thread->port == threads[i])
  856. /* We already know about this thread. */
  857. {
  858. matched[i] = thread;
  859. last = thread;
  860. thread = thread->next;
  861. search_start++;
  862. break;
  863. }
  864. }
  865. if (!left)
  866. {
  867. proc_debug (thread, "died!");
  868. thread->port = MACH_PORT_NULL;
  869. thread = _proc_free (thread); /* THREAD is dead. */
  870. if (last)
  871. last->next = thread;
  872. else
  873. inf->threads = thread;
  874. }
  875. }
  876. for (i = 0; i < num_threads; i++)
  877. {
  878. if (matched[i])
  879. /* Throw away the duplicate send right. */
  880. mach_port_deallocate (mach_task_self (), threads[i]);
  881. else
  882. /* THREADS[I] is a thread we don't know about yet! */
  883. {
  884. ptid_t ptid;
  885. thread = make_proc (inf, threads[i], next_thread_id++);
  886. if (last)
  887. last->next = thread;
  888. else
  889. inf->threads = thread;
  890. last = thread;
  891. proc_debug (thread, "new thread: %lu", threads[i]);
  892. ptid = ptid_t (inf->pid, thread->tid, 0);
  893. /* Tell GDB's generic thread code. */
  894. if (inferior_ptid == ptid_t (inf->pid))
  895. /* This is the first time we're hearing about thread
  896. ids, after a fork-child. */
  897. thread_change_ptid (this, inferior_ptid, ptid);
  898. else if (inf->pending_execs != 0)
  899. /* This is a shell thread. */
  900. add_thread_silent (this, ptid);
  901. else
  902. add_thread (this, ptid);
  903. }
  904. }
  905. vm_deallocate (mach_task_self (),
  906. (vm_address_t) threads, (num_threads * sizeof (thread_t)));
  907. }
  908. }
  909. /* Makes sure that INF's thread list is synced with the actual process. */
  910. int
  911. inf_update_procs (struct inf *inf)
  912. {
  913. if (!inf->task)
  914. return 0;
  915. if (!inf->threads_up_to_date)
  916. gnu_target->inf_validate_procs (inf);
  917. return !!inf->task;
  918. }
  919. /* Sets the resume_sc of each thread in inf. That of RUN_THREAD is set to 0,
  920. and others are set to their run_sc if RUN_OTHERS is true, and otherwise
  921. their pause_sc. */
  922. void
  923. gnu_nat_target::inf_set_threads_resume_sc (struct inf *inf,
  924. struct proc *run_thread, int run_others)
  925. {
  926. struct proc *thread;
  927. inf_update_procs (inf);
  928. for (thread = inf->threads; thread; thread = thread->next)
  929. if (thread == run_thread)
  930. thread->resume_sc = 0;
  931. else if (run_others)
  932. thread->resume_sc = thread->run_sc;
  933. else
  934. thread->resume_sc = thread->pause_sc;
  935. }
  936. /* Cause INF to continue execution immediately; individual threads may still
  937. be suspended (but their suspend counts will be updated). */
  938. void
  939. gnu_nat_target::inf_resume (struct inf *inf)
  940. {
  941. struct proc *thread;
  942. inf_update_procs (inf);
  943. for (thread = inf->threads; thread; thread = thread->next)
  944. thread->sc = thread->resume_sc;
  945. if (inf->task)
  946. {
  947. if (!inf->pending_execs)
  948. /* Try to make sure our task count is correct -- in the case where
  949. we're waiting for an exec though, things are too volatile, so just
  950. assume things will be reasonable (which they usually will be). */
  951. inf_validate_task_sc (inf);
  952. inf->task->sc = 0;
  953. }
  954. inf_update_suspends (inf);
  955. }
  956. /* Cause INF to stop execution immediately; individual threads may still
  957. be running. */
  958. void
  959. gnu_nat_target::inf_suspend (struct inf *inf)
  960. {
  961. struct proc *thread;
  962. inf_update_procs (inf);
  963. for (thread = inf->threads; thread; thread = thread->next)
  964. thread->sc = thread->pause_sc;
  965. if (inf->task)
  966. inf->task->sc = inf->pause_sc;
  967. inf_update_suspends (inf);
  968. }
  969. /* INF has one thread PROC that is in single-stepping mode. This
  970. function changes it to be PROC, changing any old step_thread to be
  971. a normal one. A PROC of 0 clears any existing value. */
  972. void
  973. gnu_nat_target::inf_set_step_thread (struct inf *inf, struct proc *thread)
  974. {
  975. gdb_assert (!thread || proc_is_thread (thread));
  976. if (thread)
  977. inf_debug (inf, "setting step thread: %d/%d", inf->pid, thread->tid);
  978. else
  979. inf_debug (inf, "clearing step thread");
  980. if (inf->step_thread != thread)
  981. {
  982. if (inf->step_thread && inf->step_thread->port != MACH_PORT_NULL)
  983. if (!proc_trace (inf->step_thread, 0))
  984. return;
  985. if (thread && proc_trace (thread, 1))
  986. inf->step_thread = thread;
  987. else
  988. inf->step_thread = 0;
  989. }
  990. }
  991. /* Set up the thread resume_sc's so that only the signal thread is running
  992. (plus whatever other thread are set to always run). Returns true if we
  993. did so, or false if we can't find a signal thread. */
  994. int
  995. gnu_nat_target::inf_set_threads_resume_sc_for_signal_thread (struct inf *inf)
  996. {
  997. if (inf->signal_thread)
  998. {
  999. inf_set_threads_resume_sc (inf, inf->signal_thread, 0);
  1000. return 1;
  1001. }
  1002. else
  1003. return 0;
  1004. }
  1005. static void
  1006. inf_update_signal_thread (struct inf *inf)
  1007. {
  1008. /* XXX for now we assume that if there's a msgport, the 2nd thread is
  1009. the signal thread. */
  1010. inf->signal_thread = inf->threads ? inf->threads->next : 0;
  1011. }
  1012. /* Detachs from INF's inferior task, letting it run once again... */
  1013. void
  1014. gnu_nat_target::inf_detach (struct inf *inf)
  1015. {
  1016. struct proc *task = inf->task;
  1017. inf_debug (inf, "detaching...");
  1018. inf_clear_wait (inf);
  1019. inf_set_step_thread (inf, 0);
  1020. if (task)
  1021. {
  1022. struct proc *thread;
  1023. inf_validate_procinfo (inf);
  1024. inf_set_traced (inf, 0);
  1025. if (inf->stopped)
  1026. {
  1027. if (inf->nomsg)
  1028. inf_continue (inf);
  1029. else
  1030. inf_signal (inf, GDB_SIGNAL_0);
  1031. }
  1032. proc_restore_exc_port (task);
  1033. task->sc = inf->detach_sc;
  1034. for (thread = inf->threads; thread; thread = thread->next)
  1035. {
  1036. proc_restore_exc_port (thread);
  1037. thread->sc = thread->detach_sc;
  1038. }
  1039. inf_update_suspends (inf);
  1040. }
  1041. inf_cleanup (inf);
  1042. }
  1043. /* Attaches INF to the process with process id PID, returning it in a
  1044. suspended state suitable for debugging. */
  1045. void
  1046. gnu_nat_target::inf_attach (struct inf *inf, int pid)
  1047. {
  1048. inf_debug (inf, "attaching: %d", pid);
  1049. if (inf->pid)
  1050. inf_detach (inf);
  1051. inf_startup (inf, pid);
  1052. }
  1053. /* Makes sure that we've got our exception ports entrenched in the process. */
  1054. void
  1055. gnu_nat_target::inf_steal_exc_ports (struct inf *inf)
  1056. {
  1057. struct proc *thread;
  1058. inf_debug (inf, "stealing exception ports");
  1059. inf_set_step_thread (inf, 0); /* The step thread is special. */
  1060. proc_steal_exc_port (inf->task, inf->event_port);
  1061. for (thread = inf->threads; thread; thread = thread->next)
  1062. proc_steal_exc_port (thread, MACH_PORT_NULL);
  1063. }
  1064. /* Makes sure the process has its own exception ports. */
  1065. void
  1066. gnu_nat_target::inf_restore_exc_ports (struct inf *inf)
  1067. {
  1068. struct proc *thread;
  1069. inf_debug (inf, "restoring exception ports");
  1070. inf_set_step_thread (inf, 0); /* The step thread is special. */
  1071. proc_restore_exc_port (inf->task);
  1072. for (thread = inf->threads; thread; thread = thread->next)
  1073. proc_restore_exc_port (thread);
  1074. }
  1075. /* Deliver signal SIG to INF. If INF is stopped, delivering a signal, even
  1076. signal 0, will continue it. INF is assumed to be in a paused state, and
  1077. the resume_sc's of INF's threads may be affected. */
  1078. void
  1079. gnu_nat_target::inf_signal (struct inf *inf, enum gdb_signal sig)
  1080. {
  1081. kern_return_t err = 0;
  1082. int host_sig = gdb_signal_to_host (sig);
  1083. #define NAME gdb_signal_to_name (sig)
  1084. if (host_sig >= _NSIG)
  1085. /* A mach exception. Exceptions are encoded in the signal space by
  1086. putting them after _NSIG; this assumes they're positive (and not
  1087. extremely large)! */
  1088. {
  1089. struct inf_wait *w = &inf->wait;
  1090. if (w->status.kind () == TARGET_WAITKIND_STOPPED
  1091. && w->status.sig () == sig
  1092. && w->thread && !w->thread->aborted)
  1093. /* We're passing through the last exception we received. This is
  1094. kind of bogus, because exceptions are per-thread whereas gdb
  1095. treats signals as per-process. We just forward the exception to
  1096. the correct handler, even it's not for the same thread as TID --
  1097. i.e., we pretend it's global. */
  1098. {
  1099. struct exc_state *e = &w->exc;
  1100. inf_debug (inf, "passing through exception:"
  1101. " task = %lu, thread = %lu, exc = %d"
  1102. ", code = %d, subcode = %d",
  1103. w->thread->port, inf->task->port,
  1104. e->exception, e->code, e->subcode);
  1105. err =
  1106. exception_raise_request (e->handler,
  1107. e->reply, MACH_MSG_TYPE_MOVE_SEND_ONCE,
  1108. w->thread->port, inf->task->port,
  1109. e->exception, e->code, e->subcode);
  1110. }
  1111. else
  1112. error (_("Can't forward spontaneous exception (%s)."), NAME);
  1113. }
  1114. else
  1115. /* A Unix signal. */
  1116. if (inf->stopped)
  1117. /* The process is stopped and expecting a signal. Just send off a
  1118. request and let it get handled when we resume everything. */
  1119. {
  1120. inf_debug (inf, "sending %s to stopped process", NAME);
  1121. err =
  1122. INF_MSGPORT_RPC (inf,
  1123. msg_sig_post_untraced_request (msgport,
  1124. inf->event_port,
  1125. MACH_MSG_TYPE_MAKE_SEND_ONCE,
  1126. host_sig, 0,
  1127. refport));
  1128. if (!err)
  1129. /* Posting an untraced signal automatically continues it.
  1130. We clear this here rather than when we get the reply
  1131. because we'd rather assume it's not stopped when it
  1132. actually is, than the reverse. */
  1133. inf->stopped = 0;
  1134. }
  1135. else
  1136. /* It's not expecting it. We have to let just the signal thread
  1137. run, and wait for it to get into a reasonable state before we
  1138. can continue the rest of the process. When we finally resume the
  1139. process the signal we request will be the very first thing that
  1140. happens. */
  1141. {
  1142. inf_debug (inf, "sending %s to unstopped process"
  1143. " (so resuming signal thread)", NAME);
  1144. err =
  1145. INF_RESUME_MSGPORT_RPC (inf,
  1146. msg_sig_post_untraced (msgport, host_sig,
  1147. 0, refport));
  1148. }
  1149. if (err == EIEIO)
  1150. /* Can't do too much... */
  1151. warning (_("Can't deliver signal %s: No signal thread."), NAME);
  1152. else if (err)
  1153. warning (_("Delivering signal %s: %s"), NAME, safe_strerror (err));
  1154. #undef NAME
  1155. }
  1156. /* Continue INF without delivering a signal. This is meant to be used
  1157. when INF does not have a message port. */
  1158. void
  1159. gnu_nat_target::inf_continue (struct inf *inf)
  1160. {
  1161. process_t proc;
  1162. kern_return_t err = proc_pid2proc (proc_server, inf->pid, &proc);
  1163. if (!err)
  1164. {
  1165. inf_debug (inf, "continuing process");
  1166. err = proc_mark_cont (proc);
  1167. if (!err)
  1168. {
  1169. struct proc *thread;
  1170. for (thread = inf->threads; thread; thread = thread->next)
  1171. thread_resume (thread->port);
  1172. inf->stopped = 0;
  1173. }
  1174. }
  1175. if (err)
  1176. warning (_("Can't continue process: %s"), safe_strerror (err));
  1177. }
  1178. /* The inferior used for all gdb target ops. */
  1179. struct inf *gnu_current_inf = 0;
  1180. /* The inferior being waited for by gnu_wait. Since GDB is decidely not
  1181. multi-threaded, we don't bother to lock this. */
  1182. static struct inf *waiting_inf;
  1183. /* Wait for something to happen in the inferior, returning what in STATUS. */
  1184. ptid_t
  1185. gnu_nat_target::wait (ptid_t ptid, struct target_waitstatus *status,
  1186. target_wait_flags options)
  1187. {
  1188. struct msg
  1189. {
  1190. mach_msg_header_t hdr;
  1191. mach_msg_type_t type;
  1192. int data[8000];
  1193. } msg;
  1194. kern_return_t err;
  1195. struct proc *thread;
  1196. struct inf *inf = gnu_current_inf;
  1197. gdb_assert (inf->task);
  1198. if (!inf->threads && !inf->pending_execs)
  1199. /* No threads! Assume that maybe some outside agency is frobbing our
  1200. task, and really look for new threads. If we can't find any, just tell
  1201. the user to try again later. */
  1202. {
  1203. inf_validate_procs (inf);
  1204. if (!inf->threads && !inf->task->dead)
  1205. error (_("There are no threads; try again later."));
  1206. }
  1207. waiting_inf = inf;
  1208. inf_debug (inf, "waiting for: %s", ptid.to_string ().c_str ());
  1209. rewait:
  1210. if (proc_wait_pid != inf->pid && !inf->no_wait)
  1211. /* Always get information on events from the proc server. */
  1212. {
  1213. inf_debug (inf, "requesting wait on pid %d", inf->pid);
  1214. if (proc_wait_pid)
  1215. /* The proc server is single-threaded, and only allows a single
  1216. outstanding wait request, so we have to cancel the previous one. */
  1217. {
  1218. inf_debug (inf, "cancelling previous wait on pid %d", proc_wait_pid);
  1219. interrupt_operation (proc_server, 0);
  1220. }
  1221. err =
  1222. proc_wait_request (proc_server, inf->event_port, inf->pid, WUNTRACED);
  1223. if (err)
  1224. warning (_("wait request failed: %s"), safe_strerror (err));
  1225. else
  1226. {
  1227. inf_debug (inf, "waits pending: %d", proc_waits_pending);
  1228. proc_wait_pid = inf->pid;
  1229. /* Even if proc_waits_pending was > 0 before, we still won't
  1230. get any other replies, because it was either from a
  1231. different INF, or a different process attached to INF --
  1232. and the event port, which is the wait reply port, changes
  1233. when you switch processes. */
  1234. proc_waits_pending = 1;
  1235. }
  1236. }
  1237. inf_clear_wait (inf);
  1238. /* What can happen? (1) Dead name notification; (2) Exceptions arrive;
  1239. (3) wait reply from the proc server. */
  1240. inf_debug (inf, "waiting for an event...");
  1241. err = mach_msg (&msg.hdr, MACH_RCV_MSG | MACH_RCV_INTERRUPT,
  1242. 0, sizeof (struct msg), inf->event_port,
  1243. MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
  1244. /* Re-suspend the task. */
  1245. inf_suspend (inf);
  1246. if (!inf->task && inf->pending_execs)
  1247. /* When doing an exec, it's possible that the old task wasn't reused
  1248. (e.g., setuid execs). So if the task seems to have disappeared,
  1249. attempt to refetch it, as the pid should still be the same. */
  1250. inf_set_pid (inf, inf->pid);
  1251. if (err == EMACH_RCV_INTERRUPTED)
  1252. inf_debug (inf, "interrupted");
  1253. else if (err)
  1254. error (_("Couldn't wait for an event: %s"), safe_strerror (err));
  1255. else
  1256. {
  1257. struct
  1258. {
  1259. mach_msg_header_t hdr;
  1260. mach_msg_type_t err_type;
  1261. kern_return_t err;
  1262. char noise[200];
  1263. }
  1264. reply;
  1265. inf_debug (inf, "event: msgid = %d", msg.hdr.msgh_id);
  1266. /* Handle what we got. */
  1267. if (!notify_server (&msg.hdr, &reply.hdr)
  1268. && !exc_server (&msg.hdr, &reply.hdr)
  1269. && !process_reply_server (&msg.hdr, &reply.hdr)
  1270. && !msg_reply_server (&msg.hdr, &reply.hdr))
  1271. /* Whatever it is, it's something strange. */
  1272. error (_("Got a strange event, msg id = %d."), msg.hdr.msgh_id);
  1273. if (reply.err)
  1274. error (_("Handling event, msgid = %d: %s"),
  1275. msg.hdr.msgh_id, safe_strerror (reply.err));
  1276. }
  1277. if (inf->pending_execs)
  1278. /* We're waiting for the inferior to finish execing. */
  1279. {
  1280. struct inf_wait *w = &inf->wait;
  1281. enum target_waitkind kind = w->status.kind ();
  1282. if (kind == TARGET_WAITKIND_SPURIOUS)
  1283. /* Since gdb is actually counting the number of times the inferior
  1284. stops, expecting one stop per exec, we only return major events
  1285. while execing. */
  1286. {
  1287. w->suppress = 1;
  1288. inf_debug (inf, "pending_execs, ignoring minor event");
  1289. }
  1290. else if (kind == TARGET_WAITKIND_STOPPED
  1291. && w->status.sig () == GDB_SIGNAL_TRAP)
  1292. /* Ah hah! A SIGTRAP from the inferior while starting up probably
  1293. means we've succesfully completed an exec! */
  1294. {
  1295. inf_debug (inf, "one pending exec completed");
  1296. }
  1297. else if (kind == TARGET_WAITKIND_STOPPED)
  1298. /* It's possible that this signal is because of a crashed process
  1299. being handled by the hurd crash server; in this case, the process
  1300. will have an extra task suspend, which we need to know about.
  1301. Since the code in inf_resume that normally checks for this is
  1302. disabled while INF->pending_execs, we do the check here instead. */
  1303. inf_validate_task_sc (inf);
  1304. }
  1305. if (inf->wait.suppress)
  1306. /* Some totally spurious event happened that we don't consider
  1307. worth returning to gdb. Just keep waiting. */
  1308. {
  1309. inf_debug (inf, "suppressing return, rewaiting...");
  1310. inf_resume (inf);
  1311. goto rewait;
  1312. }
  1313. /* Pass back out our results. */
  1314. *status = inf->wait.status;
  1315. thread = inf->wait.thread;
  1316. if (thread)
  1317. ptid = ptid_t (inf->pid, thread->tid, 0);
  1318. else if (ptid == minus_one_ptid)
  1319. thread = inf_tid_to_thread (inf, -1);
  1320. else
  1321. thread = inf_tid_to_thread (inf, ptid.lwp ());
  1322. if (!thread || thread->port == MACH_PORT_NULL)
  1323. {
  1324. /* TID is dead; try and find a new thread. */
  1325. if (inf_update_procs (inf) && inf->threads)
  1326. ptid = ptid_t (inf->pid, inf->threads->tid, 0); /* The first
  1327. available
  1328. thread. */
  1329. else
  1330. {
  1331. /* The process exited. */
  1332. ptid = ptid_t (inf->pid);
  1333. }
  1334. }
  1335. if (thread
  1336. && ptid != minus_one_ptid
  1337. && status->kind () != TARGET_WAITKIND_SPURIOUS
  1338. && inf->pause_sc == 0 && thread->pause_sc == 0)
  1339. /* If something actually happened to THREAD, make sure we
  1340. suspend it. */
  1341. {
  1342. thread->sc = 1;
  1343. inf_update_suspends (inf);
  1344. }
  1345. inf_debug (inf, "returning ptid = %s, %s",
  1346. ptid.to_string ().c_str (),
  1347. status->to_string ().c_str ());
  1348. return ptid;
  1349. }
  1350. /* The rpc handler called by exc_server. */
  1351. kern_return_t
  1352. S_exception_raise_request (mach_port_t port, mach_port_t reply_port,
  1353. thread_t thread_port, task_t task_port,
  1354. int exception, int code, int subcode)
  1355. {
  1356. struct inf *inf = waiting_inf;
  1357. struct proc *thread = inf_port_to_thread (inf, thread_port);
  1358. inf_debug (waiting_inf,
  1359. "thread = %lu, task = %lu, exc = %d, code = %d, subcode = %d",
  1360. thread_port, task_port, exception, code, subcode);
  1361. if (!thread)
  1362. /* We don't know about thread? */
  1363. {
  1364. inf_update_procs (inf);
  1365. thread = inf_port_to_thread (inf, thread_port);
  1366. if (!thread)
  1367. /* Give up, the generating thread is gone. */
  1368. return 0;
  1369. }
  1370. mach_port_deallocate (mach_task_self (), thread_port);
  1371. mach_port_deallocate (mach_task_self (), task_port);
  1372. if (!thread->aborted)
  1373. /* THREAD hasn't been aborted since this exception happened (abortion
  1374. clears any exception state), so it must be real. */
  1375. {
  1376. /* Store away the details; this will destroy any previous info. */
  1377. inf->wait.thread = thread;
  1378. if (exception == EXC_BREAKPOINT)
  1379. /* GDB likes to get SIGTRAP for breakpoints. */
  1380. {
  1381. inf->wait.status.set_stopped (GDB_SIGNAL_TRAP);
  1382. mach_port_deallocate (mach_task_self (), reply_port);
  1383. }
  1384. else
  1385. /* Record the exception so that we can forward it later. */
  1386. {
  1387. if (thread->exc_port == port)
  1388. {
  1389. inf_debug (waiting_inf, "Handler is thread exception port <%lu>",
  1390. thread->saved_exc_port);
  1391. inf->wait.exc.handler = thread->saved_exc_port;
  1392. }
  1393. else
  1394. {
  1395. inf_debug (waiting_inf, "Handler is task exception port <%lu>",
  1396. inf->task->saved_exc_port);
  1397. inf->wait.exc.handler = inf->task->saved_exc_port;
  1398. gdb_assert (inf->task->exc_port == port);
  1399. }
  1400. if (inf->wait.exc.handler != MACH_PORT_NULL)
  1401. /* Add a reference to the exception handler. */
  1402. mach_port_mod_refs (mach_task_self (),
  1403. inf->wait.exc.handler, MACH_PORT_RIGHT_SEND,
  1404. 1);
  1405. inf->wait.exc.exception = exception;
  1406. inf->wait.exc.code = code;
  1407. inf->wait.exc.subcode = subcode;
  1408. inf->wait.exc.reply = reply_port;
  1409. /* Exceptions are encoded in the signal space by putting
  1410. them after _NSIG; this assumes they're positive (and not
  1411. extremely large)! */
  1412. inf->wait.status.set_stopped
  1413. (gdb_signal_from_host (_NSIG + exception));
  1414. }
  1415. }
  1416. else
  1417. /* A suppressed exception, which ignore. */
  1418. {
  1419. inf->wait.suppress = 1;
  1420. mach_port_deallocate (mach_task_self (), reply_port);
  1421. }
  1422. return 0;
  1423. }
  1424. /* Fill in INF's wait field after a task has died without giving us more
  1425. detailed information. */
  1426. static void
  1427. inf_task_died_status (struct inf *inf)
  1428. {
  1429. warning (_("Pid %d died with unknown exit status, using SIGKILL."),
  1430. inf->pid);
  1431. inf->wait.status.set_signalled (GDB_SIGNAL_KILL);
  1432. }
  1433. /* Notify server routines. The only real one is dead name notification. */
  1434. kern_return_t
  1435. do_mach_notify_dead_name (mach_port_t notify, mach_port_t dead_port)
  1436. {
  1437. struct inf *inf = waiting_inf;
  1438. inf_debug (waiting_inf, "port = %lu", dead_port);
  1439. if (inf->task && inf->task->port == dead_port)
  1440. {
  1441. proc_debug (inf->task, "is dead");
  1442. inf->task->port = MACH_PORT_NULL;
  1443. if (proc_wait_pid == inf->pid)
  1444. /* We have a wait outstanding on the process, which will return more
  1445. detailed information, so delay until we get that. */
  1446. inf->wait.suppress = 1;
  1447. else
  1448. /* We never waited for the process (maybe it wasn't a child), so just
  1449. pretend it got a SIGKILL. */
  1450. inf_task_died_status (inf);
  1451. }
  1452. else
  1453. {
  1454. struct proc *thread = inf_port_to_thread (inf, dead_port);
  1455. if (thread)
  1456. {
  1457. proc_debug (thread, "is dead");
  1458. thread->port = MACH_PORT_NULL;
  1459. }
  1460. if (inf->task->dead)
  1461. /* Since the task is dead, its threads are dying with it. */
  1462. inf->wait.suppress = 1;
  1463. }
  1464. mach_port_deallocate (mach_task_self (), dead_port);
  1465. inf->threads_up_to_date = 0; /* Just in case. */
  1466. return 0;
  1467. }
  1468. #define ILL_RPC(fun, ...) \
  1469. extern "C" kern_return_t fun (__VA_ARGS__); \
  1470. kern_return_t fun (__VA_ARGS__) \
  1471. { \
  1472. warning (_("illegal rpc: %s"), #fun); \
  1473. return 0; \
  1474. }
  1475. ILL_RPC (do_mach_notify_no_senders,
  1476. mach_port_t notify, mach_port_mscount_t count)
  1477. ILL_RPC (do_mach_notify_port_deleted,
  1478. mach_port_t notify, mach_port_t name)
  1479. ILL_RPC (do_mach_notify_msg_accepted,
  1480. mach_port_t notify, mach_port_t name)
  1481. ILL_RPC (do_mach_notify_port_destroyed,
  1482. mach_port_t notify, mach_port_t name)
  1483. ILL_RPC (do_mach_notify_send_once,
  1484. mach_port_t notify)
  1485. /* Process_reply server routines. We only use process_wait_reply. */
  1486. kern_return_t
  1487. S_proc_wait_reply (mach_port_t reply, kern_return_t err,
  1488. int status, int sigcode, rusage_t rusage, pid_t pid)
  1489. {
  1490. struct inf *inf = waiting_inf;
  1491. inf_debug (inf, "err = %s, pid = %d, status = 0x%x, sigcode = %d",
  1492. err ? safe_strerror (err) : "0", pid, status, sigcode);
  1493. if (err && proc_wait_pid && (!inf->task || !inf->task->port))
  1494. /* Ack. The task has died, but the task-died notification code didn't
  1495. tell anyone because it thought a more detailed reply from the
  1496. procserver was forthcoming. However, we now learn that won't
  1497. happen... So we have to act like the task just died, and this time,
  1498. tell the world. */
  1499. inf_task_died_status (inf);
  1500. if (--proc_waits_pending == 0)
  1501. /* PROC_WAIT_PID represents the most recent wait. We will always get
  1502. replies in order because the proc server is single threaded. */
  1503. proc_wait_pid = 0;
  1504. inf_debug (inf, "waits pending now: %d", proc_waits_pending);
  1505. if (err)
  1506. {
  1507. if (err != EINTR)
  1508. {
  1509. warning (_("Can't wait for pid %d: %s"),
  1510. inf->pid, safe_strerror (err));
  1511. inf->no_wait = 1;
  1512. /* Since we can't see the inferior's signals, don't trap them. */
  1513. gnu_target->inf_set_traced (inf, 0);
  1514. }
  1515. }
  1516. else if (pid == inf->pid)
  1517. {
  1518. inf->wait.status = host_status_to_waitstatus (status);
  1519. if (inf->wait.status.kind () == TARGET_WAITKIND_STOPPED)
  1520. /* The process has sent us a signal, and stopped itself in a sane
  1521. state pending our actions. */
  1522. {
  1523. inf_debug (inf, "process has stopped itself");
  1524. inf->stopped = 1;
  1525. }
  1526. }
  1527. else
  1528. inf->wait.suppress = 1; /* Something odd happened. Ignore. */
  1529. return 0;
  1530. }
  1531. ILL_RPC (S_proc_setmsgport_reply,
  1532. mach_port_t reply_port, kern_return_t return_code,
  1533. mach_port_t oldmsgport)
  1534. ILL_RPC (S_proc_getmsgport_reply,
  1535. mach_port_t reply_port, kern_return_t return_code,
  1536. mach_port_t msgports, mach_msg_type_name_t msgportsPoly)
  1537. ILL_RPC (S_proc_pid2task_reply,
  1538. mach_port_t reply_port, kern_return_t return_code, mach_port_t task)
  1539. ILL_RPC (S_proc_task2pid_reply,
  1540. mach_port_t reply_port, kern_return_t return_code, pid_t pid)
  1541. ILL_RPC (S_proc_task2proc_reply,
  1542. mach_port_t reply_port, kern_return_t return_code,
  1543. mach_port_t proc, mach_msg_type_name_t procPoly)
  1544. ILL_RPC (S_proc_proc2task_reply,
  1545. mach_port_t reply_port, kern_return_t return_code, mach_port_t task)
  1546. ILL_RPC (S_proc_pid2proc_reply,
  1547. mach_port_t reply_port, kern_return_t return_code,
  1548. mach_port_t proc, mach_msg_type_name_t procPoly)
  1549. ILL_RPC (S_proc_getprocinfo_reply,
  1550. mach_port_t reply_port, kern_return_t return_code,
  1551. int flags, const_procinfo_t procinfo, mach_msg_type_number_t procinfoCnt,
  1552. const_data_t threadwaits, mach_msg_type_number_t threadwaitsCnt)
  1553. ILL_RPC (S_proc_getprocargs_reply,
  1554. mach_port_t reply_port, kern_return_t return_code,
  1555. const_data_t procargs, mach_msg_type_number_t procargsCnt)
  1556. ILL_RPC (S_proc_getprocenv_reply,
  1557. mach_port_t reply_port, kern_return_t return_code,
  1558. const_data_t procenv, mach_msg_type_number_t procenvCnt)
  1559. ILL_RPC (S_proc_getloginid_reply,
  1560. mach_port_t reply_port, kern_return_t return_code, pid_t login_id)
  1561. ILL_RPC (S_proc_getloginpids_reply,
  1562. mach_port_t reply_port, kern_return_t return_code,
  1563. const_pidarray_t pids, mach_msg_type_number_t pidsCnt)
  1564. ILL_RPC (S_proc_getlogin_reply,
  1565. mach_port_t reply_port, kern_return_t return_code, const_string_t logname)
  1566. ILL_RPC (S_proc_getsid_reply,
  1567. mach_port_t reply_port, kern_return_t return_code, pid_t sid)
  1568. ILL_RPC (S_proc_getsessionpgids_reply,
  1569. mach_port_t reply_port, kern_return_t return_code,
  1570. const_pidarray_t pgidset, mach_msg_type_number_t pgidsetCnt)
  1571. ILL_RPC (S_proc_getsessionpids_reply,
  1572. mach_port_t reply_port, kern_return_t return_code,
  1573. const_pidarray_t pidset, mach_msg_type_number_t pidsetCnt)
  1574. ILL_RPC (S_proc_getsidport_reply,
  1575. mach_port_t reply_port, kern_return_t return_code,
  1576. mach_port_t sessport)
  1577. ILL_RPC (S_proc_getpgrp_reply,
  1578. mach_port_t reply_port, kern_return_t return_code, pid_t pgrp)
  1579. ILL_RPC (S_proc_getpgrppids_reply,
  1580. mach_port_t reply_port, kern_return_t return_code,
  1581. const_pidarray_t pidset, mach_msg_type_number_t pidsetCnt)
  1582. ILL_RPC (S_proc_get_tty_reply,
  1583. mach_port_t reply_port, kern_return_t return_code, mach_port_t tty)
  1584. ILL_RPC (S_proc_getnports_reply,
  1585. mach_port_t reply_port, kern_return_t return_code,
  1586. mach_msg_type_number_t nports)
  1587. ILL_RPC (S_proc_is_important_reply,
  1588. mach_port_t reply_port, kern_return_t return_code,
  1589. boolean_t essential)
  1590. ILL_RPC (S_proc_get_code_reply,
  1591. mach_port_t reply_port, kern_return_t return_code,
  1592. vm_address_t start_code, vm_address_t end_code)
  1593. /* Msg_reply server routines. We only use msg_sig_post_untraced_reply. */
  1594. kern_return_t
  1595. S_msg_sig_post_untraced_reply (mach_port_t reply, kern_return_t err)
  1596. {
  1597. struct inf *inf = waiting_inf;
  1598. if (err == EBUSY)
  1599. /* EBUSY is what we get when the crash server has grabbed control of the
  1600. process and doesn't like what signal we tried to send it. Just act
  1601. like the process stopped (using a signal of 0 should mean that the
  1602. *next* time the user continues, it will pass signal 0, which the crash
  1603. server should like). */
  1604. inf->wait.status.set_stopped (GDB_SIGNAL_0);
  1605. else if (err)
  1606. warning (_("Signal delivery failed: %s"), safe_strerror (err));
  1607. if (err)
  1608. /* We only get this reply when we've posted a signal to a process which we
  1609. thought was stopped, and which we expected to continue after the signal.
  1610. Given that the signal has failed for some reason, it's reasonable to
  1611. assume it's still stopped. */
  1612. inf->stopped = 1;
  1613. else
  1614. inf->wait.suppress = 1;
  1615. return 0;
  1616. }
  1617. ILL_RPC (S_msg_sig_post_reply,
  1618. mach_port_t reply, kern_return_t err)
  1619. /* Returns the number of messages queued for the receive right PORT. */
  1620. static mach_port_msgcount_t
  1621. port_msgs_queued (mach_port_t port)
  1622. {
  1623. struct mach_port_status status;
  1624. kern_return_t err =
  1625. mach_port_get_receive_status (mach_task_self (), port, &status);
  1626. if (err)
  1627. return 0;
  1628. else
  1629. return status.mps_msgcount;
  1630. }
  1631. /* Resume execution of the inferior process.
  1632. If STEP is nonzero, single-step it.
  1633. If SIGNAL is nonzero, give it that signal.
  1634. TID STEP:
  1635. -1 true Single step the current thread allowing other threads to run.
  1636. -1 false Continue the current thread allowing other threads to run.
  1637. X true Single step the given thread, don't allow any others to run.
  1638. X false Continue the given thread, do not allow any others to run.
  1639. (Where X, of course, is anything except -1)
  1640. Note that a resume may not `take' if there are pending exceptions/&c
  1641. still unprocessed from the last resume we did (any given resume may result
  1642. in multiple events returned by wait). */
  1643. void
  1644. gnu_nat_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
  1645. {
  1646. struct proc *step_thread = 0;
  1647. int resume_all;
  1648. struct inf *inf = gnu_current_inf;
  1649. inf_debug (inf, "ptid = %s, step = %d, sig = %d",
  1650. ptid.to_string ().c_str (), step, sig);
  1651. inf_validate_procinfo (inf);
  1652. if (sig != GDB_SIGNAL_0 || inf->stopped)
  1653. {
  1654. if (sig == GDB_SIGNAL_0 && inf->nomsg)
  1655. inf_continue (inf);
  1656. else
  1657. inf_signal (inf, sig);
  1658. }
  1659. else if (inf->wait.exc.reply != MACH_PORT_NULL)
  1660. /* We received an exception to which we have chosen not to forward, so
  1661. abort the faulting thread, which will perhaps retake it. */
  1662. {
  1663. proc_abort (inf->wait.thread, 1);
  1664. warning (_("Aborting %s with unforwarded exception %s."),
  1665. proc_string (inf->wait.thread),
  1666. gdb_signal_to_name (inf->wait.status.sig ()));
  1667. }
  1668. if (port_msgs_queued (inf->event_port))
  1669. /* If there are still messages in our event queue, don't bother resuming
  1670. the process, as we're just going to stop it right away anyway. */
  1671. return;
  1672. inf_update_procs (inf);
  1673. /* A specific PTID means `step only this process id'. */
  1674. resume_all = ptid == minus_one_ptid;
  1675. if (resume_all)
  1676. /* Allow all threads to run, except perhaps single-stepping one. */
  1677. {
  1678. inf_debug (inf, "running all threads; tid = %d",
  1679. inferior_ptid.pid ());
  1680. ptid = inferior_ptid; /* What to step. */
  1681. inf_set_threads_resume_sc (inf, 0, 1);
  1682. }
  1683. else
  1684. /* Just allow a single thread to run. */
  1685. {
  1686. struct proc *thread = inf_tid_to_thread (inf, ptid.lwp ());
  1687. if (!thread)
  1688. error (_("Can't run single thread id %s: no such thread!"),
  1689. target_pid_to_str (ptid).c_str ());
  1690. inf_debug (inf, "running one thread: %s",
  1691. ptid.to_string ().c_str ());
  1692. inf_set_threads_resume_sc (inf, thread, 0);
  1693. }
  1694. if (step)
  1695. {
  1696. step_thread = inf_tid_to_thread (inf, ptid.lwp ());
  1697. if (!step_thread)
  1698. warning (_("Can't step thread id %s: no such thread."),
  1699. target_pid_to_str (ptid).c_str ());
  1700. else
  1701. inf_debug (inf, "stepping thread: %s",
  1702. ptid.to_string ().c_str ());
  1703. }
  1704. if (step_thread != inf->step_thread)
  1705. inf_set_step_thread (inf, step_thread);
  1706. inf_debug (inf, "here we go...");
  1707. inf_resume (inf);
  1708. }
  1709. void
  1710. gnu_nat_target::kill ()
  1711. {
  1712. struct proc *task = gnu_current_inf->task;
  1713. if (task)
  1714. {
  1715. proc_debug (task, "terminating...");
  1716. task_terminate (task->port);
  1717. inf_set_pid (gnu_current_inf, -1);
  1718. }
  1719. target_mourn_inferior (inferior_ptid);
  1720. }
  1721. /* Clean up after the inferior dies. */
  1722. void
  1723. gnu_nat_target::mourn_inferior ()
  1724. {
  1725. inf_debug (gnu_current_inf, "rip");
  1726. inf_detach (gnu_current_inf);
  1727. inf_child_target::mourn_inferior ();
  1728. }
  1729. /* Fork an inferior process, and start debugging it. */
  1730. /* Set INFERIOR_PID to the first thread available in the child, if any. */
  1731. static int
  1732. inf_pick_first_thread (void)
  1733. {
  1734. if (gnu_current_inf->task && gnu_current_inf->threads)
  1735. /* The first thread. */
  1736. return gnu_current_inf->threads->tid;
  1737. else
  1738. /* What may be the next thread. */
  1739. return next_thread_id;
  1740. }
  1741. static struct inf *
  1742. cur_inf (void)
  1743. {
  1744. if (!gnu_current_inf)
  1745. gnu_current_inf = make_inf ();
  1746. return gnu_current_inf;
  1747. }
  1748. static void
  1749. gnu_ptrace_me (void)
  1750. {
  1751. /* We're in the child; make this process stop as soon as it execs. */
  1752. struct inf *inf = cur_inf ();
  1753. inf_debug (inf, "tracing self");
  1754. if (ptrace (PTRACE_TRACEME) != 0)
  1755. trace_start_error_with_name ("ptrace");
  1756. }
  1757. void
  1758. gnu_nat_target::create_inferior (const char *exec_file,
  1759. const std::string &allargs,
  1760. char **env,
  1761. int from_tty)
  1762. {
  1763. struct inf *inf = cur_inf ();
  1764. inferior *inferior = current_inferior ();
  1765. int pid;
  1766. inf_debug (inf, "creating inferior");
  1767. if (!inferior->target_is_pushed (this))
  1768. inferior->push_target (this);
  1769. pid = fork_inferior (exec_file, allargs, env, gnu_ptrace_me,
  1770. NULL, NULL, NULL, NULL);
  1771. /* We have something that executes now. We'll be running through
  1772. the shell at this point (if startup-with-shell is true), but the
  1773. pid shouldn't change. */
  1774. thread_info *thr = add_thread_silent (this, ptid_t (pid));
  1775. switch_to_thread (thr);
  1776. /* Attach to the now stopped child, which is actually a shell... */
  1777. inf_debug (inf, "attaching to child: %d", pid);
  1778. inf_attach (inf, pid);
  1779. inf->pending_execs = 1;
  1780. inf->nomsg = 1;
  1781. inf->traced = 1;
  1782. /* Now let the child run again, knowing that it will stop
  1783. immediately because of the ptrace. */
  1784. inf_resume (inf);
  1785. /* We now have thread info. */
  1786. thread_change_ptid (this, inferior_ptid,
  1787. ptid_t (inf->pid, inf_pick_first_thread (), 0));
  1788. gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
  1789. inf->pending_execs = 0;
  1790. /* Get rid of the old shell threads. */
  1791. prune_threads ();
  1792. inf_validate_procinfo (inf);
  1793. inf_update_signal_thread (inf);
  1794. inf_set_traced (inf, inf->want_signals);
  1795. /* Execing the process will have trashed our exception ports; steal them
  1796. back (or make sure they're restored if the user wants that). */
  1797. if (inf->want_exceptions)
  1798. inf_steal_exc_ports (inf);
  1799. else
  1800. inf_restore_exc_ports (inf);
  1801. }
  1802. /* Attach to process PID, then initialize for debugging it
  1803. and wait for the trace-trap that results from attaching. */
  1804. void
  1805. gnu_nat_target::attach (const char *args, int from_tty)
  1806. {
  1807. int pid;
  1808. struct inf *inf = cur_inf ();
  1809. struct inferior *inferior;
  1810. pid = parse_pid_to_attach (args);
  1811. if (pid == getpid ()) /* Trying to masturbate? */
  1812. error (_("I refuse to debug myself!"));
  1813. target_announce_attach (from_tty, pid);
  1814. inf_debug (inf, "attaching to pid: %d", pid);
  1815. inf_attach (inf, pid);
  1816. inferior = current_inferior ();
  1817. inferior->push_target (this);
  1818. inferior_appeared (inferior, pid);
  1819. inferior->attach_flag = 1;
  1820. inf_update_procs (inf);
  1821. thread_info *thr
  1822. = find_thread_ptid (this, ptid_t (pid, inf_pick_first_thread ()));
  1823. switch_to_thread (thr);
  1824. /* We have to initialize the terminal settings now, since the code
  1825. below might try to restore them. */
  1826. target_terminal::init ();
  1827. /* If the process was stopped before we attached, make it continue the next
  1828. time the user does a continue. */
  1829. inf_validate_procinfo (inf);
  1830. inf_update_signal_thread (inf);
  1831. inf_set_traced (inf, inf->want_signals);
  1832. #if 0 /* Do we need this? */
  1833. renumber_threads (0); /* Give our threads reasonable names. */
  1834. #endif
  1835. }
  1836. /* Take a program previously attached to and detaches it.
  1837. The program resumes execution and will no longer stop
  1838. on signals, etc. We'd better not have left any breakpoints
  1839. in the program or it'll die when it hits one. For this
  1840. to work, it may be necessary for the process to have been
  1841. previously attached. It *might* work if the program was
  1842. started via fork. */
  1843. void
  1844. gnu_nat_target::detach (inferior *inf, int from_tty)
  1845. {
  1846. target_announce_detach (from_tty);
  1847. inf_detach (gnu_current_inf);
  1848. switch_to_no_thread ();
  1849. detach_inferior (inf);
  1850. maybe_unpush_target ();
  1851. }
  1852. void
  1853. gnu_nat_target::stop (ptid_t ptid)
  1854. {
  1855. error (_("stop target function not implemented"));
  1856. }
  1857. bool
  1858. gnu_nat_target::thread_alive (ptid_t ptid)
  1859. {
  1860. inf_update_procs (gnu_current_inf);
  1861. return !!inf_tid_to_thread (gnu_current_inf,
  1862. ptid.lwp ());
  1863. }
  1864. /* Read inferior task's LEN bytes from ADDR and copy it to MYADDR in
  1865. gdb's address space. Return 0 on failure; number of bytes read
  1866. otherwise. */
  1867. static int
  1868. gnu_read_inferior (task_t task, CORE_ADDR addr, gdb_byte *myaddr, int length)
  1869. {
  1870. kern_return_t err;
  1871. vm_address_t low_address = (vm_address_t) trunc_page (addr);
  1872. vm_size_t aligned_length =
  1873. (vm_size_t) round_page (addr + length) - low_address;
  1874. pointer_t copied;
  1875. mach_msg_type_number_t copy_count;
  1876. /* Get memory from inferior with page aligned addresses. */
  1877. err = vm_read (task, low_address, aligned_length, &copied, &copy_count);
  1878. if (err)
  1879. return 0;
  1880. err = hurd_safe_copyin (myaddr, (void *) (addr - low_address + copied),
  1881. length);
  1882. if (err)
  1883. {
  1884. warning (_("Read from inferior faulted: %s"), safe_strerror (err));
  1885. length = 0;
  1886. }
  1887. err = vm_deallocate (mach_task_self (), copied, copy_count);
  1888. if (err)
  1889. warning (_("gnu_read_inferior vm_deallocate failed: %s"),
  1890. safe_strerror (err));
  1891. return length;
  1892. }
  1893. #define CHK_GOTO_OUT(str,ret) \
  1894. do if (ret != KERN_SUCCESS) { errstr = #str; goto out; } while(0)
  1895. struct vm_region_list
  1896. {
  1897. struct vm_region_list *next;
  1898. vm_prot_t protection;
  1899. vm_address_t start;
  1900. vm_size_t length;
  1901. };
  1902. struct obstack region_obstack;
  1903. /* Write gdb's LEN bytes from MYADDR and copy it to ADDR in inferior
  1904. task's address space. */
  1905. static int
  1906. gnu_write_inferior (task_t task, CORE_ADDR addr,
  1907. const gdb_byte *myaddr, int length)
  1908. {
  1909. kern_return_t err;
  1910. vm_address_t low_address = (vm_address_t) trunc_page (addr);
  1911. vm_size_t aligned_length =
  1912. (vm_size_t) round_page (addr + length) - low_address;
  1913. pointer_t copied;
  1914. mach_msg_type_number_t copy_count;
  1915. int deallocate = 0;
  1916. const char *errstr = "Bug in gnu_write_inferior";
  1917. struct vm_region_list *region_element;
  1918. struct vm_region_list *region_head = NULL;
  1919. /* Get memory from inferior with page aligned addresses. */
  1920. err = vm_read (task,
  1921. low_address,
  1922. aligned_length,
  1923. &copied,
  1924. &copy_count);
  1925. CHK_GOTO_OUT ("gnu_write_inferior vm_read failed", err);
  1926. deallocate++;
  1927. err = hurd_safe_copyout ((void *) (addr - low_address + copied),
  1928. myaddr, length);
  1929. CHK_GOTO_OUT ("Write to inferior faulted", err);
  1930. obstack_init (&region_obstack);
  1931. /* Do writes atomically.
  1932. First check for holes and unwritable memory. */
  1933. {
  1934. vm_size_t remaining_length = aligned_length;
  1935. vm_address_t region_address = low_address;
  1936. struct vm_region_list *scan;
  1937. while (region_address < low_address + aligned_length)
  1938. {
  1939. vm_prot_t protection;
  1940. vm_prot_t max_protection;
  1941. vm_inherit_t inheritance;
  1942. boolean_t shared;
  1943. mach_port_t object_name;
  1944. vm_offset_t offset;
  1945. vm_size_t region_length = remaining_length;
  1946. vm_address_t old_address = region_address;
  1947. err = vm_region (task,
  1948. &region_address,
  1949. &region_length,
  1950. &protection,
  1951. &max_protection,
  1952. &inheritance,
  1953. &shared,
  1954. &object_name,
  1955. &offset);
  1956. CHK_GOTO_OUT ("vm_region failed", err);
  1957. /* Check for holes in memory. */
  1958. if (old_address != region_address)
  1959. {
  1960. warning (_("No memory at 0x%lx. Nothing written"),
  1961. old_address);
  1962. err = KERN_SUCCESS;
  1963. length = 0;
  1964. goto out;
  1965. }
  1966. if (!(max_protection & VM_PROT_WRITE))
  1967. {
  1968. warning (_("Memory at address 0x%lx is unwritable. "
  1969. "Nothing written"),
  1970. old_address);
  1971. err = KERN_SUCCESS;
  1972. length = 0;
  1973. goto out;
  1974. }
  1975. /* Chain the regions for later use. */
  1976. region_element = XOBNEW (&region_obstack, struct vm_region_list);
  1977. region_element->protection = protection;
  1978. region_element->start = region_address;
  1979. region_element->length = region_length;
  1980. /* Chain the regions along with protections. */
  1981. region_element->next = region_head;
  1982. region_head = region_element;
  1983. region_address += region_length;
  1984. remaining_length = remaining_length - region_length;
  1985. }
  1986. /* If things fail after this, we give up.
  1987. Somebody is messing up inferior_task's mappings. */
  1988. /* Enable writes to the chained vm regions. */
  1989. for (scan = region_head; scan; scan = scan->next)
  1990. {
  1991. if (!(scan->protection & VM_PROT_WRITE))
  1992. {
  1993. err = vm_protect (task,
  1994. scan->start,
  1995. scan->length,
  1996. FALSE,
  1997. scan->protection | VM_PROT_WRITE);
  1998. CHK_GOTO_OUT ("vm_protect: enable write failed", err);
  1999. }
  2000. }
  2001. err = vm_write (task,
  2002. low_address,
  2003. copied,
  2004. aligned_length);
  2005. CHK_GOTO_OUT ("vm_write failed", err);
  2006. /* Set up the original region protections, if they were changed. */
  2007. for (scan = region_head; scan; scan = scan->next)
  2008. {
  2009. if (!(scan->protection & VM_PROT_WRITE))
  2010. {
  2011. err = vm_protect (task,
  2012. scan->start,
  2013. scan->length,
  2014. FALSE,
  2015. scan->protection);
  2016. CHK_GOTO_OUT ("vm_protect: enable write failed", err);
  2017. }
  2018. }
  2019. }
  2020. out:
  2021. if (deallocate)
  2022. {
  2023. obstack_free (&region_obstack, 0);
  2024. (void) vm_deallocate (mach_task_self (),
  2025. copied,
  2026. copy_count);
  2027. }
  2028. if (err != KERN_SUCCESS)
  2029. {
  2030. warning (_("%s: %s"), errstr, mach_error_string (err));
  2031. return 0;
  2032. }
  2033. return length;
  2034. }
  2035. /* Implement the to_xfer_partial target_ops method for
  2036. TARGET_OBJECT_MEMORY. */
  2037. static enum target_xfer_status
  2038. gnu_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
  2039. CORE_ADDR memaddr, ULONGEST len, ULONGEST *xfered_len)
  2040. {
  2041. task_t task = (gnu_current_inf
  2042. ? (gnu_current_inf->task
  2043. ? gnu_current_inf->task->port : 0)
  2044. : 0);
  2045. int res;
  2046. if (task == MACH_PORT_NULL)
  2047. return TARGET_XFER_E_IO;
  2048. if (writebuf != NULL)
  2049. {
  2050. inf_debug (gnu_current_inf, "writing %s[%s] <-- %s",
  2051. paddress (target_gdbarch (), memaddr), pulongest (len),
  2052. host_address_to_string (writebuf));
  2053. res = gnu_write_inferior (task, memaddr, writebuf, len);
  2054. }
  2055. else
  2056. {
  2057. inf_debug (gnu_current_inf, "reading %s[%s] --> %s",
  2058. paddress (target_gdbarch (), memaddr), pulongest (len),
  2059. host_address_to_string (readbuf));
  2060. res = gnu_read_inferior (task, memaddr, readbuf, len);
  2061. }
  2062. gdb_assert (res >= 0);
  2063. if (res == 0)
  2064. return TARGET_XFER_E_IO;
  2065. else
  2066. {
  2067. *xfered_len = (ULONGEST) res;
  2068. return TARGET_XFER_OK;
  2069. }
  2070. }
  2071. /* GNU does not have auxv, but we can at least fake the AT_ENTRY entry for PIE
  2072. binaries. */
  2073. static enum target_xfer_status
  2074. gnu_xfer_auxv (gdb_byte *readbuf, const gdb_byte *writebuf,
  2075. CORE_ADDR memaddr, ULONGEST len, ULONGEST *xfered_len)
  2076. {
  2077. task_t task = (gnu_current_inf
  2078. ? (gnu_current_inf->task
  2079. ? gnu_current_inf->task->port : 0)
  2080. : 0);
  2081. process_t proc;
  2082. kern_return_t err;
  2083. vm_address_t entry;
  2084. ElfW(auxv_t) auxv[2];
  2085. if (task == MACH_PORT_NULL)
  2086. return TARGET_XFER_E_IO;
  2087. if (writebuf != NULL)
  2088. return TARGET_XFER_E_IO;
  2089. if (memaddr == sizeof (auxv))
  2090. return TARGET_XFER_EOF;
  2091. if (memaddr > sizeof (auxv))
  2092. return TARGET_XFER_E_IO;
  2093. err = proc_task2proc (proc_server, task, &proc);
  2094. if (err != 0)
  2095. return TARGET_XFER_E_IO;
  2096. /* Get entry from proc server. */
  2097. err = proc_get_entry (proc, &entry);
  2098. if (err != 0)
  2099. return TARGET_XFER_E_IO;
  2100. /* Fake auxv entry. */
  2101. auxv[0].a_type = AT_ENTRY;
  2102. auxv[0].a_un.a_val = entry;
  2103. auxv[1].a_type = AT_NULL;
  2104. auxv[1].a_un.a_val = 0;
  2105. inf_debug (gnu_current_inf, "reading auxv %s[%s] --> %s",
  2106. paddress (target_gdbarch (), memaddr), pulongest (len),
  2107. host_address_to_string (readbuf));
  2108. if (memaddr + len > sizeof (auxv))
  2109. len = sizeof (auxv) - memaddr;
  2110. memcpy (readbuf, (gdb_byte *) &auxv + memaddr, len);
  2111. *xfered_len = len;
  2112. return TARGET_XFER_OK;
  2113. }
  2114. /* Target to_xfer_partial implementation. */
  2115. enum target_xfer_status
  2116. gnu_nat_target::xfer_partial (enum target_object object,
  2117. const char *annex, gdb_byte *readbuf,
  2118. const gdb_byte *writebuf, ULONGEST offset,
  2119. ULONGEST len, ULONGEST *xfered_len)
  2120. {
  2121. switch (object)
  2122. {
  2123. case TARGET_OBJECT_MEMORY:
  2124. return gnu_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
  2125. case TARGET_OBJECT_AUXV:
  2126. return gnu_xfer_auxv (readbuf, writebuf, offset, len, xfered_len);
  2127. default:
  2128. return TARGET_XFER_E_IO;
  2129. }
  2130. }
  2131. /* Call FUNC on each memory region in the task. */
  2132. int
  2133. gnu_nat_target::find_memory_regions (find_memory_region_ftype func,
  2134. void *data)
  2135. {
  2136. kern_return_t err;
  2137. task_t task;
  2138. vm_address_t region_address, last_region_address, last_region_end;
  2139. vm_prot_t last_protection;
  2140. if (gnu_current_inf == 0 || gnu_current_inf->task == 0)
  2141. return 0;
  2142. task = gnu_current_inf->task->port;
  2143. if (task == MACH_PORT_NULL)
  2144. return 0;
  2145. region_address = last_region_address = last_region_end = VM_MIN_ADDRESS;
  2146. last_protection = VM_PROT_NONE;
  2147. while (region_address < VM_MAX_ADDRESS)
  2148. {
  2149. vm_prot_t protection;
  2150. vm_prot_t max_protection;
  2151. vm_inherit_t inheritance;
  2152. boolean_t shared;
  2153. mach_port_t object_name;
  2154. vm_offset_t offset;
  2155. vm_size_t region_length = VM_MAX_ADDRESS - region_address;
  2156. err = vm_region (task,
  2157. &region_address,
  2158. &region_length,
  2159. &protection,
  2160. &max_protection,
  2161. &inheritance,
  2162. &shared,
  2163. &object_name,
  2164. &offset);
  2165. if (err == KERN_NO_SPACE)
  2166. break;
  2167. if (err != KERN_SUCCESS)
  2168. {
  2169. warning (_("vm_region failed: %s"), mach_error_string (err));
  2170. return -1;
  2171. }
  2172. if (protection == last_protection && region_address == last_region_end)
  2173. /* This region is contiguous with and indistinguishable from
  2174. the previous one, so we just extend that one. */
  2175. last_region_end = region_address += region_length;
  2176. else
  2177. {
  2178. /* This region is distinct from the last one we saw, so report
  2179. that previous one. */
  2180. if (last_protection != VM_PROT_NONE)
  2181. (*func) (last_region_address,
  2182. last_region_end - last_region_address,
  2183. last_protection & VM_PROT_READ,
  2184. last_protection & VM_PROT_WRITE,
  2185. last_protection & VM_PROT_EXECUTE,
  2186. 1, /* MODIFIED is unknown, pass it as true. */
  2187. data);
  2188. last_region_address = region_address;
  2189. last_region_end = region_address += region_length;
  2190. last_protection = protection;
  2191. }
  2192. }
  2193. /* Report the final region. */
  2194. if (last_region_end > last_region_address && last_protection != VM_PROT_NONE)
  2195. (*func) (last_region_address, last_region_end - last_region_address,
  2196. last_protection & VM_PROT_READ,
  2197. last_protection & VM_PROT_WRITE,
  2198. last_protection & VM_PROT_EXECUTE,
  2199. 1, /* MODIFIED is unknown, pass it as true. */
  2200. data);
  2201. return 0;
  2202. }
  2203. /* Return printable description of proc. */
  2204. char *
  2205. proc_string (struct proc *proc)
  2206. {
  2207. static char tid_str[80];
  2208. if (proc_is_task (proc))
  2209. xsnprintf (tid_str, sizeof (tid_str), "process %d", proc->inf->pid);
  2210. else
  2211. xsnprintf (tid_str, sizeof (tid_str), "Thread %d.%d",
  2212. proc->inf->pid, proc->tid);
  2213. return tid_str;
  2214. }
  2215. std::string
  2216. gnu_nat_target::pid_to_str (ptid_t ptid)
  2217. {
  2218. struct inf *inf = gnu_current_inf;
  2219. int tid = ptid.lwp ();
  2220. struct proc *thread = inf_tid_to_thread (inf, tid);
  2221. if (thread)
  2222. return proc_string (thread);
  2223. else
  2224. return string_printf ("bogus thread id %d", tid);
  2225. }
  2226. /* User task commands. */
  2227. static struct cmd_list_element *set_task_cmd_list = 0;
  2228. static struct cmd_list_element *show_task_cmd_list = 0;
  2229. /* User thread commands. */
  2230. /* Commands with a prefix of `set/show thread'. */
  2231. extern struct cmd_list_element *thread_cmd_list;
  2232. struct cmd_list_element *set_thread_cmd_list = NULL;
  2233. struct cmd_list_element *show_thread_cmd_list = NULL;
  2234. /* Commands with a prefix of `set/show thread default'. */
  2235. struct cmd_list_element *set_thread_default_cmd_list = NULL;
  2236. struct cmd_list_element *show_thread_default_cmd_list = NULL;
  2237. static int
  2238. parse_int_arg (const char *args, const char *cmd_prefix)
  2239. {
  2240. if (args)
  2241. {
  2242. char *arg_end;
  2243. int val = strtoul (args, &arg_end, 10);
  2244. if (*args && *arg_end == '\0')
  2245. return val;
  2246. }
  2247. error (_("Illegal argument for \"%s\" command, should be an integer."),
  2248. cmd_prefix);
  2249. }
  2250. static int
  2251. _parse_bool_arg (const char *args, const char *t_val, const char *f_val,
  2252. const char *cmd_prefix)
  2253. {
  2254. if (!args || strcmp (args, t_val) == 0)
  2255. return 1;
  2256. else if (strcmp (args, f_val) == 0)
  2257. return 0;
  2258. else
  2259. error (_("Illegal argument for \"%s\" command, "
  2260. "should be \"%s\" or \"%s\"."),
  2261. cmd_prefix, t_val, f_val);
  2262. }
  2263. #define parse_bool_arg(args, cmd_prefix) \
  2264. _parse_bool_arg (args, "on", "off", cmd_prefix)
  2265. static void
  2266. check_empty (const char *args, const char *cmd_prefix)
  2267. {
  2268. if (args)
  2269. error (_("Garbage after \"%s\" command: `%s'"), cmd_prefix, args);
  2270. }
  2271. /* Returns the alive thread named by INFERIOR_PID, or signals an error. */
  2272. static struct proc *
  2273. cur_thread (void)
  2274. {
  2275. struct inf *inf = cur_inf ();
  2276. struct proc *thread = inf_tid_to_thread (inf,
  2277. inferior_ptid.lwp ());
  2278. if (!thread)
  2279. error (_("No current thread."));
  2280. return thread;
  2281. }
  2282. /* Returns the current inferior, but signals an error if it has no task. */
  2283. static struct inf *
  2284. active_inf (void)
  2285. {
  2286. struct inf *inf = cur_inf ();
  2287. if (!inf->task)
  2288. error (_("No current process."));
  2289. return inf;
  2290. }
  2291. static void
  2292. set_task_pause_cmd (int arg, int from_tty)
  2293. {
  2294. struct inf *inf = cur_inf ();
  2295. int old_sc = inf->pause_sc;
  2296. inf->pause_sc = arg;
  2297. if (old_sc == 0 && inf->pause_sc != 0)
  2298. /* If the task is currently unsuspended, immediately suspend it,
  2299. otherwise wait until the next time it gets control. */
  2300. gnu_target->inf_suspend (inf);
  2301. }
  2302. static void
  2303. set_task_pause_cmd (const char *args, int from_tty)
  2304. {
  2305. set_task_pause_cmd (parse_bool_arg (args, "set task pause"), from_tty);
  2306. }
  2307. static void
  2308. show_task_pause_cmd (const char *args, int from_tty)
  2309. {
  2310. struct inf *inf = cur_inf ();
  2311. check_empty (args, "show task pause");
  2312. gdb_printf ("The inferior task %s suspended while gdb has control.\n",
  2313. inf->task
  2314. ? (inf->pause_sc == 0 ? "isn't" : "is")
  2315. : (inf->pause_sc == 0 ? "won't be" : "will be"));
  2316. }
  2317. static void
  2318. set_task_detach_sc_cmd (const char *args, int from_tty)
  2319. {
  2320. cur_inf ()->detach_sc = parse_int_arg (args,
  2321. "set task detach-suspend-count");
  2322. }
  2323. static void
  2324. show_task_detach_sc_cmd (const char *args, int from_tty)
  2325. {
  2326. check_empty (args, "show task detach-suspend-count");
  2327. gdb_printf ("The inferior task will be left with a "
  2328. "suspend count of %d when detaching.\n",
  2329. cur_inf ()->detach_sc);
  2330. }
  2331. static void
  2332. set_thread_default_pause_cmd (const char *args, int from_tty)
  2333. {
  2334. struct inf *inf = cur_inf ();
  2335. inf->default_thread_pause_sc =
  2336. parse_bool_arg (args, "set thread default pause") ? 0 : 1;
  2337. }
  2338. static void
  2339. show_thread_default_pause_cmd (const char *args, int from_tty)
  2340. {
  2341. struct inf *inf = cur_inf ();
  2342. int sc = inf->default_thread_pause_sc;
  2343. check_empty (args, "show thread default pause");
  2344. gdb_printf ("New threads %s suspended while gdb has control%s.\n",
  2345. sc ? "are" : "aren't",
  2346. !sc && inf->pause_sc ? " (but the task is)" : "");
  2347. }
  2348. static void
  2349. set_thread_default_run_cmd (const char *args, int from_tty)
  2350. {
  2351. struct inf *inf = cur_inf ();
  2352. inf->default_thread_run_sc =
  2353. parse_bool_arg (args, "set thread default run") ? 0 : 1;
  2354. }
  2355. static void
  2356. show_thread_default_run_cmd (const char *args, int from_tty)
  2357. {
  2358. struct inf *inf = cur_inf ();
  2359. check_empty (args, "show thread default run");
  2360. gdb_printf ("New threads %s allowed to run.\n",
  2361. inf->default_thread_run_sc == 0 ? "are" : "aren't");
  2362. }
  2363. static void
  2364. set_thread_default_detach_sc_cmd (const char *args, int from_tty)
  2365. {
  2366. cur_inf ()->default_thread_detach_sc =
  2367. parse_int_arg (args, "set thread default detach-suspend-count");
  2368. }
  2369. static void
  2370. show_thread_default_detach_sc_cmd (const char *args, int from_tty)
  2371. {
  2372. check_empty (args, "show thread default detach-suspend-count");
  2373. gdb_printf ("New threads will get a detach-suspend-count of %d.\n",
  2374. cur_inf ()->default_thread_detach_sc);
  2375. }
  2376. /* Steal a send right called NAME in the inferior task, and make it PROC's
  2377. saved exception port. */
  2378. void
  2379. gnu_nat_target::steal_exc_port (struct proc *proc, mach_port_t name)
  2380. {
  2381. kern_return_t err;
  2382. mach_port_t port;
  2383. mach_msg_type_name_t port_type;
  2384. if (!proc || !proc->inf->task)
  2385. error (_("No inferior task."));
  2386. err = mach_port_extract_right (proc->inf->task->port,
  2387. name, MACH_MSG_TYPE_COPY_SEND,
  2388. &port, &port_type);
  2389. if (err)
  2390. error (_("Couldn't extract send right %lu from inferior: %s"),
  2391. name, safe_strerror (err));
  2392. if (proc->saved_exc_port)
  2393. /* Get rid of our reference to the old one. */
  2394. mach_port_deallocate (mach_task_self (), proc->saved_exc_port);
  2395. proc->saved_exc_port = port;
  2396. if (!proc->exc_port)
  2397. /* If PROC is a thread, we may not have set its exception port
  2398. before. We can't use proc_steal_exc_port because it also sets
  2399. saved_exc_port. */
  2400. {
  2401. proc->exc_port = proc->inf->event_port;
  2402. err = proc_set_exception_port (proc, proc->exc_port);
  2403. error (_("Can't set exception port for %s: %s"),
  2404. proc_string (proc), safe_strerror (err));
  2405. }
  2406. }
  2407. static void
  2408. set_task_exc_port_cmd (const char *args, int from_tty)
  2409. {
  2410. struct inf *inf = cur_inf ();
  2411. if (!args)
  2412. error (_("No argument to \"set task exception-port\" command."));
  2413. gnu_target->steal_exc_port (inf->task, parse_and_eval_address (args));
  2414. }
  2415. static void
  2416. set_stopped_cmd (const char *args, int from_tty)
  2417. {
  2418. cur_inf ()->stopped = _parse_bool_arg (args, "yes", "no", "set stopped");
  2419. }
  2420. static void
  2421. show_stopped_cmd (const char *args, int from_tty)
  2422. {
  2423. struct inf *inf = active_inf ();
  2424. check_empty (args, "show stopped");
  2425. gdb_printf ("The inferior process %s stopped.\n",
  2426. inf->stopped ? "is" : "isn't");
  2427. }
  2428. static void
  2429. set_sig_thread_cmd (const char *args, int from_tty)
  2430. {
  2431. struct inf *inf = cur_inf ();
  2432. if (!args || (!isdigit (*args) && strcmp (args, "none") != 0))
  2433. error (_("Illegal argument to \"set signal-thread\" command.\n"
  2434. "Should be a thread ID, or \"none\"."));
  2435. if (strcmp (args, "none") == 0)
  2436. inf->signal_thread = 0;
  2437. else
  2438. {
  2439. struct thread_info *tp = parse_thread_id (args, NULL);
  2440. inf->signal_thread = inf_tid_to_thread (inf, tp->ptid.lwp ());
  2441. }
  2442. }
  2443. static void
  2444. show_sig_thread_cmd (const char *args, int from_tty)
  2445. {
  2446. struct inf *inf = active_inf ();
  2447. check_empty (args, "show signal-thread");
  2448. if (inf->signal_thread)
  2449. gdb_printf ("The signal thread is %s.\n",
  2450. proc_string (inf->signal_thread));
  2451. else
  2452. gdb_printf ("There is no signal thread.\n");
  2453. }
  2454. static void
  2455. set_signals_cmd (int arg, int from_tty)
  2456. {
  2457. struct inf *inf = cur_inf ();
  2458. inf->want_signals = arg;
  2459. if (inf->task && inf->want_signals != inf->traced)
  2460. /* Make this take effect immediately in a running process. */
  2461. gnu_target->inf_set_traced (inf, inf->want_signals);
  2462. }
  2463. static void
  2464. set_signals_cmd (const char *args, int from_tty)
  2465. {
  2466. set_signals_cmd(parse_bool_arg (args, "set signals"), from_tty);
  2467. }
  2468. static void
  2469. show_signals_cmd (const char *args, int from_tty)
  2470. {
  2471. struct inf *inf = cur_inf ();
  2472. check_empty (args, "show signals");
  2473. gdb_printf ("The inferior process's signals %s intercepted.\n",
  2474. inf->task
  2475. ? (inf->traced ? "are" : "aren't")
  2476. : (inf->want_signals ? "will be" : "won't be"));
  2477. }
  2478. static void
  2479. set_exceptions_cmd (int arg, int from_tty)
  2480. {
  2481. struct inf *inf = cur_inf ();
  2482. /* Make this take effect immediately in a running process. */
  2483. /* XXX */ ;
  2484. inf->want_exceptions = arg;
  2485. }
  2486. static void
  2487. set_exceptions_cmd (const char *args, int from_tty)
  2488. {
  2489. set_exceptions_cmd (parse_bool_arg (args, "set exceptions"), from_tty);
  2490. }
  2491. static void
  2492. show_exceptions_cmd (const char *args, int from_tty)
  2493. {
  2494. struct inf *inf = cur_inf ();
  2495. check_empty (args, "show exceptions");
  2496. gdb_printf ("Exceptions in the inferior %s trapped.\n",
  2497. inf->task
  2498. ? (inf->want_exceptions ? "are" : "aren't")
  2499. : (inf->want_exceptions ? "will be" : "won't be"));
  2500. }
  2501. static void
  2502. set_task_cmd (const char *args, int from_tty)
  2503. {
  2504. gdb_printf ("\"set task\" must be followed by the name"
  2505. " of a task property.\n");
  2506. }
  2507. static void
  2508. show_task_cmd (const char *args, int from_tty)
  2509. {
  2510. struct inf *inf = cur_inf ();
  2511. check_empty (args, "show task");
  2512. show_signals_cmd (0, from_tty);
  2513. show_exceptions_cmd (0, from_tty);
  2514. show_task_pause_cmd (0, from_tty);
  2515. if (inf->pause_sc == 0)
  2516. show_thread_default_pause_cmd (0, from_tty);
  2517. show_thread_default_run_cmd (0, from_tty);
  2518. if (inf->task)
  2519. {
  2520. show_stopped_cmd (0, from_tty);
  2521. show_sig_thread_cmd (0, from_tty);
  2522. }
  2523. if (inf->detach_sc != 0)
  2524. show_task_detach_sc_cmd (0, from_tty);
  2525. if (inf->default_thread_detach_sc != 0)
  2526. show_thread_default_detach_sc_cmd (0, from_tty);
  2527. }
  2528. static void
  2529. set_noninvasive_cmd (const char *args, int from_tty)
  2530. {
  2531. /* Invert the sense of the arg for each component. */
  2532. int inv_arg = parse_bool_arg (args, "set noninvasive") ? 0 : 1;
  2533. set_task_pause_cmd (inv_arg, from_tty);
  2534. set_signals_cmd (inv_arg, from_tty);
  2535. set_exceptions_cmd (inv_arg, from_tty);
  2536. }
  2537. static void
  2538. info_port_rights (const char *args, mach_port_type_t only)
  2539. {
  2540. struct inf *inf = active_inf ();
  2541. struct value *vmark = value_mark ();
  2542. if (args)
  2543. /* Explicit list of port rights. */
  2544. {
  2545. while (*args)
  2546. {
  2547. struct value *val = parse_to_comma_and_eval (&args);
  2548. long right = value_as_long (val);
  2549. error_t err =
  2550. print_port_info (right, 0, inf->task->port, PORTINFO_DETAILS,
  2551. stdout);
  2552. if (err)
  2553. error (_("%ld: %s."), right, safe_strerror (err));
  2554. }
  2555. }
  2556. else
  2557. /* Print all of them. */
  2558. {
  2559. error_t err =
  2560. print_task_ports_info (inf->task->port, only, PORTINFO_DETAILS,
  2561. stdout);
  2562. if (err)
  2563. error (_("%s."), safe_strerror (err));
  2564. }
  2565. value_free_to_mark (vmark);
  2566. }
  2567. static void
  2568. info_send_rights_cmd (const char *args, int from_tty)
  2569. {
  2570. info_port_rights (args, MACH_PORT_TYPE_SEND);
  2571. }
  2572. static void
  2573. info_recv_rights_cmd (const char *args, int from_tty)
  2574. {
  2575. info_port_rights (args, MACH_PORT_TYPE_RECEIVE);
  2576. }
  2577. static void
  2578. info_port_sets_cmd (const char *args, int from_tty)
  2579. {
  2580. info_port_rights (args, MACH_PORT_TYPE_PORT_SET);
  2581. }
  2582. static void
  2583. info_dead_names_cmd (const char *args, int from_tty)
  2584. {
  2585. info_port_rights (args, MACH_PORT_TYPE_DEAD_NAME);
  2586. }
  2587. static void
  2588. info_port_rights_cmd (const char *args, int from_tty)
  2589. {
  2590. info_port_rights (args, ~0);
  2591. }
  2592. static void
  2593. add_task_commands (void)
  2594. {
  2595. add_cmd ("pause", class_run, set_thread_default_pause_cmd, _("\
  2596. Set whether the new threads are suspended while gdb has control.\n\
  2597. This property normally has no effect because the whole task is\n\
  2598. suspended, however, that may be disabled with \"set task pause off\".\n\
  2599. The default value is \"off\"."),
  2600. &set_thread_default_cmd_list);
  2601. add_cmd ("pause", no_class, show_thread_default_pause_cmd, _("\
  2602. Show whether new threads are suspended while gdb has control."),
  2603. &show_thread_default_cmd_list);
  2604. add_cmd ("run", class_run, set_thread_default_run_cmd, _("\
  2605. Set whether new threads are allowed to run (once gdb has noticed them)."),
  2606. &set_thread_default_cmd_list);
  2607. add_cmd ("run", no_class, show_thread_default_run_cmd, _("\
  2608. Show whether new threads are allowed to run (once gdb has noticed them)."),
  2609. &show_thread_default_cmd_list);
  2610. add_cmd ("detach-suspend-count", class_run, set_thread_default_detach_sc_cmd,
  2611. _("Set the default detach-suspend-count value for new threads."),
  2612. &set_thread_default_cmd_list);
  2613. add_cmd ("detach-suspend-count", no_class, show_thread_default_detach_sc_cmd,
  2614. _("Show the default detach-suspend-count value for new threads."),
  2615. &show_thread_default_cmd_list);
  2616. cmd_list_element *set_signals_cmd_
  2617. = add_cmd ("signals", class_run, set_signals_cmd, _("\
  2618. Set whether the inferior process's signals will be intercepted.\n\
  2619. Mach exceptions (such as breakpoint traps) are not affected."),
  2620. &setlist);
  2621. add_alias_cmd ("sigs", set_signals_cmd_, class_run, 1, &setlist);
  2622. cmd_list_element *show_signals_cmd_
  2623. = add_cmd ("signals", no_class, show_signals_cmd, _("\
  2624. Show whether the inferior process's signals will be intercepted."),
  2625. &showlist);
  2626. add_alias_cmd ("sigs", show_signals_cmd_, no_class, 1, &showlist);
  2627. cmd_list_element *set_signal_thread_cmd_
  2628. = add_cmd ("signal-thread", class_run, set_sig_thread_cmd, _("\
  2629. Set the thread that gdb thinks is the libc signal thread.\n\
  2630. This thread is run when delivering a signal to a non-stopped process."),
  2631. &setlist);
  2632. add_alias_cmd ("sigthread", set_signal_thread_cmd_, class_run, 1, &setlist);
  2633. cmd_list_element *show_signal_thread_cmd_
  2634. = add_cmd ("signal-thread", no_class, show_sig_thread_cmd, _("\
  2635. Set the thread that gdb thinks is the libc signal thread."),
  2636. &showlist);
  2637. add_alias_cmd ("sigthread", show_signal_thread_cmd_, no_class, 1, &showlist);
  2638. add_cmd ("stopped", class_run, set_stopped_cmd, _("\
  2639. Set whether gdb thinks the inferior process is stopped as with SIGSTOP.\n\
  2640. Stopped process will be continued by sending them a signal."),
  2641. &setlist);
  2642. add_cmd ("stopped", no_class, show_stopped_cmd, _("\
  2643. Show whether gdb thinks the inferior process is stopped as with SIGSTOP."),
  2644. &showlist);
  2645. cmd_list_element *set_exceptions_cmd_
  2646. = add_cmd ("exceptions", class_run, set_exceptions_cmd, _("\
  2647. Set whether exceptions in the inferior process will be trapped.\n\
  2648. When exceptions are turned off, neither breakpoints nor single-stepping\n\
  2649. will work."), &setlist);
  2650. /* Allow `set exc' despite conflict with `set exception-port'. */
  2651. add_alias_cmd ("exc", set_exceptions_cmd_, class_run, 1, &setlist);
  2652. add_cmd ("exceptions", no_class, show_exceptions_cmd, _("\
  2653. Show whether exceptions in the inferior process will be trapped."),
  2654. &showlist);
  2655. add_prefix_cmd ("task", no_class, set_task_cmd,
  2656. _("Command prefix for setting task attributes."),
  2657. &set_task_cmd_list, 0, &setlist);
  2658. add_prefix_cmd ("task", no_class, show_task_cmd,
  2659. _("Command prefix for showing task attributes."),
  2660. &show_task_cmd_list, 0, &showlist);
  2661. add_cmd ("pause", class_run, set_task_pause_cmd, _("\
  2662. Set whether the task is suspended while gdb has control.\n\
  2663. A value of \"on\" takes effect immediately, otherwise nothing happens\n\
  2664. until the next time the program is continued.\n\
  2665. When setting this to \"off\", \"set thread default pause on\" can be\n\
  2666. used to pause individual threads by default instead."),
  2667. &set_task_cmd_list);
  2668. add_cmd ("pause", no_class, show_task_pause_cmd,
  2669. _("Show whether the task is suspended while gdb has control."),
  2670. &show_task_cmd_list);
  2671. add_cmd ("detach-suspend-count", class_run, set_task_detach_sc_cmd,
  2672. _("Set the suspend count will leave on the thread when detaching."),
  2673. &set_task_cmd_list);
  2674. add_cmd ("detach-suspend-count", no_class, show_task_detach_sc_cmd,
  2675. _("Show the suspend count will leave "
  2676. "on the thread when detaching."),
  2677. &show_task_cmd_list);
  2678. cmd_list_element *set_task_exception_port_cmd_
  2679. = add_cmd ("exception-port", no_class, set_task_exc_port_cmd, _("\
  2680. Set the task exception port to which we forward exceptions.\n\
  2681. The argument should be the value of the send right in the task."),
  2682. &set_task_cmd_list);
  2683. add_alias_cmd ("excp", set_task_exception_port_cmd_, no_class, 1,
  2684. &set_task_cmd_list);
  2685. add_alias_cmd ("exc-port", set_task_exception_port_cmd_, no_class, 1,
  2686. &set_task_cmd_list);
  2687. /* A convenient way of turning on all options require to noninvasively
  2688. debug running tasks. */
  2689. add_cmd ("noninvasive", no_class, set_noninvasive_cmd, _("\
  2690. Set task options so that we interfere as little as possible.\n\
  2691. This is the same as setting `task pause', `exceptions', and\n\
  2692. `signals' to the opposite value."),
  2693. &setlist);
  2694. /* Commands to show information about the task's ports. */
  2695. add_info ("send-rights", info_send_rights_cmd,
  2696. _("Show information about the task's send rights."));
  2697. add_info ("receive-rights", info_recv_rights_cmd,
  2698. _("Show information about the task's receive rights."));
  2699. cmd_list_element *port_rights_cmd
  2700. = add_info ("port-rights", info_port_rights_cmd,
  2701. _("Show information about the task's port rights."));
  2702. cmd_list_element *port_sets_cmd
  2703. = add_info ("port-sets", info_port_sets_cmd,
  2704. _("Show information about the task's port sets."));
  2705. add_info ("dead-names", info_dead_names_cmd,
  2706. _("Show information about the task's dead names."));
  2707. add_info_alias ("ports", port_rights_cmd, 1);
  2708. add_info_alias ("port", port_rights_cmd, 1);
  2709. add_info_alias ("psets", port_sets_cmd, 1);
  2710. }
  2711. static void
  2712. set_thread_pause_cmd (const char *args, int from_tty)
  2713. {
  2714. struct proc *thread = cur_thread ();
  2715. int old_sc = thread->pause_sc;
  2716. thread->pause_sc = parse_bool_arg (args, "set thread pause");
  2717. if (old_sc == 0 && thread->pause_sc != 0 && thread->inf->pause_sc == 0)
  2718. /* If the task is currently unsuspended, immediately suspend it,
  2719. otherwise wait until the next time it gets control. */
  2720. gnu_target->inf_suspend (thread->inf);
  2721. }
  2722. static void
  2723. show_thread_pause_cmd (const char *args, int from_tty)
  2724. {
  2725. struct proc *thread = cur_thread ();
  2726. int sc = thread->pause_sc;
  2727. check_empty (args, "show task pause");
  2728. gdb_printf ("Thread %s %s suspended while gdb has control%s.\n",
  2729. proc_string (thread),
  2730. sc ? "is" : "isn't",
  2731. !sc && thread->inf->pause_sc ? " (but the task is)" : "");
  2732. }
  2733. static void
  2734. set_thread_run_cmd (const char *args, int from_tty)
  2735. {
  2736. struct proc *thread = cur_thread ();
  2737. thread->run_sc = parse_bool_arg (args, "set thread run") ? 0 : 1;
  2738. }
  2739. static void
  2740. show_thread_run_cmd (const char *args, int from_tty)
  2741. {
  2742. struct proc *thread = cur_thread ();
  2743. check_empty (args, "show thread run");
  2744. gdb_printf ("Thread %s %s allowed to run.",
  2745. proc_string (thread),
  2746. thread->run_sc == 0 ? "is" : "isn't");
  2747. }
  2748. static void
  2749. set_thread_detach_sc_cmd (const char *args, int from_tty)
  2750. {
  2751. cur_thread ()->detach_sc = parse_int_arg (args,
  2752. "set thread detach-suspend-count");
  2753. }
  2754. static void
  2755. show_thread_detach_sc_cmd (const char *args, int from_tty)
  2756. {
  2757. struct proc *thread = cur_thread ();
  2758. check_empty (args, "show thread detach-suspend-count");
  2759. gdb_printf ("Thread %s will be left with a suspend count"
  2760. " of %d when detaching.\n",
  2761. proc_string (thread),
  2762. thread->detach_sc);
  2763. }
  2764. static void
  2765. set_thread_exc_port_cmd (const char *args, int from_tty)
  2766. {
  2767. struct proc *thread = cur_thread ();
  2768. if (!args)
  2769. error (_("No argument to \"set thread exception-port\" command."));
  2770. gnu_target->steal_exc_port (thread, parse_and_eval_address (args));
  2771. }
  2772. #if 0
  2773. static void
  2774. show_thread_cmd (char *args, int from_tty)
  2775. {
  2776. struct proc *thread = cur_thread ();
  2777. check_empty (args, "show thread");
  2778. show_thread_run_cmd (0, from_tty);
  2779. show_thread_pause_cmd (0, from_tty);
  2780. if (thread->detach_sc != 0)
  2781. show_thread_detach_sc_cmd (0, from_tty);
  2782. }
  2783. #endif
  2784. static void
  2785. thread_takeover_sc_cmd (const char *args, int from_tty)
  2786. {
  2787. struct proc *thread = cur_thread ();
  2788. thread_basic_info_data_t _info;
  2789. thread_basic_info_t info = &_info;
  2790. mach_msg_type_number_t info_len = THREAD_BASIC_INFO_COUNT;
  2791. kern_return_t err
  2792. = mach_thread_info (thread->port, THREAD_BASIC_INFO,
  2793. (int *) &info, &info_len);
  2794. if (err)
  2795. error (("%s."), safe_strerror (err));
  2796. thread->sc = info->suspend_count;
  2797. if (from_tty)
  2798. gdb_printf ("Suspend count was %d.\n", thread->sc);
  2799. if (info != &_info)
  2800. vm_deallocate (mach_task_self (), (vm_address_t) info,
  2801. info_len * sizeof (int));
  2802. }
  2803. static void
  2804. add_thread_commands (void)
  2805. {
  2806. add_setshow_prefix_cmd ("thread", no_class,
  2807. _("Command prefix for setting thread properties."),
  2808. _("Command prefix for showing thread properties."),
  2809. &set_thread_cmd_list,
  2810. &show_thread_cmd_list,
  2811. &setlist, &showlist);
  2812. add_setshow_prefix_cmd ("default", no_class,
  2813. _("Command prefix for setting default thread properties."),
  2814. _("Command prefix for showing default thread properties."),
  2815. &set_thread_default_cmd_list,
  2816. &show_thread_default_cmd_list,
  2817. &set_thread_cmd_list, &show_thread_cmd_list);
  2818. add_cmd ("pause", class_run, set_thread_pause_cmd, _("\
  2819. Set whether the current thread is suspended while gdb has control.\n\
  2820. A value of \"on\" takes effect immediately, otherwise nothing happens\n\
  2821. until the next time the program is continued. This property normally\n\
  2822. has no effect because the whole task is suspended, however, that may\n\
  2823. be disabled with \"set task pause off\".\n\
  2824. The default value is \"off\"."),
  2825. &set_thread_cmd_list);
  2826. add_cmd ("pause", no_class, show_thread_pause_cmd, _("\
  2827. Show whether the current thread is suspended while gdb has control."),
  2828. &show_thread_cmd_list);
  2829. add_cmd ("run", class_run, set_thread_run_cmd,
  2830. _("Set whether the current thread is allowed to run."),
  2831. &set_thread_cmd_list);
  2832. add_cmd ("run", no_class, show_thread_run_cmd,
  2833. _("Show whether the current thread is allowed to run."),
  2834. &show_thread_cmd_list);
  2835. add_cmd ("detach-suspend-count", class_run, set_thread_detach_sc_cmd, _("\
  2836. Set the suspend count will leave on the thread when detaching.\n\
  2837. Note that this is relative to suspend count when gdb noticed the thread;\n\
  2838. use the `thread takeover-suspend-count' to force it to an absolute value."),
  2839. &set_thread_cmd_list);
  2840. add_cmd ("detach-suspend-count", no_class, show_thread_detach_sc_cmd, _("\
  2841. Show the suspend count will leave on the thread when detaching.\n\
  2842. Note that this is relative to suspend count when gdb noticed the thread;\n\
  2843. use the `thread takeover-suspend-count' to force it to an absolute value."),
  2844. &show_thread_cmd_list);
  2845. cmd_list_element *set_thread_exception_port_cmd_
  2846. = add_cmd ("exception-port", no_class, set_thread_exc_port_cmd, _("\
  2847. Set the thread exception port to which we forward exceptions.\n\
  2848. This overrides the task exception port.\n\
  2849. The argument should be the value of the send right in the task."),
  2850. &set_thread_cmd_list);
  2851. add_alias_cmd ("excp", set_thread_exception_port_cmd_, no_class, 1,
  2852. &set_thread_cmd_list);
  2853. add_alias_cmd ("exc-port", set_thread_exception_port_cmd_, no_class, 1,
  2854. &set_thread_cmd_list);
  2855. add_cmd ("takeover-suspend-count", no_class, thread_takeover_sc_cmd, _("\
  2856. Force the threads absolute suspend-count to be gdb's.\n\
  2857. Prior to giving this command, gdb's thread suspend-counts are relative\n\
  2858. to the thread's initial suspend-count when gdb notices the threads."),
  2859. &thread_cmd_list);
  2860. }
  2861. void _initialize_gnu_nat ();
  2862. void
  2863. _initialize_gnu_nat ()
  2864. {
  2865. proc_server = getproc ();
  2866. add_task_commands ();
  2867. add_thread_commands ();
  2868. add_setshow_boolean_cmd ("gnu-nat", class_maintenance,
  2869. &gnu_debug_flag,
  2870. _("Set debugging output for the gnu backend."),
  2871. _("Show debugging output for the gnu backend."),
  2872. NULL,
  2873. NULL,
  2874. NULL,
  2875. &setdebuglist,
  2876. &showdebuglist);
  2877. }
  2878. #ifdef FLUSH_INFERIOR_CACHE
  2879. /* When over-writing code on some machines the I-Cache must be flushed
  2880. explicitly, because it is not kept coherent by the lazy hardware.
  2881. This definitely includes breakpoints, for instance, or else we
  2882. end up looping in mysterious Bpt traps. */
  2883. void
  2884. flush_inferior_icache (CORE_ADDR pc, int amount)
  2885. {
  2886. vm_machine_attribute_val_t flush = MATTR_VAL_ICACHE_FLUSH;
  2887. kern_return_t ret;
  2888. ret = vm_machine_attribute (gnu_current_inf->task->port,
  2889. pc,
  2890. amount,
  2891. MATTR_CACHE,
  2892. &flush);
  2893. if (ret != KERN_SUCCESS)
  2894. warning (_("Error flushing inferior's cache : %s"), safe_strerror (ret));
  2895. }
  2896. #endif /* FLUSH_INFERIOR_CACHE */