procfs.c 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630
  1. /* Machine independent support for Solaris /proc (process file system) for GDB.
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. Written by Michael Snyder at Cygnus Solutions.
  4. Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
  5. This file is part of GDB.
  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 "defs.h"
  17. #include "inferior.h"
  18. #include "infrun.h"
  19. #include "target.h"
  20. #include "gdbcore.h"
  21. #include "elf-bfd.h" /* for elfcore_write_* */
  22. #include "gdbcmd.h"
  23. #include "gdbthread.h"
  24. #include "regcache.h"
  25. #include "inf-child.h"
  26. #include "nat/fork-inferior.h"
  27. #include "gdbarch.h"
  28. #include <sys/procfs.h>
  29. #include <sys/fault.h>
  30. #include <sys/syscall.h>
  31. #include "gdbsupport/gdb_wait.h"
  32. #include <signal.h>
  33. #include <ctype.h>
  34. #include "gdb_bfd.h"
  35. #include "auxv.h"
  36. #include "procfs.h"
  37. #include "observable.h"
  38. #include "gdbsupport/scoped_fd.h"
  39. #include "gdbsupport/pathstuff.h"
  40. #include "gdbsupport/buildargv.h"
  41. /* This module provides the interface between GDB and the
  42. /proc file system, which is used on many versions of Unix
  43. as a means for debuggers to control other processes.
  44. /proc works by imitating a file system: you open a simulated file
  45. that represents the process you wish to interact with, and perform
  46. operations on that "file" in order to examine or change the state
  47. of the other process.
  48. The most important thing to know about /proc and this module is
  49. that there are two very different interfaces to /proc:
  50. One that uses the ioctl system call, and another that uses read
  51. and write system calls.
  52. This module supports only the Solaris version of the read/write
  53. interface. */
  54. #include <sys/types.h>
  55. #include <dirent.h> /* opendir/readdir, for listing the LWP's */
  56. #include <fcntl.h> /* for O_RDONLY */
  57. #include <unistd.h> /* for "X_OK" */
  58. #include <sys/stat.h> /* for struct stat */
  59. /* Note: procfs-utils.h must be included after the above system header
  60. files, because it redefines various system calls using macros.
  61. This may be incompatible with the prototype declarations. */
  62. #include "proc-utils.h"
  63. /* Prototypes for supply_gregset etc. */
  64. #include "gregset.h"
  65. /* =================== TARGET_OPS "MODULE" =================== */
  66. /* This module defines the GDB target vector and its methods. */
  67. static enum target_xfer_status procfs_xfer_memory (gdb_byte *,
  68. const gdb_byte *,
  69. ULONGEST, ULONGEST,
  70. ULONGEST *);
  71. class procfs_target final : public inf_child_target
  72. {
  73. public:
  74. void create_inferior (const char *, const std::string &,
  75. char **, int) override;
  76. void kill () override;
  77. void mourn_inferior () override;
  78. void attach (const char *, int) override;
  79. void detach (inferior *inf, int) override;
  80. void resume (ptid_t, int, enum gdb_signal) override;
  81. ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
  82. void fetch_registers (struct regcache *, int) override;
  83. void store_registers (struct regcache *, int) override;
  84. enum target_xfer_status xfer_partial (enum target_object object,
  85. const char *annex,
  86. gdb_byte *readbuf,
  87. const gdb_byte *writebuf,
  88. ULONGEST offset, ULONGEST len,
  89. ULONGEST *xfered_len) override;
  90. void pass_signals (gdb::array_view<const unsigned char>) override;
  91. void files_info () override;
  92. void update_thread_list () override;
  93. bool thread_alive (ptid_t ptid) override;
  94. std::string pid_to_str (ptid_t) override;
  95. char *pid_to_exec_file (int pid) override;
  96. thread_control_capabilities get_thread_control_capabilities () override
  97. { return tc_schedlock; }
  98. /* find_memory_regions support method for gcore */
  99. int find_memory_regions (find_memory_region_ftype func, void *data)
  100. override;
  101. gdb::unique_xmalloc_ptr<char> make_corefile_notes (bfd *, int *) override;
  102. bool info_proc (const char *, enum info_proc_what) override;
  103. #if PR_MODEL_NATIVE == PR_MODEL_LP64
  104. int auxv_parse (gdb_byte **readptr,
  105. gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
  106. override;
  107. #endif
  108. bool stopped_by_watchpoint () override;
  109. int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
  110. struct expression *) override;
  111. int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
  112. struct expression *) override;
  113. int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
  114. int can_use_hw_breakpoint (enum bptype, int, int) override;
  115. bool stopped_data_address (CORE_ADDR *) override;
  116. void procfs_init_inferior (int pid);
  117. };
  118. static procfs_target the_procfs_target;
  119. #if PR_MODEL_NATIVE == PR_MODEL_LP64
  120. /* When GDB is built as 64-bit application on Solaris, the auxv data
  121. is presented in 64-bit format. We need to provide a custom parser
  122. to handle that. */
  123. int
  124. procfs_target::auxv_parse (gdb_byte **readptr,
  125. gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
  126. {
  127. enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  128. gdb_byte *ptr = *readptr;
  129. if (endptr == ptr)
  130. return 0;
  131. if (endptr - ptr < 8 * 2)
  132. return -1;
  133. *typep = extract_unsigned_integer (ptr, 4, byte_order);
  134. ptr += 8;
  135. /* The size of data is always 64-bit. If the application is 32-bit,
  136. it will be zero extended, as expected. */
  137. *valp = extract_unsigned_integer (ptr, 8, byte_order);
  138. ptr += 8;
  139. *readptr = ptr;
  140. return 1;
  141. }
  142. #endif
  143. /* =================== END, TARGET_OPS "MODULE" =================== */
  144. /* =================== STRUCT PROCINFO "MODULE" =================== */
  145. /* FIXME: this comment will soon be out of date W.R.T. threads. */
  146. /* The procinfo struct is a wrapper to hold all the state information
  147. concerning a /proc process. There should be exactly one procinfo
  148. for each process, and since GDB currently can debug only one
  149. process at a time, that means there should be only one procinfo.
  150. All of the LWP's of a process can be accessed indirectly thru the
  151. single process procinfo.
  152. However, against the day when GDB may debug more than one process,
  153. this data structure is kept in a list (which for now will hold no
  154. more than one member), and many functions will have a pointer to a
  155. procinfo as an argument.
  156. There will be a separate procinfo structure for use by the (not yet
  157. implemented) "info proc" command, so that we can print useful
  158. information about any random process without interfering with the
  159. inferior's procinfo information. */
  160. /* format strings for /proc paths */
  161. #define CTL_PROC_NAME_FMT "/proc/%d/ctl"
  162. #define AS_PROC_NAME_FMT "/proc/%d/as"
  163. #define MAP_PROC_NAME_FMT "/proc/%d/map"
  164. #define STATUS_PROC_NAME_FMT "/proc/%d/status"
  165. #define MAX_PROC_NAME_SIZE sizeof("/proc/999999/lwp/0123456789/lwpstatus")
  166. typedef struct procinfo {
  167. struct procinfo *next;
  168. int pid; /* Process ID */
  169. int tid; /* Thread/LWP id */
  170. /* process state */
  171. int was_stopped;
  172. int ignore_next_sigstop;
  173. int ctl_fd; /* File descriptor for /proc control file */
  174. int status_fd; /* File descriptor for /proc status file */
  175. int as_fd; /* File descriptor for /proc as file */
  176. char pathname[MAX_PROC_NAME_SIZE]; /* Pathname to /proc entry */
  177. fltset_t saved_fltset; /* Saved traced hardware fault set */
  178. sigset_t saved_sigset; /* Saved traced signal set */
  179. sigset_t saved_sighold; /* Saved held signal set */
  180. sysset_t *saved_exitset; /* Saved traced system call exit set */
  181. sysset_t *saved_entryset; /* Saved traced system call entry set */
  182. pstatus_t prstatus; /* Current process status info */
  183. struct procinfo *thread_list;
  184. int status_valid : 1;
  185. int gregs_valid : 1;
  186. int fpregs_valid : 1;
  187. int threads_valid: 1;
  188. } procinfo;
  189. static char errmsg[128]; /* shared error msg buffer */
  190. /* Function prototypes for procinfo module: */
  191. static procinfo *find_procinfo_or_die (int pid, int tid);
  192. static procinfo *find_procinfo (int pid, int tid);
  193. static procinfo *create_procinfo (int pid, int tid);
  194. static void destroy_procinfo (procinfo *p);
  195. static void dead_procinfo (procinfo *p, const char *msg, int killp);
  196. static int open_procinfo_files (procinfo *p, int which);
  197. static void close_procinfo_files (procinfo *p);
  198. static int iterate_over_mappings
  199. (procinfo *pi, find_memory_region_ftype child_func, void *data,
  200. int (*func) (struct prmap *map, find_memory_region_ftype child_func,
  201. void *data));
  202. /* The head of the procinfo list: */
  203. static procinfo *procinfo_list;
  204. /* Search the procinfo list. Return a pointer to procinfo, or NULL if
  205. not found. */
  206. static procinfo *
  207. find_procinfo (int pid, int tid)
  208. {
  209. procinfo *pi;
  210. for (pi = procinfo_list; pi; pi = pi->next)
  211. if (pi->pid == pid)
  212. break;
  213. if (pi)
  214. if (tid)
  215. {
  216. /* Don't check threads_valid. If we're updating the
  217. thread_list, we want to find whatever threads are already
  218. here. This means that in general it is the caller's
  219. responsibility to check threads_valid and update before
  220. calling find_procinfo, if the caller wants to find a new
  221. thread. */
  222. for (pi = pi->thread_list; pi; pi = pi->next)
  223. if (pi->tid == tid)
  224. break;
  225. }
  226. return pi;
  227. }
  228. /* Calls find_procinfo, but errors on failure. */
  229. static procinfo *
  230. find_procinfo_or_die (int pid, int tid)
  231. {
  232. procinfo *pi = find_procinfo (pid, tid);
  233. if (pi == NULL)
  234. {
  235. if (tid)
  236. error (_("procfs: couldn't find pid %d "
  237. "(kernel thread %d) in procinfo list."),
  238. pid, tid);
  239. else
  240. error (_("procfs: couldn't find pid %d in procinfo list."), pid);
  241. }
  242. return pi;
  243. }
  244. /* Wrapper for `open'. The appropriate open call is attempted; if
  245. unsuccessful, it will be retried as many times as needed for the
  246. EAGAIN and EINTR conditions.
  247. For other conditions, retry the open a limited number of times. In
  248. addition, a short sleep is imposed prior to retrying the open. The
  249. reason for this sleep is to give the kernel a chance to catch up
  250. and create the file in question in the event that GDB "wins" the
  251. race to open a file before the kernel has created it. */
  252. static int
  253. open_with_retry (const char *pathname, int flags)
  254. {
  255. int retries_remaining, status;
  256. retries_remaining = 2;
  257. while (1)
  258. {
  259. status = open (pathname, flags);
  260. if (status >= 0 || retries_remaining == 0)
  261. break;
  262. else if (errno != EINTR && errno != EAGAIN)
  263. {
  264. retries_remaining--;
  265. sleep (1);
  266. }
  267. }
  268. return status;
  269. }
  270. /* Open the file descriptor for the process or LWP. We only open the
  271. control file descriptor; the others are opened lazily as needed.
  272. Returns the file descriptor, or zero for failure. */
  273. enum { FD_CTL, FD_STATUS, FD_AS };
  274. static int
  275. open_procinfo_files (procinfo *pi, int which)
  276. {
  277. char tmp[MAX_PROC_NAME_SIZE];
  278. int fd;
  279. /* This function is getting ALMOST long enough to break up into
  280. several. Here is some rationale:
  281. There are several file descriptors that may need to be open
  282. for any given process or LWP. The ones we're interested in are:
  283. - control (ctl) write-only change the state
  284. - status (status) read-only query the state
  285. - address space (as) read/write access memory
  286. - map (map) read-only virtual addr map
  287. Most of these are opened lazily as they are needed.
  288. The pathnames for the 'files' for an LWP look slightly
  289. different from those of a first-class process:
  290. Pathnames for a process (<proc-id>):
  291. /proc/<proc-id>/ctl
  292. /proc/<proc-id>/status
  293. /proc/<proc-id>/as
  294. /proc/<proc-id>/map
  295. Pathnames for an LWP (lwp-id):
  296. /proc/<proc-id>/lwp/<lwp-id>/lwpctl
  297. /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
  298. An LWP has no map or address space file descriptor, since
  299. the memory map and address space are shared by all LWPs. */
  300. /* In this case, there are several different file descriptors that
  301. we might be asked to open. The control file descriptor will be
  302. opened early, but the others will be opened lazily as they are
  303. needed. */
  304. strcpy (tmp, pi->pathname);
  305. switch (which) { /* Which file descriptor to open? */
  306. case FD_CTL:
  307. if (pi->tid)
  308. strcat (tmp, "/lwpctl");
  309. else
  310. strcat (tmp, "/ctl");
  311. fd = open_with_retry (tmp, O_WRONLY);
  312. if (fd < 0)
  313. return 0; /* fail */
  314. pi->ctl_fd = fd;
  315. break;
  316. case FD_AS:
  317. if (pi->tid)
  318. return 0; /* There is no 'as' file descriptor for an lwp. */
  319. strcat (tmp, "/as");
  320. fd = open_with_retry (tmp, O_RDWR);
  321. if (fd < 0)
  322. return 0; /* fail */
  323. pi->as_fd = fd;
  324. break;
  325. case FD_STATUS:
  326. if (pi->tid)
  327. strcat (tmp, "/lwpstatus");
  328. else
  329. strcat (tmp, "/status");
  330. fd = open_with_retry (tmp, O_RDONLY);
  331. if (fd < 0)
  332. return 0; /* fail */
  333. pi->status_fd = fd;
  334. break;
  335. default:
  336. return 0; /* unknown file descriptor */
  337. }
  338. return 1; /* success */
  339. }
  340. /* Allocate a data structure and link it into the procinfo list.
  341. First tries to find a pre-existing one (FIXME: why?). Returns the
  342. pointer to new procinfo struct. */
  343. static procinfo *
  344. create_procinfo (int pid, int tid)
  345. {
  346. procinfo *pi, *parent = NULL;
  347. pi = find_procinfo (pid, tid);
  348. if (pi != NULL)
  349. return pi; /* Already exists, nothing to do. */
  350. /* Find parent before doing malloc, to save having to cleanup. */
  351. if (tid != 0)
  352. parent = find_procinfo_or_die (pid, 0); /* FIXME: should I
  353. create it if it
  354. doesn't exist yet? */
  355. pi = XNEW (procinfo);
  356. memset (pi, 0, sizeof (procinfo));
  357. pi->pid = pid;
  358. pi->tid = tid;
  359. pi->saved_entryset = XNEW (sysset_t);
  360. pi->saved_exitset = XNEW (sysset_t);
  361. /* Chain into list. */
  362. if (tid == 0)
  363. {
  364. xsnprintf (pi->pathname, sizeof (pi->pathname), "/proc/%d", pid);
  365. pi->next = procinfo_list;
  366. procinfo_list = pi;
  367. }
  368. else
  369. {
  370. xsnprintf (pi->pathname, sizeof (pi->pathname), "/proc/%d/lwp/%d",
  371. pid, tid);
  372. pi->next = parent->thread_list;
  373. parent->thread_list = pi;
  374. }
  375. return pi;
  376. }
  377. /* Close all file descriptors associated with the procinfo. */
  378. static void
  379. close_procinfo_files (procinfo *pi)
  380. {
  381. if (pi->ctl_fd > 0)
  382. close (pi->ctl_fd);
  383. if (pi->as_fd > 0)
  384. close (pi->as_fd);
  385. if (pi->status_fd > 0)
  386. close (pi->status_fd);
  387. pi->ctl_fd = pi->as_fd = pi->status_fd = 0;
  388. }
  389. /* Destructor function. Close, unlink and deallocate the object. */
  390. static void
  391. destroy_one_procinfo (procinfo **list, procinfo *pi)
  392. {
  393. procinfo *ptr;
  394. /* Step one: unlink the procinfo from its list. */
  395. if (pi == *list)
  396. *list = pi->next;
  397. else
  398. for (ptr = *list; ptr; ptr = ptr->next)
  399. if (ptr->next == pi)
  400. {
  401. ptr->next = pi->next;
  402. break;
  403. }
  404. /* Step two: close any open file descriptors. */
  405. close_procinfo_files (pi);
  406. /* Step three: free the memory. */
  407. xfree (pi->saved_entryset);
  408. xfree (pi->saved_exitset);
  409. xfree (pi);
  410. }
  411. static void
  412. destroy_procinfo (procinfo *pi)
  413. {
  414. procinfo *tmp;
  415. if (pi->tid != 0) /* Destroy a thread procinfo. */
  416. {
  417. tmp = find_procinfo (pi->pid, 0); /* Find the parent process. */
  418. destroy_one_procinfo (&tmp->thread_list, pi);
  419. }
  420. else /* Destroy a process procinfo and all its threads. */
  421. {
  422. /* First destroy the children, if any; */
  423. while (pi->thread_list != NULL)
  424. destroy_one_procinfo (&pi->thread_list, pi->thread_list);
  425. /* Then destroy the parent. Genocide!!! */
  426. destroy_one_procinfo (&procinfo_list, pi);
  427. }
  428. }
  429. /* A deleter that calls destroy_procinfo. */
  430. struct procinfo_deleter
  431. {
  432. void operator() (procinfo *pi) const
  433. {
  434. destroy_procinfo (pi);
  435. }
  436. };
  437. typedef std::unique_ptr<procinfo, procinfo_deleter> procinfo_up;
  438. enum { NOKILL, KILL };
  439. /* To be called on a non_recoverable error for a procinfo. Prints
  440. error messages, optionally sends a SIGKILL to the process, then
  441. destroys the data structure. */
  442. static void
  443. dead_procinfo (procinfo *pi, const char *msg, int kill_p)
  444. {
  445. char procfile[80];
  446. if (pi->pathname)
  447. print_sys_errmsg (pi->pathname, errno);
  448. else
  449. {
  450. xsnprintf (procfile, sizeof (procfile), "process %d", pi->pid);
  451. print_sys_errmsg (procfile, errno);
  452. }
  453. if (kill_p == KILL)
  454. kill (pi->pid, SIGKILL);
  455. destroy_procinfo (pi);
  456. error ("%s", msg);
  457. }
  458. /* =================== END, STRUCT PROCINFO "MODULE" =================== */
  459. /* =================== /proc "MODULE" =================== */
  460. /* This "module" is the interface layer between the /proc system API
  461. and the gdb target vector functions. This layer consists of access
  462. functions that encapsulate each of the basic operations that we
  463. need to use from the /proc API.
  464. The main motivation for this layer is to hide the fact that there
  465. were two very different implementations of the /proc API. */
  466. static long proc_flags (procinfo *pi);
  467. static int proc_why (procinfo *pi);
  468. static int proc_what (procinfo *pi);
  469. static int proc_set_current_signal (procinfo *pi, int signo);
  470. static int proc_get_current_thread (procinfo *pi);
  471. static int proc_iterate_over_threads
  472. (procinfo *pi,
  473. int (*func) (procinfo *, procinfo *, void *),
  474. void *ptr);
  475. static void
  476. proc_warn (procinfo *pi, const char *func, int line)
  477. {
  478. xsnprintf (errmsg, sizeof (errmsg), "procfs: %s line %d, %s",
  479. func, line, pi->pathname);
  480. print_sys_errmsg (errmsg, errno);
  481. }
  482. static void
  483. proc_error (procinfo *pi, const char *func, int line)
  484. {
  485. xsnprintf (errmsg, sizeof (errmsg), "procfs: %s line %d, %s",
  486. func, line, pi->pathname);
  487. perror_with_name (errmsg);
  488. }
  489. /* Updates the status struct in the procinfo. There is a 'valid'
  490. flag, to let other functions know when this function needs to be
  491. called (so the status is only read when it is needed). The status
  492. file descriptor is also only opened when it is needed. Returns
  493. non-zero for success, zero for failure. */
  494. static int
  495. proc_get_status (procinfo *pi)
  496. {
  497. /* Status file descriptor is opened "lazily". */
  498. if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
  499. {
  500. pi->status_valid = 0;
  501. return 0;
  502. }
  503. if (lseek (pi->status_fd, 0, SEEK_SET) < 0)
  504. pi->status_valid = 0; /* fail */
  505. else
  506. {
  507. /* Sigh... I have to read a different data structure,
  508. depending on whether this is a main process or an LWP. */
  509. if (pi->tid)
  510. pi->status_valid = (read (pi->status_fd,
  511. (char *) &pi->prstatus.pr_lwp,
  512. sizeof (lwpstatus_t))
  513. == sizeof (lwpstatus_t));
  514. else
  515. {
  516. pi->status_valid = (read (pi->status_fd,
  517. (char *) &pi->prstatus,
  518. sizeof (pstatus_t))
  519. == sizeof (pstatus_t));
  520. }
  521. }
  522. if (pi->status_valid)
  523. {
  524. PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
  525. proc_why (pi),
  526. proc_what (pi),
  527. proc_get_current_thread (pi));
  528. }
  529. /* The status struct includes general regs, so mark them valid too. */
  530. pi->gregs_valid = pi->status_valid;
  531. /* In the read/write multiple-fd model, the status struct includes
  532. the fp regs too, so mark them valid too. */
  533. pi->fpregs_valid = pi->status_valid;
  534. return pi->status_valid; /* True if success, false if failure. */
  535. }
  536. /* Returns the process flags (pr_flags field). */
  537. static long
  538. proc_flags (procinfo *pi)
  539. {
  540. if (!pi->status_valid)
  541. if (!proc_get_status (pi))
  542. return 0; /* FIXME: not a good failure value (but what is?) */
  543. return pi->prstatus.pr_lwp.pr_flags;
  544. }
  545. /* Returns the pr_why field (why the process stopped). */
  546. static int
  547. proc_why (procinfo *pi)
  548. {
  549. if (!pi->status_valid)
  550. if (!proc_get_status (pi))
  551. return 0; /* FIXME: not a good failure value (but what is?) */
  552. return pi->prstatus.pr_lwp.pr_why;
  553. }
  554. /* Returns the pr_what field (details of why the process stopped). */
  555. static int
  556. proc_what (procinfo *pi)
  557. {
  558. if (!pi->status_valid)
  559. if (!proc_get_status (pi))
  560. return 0; /* FIXME: not a good failure value (but what is?) */
  561. return pi->prstatus.pr_lwp.pr_what;
  562. }
  563. /* This function is only called when PI is stopped by a watchpoint.
  564. Assuming the OS supports it, write to *ADDR the data address which
  565. triggered it and return 1. Return 0 if it is not possible to know
  566. the address. */
  567. static int
  568. proc_watchpoint_address (procinfo *pi, CORE_ADDR *addr)
  569. {
  570. if (!pi->status_valid)
  571. if (!proc_get_status (pi))
  572. return 0;
  573. *addr = (CORE_ADDR) gdbarch_pointer_to_address (target_gdbarch (),
  574. builtin_type (target_gdbarch ())->builtin_data_ptr,
  575. (gdb_byte *) &pi->prstatus.pr_lwp.pr_info.si_addr);
  576. return 1;
  577. }
  578. /* Returns the pr_nsysarg field (number of args to the current
  579. syscall). */
  580. static int
  581. proc_nsysarg (procinfo *pi)
  582. {
  583. if (!pi->status_valid)
  584. if (!proc_get_status (pi))
  585. return 0;
  586. return pi->prstatus.pr_lwp.pr_nsysarg;
  587. }
  588. /* Returns the pr_sysarg field (pointer to the arguments of current
  589. syscall). */
  590. static long *
  591. proc_sysargs (procinfo *pi)
  592. {
  593. if (!pi->status_valid)
  594. if (!proc_get_status (pi))
  595. return NULL;
  596. return (long *) &pi->prstatus.pr_lwp.pr_sysarg;
  597. }
  598. /* Set or reset any of the following process flags:
  599. PR_FORK -- forked child will inherit trace flags
  600. PR_RLC -- traced process runs when last /proc file closed.
  601. PR_KLC -- traced process is killed when last /proc file closed.
  602. PR_ASYNC -- LWP's get to run/stop independently.
  603. This function is done using read/write [PCSET/PCRESET/PCUNSET].
  604. Arguments:
  605. pi -- the procinfo
  606. flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
  607. mode -- 1 for set, 0 for reset.
  608. Returns non-zero for success, zero for failure. */
  609. enum { FLAG_RESET, FLAG_SET };
  610. static int
  611. proc_modify_flag (procinfo *pi, long flag, long mode)
  612. {
  613. long win = 0; /* default to fail */
  614. /* These operations affect the process as a whole, and applying them
  615. to an individual LWP has the same meaning as applying them to the
  616. main process. Therefore, if we're ever called with a pointer to
  617. an LWP's procinfo, let's substitute the process's procinfo and
  618. avoid opening the LWP's file descriptor unnecessarily. */
  619. if (pi->pid != 0)
  620. pi = find_procinfo_or_die (pi->pid, 0);
  621. procfs_ctl_t arg[2];
  622. if (mode == FLAG_SET) /* Set the flag (RLC, FORK, or ASYNC). */
  623. arg[0] = PCSET;
  624. else /* Reset the flag. */
  625. arg[0] = PCUNSET;
  626. arg[1] = flag;
  627. win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
  628. /* The above operation renders the procinfo's cached pstatus
  629. obsolete. */
  630. pi->status_valid = 0;
  631. if (!win)
  632. warning (_("procfs: modify_flag failed to turn %s %s"),
  633. flag == PR_FORK ? "PR_FORK" :
  634. flag == PR_RLC ? "PR_RLC" :
  635. flag == PR_ASYNC ? "PR_ASYNC" :
  636. flag == PR_KLC ? "PR_KLC" :
  637. "<unknown flag>",
  638. mode == FLAG_RESET ? "off" : "on");
  639. return win;
  640. }
  641. /* Set the run_on_last_close flag. Process with all threads will
  642. become runnable when debugger closes all /proc fds. Returns
  643. non-zero for success, zero for failure. */
  644. static int
  645. proc_set_run_on_last_close (procinfo *pi)
  646. {
  647. return proc_modify_flag (pi, PR_RLC, FLAG_SET);
  648. }
  649. /* Reset the run_on_last_close flag. The process will NOT become
  650. runnable when debugger closes its file handles. Returns non-zero
  651. for success, zero for failure. */
  652. static int
  653. proc_unset_run_on_last_close (procinfo *pi)
  654. {
  655. return proc_modify_flag (pi, PR_RLC, FLAG_RESET);
  656. }
  657. /* Reset inherit_on_fork flag. If the process forks a child while we
  658. are registered for events in the parent, then we will NOT receive
  659. events from the child. Returns non-zero for success, zero for
  660. failure. */
  661. static int
  662. proc_unset_inherit_on_fork (procinfo *pi)
  663. {
  664. return proc_modify_flag (pi, PR_FORK, FLAG_RESET);
  665. }
  666. /* Set PR_ASYNC flag. If one LWP stops because of a debug event
  667. (signal etc.), the remaining LWPs will continue to run. Returns
  668. non-zero for success, zero for failure. */
  669. static int
  670. proc_set_async (procinfo *pi)
  671. {
  672. return proc_modify_flag (pi, PR_ASYNC, FLAG_SET);
  673. }
  674. /* Reset PR_ASYNC flag. If one LWP stops because of a debug event
  675. (signal etc.), then all other LWPs will stop as well. Returns
  676. non-zero for success, zero for failure. */
  677. static int
  678. proc_unset_async (procinfo *pi)
  679. {
  680. return proc_modify_flag (pi, PR_ASYNC, FLAG_RESET);
  681. }
  682. /* Request the process/LWP to stop. Does not wait. Returns non-zero
  683. for success, zero for failure. */
  684. static int
  685. proc_stop_process (procinfo *pi)
  686. {
  687. int win;
  688. /* We might conceivably apply this operation to an LWP, and the
  689. LWP's ctl file descriptor might not be open. */
  690. if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
  691. return 0;
  692. else
  693. {
  694. procfs_ctl_t cmd = PCSTOP;
  695. win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
  696. }
  697. return win;
  698. }
  699. /* Wait for the process or LWP to stop (block until it does). Returns
  700. non-zero for success, zero for failure. */
  701. static int
  702. proc_wait_for_stop (procinfo *pi)
  703. {
  704. int win;
  705. /* We should never have to apply this operation to any procinfo
  706. except the one for the main process. If that ever changes for
  707. any reason, then take out the following clause and replace it
  708. with one that makes sure the ctl_fd is open. */
  709. if (pi->tid != 0)
  710. pi = find_procinfo_or_die (pi->pid, 0);
  711. procfs_ctl_t cmd = PCWSTOP;
  712. set_sigint_trap ();
  713. win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
  714. clear_sigint_trap ();
  715. /* We been runnin' and we stopped -- need to update status. */
  716. pi->status_valid = 0;
  717. return win;
  718. }
  719. /* Make the process or LWP runnable.
  720. Options (not all are implemented):
  721. - single-step
  722. - clear current fault
  723. - clear current signal
  724. - abort the current system call
  725. - stop as soon as finished with system call
  726. Always clears the current fault. PI is the process or LWP to
  727. operate on. If STEP is true, set the process or LWP to trap after
  728. one instruction. If SIGNO is zero, clear the current signal if
  729. any; if non-zero, set the current signal to this one. Returns
  730. non-zero for success, zero for failure. */
  731. static int
  732. proc_run_process (procinfo *pi, int step, int signo)
  733. {
  734. int win;
  735. int runflags;
  736. /* We will probably have to apply this operation to individual
  737. threads, so make sure the control file descriptor is open. */
  738. if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
  739. return 0;
  740. runflags = PRCFAULT; /* Always clear current fault. */
  741. if (step)
  742. runflags |= PRSTEP;
  743. if (signo == 0)
  744. runflags |= PRCSIG;
  745. else if (signo != -1) /* -1 means do nothing W.R.T. signals. */
  746. proc_set_current_signal (pi, signo);
  747. procfs_ctl_t cmd[2];
  748. cmd[0] = PCRUN;
  749. cmd[1] = runflags;
  750. win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
  751. return win;
  752. }
  753. /* Register to trace signals in the process or LWP. Returns non-zero
  754. for success, zero for failure. */
  755. static int
  756. proc_set_traced_signals (procinfo *pi, sigset_t *sigset)
  757. {
  758. int win;
  759. /* We should never have to apply this operation to any procinfo
  760. except the one for the main process. If that ever changes for
  761. any reason, then take out the following clause and replace it
  762. with one that makes sure the ctl_fd is open. */
  763. if (pi->tid != 0)
  764. pi = find_procinfo_or_die (pi->pid, 0);
  765. struct {
  766. procfs_ctl_t cmd;
  767. /* Use char array to avoid alignment issues. */
  768. char sigset[sizeof (sigset_t)];
  769. } arg;
  770. arg.cmd = PCSTRACE;
  771. memcpy (&arg.sigset, sigset, sizeof (sigset_t));
  772. win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
  773. /* The above operation renders the procinfo's cached pstatus obsolete. */
  774. pi->status_valid = 0;
  775. if (!win)
  776. warning (_("procfs: set_traced_signals failed"));
  777. return win;
  778. }
  779. /* Register to trace hardware faults in the process or LWP. Returns
  780. non-zero for success, zero for failure. */
  781. static int
  782. proc_set_traced_faults (procinfo *pi, fltset_t *fltset)
  783. {
  784. int win;
  785. /* We should never have to apply this operation to any procinfo
  786. except the one for the main process. If that ever changes for
  787. any reason, then take out the following clause and replace it
  788. with one that makes sure the ctl_fd is open. */
  789. if (pi->tid != 0)
  790. pi = find_procinfo_or_die (pi->pid, 0);
  791. struct {
  792. procfs_ctl_t cmd;
  793. /* Use char array to avoid alignment issues. */
  794. char fltset[sizeof (fltset_t)];
  795. } arg;
  796. arg.cmd = PCSFAULT;
  797. memcpy (&arg.fltset, fltset, sizeof (fltset_t));
  798. win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
  799. /* The above operation renders the procinfo's cached pstatus obsolete. */
  800. pi->status_valid = 0;
  801. return win;
  802. }
  803. /* Register to trace entry to system calls in the process or LWP.
  804. Returns non-zero for success, zero for failure. */
  805. static int
  806. proc_set_traced_sysentry (procinfo *pi, sysset_t *sysset)
  807. {
  808. int win;
  809. /* We should never have to apply this operation to any procinfo
  810. except the one for the main process. If that ever changes for
  811. any reason, then take out the following clause and replace it
  812. with one that makes sure the ctl_fd is open. */
  813. if (pi->tid != 0)
  814. pi = find_procinfo_or_die (pi->pid, 0);
  815. struct {
  816. procfs_ctl_t cmd;
  817. /* Use char array to avoid alignment issues. */
  818. char sysset[sizeof (sysset_t)];
  819. } arg;
  820. arg.cmd = PCSENTRY;
  821. memcpy (&arg.sysset, sysset, sizeof (sysset_t));
  822. win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
  823. /* The above operation renders the procinfo's cached pstatus
  824. obsolete. */
  825. pi->status_valid = 0;
  826. return win;
  827. }
  828. /* Register to trace exit from system calls in the process or LWP.
  829. Returns non-zero for success, zero for failure. */
  830. static int
  831. proc_set_traced_sysexit (procinfo *pi, sysset_t *sysset)
  832. {
  833. int win;
  834. /* We should never have to apply this operation to any procinfo
  835. except the one for the main process. If that ever changes for
  836. any reason, then take out the following clause and replace it
  837. with one that makes sure the ctl_fd is open. */
  838. if (pi->tid != 0)
  839. pi = find_procinfo_or_die (pi->pid, 0);
  840. struct gdb_proc_ctl_pcsexit {
  841. procfs_ctl_t cmd;
  842. /* Use char array to avoid alignment issues. */
  843. char sysset[sizeof (sysset_t)];
  844. } arg;
  845. arg.cmd = PCSEXIT;
  846. memcpy (&arg.sysset, sysset, sizeof (sysset_t));
  847. win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
  848. /* The above operation renders the procinfo's cached pstatus
  849. obsolete. */
  850. pi->status_valid = 0;
  851. return win;
  852. }
  853. /* Specify the set of blocked / held signals in the process or LWP.
  854. Returns non-zero for success, zero for failure. */
  855. static int
  856. proc_set_held_signals (procinfo *pi, sigset_t *sighold)
  857. {
  858. int win;
  859. /* We should never have to apply this operation to any procinfo
  860. except the one for the main process. If that ever changes for
  861. any reason, then take out the following clause and replace it
  862. with one that makes sure the ctl_fd is open. */
  863. if (pi->tid != 0)
  864. pi = find_procinfo_or_die (pi->pid, 0);
  865. struct {
  866. procfs_ctl_t cmd;
  867. /* Use char array to avoid alignment issues. */
  868. char hold[sizeof (sigset_t)];
  869. } arg;
  870. arg.cmd = PCSHOLD;
  871. memcpy (&arg.hold, sighold, sizeof (sigset_t));
  872. win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
  873. /* The above operation renders the procinfo's cached pstatus
  874. obsolete. */
  875. pi->status_valid = 0;
  876. return win;
  877. }
  878. /* Returns the set of signals that are held / blocked. Will also copy
  879. the sigset if SAVE is non-zero. */
  880. static sigset_t *
  881. proc_get_held_signals (procinfo *pi, sigset_t *save)
  882. {
  883. sigset_t *ret = NULL;
  884. /* We should never have to apply this operation to any procinfo
  885. except the one for the main process. If that ever changes for
  886. any reason, then take out the following clause and replace it
  887. with one that makes sure the ctl_fd is open. */
  888. if (pi->tid != 0)
  889. pi = find_procinfo_or_die (pi->pid, 0);
  890. if (!pi->status_valid)
  891. if (!proc_get_status (pi))
  892. return NULL;
  893. ret = &pi->prstatus.pr_lwp.pr_lwphold;
  894. if (save && ret)
  895. memcpy (save, ret, sizeof (sigset_t));
  896. return ret;
  897. }
  898. /* Returns the set of signals that are traced / debugged. Will also
  899. copy the sigset if SAVE is non-zero. */
  900. static sigset_t *
  901. proc_get_traced_signals (procinfo *pi, sigset_t *save)
  902. {
  903. sigset_t *ret = NULL;
  904. /* We should never have to apply this operation to any procinfo
  905. except the one for the main process. If that ever changes for
  906. any reason, then take out the following clause and replace it
  907. with one that makes sure the ctl_fd is open. */
  908. if (pi->tid != 0)
  909. pi = find_procinfo_or_die (pi->pid, 0);
  910. if (!pi->status_valid)
  911. if (!proc_get_status (pi))
  912. return NULL;
  913. ret = &pi->prstatus.pr_sigtrace;
  914. if (save && ret)
  915. memcpy (save, ret, sizeof (sigset_t));
  916. return ret;
  917. }
  918. /* Returns the set of hardware faults that are traced /debugged. Will
  919. also copy the faultset if SAVE is non-zero. */
  920. static fltset_t *
  921. proc_get_traced_faults (procinfo *pi, fltset_t *save)
  922. {
  923. fltset_t *ret = NULL;
  924. /* We should never have to apply this operation to any procinfo
  925. except the one for the main process. If that ever changes for
  926. any reason, then take out the following clause and replace it
  927. with one that makes sure the ctl_fd is open. */
  928. if (pi->tid != 0)
  929. pi = find_procinfo_or_die (pi->pid, 0);
  930. if (!pi->status_valid)
  931. if (!proc_get_status (pi))
  932. return NULL;
  933. ret = &pi->prstatus.pr_flttrace;
  934. if (save && ret)
  935. memcpy (save, ret, sizeof (fltset_t));
  936. return ret;
  937. }
  938. /* Returns the set of syscalls that are traced /debugged on entry.
  939. Will also copy the syscall set if SAVE is non-zero. */
  940. static sysset_t *
  941. proc_get_traced_sysentry (procinfo *pi, sysset_t *save)
  942. {
  943. sysset_t *ret = NULL;
  944. /* We should never have to apply this operation to any procinfo
  945. except the one for the main process. If that ever changes for
  946. any reason, then take out the following clause and replace it
  947. with one that makes sure the ctl_fd is open. */
  948. if (pi->tid != 0)
  949. pi = find_procinfo_or_die (pi->pid, 0);
  950. if (!pi->status_valid)
  951. if (!proc_get_status (pi))
  952. return NULL;
  953. ret = &pi->prstatus.pr_sysentry;
  954. if (save && ret)
  955. memcpy (save, ret, sizeof (sysset_t));
  956. return ret;
  957. }
  958. /* Returns the set of syscalls that are traced /debugged on exit.
  959. Will also copy the syscall set if SAVE is non-zero. */
  960. static sysset_t *
  961. proc_get_traced_sysexit (procinfo *pi, sysset_t *save)
  962. {
  963. sysset_t *ret = NULL;
  964. /* We should never have to apply this operation to any procinfo
  965. except the one for the main process. If that ever changes for
  966. any reason, then take out the following clause and replace it
  967. with one that makes sure the ctl_fd is open. */
  968. if (pi->tid != 0)
  969. pi = find_procinfo_or_die (pi->pid, 0);
  970. if (!pi->status_valid)
  971. if (!proc_get_status (pi))
  972. return NULL;
  973. ret = &pi->prstatus.pr_sysexit;
  974. if (save && ret)
  975. memcpy (save, ret, sizeof (sysset_t));
  976. return ret;
  977. }
  978. /* The current fault (if any) is cleared; the associated signal will
  979. not be sent to the process or LWP when it resumes. Returns
  980. non-zero for success, zero for failure. */
  981. static int
  982. proc_clear_current_fault (procinfo *pi)
  983. {
  984. int win;
  985. /* We should never have to apply this operation to any procinfo
  986. except the one for the main process. If that ever changes for
  987. any reason, then take out the following clause and replace it
  988. with one that makes sure the ctl_fd is open. */
  989. if (pi->tid != 0)
  990. pi = find_procinfo_or_die (pi->pid, 0);
  991. procfs_ctl_t cmd = PCCFAULT;
  992. win = (write (pi->ctl_fd, (void *) &cmd, sizeof (cmd)) == sizeof (cmd));
  993. return win;
  994. }
  995. /* Set the "current signal" that will be delivered next to the
  996. process. NOTE: semantics are different from those of KILL. This
  997. signal will be delivered to the process or LWP immediately when it
  998. is resumed (even if the signal is held/blocked); it will NOT
  999. immediately cause another event of interest, and will NOT first
  1000. trap back to the debugger. Returns non-zero for success, zero for
  1001. failure. */
  1002. static int
  1003. proc_set_current_signal (procinfo *pi, int signo)
  1004. {
  1005. int win;
  1006. struct {
  1007. procfs_ctl_t cmd;
  1008. /* Use char array to avoid alignment issues. */
  1009. char sinfo[sizeof (siginfo_t)];
  1010. } arg;
  1011. siginfo_t mysinfo;
  1012. process_stratum_target *wait_target;
  1013. ptid_t wait_ptid;
  1014. struct target_waitstatus wait_status;
  1015. /* We should never have to apply this operation to any procinfo
  1016. except the one for the main process. If that ever changes for
  1017. any reason, then take out the following clause and replace it
  1018. with one that makes sure the ctl_fd is open. */
  1019. if (pi->tid != 0)
  1020. pi = find_procinfo_or_die (pi->pid, 0);
  1021. /* The pointer is just a type alias. */
  1022. get_last_target_status (&wait_target, &wait_ptid, &wait_status);
  1023. if (wait_target == &the_procfs_target
  1024. && wait_ptid == inferior_ptid
  1025. && wait_status.kind () == TARGET_WAITKIND_STOPPED
  1026. && wait_status.sig () == gdb_signal_from_host (signo)
  1027. && proc_get_status (pi)
  1028. && pi->prstatus.pr_lwp.pr_info.si_signo == signo
  1029. )
  1030. /* Use the siginfo associated with the signal being
  1031. redelivered. */
  1032. memcpy (arg.sinfo, &pi->prstatus.pr_lwp.pr_info, sizeof (siginfo_t));
  1033. else
  1034. {
  1035. mysinfo.si_signo = signo;
  1036. mysinfo.si_code = 0;
  1037. mysinfo.si_pid = getpid (); /* ?why? */
  1038. mysinfo.si_uid = getuid (); /* ?why? */
  1039. memcpy (arg.sinfo, &mysinfo, sizeof (siginfo_t));
  1040. }
  1041. arg.cmd = PCSSIG;
  1042. win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
  1043. return win;
  1044. }
  1045. /* The current signal (if any) is cleared, and is not sent to the
  1046. process or LWP when it resumes. Returns non-zero for success, zero
  1047. for failure. */
  1048. static int
  1049. proc_clear_current_signal (procinfo *pi)
  1050. {
  1051. int win;
  1052. /* We should never have to apply this operation to any procinfo
  1053. except the one for the main process. If that ever changes for
  1054. any reason, then take out the following clause and replace it
  1055. with one that makes sure the ctl_fd is open. */
  1056. if (pi->tid != 0)
  1057. pi = find_procinfo_or_die (pi->pid, 0);
  1058. struct {
  1059. procfs_ctl_t cmd;
  1060. /* Use char array to avoid alignment issues. */
  1061. char sinfo[sizeof (siginfo_t)];
  1062. } arg;
  1063. siginfo_t mysinfo;
  1064. arg.cmd = PCSSIG;
  1065. /* The pointer is just a type alias. */
  1066. mysinfo.si_signo = 0;
  1067. mysinfo.si_code = 0;
  1068. mysinfo.si_errno = 0;
  1069. mysinfo.si_pid = getpid (); /* ?why? */
  1070. mysinfo.si_uid = getuid (); /* ?why? */
  1071. memcpy (arg.sinfo, &mysinfo, sizeof (siginfo_t));
  1072. win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
  1073. return win;
  1074. }
  1075. /* Return the general-purpose registers for the process or LWP
  1076. corresponding to PI. Upon failure, return NULL. */
  1077. static gdb_gregset_t *
  1078. proc_get_gregs (procinfo *pi)
  1079. {
  1080. if (!pi->status_valid || !pi->gregs_valid)
  1081. if (!proc_get_status (pi))
  1082. return NULL;
  1083. return &pi->prstatus.pr_lwp.pr_reg;
  1084. }
  1085. /* Return the general-purpose registers for the process or LWP
  1086. corresponding to PI. Upon failure, return NULL. */
  1087. static gdb_fpregset_t *
  1088. proc_get_fpregs (procinfo *pi)
  1089. {
  1090. if (!pi->status_valid || !pi->fpregs_valid)
  1091. if (!proc_get_status (pi))
  1092. return NULL;
  1093. return &pi->prstatus.pr_lwp.pr_fpreg;
  1094. }
  1095. /* Write the general-purpose registers back to the process or LWP
  1096. corresponding to PI. Return non-zero for success, zero for
  1097. failure. */
  1098. static int
  1099. proc_set_gregs (procinfo *pi)
  1100. {
  1101. gdb_gregset_t *gregs;
  1102. int win;
  1103. gregs = proc_get_gregs (pi);
  1104. if (gregs == NULL)
  1105. return 0; /* proc_get_regs has already warned. */
  1106. if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
  1107. return 0;
  1108. else
  1109. {
  1110. struct {
  1111. procfs_ctl_t cmd;
  1112. /* Use char array to avoid alignment issues. */
  1113. char gregs[sizeof (gdb_gregset_t)];
  1114. } arg;
  1115. arg.cmd = PCSREG;
  1116. memcpy (&arg.gregs, gregs, sizeof (arg.gregs));
  1117. win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
  1118. }
  1119. /* Policy: writing the registers invalidates our cache. */
  1120. pi->gregs_valid = 0;
  1121. return win;
  1122. }
  1123. /* Write the floating-pointer registers back to the process or LWP
  1124. corresponding to PI. Return non-zero for success, zero for
  1125. failure. */
  1126. static int
  1127. proc_set_fpregs (procinfo *pi)
  1128. {
  1129. gdb_fpregset_t *fpregs;
  1130. int win;
  1131. fpregs = proc_get_fpregs (pi);
  1132. if (fpregs == NULL)
  1133. return 0; /* proc_get_fpregs has already warned. */
  1134. if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
  1135. return 0;
  1136. else
  1137. {
  1138. struct {
  1139. procfs_ctl_t cmd;
  1140. /* Use char array to avoid alignment issues. */
  1141. char fpregs[sizeof (gdb_fpregset_t)];
  1142. } arg;
  1143. arg.cmd = PCSFPREG;
  1144. memcpy (&arg.fpregs, fpregs, sizeof (arg.fpregs));
  1145. win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
  1146. }
  1147. /* Policy: writing the registers invalidates our cache. */
  1148. pi->fpregs_valid = 0;
  1149. return win;
  1150. }
  1151. /* Send a signal to the proc or lwp with the semantics of "kill()".
  1152. Returns non-zero for success, zero for failure. */
  1153. static int
  1154. proc_kill (procinfo *pi, int signo)
  1155. {
  1156. int win;
  1157. /* We might conceivably apply this operation to an LWP, and the
  1158. LWP's ctl file descriptor might not be open. */
  1159. if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
  1160. return 0;
  1161. else
  1162. {
  1163. procfs_ctl_t cmd[2];
  1164. cmd[0] = PCKILL;
  1165. cmd[1] = signo;
  1166. win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
  1167. }
  1168. return win;
  1169. }
  1170. /* Find the pid of the process that started this one. Returns the
  1171. parent process pid, or zero. */
  1172. static int
  1173. proc_parent_pid (procinfo *pi)
  1174. {
  1175. /* We should never have to apply this operation to any procinfo
  1176. except the one for the main process. If that ever changes for
  1177. any reason, then take out the following clause and replace it
  1178. with one that makes sure the ctl_fd is open. */
  1179. if (pi->tid != 0)
  1180. pi = find_procinfo_or_die (pi->pid, 0);
  1181. if (!pi->status_valid)
  1182. if (!proc_get_status (pi))
  1183. return 0;
  1184. return pi->prstatus.pr_ppid;
  1185. }
  1186. /* Convert a target address (a.k.a. CORE_ADDR) into a host address
  1187. (a.k.a void pointer)! */
  1188. static void *
  1189. procfs_address_to_host_pointer (CORE_ADDR addr)
  1190. {
  1191. struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  1192. void *ptr;
  1193. gdb_assert (sizeof (ptr) == TYPE_LENGTH (ptr_type));
  1194. gdbarch_address_to_pointer (target_gdbarch (), ptr_type,
  1195. (gdb_byte *) &ptr, addr);
  1196. return ptr;
  1197. }
  1198. static int
  1199. proc_set_watchpoint (procinfo *pi, CORE_ADDR addr, int len, int wflags)
  1200. {
  1201. struct {
  1202. procfs_ctl_t cmd;
  1203. char watch[sizeof (prwatch_t)];
  1204. } arg;
  1205. prwatch_t pwatch;
  1206. /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
  1207. convert a target address into something that can be stored in a
  1208. native data structure. */
  1209. pwatch.pr_vaddr = (uintptr_t) procfs_address_to_host_pointer (addr);
  1210. pwatch.pr_size = len;
  1211. pwatch.pr_wflags = wflags;
  1212. arg.cmd = PCWATCH;
  1213. memcpy (arg.watch, &pwatch, sizeof (prwatch_t));
  1214. return (write (pi->ctl_fd, &arg, sizeof (arg)) == sizeof (arg));
  1215. }
  1216. /* =============== END, non-thread part of /proc "MODULE" =============== */
  1217. /* =================== Thread "MODULE" =================== */
  1218. /* Returns the number of threads for the process. */
  1219. static int
  1220. proc_get_nthreads (procinfo *pi)
  1221. {
  1222. if (!pi->status_valid)
  1223. if (!proc_get_status (pi))
  1224. return 0;
  1225. /* Only works for the process procinfo, because the LWP procinfos do not
  1226. get prstatus filled in. */
  1227. if (pi->tid != 0) /* Find the parent process procinfo. */
  1228. pi = find_procinfo_or_die (pi->pid, 0);
  1229. return pi->prstatus.pr_nlwp;
  1230. }
  1231. /* Return the ID of the thread that had an event of interest.
  1232. (ie. the one that hit a breakpoint or other traced event). All
  1233. other things being equal, this should be the ID of a thread that is
  1234. currently executing. */
  1235. static int
  1236. proc_get_current_thread (procinfo *pi)
  1237. {
  1238. /* Note: this should be applied to the root procinfo for the
  1239. process, not to the procinfo for an LWP. If applied to the
  1240. procinfo for an LWP, it will simply return that LWP's ID. In
  1241. that case, find the parent process procinfo. */
  1242. if (pi->tid != 0)
  1243. pi = find_procinfo_or_die (pi->pid, 0);
  1244. if (!pi->status_valid)
  1245. if (!proc_get_status (pi))
  1246. return 0;
  1247. return pi->prstatus.pr_lwp.pr_lwpid;
  1248. }
  1249. /* Discover the IDs of all the threads within the process, and create
  1250. a procinfo for each of them (chained to the parent). Returns
  1251. non-zero for success, zero for failure. */
  1252. static int
  1253. proc_delete_dead_threads (procinfo *parent, procinfo *thread, void *ignore)
  1254. {
  1255. if (thread && parent) /* sanity */
  1256. {
  1257. thread->status_valid = 0;
  1258. if (!proc_get_status (thread))
  1259. destroy_one_procinfo (&parent->thread_list, thread);
  1260. }
  1261. return 0; /* keep iterating */
  1262. }
  1263. static int
  1264. proc_update_threads (procinfo *pi)
  1265. {
  1266. char pathname[MAX_PROC_NAME_SIZE + 16];
  1267. struct dirent *direntry;
  1268. procinfo *thread;
  1269. gdb_dir_up dirp;
  1270. int lwpid;
  1271. /* We should never have to apply this operation to any procinfo
  1272. except the one for the main process. If that ever changes for
  1273. any reason, then take out the following clause and replace it
  1274. with one that makes sure the ctl_fd is open. */
  1275. if (pi->tid != 0)
  1276. pi = find_procinfo_or_die (pi->pid, 0);
  1277. proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
  1278. /* Note: this brute-force method was originally devised for Unixware
  1279. (support removed since), and will also work on Solaris 2.6 and
  1280. 2.7. The original comment mentioned the existence of a much
  1281. simpler and more elegant way to do this on Solaris, but didn't
  1282. point out what that was. */
  1283. strcpy (pathname, pi->pathname);
  1284. strcat (pathname, "/lwp");
  1285. dirp.reset (opendir (pathname));
  1286. if (dirp == NULL)
  1287. proc_error (pi, "update_threads, opendir", __LINE__);
  1288. while ((direntry = readdir (dirp.get ())) != NULL)
  1289. if (direntry->d_name[0] != '.') /* skip '.' and '..' */
  1290. {
  1291. lwpid = atoi (&direntry->d_name[0]);
  1292. thread = create_procinfo (pi->pid, lwpid);
  1293. if (thread == NULL)
  1294. proc_error (pi, "update_threads, create_procinfo", __LINE__);
  1295. }
  1296. pi->threads_valid = 1;
  1297. return 1;
  1298. }
  1299. /* Given a pointer to a function, call that function once for each lwp
  1300. in the procinfo list, until the function returns non-zero, in which
  1301. event return the value returned by the function.
  1302. Note: this function does NOT call update_threads. If you want to
  1303. discover new threads first, you must call that function explicitly.
  1304. This function just makes a quick pass over the currently-known
  1305. procinfos.
  1306. PI is the parent process procinfo. FUNC is the per-thread
  1307. function. PTR is an opaque parameter for function. Returns the
  1308. first non-zero return value from the callee, or zero. */
  1309. static int
  1310. proc_iterate_over_threads (procinfo *pi,
  1311. int (*func) (procinfo *, procinfo *, void *),
  1312. void *ptr)
  1313. {
  1314. procinfo *thread, *next;
  1315. int retval = 0;
  1316. /* We should never have to apply this operation to any procinfo
  1317. except the one for the main process. If that ever changes for
  1318. any reason, then take out the following clause and replace it
  1319. with one that makes sure the ctl_fd is open. */
  1320. if (pi->tid != 0)
  1321. pi = find_procinfo_or_die (pi->pid, 0);
  1322. for (thread = pi->thread_list; thread != NULL; thread = next)
  1323. {
  1324. next = thread->next; /* In case thread is destroyed. */
  1325. retval = (*func) (pi, thread, ptr);
  1326. if (retval != 0)
  1327. break;
  1328. }
  1329. return retval;
  1330. }
  1331. /* =================== END, Thread "MODULE" =================== */
  1332. /* =================== END, /proc "MODULE" =================== */
  1333. /* =================== GDB "MODULE" =================== */
  1334. /* Here are all of the gdb target vector functions and their
  1335. friends. */
  1336. static void do_attach (ptid_t ptid);
  1337. static void do_detach ();
  1338. static void proc_trace_syscalls_1 (procinfo *pi, int syscallnum,
  1339. int entry_or_exit, int mode, int from_tty);
  1340. /* Sets up the inferior to be debugged. Registers to trace signals,
  1341. hardware faults, and syscalls. Note: does not set RLC flag: caller
  1342. may want to customize that. Returns zero for success (note!
  1343. unlike most functions in this module); on failure, returns the LINE
  1344. NUMBER where it failed! */
  1345. static int
  1346. procfs_debug_inferior (procinfo *pi)
  1347. {
  1348. fltset_t traced_faults;
  1349. sigset_t traced_signals;
  1350. sysset_t *traced_syscall_entries;
  1351. sysset_t *traced_syscall_exits;
  1352. int status;
  1353. /* Register to trace hardware faults in the child. */
  1354. prfillset (&traced_faults); /* trace all faults... */
  1355. prdelset (&traced_faults, FLTPAGE); /* except page fault. */
  1356. if (!proc_set_traced_faults (pi, &traced_faults))
  1357. return __LINE__;
  1358. /* Initially, register to trace all signals in the child. */
  1359. prfillset (&traced_signals);
  1360. if (!proc_set_traced_signals (pi, &traced_signals))
  1361. return __LINE__;
  1362. /* Register to trace the 'exit' system call (on entry). */
  1363. traced_syscall_entries = XNEW (sysset_t);
  1364. premptyset (traced_syscall_entries);
  1365. praddset (traced_syscall_entries, SYS_exit);
  1366. praddset (traced_syscall_entries, SYS_lwp_exit);
  1367. status = proc_set_traced_sysentry (pi, traced_syscall_entries);
  1368. xfree (traced_syscall_entries);
  1369. if (!status)
  1370. return __LINE__;
  1371. /* Method for tracing exec syscalls. */
  1372. traced_syscall_exits = XNEW (sysset_t);
  1373. premptyset (traced_syscall_exits);
  1374. praddset (traced_syscall_exits, SYS_execve);
  1375. praddset (traced_syscall_exits, SYS_lwp_create);
  1376. praddset (traced_syscall_exits, SYS_lwp_exit);
  1377. status = proc_set_traced_sysexit (pi, traced_syscall_exits);
  1378. xfree (traced_syscall_exits);
  1379. if (!status)
  1380. return __LINE__;
  1381. return 0;
  1382. }
  1383. void
  1384. procfs_target::attach (const char *args, int from_tty)
  1385. {
  1386. int pid;
  1387. pid = parse_pid_to_attach (args);
  1388. if (pid == getpid ())
  1389. error (_("Attaching GDB to itself is not a good idea..."));
  1390. /* Push the target if needed, ensure it gets un-pushed it if attach fails. */
  1391. inferior *inf = current_inferior ();
  1392. target_unpush_up unpusher;
  1393. if (!inf->target_is_pushed (this))
  1394. {
  1395. inf->push_target (this);
  1396. unpusher.reset (this);
  1397. }
  1398. target_announce_attach (from_tty, pid);
  1399. do_attach (ptid_t (pid));
  1400. /* Everything went fine, keep the target pushed. */
  1401. unpusher.release ();
  1402. }
  1403. void
  1404. procfs_target::detach (inferior *inf, int from_tty)
  1405. {
  1406. target_announce_detach (from_tty);
  1407. do_detach ();
  1408. switch_to_no_thread ();
  1409. detach_inferior (inf);
  1410. maybe_unpush_target ();
  1411. }
  1412. static void
  1413. do_attach (ptid_t ptid)
  1414. {
  1415. procinfo *pi;
  1416. struct inferior *inf;
  1417. int fail;
  1418. int lwpid;
  1419. pi = create_procinfo (ptid.pid (), 0);
  1420. if (pi == NULL)
  1421. perror (_("procfs: out of memory in 'attach'"));
  1422. if (!open_procinfo_files (pi, FD_CTL))
  1423. {
  1424. gdb_printf (gdb_stderr, "procfs:%d -- ", __LINE__);
  1425. xsnprintf (errmsg, sizeof (errmsg),
  1426. "do_attach: couldn't open /proc file for process %d",
  1427. ptid.pid ());
  1428. dead_procinfo (pi, errmsg, NOKILL);
  1429. }
  1430. /* Stop the process (if it isn't already stopped). */
  1431. if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
  1432. {
  1433. pi->was_stopped = 1;
  1434. proc_prettyprint_why (proc_why (pi), proc_what (pi), 1);
  1435. }
  1436. else
  1437. {
  1438. pi->was_stopped = 0;
  1439. /* Set the process to run again when we close it. */
  1440. if (!proc_set_run_on_last_close (pi))
  1441. dead_procinfo (pi, "do_attach: couldn't set RLC.", NOKILL);
  1442. /* Now stop the process. */
  1443. if (!proc_stop_process (pi))
  1444. dead_procinfo (pi, "do_attach: couldn't stop the process.", NOKILL);
  1445. pi->ignore_next_sigstop = 1;
  1446. }
  1447. /* Save some of the /proc state to be restored if we detach. */
  1448. if (!proc_get_traced_faults (pi, &pi->saved_fltset))
  1449. dead_procinfo (pi, "do_attach: couldn't save traced faults.", NOKILL);
  1450. if (!proc_get_traced_signals (pi, &pi->saved_sigset))
  1451. dead_procinfo (pi, "do_attach: couldn't save traced signals.", NOKILL);
  1452. if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
  1453. dead_procinfo (pi, "do_attach: couldn't save traced syscall entries.",
  1454. NOKILL);
  1455. if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
  1456. dead_procinfo (pi, "do_attach: couldn't save traced syscall exits.",
  1457. NOKILL);
  1458. if (!proc_get_held_signals (pi, &pi->saved_sighold))
  1459. dead_procinfo (pi, "do_attach: couldn't save held signals.", NOKILL);
  1460. fail = procfs_debug_inferior (pi);
  1461. if (fail != 0)
  1462. dead_procinfo (pi, "do_attach: failed in procfs_debug_inferior", NOKILL);
  1463. inf = current_inferior ();
  1464. inferior_appeared (inf, pi->pid);
  1465. /* Let GDB know that the inferior was attached. */
  1466. inf->attach_flag = 1;
  1467. /* Create a procinfo for the current lwp. */
  1468. lwpid = proc_get_current_thread (pi);
  1469. create_procinfo (pi->pid, lwpid);
  1470. /* Add it to gdb's thread list. */
  1471. ptid = ptid_t (pi->pid, lwpid, 0);
  1472. thread_info *thr = add_thread (&the_procfs_target, ptid);
  1473. switch_to_thread (thr);
  1474. }
  1475. static void
  1476. do_detach ()
  1477. {
  1478. procinfo *pi;
  1479. /* Find procinfo for the main process. */
  1480. pi = find_procinfo_or_die (inferior_ptid.pid (),
  1481. 0); /* FIXME: threads */
  1482. if (!proc_set_traced_signals (pi, &pi->saved_sigset))
  1483. proc_warn (pi, "do_detach, set_traced_signal", __LINE__);
  1484. if (!proc_set_traced_faults (pi, &pi->saved_fltset))
  1485. proc_warn (pi, "do_detach, set_traced_faults", __LINE__);
  1486. if (!proc_set_traced_sysentry (pi, pi->saved_entryset))
  1487. proc_warn (pi, "do_detach, set_traced_sysentry", __LINE__);
  1488. if (!proc_set_traced_sysexit (pi, pi->saved_exitset))
  1489. proc_warn (pi, "do_detach, set_traced_sysexit", __LINE__);
  1490. if (!proc_set_held_signals (pi, &pi->saved_sighold))
  1491. proc_warn (pi, "do_detach, set_held_signals", __LINE__);
  1492. if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
  1493. if (!(pi->was_stopped)
  1494. || query (_("Was stopped when attached, make it runnable again? ")))
  1495. {
  1496. /* Clear any pending signal. */
  1497. if (!proc_clear_current_fault (pi))
  1498. proc_warn (pi, "do_detach, clear_current_fault", __LINE__);
  1499. if (!proc_clear_current_signal (pi))
  1500. proc_warn (pi, "do_detach, clear_current_signal", __LINE__);
  1501. if (!proc_set_run_on_last_close (pi))
  1502. proc_warn (pi, "do_detach, set_rlc", __LINE__);
  1503. }
  1504. destroy_procinfo (pi);
  1505. }
  1506. /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
  1507. for all registers.
  1508. NOTE: Since the /proc interface cannot give us individual
  1509. registers, we pay no attention to REGNUM, and just fetch them all.
  1510. This results in the possibility that we will do unnecessarily many
  1511. fetches, since we may be called repeatedly for individual
  1512. registers. So we cache the results, and mark the cache invalid
  1513. when the process is resumed. */
  1514. void
  1515. procfs_target::fetch_registers (struct regcache *regcache, int regnum)
  1516. {
  1517. gdb_gregset_t *gregs;
  1518. procinfo *pi;
  1519. ptid_t ptid = regcache->ptid ();
  1520. int pid = ptid.pid ();
  1521. int tid = ptid.lwp ();
  1522. struct gdbarch *gdbarch = regcache->arch ();
  1523. pi = find_procinfo_or_die (pid, tid);
  1524. if (pi == NULL)
  1525. error (_("procfs: fetch_registers failed to find procinfo for %s"),
  1526. target_pid_to_str (ptid).c_str ());
  1527. gregs = proc_get_gregs (pi);
  1528. if (gregs == NULL)
  1529. proc_error (pi, "fetch_registers, get_gregs", __LINE__);
  1530. supply_gregset (regcache, (const gdb_gregset_t *) gregs);
  1531. if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */
  1532. {
  1533. gdb_fpregset_t *fpregs;
  1534. if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch))
  1535. || regnum == gdbarch_pc_regnum (gdbarch)
  1536. || regnum == gdbarch_sp_regnum (gdbarch))
  1537. return; /* Not a floating point register. */
  1538. fpregs = proc_get_fpregs (pi);
  1539. if (fpregs == NULL)
  1540. proc_error (pi, "fetch_registers, get_fpregs", __LINE__);
  1541. supply_fpregset (regcache, (const gdb_fpregset_t *) fpregs);
  1542. }
  1543. }
  1544. /* Store register REGNUM back into the inferior. If REGNUM is -1, do
  1545. this for all registers.
  1546. NOTE: Since the /proc interface will not read individual registers,
  1547. we will cache these requests until the process is resumed, and only
  1548. then write them back to the inferior process.
  1549. FIXME: is that a really bad idea? Have to think about cases where
  1550. writing one register might affect the value of others, etc. */
  1551. void
  1552. procfs_target::store_registers (struct regcache *regcache, int regnum)
  1553. {
  1554. gdb_gregset_t *gregs;
  1555. procinfo *pi;
  1556. ptid_t ptid = regcache->ptid ();
  1557. int pid = ptid.pid ();
  1558. int tid = ptid.lwp ();
  1559. struct gdbarch *gdbarch = regcache->arch ();
  1560. pi = find_procinfo_or_die (pid, tid);
  1561. if (pi == NULL)
  1562. error (_("procfs: store_registers: failed to find procinfo for %s"),
  1563. target_pid_to_str (ptid).c_str ());
  1564. gregs = proc_get_gregs (pi);
  1565. if (gregs == NULL)
  1566. proc_error (pi, "store_registers, get_gregs", __LINE__);
  1567. fill_gregset (regcache, gregs, regnum);
  1568. if (!proc_set_gregs (pi))
  1569. proc_error (pi, "store_registers, set_gregs", __LINE__);
  1570. if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */
  1571. {
  1572. gdb_fpregset_t *fpregs;
  1573. if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch))
  1574. || regnum == gdbarch_pc_regnum (gdbarch)
  1575. || regnum == gdbarch_sp_regnum (gdbarch))
  1576. return; /* Not a floating point register. */
  1577. fpregs = proc_get_fpregs (pi);
  1578. if (fpregs == NULL)
  1579. proc_error (pi, "store_registers, get_fpregs", __LINE__);
  1580. fill_fpregset (regcache, fpregs, regnum);
  1581. if (!proc_set_fpregs (pi))
  1582. proc_error (pi, "store_registers, set_fpregs", __LINE__);
  1583. }
  1584. }
  1585. /* Retrieve the next stop event from the child process. If child has
  1586. not stopped yet, wait for it to stop. Translate /proc eventcodes
  1587. (or possibly wait eventcodes) into gdb internal event codes.
  1588. Returns the id of process (and possibly thread) that incurred the
  1589. event. Event codes are returned through a pointer parameter. */
  1590. ptid_t
  1591. procfs_target::wait (ptid_t ptid, struct target_waitstatus *status,
  1592. target_wait_flags options)
  1593. {
  1594. /* First cut: loosely based on original version 2.1. */
  1595. procinfo *pi;
  1596. int wstat;
  1597. int temp_tid;
  1598. ptid_t retval, temp_ptid;
  1599. int why, what, flags;
  1600. int retry = 0;
  1601. wait_again:
  1602. retry++;
  1603. wstat = 0;
  1604. retval = ptid_t (-1);
  1605. /* Find procinfo for main process. */
  1606. /* procfs_target currently only supports one inferior. */
  1607. inferior *inf = current_inferior ();
  1608. pi = find_procinfo_or_die (inf->pid, 0);
  1609. if (pi)
  1610. {
  1611. /* We must assume that the status is stale now... */
  1612. pi->status_valid = 0;
  1613. pi->gregs_valid = 0;
  1614. pi->fpregs_valid = 0;
  1615. #if 0 /* just try this out... */
  1616. flags = proc_flags (pi);
  1617. why = proc_why (pi);
  1618. if ((flags & PR_STOPPED) && (why == PR_REQUESTED))
  1619. pi->status_valid = 0; /* re-read again, IMMEDIATELY... */
  1620. #endif
  1621. /* If child is not stopped, wait for it to stop. */
  1622. if (!(proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
  1623. && !proc_wait_for_stop (pi))
  1624. {
  1625. /* wait_for_stop failed: has the child terminated? */
  1626. if (errno == ENOENT)
  1627. {
  1628. int wait_retval;
  1629. /* /proc file not found; presumably child has terminated. */
  1630. wait_retval = ::wait (&wstat); /* "wait" for the child's exit. */
  1631. /* Wrong child? */
  1632. if (wait_retval != inf->pid)
  1633. error (_("procfs: couldn't stop "
  1634. "process %d: wait returned %d."),
  1635. inf->pid, wait_retval);
  1636. /* FIXME: might I not just use waitpid?
  1637. Or try find_procinfo to see if I know about this child? */
  1638. retval = ptid_t (wait_retval);
  1639. }
  1640. else if (errno == EINTR)
  1641. goto wait_again;
  1642. else
  1643. {
  1644. /* Unknown error from wait_for_stop. */
  1645. proc_error (pi, "target_wait (wait_for_stop)", __LINE__);
  1646. }
  1647. }
  1648. else
  1649. {
  1650. /* This long block is reached if either:
  1651. a) the child was already stopped, or
  1652. b) we successfully waited for the child with wait_for_stop.
  1653. This block will analyze the /proc status, and translate it
  1654. into a waitstatus for GDB.
  1655. If we actually had to call wait because the /proc file
  1656. is gone (child terminated), then we skip this block,
  1657. because we already have a waitstatus. */
  1658. flags = proc_flags (pi);
  1659. why = proc_why (pi);
  1660. what = proc_what (pi);
  1661. if (flags & (PR_STOPPED | PR_ISTOP))
  1662. {
  1663. /* If it's running async (for single_thread control),
  1664. set it back to normal again. */
  1665. if (flags & PR_ASYNC)
  1666. if (!proc_unset_async (pi))
  1667. proc_error (pi, "target_wait, unset_async", __LINE__);
  1668. if (info_verbose)
  1669. proc_prettyprint_why (why, what, 1);
  1670. /* The 'pid' we will return to GDB is composed of
  1671. the process ID plus the lwp ID. */
  1672. retval = ptid_t (pi->pid, proc_get_current_thread (pi), 0);
  1673. switch (why) {
  1674. case PR_SIGNALLED:
  1675. wstat = (what << 8) | 0177;
  1676. break;
  1677. case PR_SYSENTRY:
  1678. if (what == SYS_lwp_exit)
  1679. {
  1680. if (print_thread_events)
  1681. gdb_printf (_("[%s exited]\n"),
  1682. target_pid_to_str (retval).c_str ());
  1683. delete_thread (find_thread_ptid (this, retval));
  1684. target_continue_no_signal (ptid);
  1685. goto wait_again;
  1686. }
  1687. else if (what == SYS_exit)
  1688. {
  1689. /* Handle SYS_exit call only. */
  1690. /* Stopped at entry to SYS_exit.
  1691. Make it runnable, resume it, then use
  1692. the wait system call to get its exit code.
  1693. Proc_run_process always clears the current
  1694. fault and signal.
  1695. Then return its exit status. */
  1696. pi->status_valid = 0;
  1697. wstat = 0;
  1698. /* FIXME: what we should do is return
  1699. TARGET_WAITKIND_SPURIOUS. */
  1700. if (!proc_run_process (pi, 0, 0))
  1701. proc_error (pi, "target_wait, run_process", __LINE__);
  1702. if (inf->attach_flag)
  1703. {
  1704. /* Don't call wait: simulate waiting for exit,
  1705. return a "success" exit code. Bogus: what if
  1706. it returns something else? */
  1707. wstat = 0;
  1708. retval = ptid_t (inf->pid); /* ? ? ? */
  1709. }
  1710. else
  1711. {
  1712. int temp = ::wait (&wstat);
  1713. /* FIXME: shouldn't I make sure I get the right
  1714. event from the right process? If (for
  1715. instance) I have killed an earlier inferior
  1716. process but failed to clean up after it
  1717. somehow, I could get its termination event
  1718. here. */
  1719. /* If wait returns -1, that's what we return
  1720. to GDB. */
  1721. if (temp < 0)
  1722. retval = ptid_t (temp);
  1723. }
  1724. }
  1725. else
  1726. {
  1727. gdb_printf (_("procfs: trapped on entry to "));
  1728. proc_prettyprint_syscall (proc_what (pi), 0);
  1729. gdb_printf ("\n");
  1730. long i, nsysargs, *sysargs;
  1731. nsysargs = proc_nsysarg (pi);
  1732. sysargs = proc_sysargs (pi);
  1733. if (nsysargs > 0 && sysargs != NULL)
  1734. {
  1735. gdb_printf (_("%ld syscall arguments:\n"),
  1736. nsysargs);
  1737. for (i = 0; i < nsysargs; i++)
  1738. gdb_printf ("#%ld: 0x%08lx\n",
  1739. i, sysargs[i]);
  1740. }
  1741. /* How to keep going without returning to wfi: */
  1742. target_continue_no_signal (ptid);
  1743. goto wait_again;
  1744. }
  1745. break;
  1746. case PR_SYSEXIT:
  1747. if (what == SYS_execve)
  1748. {
  1749. /* Hopefully this is our own "fork-child" execing
  1750. the real child. Hoax this event into a trap, and
  1751. GDB will see the child about to execute its start
  1752. address. */
  1753. wstat = (SIGTRAP << 8) | 0177;
  1754. }
  1755. else if (what == SYS_lwp_create)
  1756. {
  1757. /* This syscall is somewhat like fork/exec. We
  1758. will get the event twice: once for the parent
  1759. LWP, and once for the child. We should already
  1760. know about the parent LWP, but the child will
  1761. be new to us. So, whenever we get this event,
  1762. if it represents a new thread, simply add the
  1763. thread to the list. */
  1764. /* If not in procinfo list, add it. */
  1765. temp_tid = proc_get_current_thread (pi);
  1766. if (!find_procinfo (pi->pid, temp_tid))
  1767. create_procinfo (pi->pid, temp_tid);
  1768. temp_ptid = ptid_t (pi->pid, temp_tid, 0);
  1769. /* If not in GDB's thread list, add it. */
  1770. if (!in_thread_list (this, temp_ptid))
  1771. add_thread (this, temp_ptid);
  1772. target_continue_no_signal (ptid);
  1773. goto wait_again;
  1774. }
  1775. else if (what == SYS_lwp_exit)
  1776. {
  1777. if (print_thread_events)
  1778. gdb_printf (_("[%s exited]\n"),
  1779. target_pid_to_str (retval).c_str ());
  1780. delete_thread (find_thread_ptid (this, retval));
  1781. status->set_spurious ();
  1782. return retval;
  1783. }
  1784. else
  1785. {
  1786. gdb_printf (_("procfs: trapped on exit from "));
  1787. proc_prettyprint_syscall (proc_what (pi), 0);
  1788. gdb_printf ("\n");
  1789. long i, nsysargs, *sysargs;
  1790. nsysargs = proc_nsysarg (pi);
  1791. sysargs = proc_sysargs (pi);
  1792. if (nsysargs > 0 && sysargs != NULL)
  1793. {
  1794. gdb_printf (_("%ld syscall arguments:\n"),
  1795. nsysargs);
  1796. for (i = 0; i < nsysargs; i++)
  1797. gdb_printf ("#%ld: 0x%08lx\n",
  1798. i, sysargs[i]);
  1799. }
  1800. target_continue_no_signal (ptid);
  1801. goto wait_again;
  1802. }
  1803. break;
  1804. case PR_REQUESTED:
  1805. #if 0 /* FIXME */
  1806. wstat = (SIGSTOP << 8) | 0177;
  1807. break;
  1808. #else
  1809. if (retry < 5)
  1810. {
  1811. gdb_printf (_("Retry #%d:\n"), retry);
  1812. pi->status_valid = 0;
  1813. goto wait_again;
  1814. }
  1815. else
  1816. {
  1817. /* If not in procinfo list, add it. */
  1818. temp_tid = proc_get_current_thread (pi);
  1819. if (!find_procinfo (pi->pid, temp_tid))
  1820. create_procinfo (pi->pid, temp_tid);
  1821. /* If not in GDB's thread list, add it. */
  1822. temp_ptid = ptid_t (pi->pid, temp_tid, 0);
  1823. if (!in_thread_list (this, temp_ptid))
  1824. add_thread (this, temp_ptid);
  1825. status->set_stopped (GDB_SIGNAL_0);
  1826. return retval;
  1827. }
  1828. #endif
  1829. case PR_JOBCONTROL:
  1830. wstat = (what << 8) | 0177;
  1831. break;
  1832. case PR_FAULTED:
  1833. {
  1834. int signo = pi->prstatus.pr_lwp.pr_info.si_signo;
  1835. if (signo != 0)
  1836. wstat = (signo << 8) | 0177;
  1837. }
  1838. break;
  1839. default: /* switch (why) unmatched */
  1840. gdb_printf ("procfs:%d -- ", __LINE__);
  1841. gdb_printf (_("child stopped for unknown reason:\n"));
  1842. proc_prettyprint_why (why, what, 1);
  1843. error (_("... giving up..."));
  1844. break;
  1845. }
  1846. /* Got this far without error: If retval isn't in the
  1847. threads database, add it. */
  1848. if (retval.pid () > 0
  1849. && !in_thread_list (this, retval))
  1850. {
  1851. /* We have a new thread. We need to add it both to
  1852. GDB's list and to our own. If we don't create a
  1853. procinfo, resume may be unhappy later. */
  1854. add_thread (this, retval);
  1855. if (find_procinfo (retval.pid (),
  1856. retval.lwp ()) == NULL)
  1857. create_procinfo (retval.pid (),
  1858. retval.lwp ());
  1859. }
  1860. }
  1861. else /* Flags do not indicate STOPPED. */
  1862. {
  1863. /* surely this can't happen... */
  1864. gdb_printf ("procfs:%d -- process not stopped.\n",
  1865. __LINE__);
  1866. proc_prettyprint_flags (flags, 1);
  1867. error (_("procfs: ...giving up..."));
  1868. }
  1869. }
  1870. if (status)
  1871. *status = host_status_to_waitstatus (wstat);
  1872. }
  1873. return retval;
  1874. }
  1875. /* Perform a partial transfer to/from the specified object. For
  1876. memory transfers, fall back to the old memory xfer functions. */
  1877. enum target_xfer_status
  1878. procfs_target::xfer_partial (enum target_object object,
  1879. const char *annex, gdb_byte *readbuf,
  1880. const gdb_byte *writebuf, ULONGEST offset,
  1881. ULONGEST len, ULONGEST *xfered_len)
  1882. {
  1883. switch (object)
  1884. {
  1885. case TARGET_OBJECT_MEMORY:
  1886. return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
  1887. case TARGET_OBJECT_AUXV:
  1888. return memory_xfer_auxv (this, object, annex, readbuf, writebuf,
  1889. offset, len, xfered_len);
  1890. default:
  1891. return this->beneath ()->xfer_partial (object, annex,
  1892. readbuf, writebuf, offset, len,
  1893. xfered_len);
  1894. }
  1895. }
  1896. /* Helper for procfs_xfer_partial that handles memory transfers.
  1897. Arguments are like target_xfer_partial. */
  1898. static enum target_xfer_status
  1899. procfs_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
  1900. ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
  1901. {
  1902. procinfo *pi;
  1903. int nbytes;
  1904. /* Find procinfo for main process. */
  1905. pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
  1906. if (pi->as_fd == 0 && open_procinfo_files (pi, FD_AS) == 0)
  1907. {
  1908. proc_warn (pi, "xfer_memory, open_proc_files", __LINE__);
  1909. return TARGET_XFER_E_IO;
  1910. }
  1911. if (lseek (pi->as_fd, (off_t) memaddr, SEEK_SET) != (off_t) memaddr)
  1912. return TARGET_XFER_E_IO;
  1913. if (writebuf != NULL)
  1914. {
  1915. PROCFS_NOTE ("write memory:\n");
  1916. nbytes = write (pi->as_fd, writebuf, len);
  1917. }
  1918. else
  1919. {
  1920. PROCFS_NOTE ("read memory:\n");
  1921. nbytes = read (pi->as_fd, readbuf, len);
  1922. }
  1923. if (nbytes <= 0)
  1924. return TARGET_XFER_E_IO;
  1925. *xfered_len = nbytes;
  1926. return TARGET_XFER_OK;
  1927. }
  1928. /* Called by target_resume before making child runnable. Mark cached
  1929. registers and status's invalid. If there are "dirty" caches that
  1930. need to be written back to the child process, do that.
  1931. File descriptors are also cached. As they are a limited resource,
  1932. we cannot hold onto them indefinitely. However, as they are
  1933. expensive to open, we don't want to throw them away
  1934. indiscriminately either. As a compromise, we will keep the file
  1935. descriptors for the parent process, but discard any file
  1936. descriptors we may have accumulated for the threads.
  1937. As this function is called by iterate_over_threads, it always
  1938. returns zero (so that iterate_over_threads will keep
  1939. iterating). */
  1940. static int
  1941. invalidate_cache (procinfo *parent, procinfo *pi, void *ptr)
  1942. {
  1943. /* About to run the child; invalidate caches and do any other
  1944. cleanup. */
  1945. if (parent != NULL)
  1946. {
  1947. /* The presence of a parent indicates that this is an LWP.
  1948. Close any file descriptors that it might have open.
  1949. We don't do this to the master (parent) procinfo. */
  1950. close_procinfo_files (pi);
  1951. }
  1952. pi->gregs_valid = 0;
  1953. pi->fpregs_valid = 0;
  1954. pi->status_valid = 0;
  1955. pi->threads_valid = 0;
  1956. return 0;
  1957. }
  1958. /* Make the child process runnable. Normally we will then call
  1959. procfs_wait and wait for it to stop again (unless gdb is async).
  1960. If STEP is true, then arrange for the child to stop again after
  1961. executing a single instruction. If SIGNO is zero, then cancel any
  1962. pending signal; if non-zero, then arrange for the indicated signal
  1963. to be delivered to the child when it runs. If PID is -1, then
  1964. allow any child thread to run; if non-zero, then allow only the
  1965. indicated thread to run. (not implemented yet). */
  1966. void
  1967. procfs_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
  1968. {
  1969. procinfo *pi, *thread;
  1970. int native_signo;
  1971. /* FIXME: Check/reword. */
  1972. /* prrun.prflags |= PRCFAULT; clear current fault.
  1973. PRCFAULT may be replaced by a PCCFAULT call (proc_clear_current_fault)
  1974. This basically leaves PRSTEP and PRCSIG.
  1975. PRCSIG is like PCSSIG (proc_clear_current_signal).
  1976. So basically PR_STEP is the sole argument that must be passed
  1977. to proc_run_process. */
  1978. /* Find procinfo for main process. */
  1979. pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
  1980. /* First cut: ignore pid argument. */
  1981. errno = 0;
  1982. /* Convert signal to host numbering. */
  1983. if (signo == 0 || (signo == GDB_SIGNAL_STOP && pi->ignore_next_sigstop))
  1984. native_signo = 0;
  1985. else
  1986. native_signo = gdb_signal_to_host (signo);
  1987. pi->ignore_next_sigstop = 0;
  1988. /* Running the process voids all cached registers and status. */
  1989. /* Void the threads' caches first. */
  1990. proc_iterate_over_threads (pi, invalidate_cache, NULL);
  1991. /* Void the process procinfo's caches. */
  1992. invalidate_cache (NULL, pi, NULL);
  1993. if (ptid.pid () != -1)
  1994. {
  1995. /* Resume a specific thread, presumably suppressing the
  1996. others. */
  1997. thread = find_procinfo (ptid.pid (), ptid.lwp ());
  1998. if (thread != NULL)
  1999. {
  2000. if (thread->tid != 0)
  2001. {
  2002. /* We're to resume a specific thread, and not the
  2003. others. Set the child process's PR_ASYNC flag. */
  2004. if (!proc_set_async (pi))
  2005. proc_error (pi, "target_resume, set_async", __LINE__);
  2006. pi = thread; /* Substitute the thread's procinfo
  2007. for run. */
  2008. }
  2009. }
  2010. }
  2011. if (!proc_run_process (pi, step, native_signo))
  2012. {
  2013. if (errno == EBUSY)
  2014. warning (_("resume: target already running. "
  2015. "Pretend to resume, and hope for the best!"));
  2016. else
  2017. proc_error (pi, "target_resume", __LINE__);
  2018. }
  2019. }
  2020. /* Set up to trace signals in the child process. */
  2021. void
  2022. procfs_target::pass_signals (gdb::array_view<const unsigned char> pass_signals)
  2023. {
  2024. sigset_t signals;
  2025. procinfo *pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
  2026. int signo;
  2027. prfillset (&signals);
  2028. for (signo = 0; signo < NSIG; signo++)
  2029. {
  2030. int target_signo = gdb_signal_from_host (signo);
  2031. if (target_signo < pass_signals.size () && pass_signals[target_signo])
  2032. prdelset (&signals, signo);
  2033. }
  2034. if (!proc_set_traced_signals (pi, &signals))
  2035. proc_error (pi, "pass_signals", __LINE__);
  2036. }
  2037. /* Print status information about the child process. */
  2038. void
  2039. procfs_target::files_info ()
  2040. {
  2041. struct inferior *inf = current_inferior ();
  2042. gdb_printf (_("\tUsing the running image of %s %s via /proc.\n"),
  2043. inf->attach_flag? "attached": "child",
  2044. target_pid_to_str (inferior_ptid).c_str ());
  2045. }
  2046. /* Make it die. Wait for it to die. Clean up after it. Note: this
  2047. should only be applied to the real process, not to an LWP, because
  2048. of the check for parent-process. If we need this to work for an
  2049. LWP, it needs some more logic. */
  2050. static void
  2051. unconditionally_kill_inferior (procinfo *pi)
  2052. {
  2053. int parent_pid;
  2054. parent_pid = proc_parent_pid (pi);
  2055. if (!proc_kill (pi, SIGKILL))
  2056. proc_error (pi, "unconditionally_kill, proc_kill", __LINE__);
  2057. destroy_procinfo (pi);
  2058. /* If pi is GDB's child, wait for it to die. */
  2059. if (parent_pid == getpid ())
  2060. /* FIXME: should we use waitpid to make sure we get the right event?
  2061. Should we check the returned event? */
  2062. {
  2063. #if 0
  2064. int status, ret;
  2065. ret = waitpid (pi->pid, &status, 0);
  2066. #else
  2067. wait (NULL);
  2068. #endif
  2069. }
  2070. }
  2071. /* We're done debugging it, and we want it to go away. Then we want
  2072. GDB to forget all about it. */
  2073. void
  2074. procfs_target::kill ()
  2075. {
  2076. if (inferior_ptid != null_ptid) /* ? */
  2077. {
  2078. /* Find procinfo for main process. */
  2079. procinfo *pi = find_procinfo (inferior_ptid.pid (), 0);
  2080. if (pi)
  2081. unconditionally_kill_inferior (pi);
  2082. target_mourn_inferior (inferior_ptid);
  2083. }
  2084. }
  2085. /* Forget we ever debugged this thing! */
  2086. void
  2087. procfs_target::mourn_inferior ()
  2088. {
  2089. procinfo *pi;
  2090. if (inferior_ptid != null_ptid)
  2091. {
  2092. /* Find procinfo for main process. */
  2093. pi = find_procinfo (inferior_ptid.pid (), 0);
  2094. if (pi)
  2095. destroy_procinfo (pi);
  2096. }
  2097. generic_mourn_inferior ();
  2098. maybe_unpush_target ();
  2099. }
  2100. /* When GDB forks to create a runnable inferior process, this function
  2101. is called on the parent side of the fork. It's job is to do
  2102. whatever is necessary to make the child ready to be debugged, and
  2103. then wait for the child to synchronize. */
  2104. void
  2105. procfs_target::procfs_init_inferior (int pid)
  2106. {
  2107. procinfo *pi;
  2108. int fail;
  2109. int lwpid;
  2110. pi = create_procinfo (pid, 0);
  2111. if (pi == NULL)
  2112. perror (_("procfs: out of memory in 'init_inferior'"));
  2113. if (!open_procinfo_files (pi, FD_CTL))
  2114. proc_error (pi, "init_inferior, open_proc_files", __LINE__);
  2115. /*
  2116. xmalloc // done
  2117. open_procinfo_files // done
  2118. link list // done
  2119. prfillset (trace)
  2120. procfs_notice_signals
  2121. prfillset (fault)
  2122. prdelset (FLTPAGE)
  2123. */
  2124. /* If not stopped yet, wait for it to stop. */
  2125. if (!(proc_flags (pi) & PR_STOPPED) && !(proc_wait_for_stop (pi)))
  2126. dead_procinfo (pi, "init_inferior: wait_for_stop failed", KILL);
  2127. /* Save some of the /proc state to be restored if we detach. */
  2128. /* FIXME: Why? In case another debugger was debugging it?
  2129. We're it's parent, for Ghu's sake! */
  2130. if (!proc_get_traced_signals (pi, &pi->saved_sigset))
  2131. proc_error (pi, "init_inferior, get_traced_signals", __LINE__);
  2132. if (!proc_get_held_signals (pi, &pi->saved_sighold))
  2133. proc_error (pi, "init_inferior, get_held_signals", __LINE__);
  2134. if (!proc_get_traced_faults (pi, &pi->saved_fltset))
  2135. proc_error (pi, "init_inferior, get_traced_faults", __LINE__);
  2136. if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
  2137. proc_error (pi, "init_inferior, get_traced_sysentry", __LINE__);
  2138. if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
  2139. proc_error (pi, "init_inferior, get_traced_sysexit", __LINE__);
  2140. fail = procfs_debug_inferior (pi);
  2141. if (fail != 0)
  2142. proc_error (pi, "init_inferior (procfs_debug_inferior)", fail);
  2143. /* FIXME: logically, we should really be turning OFF run-on-last-close,
  2144. and possibly even turning ON kill-on-last-close at this point. But
  2145. I can't make that change without careful testing which I don't have
  2146. time to do right now... */
  2147. /* Turn on run-on-last-close flag so that the child
  2148. will die if GDB goes away for some reason. */
  2149. if (!proc_set_run_on_last_close (pi))
  2150. proc_error (pi, "init_inferior, set_RLC", __LINE__);
  2151. /* We now have have access to the lwpid of the main thread/lwp. */
  2152. lwpid = proc_get_current_thread (pi);
  2153. /* Create a procinfo for the main lwp. */
  2154. create_procinfo (pid, lwpid);
  2155. /* We already have a main thread registered in the thread table at
  2156. this point, but it didn't have any lwp info yet. Notify the core
  2157. about it. This changes inferior_ptid as well. */
  2158. thread_change_ptid (this, ptid_t (pid), ptid_t (pid, lwpid, 0));
  2159. gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
  2160. }
  2161. /* When GDB forks to create a new process, this function is called on
  2162. the child side of the fork before GDB exec's the user program. Its
  2163. job is to make the child minimally debuggable, so that the parent
  2164. GDB process can connect to the child and take over. This function
  2165. should do only the minimum to make that possible, and to
  2166. synchronize with the parent process. The parent process should
  2167. take care of the details. */
  2168. static void
  2169. procfs_set_exec_trap (void)
  2170. {
  2171. /* This routine called on the child side (inferior side)
  2172. after GDB forks the inferior. It must use only local variables,
  2173. because it may be sharing data space with its parent. */
  2174. procinfo *pi;
  2175. sysset_t *exitset;
  2176. pi = create_procinfo (getpid (), 0);
  2177. if (pi == NULL)
  2178. perror_with_name (_("procfs: create_procinfo failed in child."));
  2179. if (open_procinfo_files (pi, FD_CTL) == 0)
  2180. {
  2181. proc_warn (pi, "set_exec_trap, open_proc_files", __LINE__);
  2182. gdb_flush (gdb_stderr);
  2183. /* No need to call "dead_procinfo", because we're going to
  2184. exit. */
  2185. _exit (127);
  2186. }
  2187. exitset = XNEW (sysset_t);
  2188. premptyset (exitset);
  2189. praddset (exitset, SYS_execve);
  2190. if (!proc_set_traced_sysexit (pi, exitset))
  2191. {
  2192. proc_warn (pi, "set_exec_trap, set_traced_sysexit", __LINE__);
  2193. gdb_flush (gdb_stderr);
  2194. _exit (127);
  2195. }
  2196. /* FIXME: should this be done in the parent instead? */
  2197. /* Turn off inherit on fork flag so that all grand-children
  2198. of gdb start with tracing flags cleared. */
  2199. if (!proc_unset_inherit_on_fork (pi))
  2200. proc_warn (pi, "set_exec_trap, unset_inherit", __LINE__);
  2201. /* Turn off run on last close flag, so that the child process
  2202. cannot run away just because we close our handle on it.
  2203. We want it to wait for the parent to attach. */
  2204. if (!proc_unset_run_on_last_close (pi))
  2205. proc_warn (pi, "set_exec_trap, unset_RLC", __LINE__);
  2206. /* FIXME: No need to destroy the procinfo --
  2207. we have our own address space, and we're about to do an exec! */
  2208. /*destroy_procinfo (pi);*/
  2209. }
  2210. /* Dummy function to be sure fork_inferior uses fork(2) and not vfork(2).
  2211. This avoids a possible deadlock gdb and its vfork'ed child. */
  2212. static void
  2213. procfs_pre_trace (void)
  2214. {
  2215. }
  2216. /* This function is called BEFORE gdb forks the inferior process. Its
  2217. only real responsibility is to set things up for the fork, and tell
  2218. GDB which two functions to call after the fork (one for the parent,
  2219. and one for the child).
  2220. This function does a complicated search for a unix shell program,
  2221. which it then uses to parse arguments and environment variables to
  2222. be sent to the child. I wonder whether this code could not be
  2223. abstracted out and shared with other unix targets such as
  2224. inf-ptrace? */
  2225. void
  2226. procfs_target::create_inferior (const char *exec_file,
  2227. const std::string &allargs,
  2228. char **env, int from_tty)
  2229. {
  2230. const char *shell_file = get_shell ();
  2231. char *tryname;
  2232. int pid;
  2233. if (strchr (shell_file, '/') == NULL)
  2234. {
  2235. /* We will be looking down the PATH to find shell_file. If we
  2236. just do this the normal way (via execlp, which operates by
  2237. attempting an exec for each element of the PATH until it
  2238. finds one which succeeds), then there will be an exec for
  2239. each failed attempt, each of which will cause a PR_SYSEXIT
  2240. stop, and we won't know how to distinguish the PR_SYSEXIT's
  2241. for these failed execs with the ones for successful execs
  2242. (whether the exec has succeeded is stored at that time in the
  2243. carry bit or some such architecture-specific and
  2244. non-ABI-specified place).
  2245. So I can't think of anything better than to search the PATH
  2246. now. This has several disadvantages: (1) There is a race
  2247. condition; if we find a file now and it is deleted before we
  2248. exec it, we lose, even if the deletion leaves a valid file
  2249. further down in the PATH, (2) there is no way to know exactly
  2250. what an executable (in the sense of "capable of being
  2251. exec'd") file is. Using access() loses because it may lose
  2252. if the caller is the superuser; failing to use it loses if
  2253. there are ACLs or some such. */
  2254. const char *p;
  2255. const char *p1;
  2256. /* FIXME-maybe: might want "set path" command so user can change what
  2257. path is used from within GDB. */
  2258. const char *path = getenv ("PATH");
  2259. int len;
  2260. struct stat statbuf;
  2261. if (path == NULL)
  2262. path = "/bin:/usr/bin";
  2263. tryname = (char *) alloca (strlen (path) + strlen (shell_file) + 2);
  2264. for (p = path; p != NULL; p = p1 ? p1 + 1: NULL)
  2265. {
  2266. p1 = strchr (p, ':');
  2267. if (p1 != NULL)
  2268. len = p1 - p;
  2269. else
  2270. len = strlen (p);
  2271. strncpy (tryname, p, len);
  2272. tryname[len] = '\0';
  2273. strcat (tryname, "/");
  2274. strcat (tryname, shell_file);
  2275. if (access (tryname, X_OK) < 0)
  2276. continue;
  2277. if (stat (tryname, &statbuf) < 0)
  2278. continue;
  2279. if (!S_ISREG (statbuf.st_mode))
  2280. /* We certainly need to reject directories. I'm not quite
  2281. as sure about FIFOs, sockets, etc., but I kind of doubt
  2282. that people want to exec() these things. */
  2283. continue;
  2284. break;
  2285. }
  2286. if (p == NULL)
  2287. /* Not found. This must be an error rather than merely passing
  2288. the file to execlp(), because execlp() would try all the
  2289. exec()s, causing GDB to get confused. */
  2290. error (_("procfs:%d -- Can't find shell %s in PATH"),
  2291. __LINE__, shell_file);
  2292. shell_file = tryname;
  2293. }
  2294. inferior *inf = current_inferior ();
  2295. if (!inf->target_is_pushed (this))
  2296. inf->push_target (this);
  2297. pid = fork_inferior (exec_file, allargs, env, procfs_set_exec_trap,
  2298. NULL, procfs_pre_trace, shell_file, NULL);
  2299. /* We have something that executes now. We'll be running through
  2300. the shell at this point (if startup-with-shell is true), but the
  2301. pid shouldn't change. */
  2302. thread_info *thr = add_thread_silent (this, ptid_t (pid));
  2303. switch_to_thread (thr);
  2304. procfs_init_inferior (pid);
  2305. }
  2306. /* Callback for update_thread_list. Calls "add_thread". */
  2307. static int
  2308. procfs_notice_thread (procinfo *pi, procinfo *thread, void *ptr)
  2309. {
  2310. ptid_t gdb_threadid = ptid_t (pi->pid, thread->tid, 0);
  2311. thread_info *thr = find_thread_ptid (&the_procfs_target, gdb_threadid);
  2312. if (thr == NULL || thr->state == THREAD_EXITED)
  2313. add_thread (&the_procfs_target, gdb_threadid);
  2314. return 0;
  2315. }
  2316. /* Query all the threads that the target knows about, and give them
  2317. back to GDB to add to its list. */
  2318. void
  2319. procfs_target::update_thread_list ()
  2320. {
  2321. procinfo *pi;
  2322. prune_threads ();
  2323. /* Find procinfo for main process. */
  2324. pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
  2325. proc_update_threads (pi);
  2326. proc_iterate_over_threads (pi, procfs_notice_thread, NULL);
  2327. }
  2328. /* Return true if the thread is still 'alive'. This guy doesn't
  2329. really seem to be doing his job. Got to investigate how to tell
  2330. when a thread is really gone. */
  2331. bool
  2332. procfs_target::thread_alive (ptid_t ptid)
  2333. {
  2334. int proc, thread;
  2335. procinfo *pi;
  2336. proc = ptid.pid ();
  2337. thread = ptid.lwp ();
  2338. /* If I don't know it, it ain't alive! */
  2339. pi = find_procinfo (proc, thread);
  2340. if (pi == NULL)
  2341. return false;
  2342. /* If I can't get its status, it ain't alive!
  2343. What's more, I need to forget about it! */
  2344. if (!proc_get_status (pi))
  2345. {
  2346. destroy_procinfo (pi);
  2347. return false;
  2348. }
  2349. /* I couldn't have got its status if it weren't alive, so it's
  2350. alive. */
  2351. return true;
  2352. }
  2353. /* Convert PTID to a string. */
  2354. std::string
  2355. procfs_target::pid_to_str (ptid_t ptid)
  2356. {
  2357. if (ptid.lwp () == 0)
  2358. return string_printf ("process %d", ptid.pid ());
  2359. else
  2360. return string_printf ("LWP %ld", ptid.lwp ());
  2361. }
  2362. /* Accepts an integer PID; Returns a string representing a file that
  2363. can be opened to get the symbols for the child process. */
  2364. char *
  2365. procfs_target::pid_to_exec_file (int pid)
  2366. {
  2367. static char buf[PATH_MAX];
  2368. char name[PATH_MAX];
  2369. /* Solaris 11 introduced /proc/<proc-id>/execname. */
  2370. xsnprintf (name, sizeof (name), "/proc/%d/execname", pid);
  2371. scoped_fd fd (gdb_open_cloexec (name, O_RDONLY, 0));
  2372. if (fd.get () < 0 || read (fd.get (), buf, PATH_MAX - 1) < 0)
  2373. {
  2374. /* If that fails, fall back to /proc/<proc-id>/path/a.out introduced in
  2375. Solaris 10. */
  2376. ssize_t len;
  2377. xsnprintf (name, sizeof (name), "/proc/%d/path/a.out", pid);
  2378. len = readlink (name, buf, PATH_MAX - 1);
  2379. if (len <= 0)
  2380. strcpy (buf, name);
  2381. else
  2382. buf[len] = '\0';
  2383. }
  2384. return buf;
  2385. }
  2386. /* Insert a watchpoint. */
  2387. static int
  2388. procfs_set_watchpoint (ptid_t ptid, CORE_ADDR addr, int len, int rwflag,
  2389. int after)
  2390. {
  2391. int pflags = 0;
  2392. procinfo *pi;
  2393. pi = find_procinfo_or_die (ptid.pid () == -1 ?
  2394. inferior_ptid.pid () : ptid.pid (),
  2395. 0);
  2396. /* Translate from GDB's flags to /proc's. */
  2397. if (len > 0) /* len == 0 means delete watchpoint. */
  2398. {
  2399. switch (rwflag) { /* FIXME: need an enum! */
  2400. case hw_write: /* default watchpoint (write) */
  2401. pflags = WA_WRITE;
  2402. break;
  2403. case hw_read: /* read watchpoint */
  2404. pflags = WA_READ;
  2405. break;
  2406. case hw_access: /* access watchpoint */
  2407. pflags = WA_READ | WA_WRITE;
  2408. break;
  2409. case hw_execute: /* execution HW breakpoint */
  2410. pflags = WA_EXEC;
  2411. break;
  2412. default: /* Something weird. Return error. */
  2413. return -1;
  2414. }
  2415. if (after) /* Stop after r/w access is completed. */
  2416. pflags |= WA_TRAPAFTER;
  2417. }
  2418. if (!proc_set_watchpoint (pi, addr, len, pflags))
  2419. {
  2420. if (errno == E2BIG) /* Typical error for no resources. */
  2421. return -1; /* fail */
  2422. /* GDB may try to remove the same watchpoint twice.
  2423. If a remove request returns no match, don't error. */
  2424. if (errno == ESRCH && len == 0)
  2425. return 0; /* ignore */
  2426. proc_error (pi, "set_watchpoint", __LINE__);
  2427. }
  2428. return 0;
  2429. }
  2430. /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
  2431. is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
  2432. or bp_hardware_watchpoint. CNT is the number of watchpoints used so
  2433. far. */
  2434. int
  2435. procfs_target::can_use_hw_breakpoint (enum bptype type, int cnt, int othertype)
  2436. {
  2437. /* Due to the way that proc_set_watchpoint() is implemented, host
  2438. and target pointers must be of the same size. If they are not,
  2439. we can't use hardware watchpoints. This limitation is due to the
  2440. fact that proc_set_watchpoint() calls
  2441. procfs_address_to_host_pointer(); a close inspection of
  2442. procfs_address_to_host_pointer will reveal that an internal error
  2443. will be generated when the host and target pointer sizes are
  2444. different. */
  2445. struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  2446. if (sizeof (void *) != TYPE_LENGTH (ptr_type))
  2447. return 0;
  2448. /* Other tests here??? */
  2449. return 1;
  2450. }
  2451. /* Returns non-zero if process is stopped on a hardware watchpoint
  2452. fault, else returns zero. */
  2453. bool
  2454. procfs_target::stopped_by_watchpoint ()
  2455. {
  2456. procinfo *pi;
  2457. pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
  2458. if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
  2459. if (proc_why (pi) == PR_FAULTED)
  2460. if (proc_what (pi) == FLTWATCH)
  2461. return true;
  2462. return false;
  2463. }
  2464. /* Returns 1 if the OS knows the position of the triggered watchpoint,
  2465. and sets *ADDR to that address. Returns 0 if OS cannot report that
  2466. address. This function is only called if
  2467. procfs_stopped_by_watchpoint returned 1, thus no further checks are
  2468. done. The function also assumes that ADDR is not NULL. */
  2469. bool
  2470. procfs_target::stopped_data_address (CORE_ADDR *addr)
  2471. {
  2472. procinfo *pi;
  2473. pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
  2474. return proc_watchpoint_address (pi, addr);
  2475. }
  2476. int
  2477. procfs_target::insert_watchpoint (CORE_ADDR addr, int len,
  2478. enum target_hw_bp_type type,
  2479. struct expression *cond)
  2480. {
  2481. if (!target_have_steppable_watchpoint ()
  2482. && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch ()))
  2483. /* When a hardware watchpoint fires off the PC will be left at
  2484. the instruction following the one which caused the
  2485. watchpoint. It will *NOT* be necessary for GDB to step over
  2486. the watchpoint. */
  2487. return procfs_set_watchpoint (inferior_ptid, addr, len, type, 1);
  2488. else
  2489. /* When a hardware watchpoint fires off the PC will be left at
  2490. the instruction which caused the watchpoint. It will be
  2491. necessary for GDB to step over the watchpoint. */
  2492. return procfs_set_watchpoint (inferior_ptid, addr, len, type, 0);
  2493. }
  2494. int
  2495. procfs_target::remove_watchpoint (CORE_ADDR addr, int len,
  2496. enum target_hw_bp_type type,
  2497. struct expression *cond)
  2498. {
  2499. return procfs_set_watchpoint (inferior_ptid, addr, 0, 0, 0);
  2500. }
  2501. int
  2502. procfs_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
  2503. {
  2504. /* The man page for proc(4) on Solaris 2.6 and up says that the
  2505. system can support "thousands" of hardware watchpoints, but gives
  2506. no method for finding out how many; It doesn't say anything about
  2507. the allowed size for the watched area either. So we just tell
  2508. GDB 'yes'. */
  2509. return 1;
  2510. }
  2511. /* Memory Mappings Functions: */
  2512. /* Call a callback function once for each mapping, passing it the
  2513. mapping, an optional secondary callback function, and some optional
  2514. opaque data. Quit and return the first non-zero value returned
  2515. from the callback.
  2516. PI is the procinfo struct for the process to be mapped. FUNC is
  2517. the callback function to be called by this iterator. DATA is the
  2518. optional opaque data to be passed to the callback function.
  2519. CHILD_FUNC is the optional secondary function pointer to be passed
  2520. to the child function. Returns the first non-zero return value
  2521. from the callback function, or zero. */
  2522. static int
  2523. iterate_over_mappings (procinfo *pi, find_memory_region_ftype child_func,
  2524. void *data,
  2525. int (*func) (struct prmap *map,
  2526. find_memory_region_ftype child_func,
  2527. void *data))
  2528. {
  2529. char pathname[MAX_PROC_NAME_SIZE];
  2530. struct prmap *prmaps;
  2531. struct prmap *prmap;
  2532. int funcstat;
  2533. int nmap;
  2534. struct stat sbuf;
  2535. /* Get the number of mappings, allocate space,
  2536. and read the mappings into prmaps. */
  2537. /* Open map fd. */
  2538. xsnprintf (pathname, sizeof (pathname), "/proc/%d/map", pi->pid);
  2539. scoped_fd map_fd (open (pathname, O_RDONLY));
  2540. if (map_fd.get () < 0)
  2541. proc_error (pi, "iterate_over_mappings (open)", __LINE__);
  2542. /* Use stat to determine the file size, and compute
  2543. the number of prmap_t objects it contains. */
  2544. if (fstat (map_fd.get (), &sbuf) != 0)
  2545. proc_error (pi, "iterate_over_mappings (fstat)", __LINE__);
  2546. nmap = sbuf.st_size / sizeof (prmap_t);
  2547. prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
  2548. if (read (map_fd.get (), (char *) prmaps, nmap * sizeof (*prmaps))
  2549. != (nmap * sizeof (*prmaps)))
  2550. proc_error (pi, "iterate_over_mappings (read)", __LINE__);
  2551. for (prmap = prmaps; nmap > 0; prmap++, nmap--)
  2552. {
  2553. funcstat = (*func) (prmap, child_func, data);
  2554. if (funcstat != 0)
  2555. return funcstat;
  2556. }
  2557. return 0;
  2558. }
  2559. /* Implements the to_find_memory_regions method. Calls an external
  2560. function for each memory region.
  2561. Returns the integer value returned by the callback. */
  2562. static int
  2563. find_memory_regions_callback (struct prmap *map,
  2564. find_memory_region_ftype func, void *data)
  2565. {
  2566. return (*func) ((CORE_ADDR) map->pr_vaddr,
  2567. map->pr_size,
  2568. (map->pr_mflags & MA_READ) != 0,
  2569. (map->pr_mflags & MA_WRITE) != 0,
  2570. (map->pr_mflags & MA_EXEC) != 0,
  2571. 1, /* MODIFIED is unknown, pass it as true. */
  2572. data);
  2573. }
  2574. /* External interface. Calls a callback function once for each
  2575. mapped memory region in the child process, passing as arguments:
  2576. CORE_ADDR virtual_address,
  2577. unsigned long size,
  2578. int read, TRUE if region is readable by the child
  2579. int write, TRUE if region is writable by the child
  2580. int execute TRUE if region is executable by the child.
  2581. Stops iterating and returns the first non-zero value returned by
  2582. the callback. */
  2583. int
  2584. procfs_target::find_memory_regions (find_memory_region_ftype func, void *data)
  2585. {
  2586. procinfo *pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
  2587. return iterate_over_mappings (pi, func, data,
  2588. find_memory_regions_callback);
  2589. }
  2590. /* Returns an ascii representation of a memory mapping's flags. */
  2591. static char *
  2592. mappingflags (long flags)
  2593. {
  2594. static char asciiflags[8];
  2595. strcpy (asciiflags, "-------");
  2596. if (flags & MA_STACK)
  2597. asciiflags[1] = 's';
  2598. if (flags & MA_BREAK)
  2599. asciiflags[2] = 'b';
  2600. if (flags & MA_SHARED)
  2601. asciiflags[3] = 's';
  2602. if (flags & MA_READ)
  2603. asciiflags[4] = 'r';
  2604. if (flags & MA_WRITE)
  2605. asciiflags[5] = 'w';
  2606. if (flags & MA_EXEC)
  2607. asciiflags[6] = 'x';
  2608. return (asciiflags);
  2609. }
  2610. /* Callback function, does the actual work for 'info proc
  2611. mappings'. */
  2612. static int
  2613. info_mappings_callback (struct prmap *map, find_memory_region_ftype ignore,
  2614. void *unused)
  2615. {
  2616. unsigned int pr_off;
  2617. pr_off = (unsigned int) map->pr_offset;
  2618. if (gdbarch_addr_bit (target_gdbarch ()) == 32)
  2619. gdb_printf ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
  2620. (unsigned long) map->pr_vaddr,
  2621. (unsigned long) map->pr_vaddr + map->pr_size - 1,
  2622. (unsigned long) map->pr_size,
  2623. pr_off,
  2624. mappingflags (map->pr_mflags));
  2625. else
  2626. gdb_printf (" %#18lx %#18lx %#10lx %#10x %7s\n",
  2627. (unsigned long) map->pr_vaddr,
  2628. (unsigned long) map->pr_vaddr + map->pr_size - 1,
  2629. (unsigned long) map->pr_size,
  2630. pr_off,
  2631. mappingflags (map->pr_mflags));
  2632. return 0;
  2633. }
  2634. /* Implement the "info proc mappings" subcommand. */
  2635. static void
  2636. info_proc_mappings (procinfo *pi, int summary)
  2637. {
  2638. if (summary)
  2639. return; /* No output for summary mode. */
  2640. gdb_printf (_("Mapped address spaces:\n\n"));
  2641. if (gdbarch_ptr_bit (target_gdbarch ()) == 32)
  2642. gdb_printf ("\t%10s %10s %10s %10s %7s\n",
  2643. "Start Addr",
  2644. " End Addr",
  2645. " Size",
  2646. " Offset",
  2647. "Flags");
  2648. else
  2649. gdb_printf (" %18s %18s %10s %10s %7s\n",
  2650. "Start Addr",
  2651. " End Addr",
  2652. " Size",
  2653. " Offset",
  2654. "Flags");
  2655. iterate_over_mappings (pi, NULL, NULL, info_mappings_callback);
  2656. gdb_printf ("\n");
  2657. }
  2658. /* Implement the "info proc" command. */
  2659. bool
  2660. procfs_target::info_proc (const char *args, enum info_proc_what what)
  2661. {
  2662. procinfo *process = NULL;
  2663. procinfo *thread = NULL;
  2664. char *tmp = NULL;
  2665. int pid = 0;
  2666. int tid = 0;
  2667. int mappings = 0;
  2668. switch (what)
  2669. {
  2670. case IP_MINIMAL:
  2671. break;
  2672. case IP_MAPPINGS:
  2673. case IP_ALL:
  2674. mappings = 1;
  2675. break;
  2676. default:
  2677. error (_("Not supported on this target."));
  2678. }
  2679. gdb_argv built_argv (args);
  2680. for (char *arg : built_argv)
  2681. {
  2682. if (isdigit (arg[0]))
  2683. {
  2684. pid = strtoul (arg, &tmp, 10);
  2685. if (*tmp == '/')
  2686. tid = strtoul (++tmp, NULL, 10);
  2687. }
  2688. else if (arg[0] == '/')
  2689. {
  2690. tid = strtoul (arg + 1, NULL, 10);
  2691. }
  2692. }
  2693. procinfo_up temporary_procinfo;
  2694. if (pid == 0)
  2695. pid = inferior_ptid.pid ();
  2696. if (pid == 0)
  2697. error (_("No current process: you must name one."));
  2698. else
  2699. {
  2700. /* Have pid, will travel.
  2701. First see if it's a process we're already debugging. */
  2702. process = find_procinfo (pid, 0);
  2703. if (process == NULL)
  2704. {
  2705. /* No. So open a procinfo for it, but
  2706. remember to close it again when finished. */
  2707. process = create_procinfo (pid, 0);
  2708. temporary_procinfo.reset (process);
  2709. if (!open_procinfo_files (process, FD_CTL))
  2710. proc_error (process, "info proc, open_procinfo_files", __LINE__);
  2711. }
  2712. }
  2713. if (tid != 0)
  2714. thread = create_procinfo (pid, tid);
  2715. if (process)
  2716. {
  2717. gdb_printf (_("process %d flags:\n"), process->pid);
  2718. proc_prettyprint_flags (proc_flags (process), 1);
  2719. if (proc_flags (process) & (PR_STOPPED | PR_ISTOP))
  2720. proc_prettyprint_why (proc_why (process), proc_what (process), 1);
  2721. if (proc_get_nthreads (process) > 1)
  2722. gdb_printf ("Process has %d threads.\n",
  2723. proc_get_nthreads (process));
  2724. }
  2725. if (thread)
  2726. {
  2727. gdb_printf (_("thread %d flags:\n"), thread->tid);
  2728. proc_prettyprint_flags (proc_flags (thread), 1);
  2729. if (proc_flags (thread) & (PR_STOPPED | PR_ISTOP))
  2730. proc_prettyprint_why (proc_why (thread), proc_what (thread), 1);
  2731. }
  2732. if (mappings)
  2733. info_proc_mappings (process, 0);
  2734. return true;
  2735. }
  2736. /* Modify the status of the system call identified by SYSCALLNUM in
  2737. the set of syscalls that are currently traced/debugged.
  2738. If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
  2739. will be updated. Otherwise, the exit syscalls set will be updated.
  2740. If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
  2741. will be disabled. */
  2742. static void
  2743. proc_trace_syscalls_1 (procinfo *pi, int syscallnum, int entry_or_exit,
  2744. int mode, int from_tty)
  2745. {
  2746. sysset_t *sysset;
  2747. if (entry_or_exit == PR_SYSENTRY)
  2748. sysset = proc_get_traced_sysentry (pi, NULL);
  2749. else
  2750. sysset = proc_get_traced_sysexit (pi, NULL);
  2751. if (sysset == NULL)
  2752. proc_error (pi, "proc-trace, get_traced_sysset", __LINE__);
  2753. if (mode == FLAG_SET)
  2754. praddset (sysset, syscallnum);
  2755. else
  2756. prdelset (sysset, syscallnum);
  2757. if (entry_or_exit == PR_SYSENTRY)
  2758. {
  2759. if (!proc_set_traced_sysentry (pi, sysset))
  2760. proc_error (pi, "proc-trace, set_traced_sysentry", __LINE__);
  2761. }
  2762. else
  2763. {
  2764. if (!proc_set_traced_sysexit (pi, sysset))
  2765. proc_error (pi, "proc-trace, set_traced_sysexit", __LINE__);
  2766. }
  2767. }
  2768. static void
  2769. proc_trace_syscalls (const char *args, int from_tty, int entry_or_exit, int mode)
  2770. {
  2771. procinfo *pi;
  2772. if (inferior_ptid.pid () <= 0)
  2773. error (_("you must be debugging a process to use this command."));
  2774. if (args == NULL || args[0] == 0)
  2775. error_no_arg (_("system call to trace"));
  2776. pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
  2777. if (isdigit (args[0]))
  2778. {
  2779. const int syscallnum = atoi (args);
  2780. proc_trace_syscalls_1 (pi, syscallnum, entry_or_exit, mode, from_tty);
  2781. }
  2782. }
  2783. static void
  2784. proc_trace_sysentry_cmd (const char *args, int from_tty)
  2785. {
  2786. proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_SET);
  2787. }
  2788. static void
  2789. proc_trace_sysexit_cmd (const char *args, int from_tty)
  2790. {
  2791. proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_SET);
  2792. }
  2793. static void
  2794. proc_untrace_sysentry_cmd (const char *args, int from_tty)
  2795. {
  2796. proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_RESET);
  2797. }
  2798. static void
  2799. proc_untrace_sysexit_cmd (const char *args, int from_tty)
  2800. {
  2801. proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_RESET);
  2802. }
  2803. void _initialize_procfs ();
  2804. void
  2805. _initialize_procfs ()
  2806. {
  2807. add_com ("proc-trace-entry", no_class, proc_trace_sysentry_cmd,
  2808. _("Give a trace of entries into the syscall."));
  2809. add_com ("proc-trace-exit", no_class, proc_trace_sysexit_cmd,
  2810. _("Give a trace of exits from the syscall."));
  2811. add_com ("proc-untrace-entry", no_class, proc_untrace_sysentry_cmd,
  2812. _("Cancel a trace of entries into the syscall."));
  2813. add_com ("proc-untrace-exit", no_class, proc_untrace_sysexit_cmd,
  2814. _("Cancel a trace of exits from the syscall."));
  2815. add_inf_child_target (&the_procfs_target);
  2816. }
  2817. /* =================== END, GDB "MODULE" =================== */
  2818. /* miscellaneous stubs: */
  2819. /* The following satisfy a few random symbols mostly created by the
  2820. solaris threads implementation, which I will chase down later. */
  2821. /* Return a pid for which we guarantee we will be able to find a
  2822. 'live' procinfo. */
  2823. ptid_t
  2824. procfs_first_available (void)
  2825. {
  2826. return ptid_t (procinfo_list ? procinfo_list->pid : -1);
  2827. }
  2828. /* =================== GCORE .NOTE "MODULE" =================== */
  2829. static void
  2830. procfs_do_thread_registers (bfd *obfd, ptid_t ptid,
  2831. gdb::unique_xmalloc_ptr<char> &note_data,
  2832. int *note_size, enum gdb_signal stop_signal)
  2833. {
  2834. struct regcache *regcache = get_thread_regcache (&the_procfs_target, ptid);
  2835. gdb_gregset_t gregs;
  2836. gdb_fpregset_t fpregs;
  2837. unsigned long merged_pid;
  2838. merged_pid = ptid.lwp () << 16 | ptid.pid ();
  2839. /* This part is the old method for fetching registers.
  2840. It should be replaced by the newer one using regsets
  2841. once it is implemented in this platform:
  2842. gdbarch_iterate_over_regset_sections(). */
  2843. target_fetch_registers (regcache, -1);
  2844. fill_gregset (regcache, &gregs, -1);
  2845. note_data.reset (elfcore_write_lwpstatus (obfd,
  2846. note_data.release (),
  2847. note_size,
  2848. merged_pid,
  2849. stop_signal,
  2850. &gregs));
  2851. fill_fpregset (regcache, &fpregs, -1);
  2852. note_data.reset (elfcore_write_prfpreg (obfd,
  2853. note_data.release (),
  2854. note_size,
  2855. &fpregs,
  2856. sizeof (fpregs)));
  2857. }
  2858. struct procfs_corefile_thread_data
  2859. {
  2860. procfs_corefile_thread_data (bfd *obfd,
  2861. gdb::unique_xmalloc_ptr<char> &note_data,
  2862. int *note_size, gdb_signal stop_signal)
  2863. : obfd (obfd), note_data (note_data), note_size (note_size),
  2864. stop_signal (stop_signal)
  2865. {}
  2866. bfd *obfd;
  2867. gdb::unique_xmalloc_ptr<char> &note_data;
  2868. int *note_size;
  2869. enum gdb_signal stop_signal;
  2870. };
  2871. static int
  2872. procfs_corefile_thread_callback (procinfo *pi, procinfo *thread, void *data)
  2873. {
  2874. struct procfs_corefile_thread_data *args
  2875. = (struct procfs_corefile_thread_data *) data;
  2876. if (pi != NULL)
  2877. {
  2878. ptid_t ptid = ptid_t (pi->pid, thread->tid, 0);
  2879. procfs_do_thread_registers (args->obfd, ptid,
  2880. args->note_data,
  2881. args->note_size,
  2882. args->stop_signal);
  2883. }
  2884. return 0;
  2885. }
  2886. static int
  2887. find_signalled_thread (struct thread_info *info, void *data)
  2888. {
  2889. if (info->stop_signal () != GDB_SIGNAL_0
  2890. && info->ptid.pid () == inferior_ptid.pid ())
  2891. return 1;
  2892. return 0;
  2893. }
  2894. static enum gdb_signal
  2895. find_stop_signal (void)
  2896. {
  2897. struct thread_info *info =
  2898. iterate_over_threads (find_signalled_thread, NULL);
  2899. if (info)
  2900. return info->stop_signal ();
  2901. else
  2902. return GDB_SIGNAL_0;
  2903. }
  2904. gdb::unique_xmalloc_ptr<char>
  2905. procfs_target::make_corefile_notes (bfd *obfd, int *note_size)
  2906. {
  2907. gdb_gregset_t gregs;
  2908. char fname[16] = {'\0'};
  2909. char psargs[80] = {'\0'};
  2910. procinfo *pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
  2911. gdb::unique_xmalloc_ptr<char> note_data;
  2912. enum gdb_signal stop_signal;
  2913. if (get_exec_file (0))
  2914. {
  2915. strncpy (fname, lbasename (get_exec_file (0)), sizeof (fname));
  2916. fname[sizeof (fname) - 1] = 0;
  2917. strncpy (psargs, get_exec_file (0), sizeof (psargs));
  2918. psargs[sizeof (psargs) - 1] = 0;
  2919. const std::string &inf_args = current_inferior ()->args ();
  2920. if (!inf_args.empty () &&
  2921. inf_args.length () < ((int) sizeof (psargs) - (int) strlen (psargs)))
  2922. {
  2923. strncat (psargs, " ",
  2924. sizeof (psargs) - strlen (psargs));
  2925. strncat (psargs, inf_args.c_str (),
  2926. sizeof (psargs) - strlen (psargs));
  2927. }
  2928. }
  2929. note_data.reset (elfcore_write_prpsinfo (obfd,
  2930. note_data.release (),
  2931. note_size,
  2932. fname,
  2933. psargs));
  2934. stop_signal = find_stop_signal ();
  2935. fill_gregset (get_current_regcache (), &gregs, -1);
  2936. note_data.reset (elfcore_write_pstatus (obfd, note_data.release (), note_size,
  2937. inferior_ptid.pid (),
  2938. stop_signal, &gregs));
  2939. procfs_corefile_thread_data thread_args (obfd, note_data, note_size,
  2940. stop_signal);
  2941. proc_iterate_over_threads (pi, procfs_corefile_thread_callback,
  2942. &thread_args);
  2943. gdb::optional<gdb::byte_vector> auxv =
  2944. target_read_alloc (current_inferior ()->top_target (),
  2945. TARGET_OBJECT_AUXV, NULL);
  2946. if (auxv && !auxv->empty ())
  2947. note_data.reset (elfcore_write_note (obfd, note_data.release (), note_size,
  2948. "CORE", NT_AUXV, auxv->data (),
  2949. auxv->size ()));
  2950. return note_data;
  2951. }
  2952. /* =================== END GCORE .NOTE "MODULE" =================== */