linux-tdep.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817
  1. /* Target-dependent code for GNU/Linux, architecture independent.
  2. Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "gdbtypes.h"
  16. #include "linux-tdep.h"
  17. #include "auxv.h"
  18. #include "target.h"
  19. #include "gdbthread.h"
  20. #include "gdbcore.h"
  21. #include "regcache.h"
  22. #include "regset.h"
  23. #include "elf/common.h"
  24. #include "elf-bfd.h" /* for elfcore_write_* */
  25. #include "inferior.h"
  26. #include "cli/cli-utils.h"
  27. #include "arch-utils.h"
  28. #include "gdbsupport/gdb_obstack.h"
  29. #include "observable.h"
  30. #include "objfiles.h"
  31. #include "infcall.h"
  32. #include "gdbcmd.h"
  33. #include "gdbsupport/gdb_regex.h"
  34. #include "gdbsupport/enum-flags.h"
  35. #include "gdbsupport/gdb_optional.h"
  36. #include "gcore.h"
  37. #include "gcore-elf.h"
  38. #include "solib-svr4.h"
  39. #include <ctype.h>
  40. #include <unordered_map>
  41. /* This enum represents the values that the user can choose when
  42. informing the Linux kernel about which memory mappings will be
  43. dumped in a corefile. They are described in the file
  44. Documentation/filesystems/proc.txt, inside the Linux kernel
  45. tree. */
  46. enum filter_flag
  47. {
  48. COREFILTER_ANON_PRIVATE = 1 << 0,
  49. COREFILTER_ANON_SHARED = 1 << 1,
  50. COREFILTER_MAPPED_PRIVATE = 1 << 2,
  51. COREFILTER_MAPPED_SHARED = 1 << 3,
  52. COREFILTER_ELF_HEADERS = 1 << 4,
  53. COREFILTER_HUGETLB_PRIVATE = 1 << 5,
  54. COREFILTER_HUGETLB_SHARED = 1 << 6,
  55. };
  56. DEF_ENUM_FLAGS_TYPE (enum filter_flag, filter_flags);
  57. /* This struct is used to map flags found in the "VmFlags:" field (in
  58. the /proc/<PID>/smaps file). */
  59. struct smaps_vmflags
  60. {
  61. /* Zero if this structure has not been initialized yet. It
  62. probably means that the Linux kernel being used does not emit
  63. the "VmFlags:" field on "/proc/PID/smaps". */
  64. unsigned int initialized_p : 1;
  65. /* Memory mapped I/O area (VM_IO, "io"). */
  66. unsigned int io_page : 1;
  67. /* Area uses huge TLB pages (VM_HUGETLB, "ht"). */
  68. unsigned int uses_huge_tlb : 1;
  69. /* Do not include this memory region on the coredump (VM_DONTDUMP, "dd"). */
  70. unsigned int exclude_coredump : 1;
  71. /* Is this a MAP_SHARED mapping (VM_SHARED, "sh"). */
  72. unsigned int shared_mapping : 1;
  73. /* Memory map has memory tagging enabled. */
  74. unsigned int memory_tagging : 1;
  75. };
  76. /* Data structure that holds the information contained in the
  77. /proc/<pid>/smaps file. */
  78. struct smaps_data
  79. {
  80. ULONGEST start_address;
  81. ULONGEST end_address;
  82. std::string filename;
  83. struct smaps_vmflags vmflags;
  84. bool read;
  85. bool write;
  86. bool exec;
  87. bool priv;
  88. bool has_anonymous;
  89. bool mapping_anon_p;
  90. bool mapping_file_p;
  91. ULONGEST inode;
  92. ULONGEST offset;
  93. };
  94. /* Whether to take the /proc/PID/coredump_filter into account when
  95. generating a corefile. */
  96. static bool use_coredump_filter = true;
  97. /* Whether the value of smaps_vmflags->exclude_coredump should be
  98. ignored, including mappings marked with the VM_DONTDUMP flag in
  99. the dump. */
  100. static bool dump_excluded_mappings = false;
  101. /* This enum represents the signals' numbers on a generic architecture
  102. running the Linux kernel. The definition of "generic" comes from
  103. the file <include/uapi/asm-generic/signal.h>, from the Linux kernel
  104. tree, which is the "de facto" implementation of signal numbers to
  105. be used by new architecture ports.
  106. For those architectures which have differences between the generic
  107. standard (e.g., Alpha), we define the different signals (and *only*
  108. those) in the specific target-dependent file (e.g.,
  109. alpha-linux-tdep.c, for Alpha). Please refer to the architecture's
  110. tdep file for more information.
  111. ARM deserves a special mention here. On the file
  112. <arch/arm/include/uapi/asm/signal.h>, it defines only one different
  113. (and ARM-only) signal, which is SIGSWI, with the same number as
  114. SIGRTMIN. This signal is used only for a very specific target,
  115. called ArthurOS (from RISCOS). Therefore, we do not handle it on
  116. the ARM-tdep file, and we can safely use the generic signal handler
  117. here for ARM targets.
  118. As stated above, this enum is derived from
  119. <include/uapi/asm-generic/signal.h>, from the Linux kernel
  120. tree. */
  121. enum
  122. {
  123. LINUX_SIGHUP = 1,
  124. LINUX_SIGINT = 2,
  125. LINUX_SIGQUIT = 3,
  126. LINUX_SIGILL = 4,
  127. LINUX_SIGTRAP = 5,
  128. LINUX_SIGABRT = 6,
  129. LINUX_SIGIOT = 6,
  130. LINUX_SIGBUS = 7,
  131. LINUX_SIGFPE = 8,
  132. LINUX_SIGKILL = 9,
  133. LINUX_SIGUSR1 = 10,
  134. LINUX_SIGSEGV = 11,
  135. LINUX_SIGUSR2 = 12,
  136. LINUX_SIGPIPE = 13,
  137. LINUX_SIGALRM = 14,
  138. LINUX_SIGTERM = 15,
  139. LINUX_SIGSTKFLT = 16,
  140. LINUX_SIGCHLD = 17,
  141. LINUX_SIGCONT = 18,
  142. LINUX_SIGSTOP = 19,
  143. LINUX_SIGTSTP = 20,
  144. LINUX_SIGTTIN = 21,
  145. LINUX_SIGTTOU = 22,
  146. LINUX_SIGURG = 23,
  147. LINUX_SIGXCPU = 24,
  148. LINUX_SIGXFSZ = 25,
  149. LINUX_SIGVTALRM = 26,
  150. LINUX_SIGPROF = 27,
  151. LINUX_SIGWINCH = 28,
  152. LINUX_SIGIO = 29,
  153. LINUX_SIGPOLL = LINUX_SIGIO,
  154. LINUX_SIGPWR = 30,
  155. LINUX_SIGSYS = 31,
  156. LINUX_SIGUNUSED = 31,
  157. LINUX_SIGRTMIN = 32,
  158. LINUX_SIGRTMAX = 64,
  159. };
  160. static struct gdbarch_data *linux_gdbarch_data_handle;
  161. struct linux_gdbarch_data
  162. {
  163. struct type *siginfo_type;
  164. int num_disp_step_buffers;
  165. };
  166. static void *
  167. init_linux_gdbarch_data (struct obstack *obstack)
  168. {
  169. return obstack_zalloc<linux_gdbarch_data> (obstack);
  170. }
  171. static struct linux_gdbarch_data *
  172. get_linux_gdbarch_data (struct gdbarch *gdbarch)
  173. {
  174. return ((struct linux_gdbarch_data *)
  175. gdbarch_data (gdbarch, linux_gdbarch_data_handle));
  176. }
  177. /* Linux-specific cached data. This is used by GDB for caching
  178. purposes for each inferior. This helps reduce the overhead of
  179. transfering data from a remote target to the local host. */
  180. struct linux_info
  181. {
  182. /* Cache of the inferior's vsyscall/vDSO mapping range. Only valid
  183. if VSYSCALL_RANGE_P is positive. This is cached because getting
  184. at this info requires an auxv lookup (which is itself cached),
  185. and looking through the inferior's mappings (which change
  186. throughout execution and therefore cannot be cached). */
  187. struct mem_range vsyscall_range {};
  188. /* Zero if we haven't tried looking up the vsyscall's range before
  189. yet. Positive if we tried looking it up, and found it. Negative
  190. if we tried looking it up but failed. */
  191. int vsyscall_range_p = 0;
  192. /* Inferior's displaced step buffers. */
  193. gdb::optional<displaced_step_buffers> disp_step_bufs;
  194. };
  195. /* Per-inferior data key. */
  196. static const struct inferior_key<linux_info> linux_inferior_data;
  197. /* Frees whatever allocated space there is to be freed and sets INF's
  198. linux cache data pointer to NULL. */
  199. static void
  200. invalidate_linux_cache_inf (struct inferior *inf)
  201. {
  202. linux_inferior_data.clear (inf);
  203. }
  204. /* Fetch the linux cache info for INF. This function always returns a
  205. valid INFO pointer. */
  206. static struct linux_info *
  207. get_linux_inferior_data (inferior *inf)
  208. {
  209. linux_info *info = linux_inferior_data.get (inf);
  210. if (info == nullptr)
  211. info = linux_inferior_data.emplace (inf);
  212. return info;
  213. }
  214. /* See linux-tdep.h. */
  215. struct type *
  216. linux_get_siginfo_type_with_fields (struct gdbarch *gdbarch,
  217. linux_siginfo_extra_fields extra_fields)
  218. {
  219. struct linux_gdbarch_data *linux_gdbarch_data;
  220. struct type *int_type, *uint_type, *long_type, *void_ptr_type, *short_type;
  221. struct type *uid_type, *pid_type;
  222. struct type *sigval_type, *clock_type;
  223. struct type *siginfo_type, *sifields_type;
  224. struct type *type;
  225. linux_gdbarch_data = get_linux_gdbarch_data (gdbarch);
  226. if (linux_gdbarch_data->siginfo_type != NULL)
  227. return linux_gdbarch_data->siginfo_type;
  228. int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
  229. 0, "int");
  230. uint_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
  231. 1, "unsigned int");
  232. long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
  233. 0, "long");
  234. short_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
  235. 0, "short");
  236. void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
  237. /* sival_t */
  238. sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
  239. sigval_type->set_name (xstrdup ("sigval_t"));
  240. append_composite_type_field (sigval_type, "sival_int", int_type);
  241. append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
  242. /* __pid_t */
  243. pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
  244. TYPE_LENGTH (int_type) * TARGET_CHAR_BIT, "__pid_t");
  245. TYPE_TARGET_TYPE (pid_type) = int_type;
  246. pid_type->set_target_is_stub (true);
  247. /* __uid_t */
  248. uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
  249. TYPE_LENGTH (uint_type) * TARGET_CHAR_BIT, "__uid_t");
  250. TYPE_TARGET_TYPE (uid_type) = uint_type;
  251. uid_type->set_target_is_stub (true);
  252. /* __clock_t */
  253. clock_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
  254. TYPE_LENGTH (long_type) * TARGET_CHAR_BIT,
  255. "__clock_t");
  256. TYPE_TARGET_TYPE (clock_type) = long_type;
  257. clock_type->set_target_is_stub (true);
  258. /* _sifields */
  259. sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
  260. {
  261. const int si_max_size = 128;
  262. int si_pad_size;
  263. int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;
  264. /* _pad */
  265. if (gdbarch_ptr_bit (gdbarch) == 64)
  266. si_pad_size = (si_max_size / size_of_int) - 4;
  267. else
  268. si_pad_size = (si_max_size / size_of_int) - 3;
  269. append_composite_type_field (sifields_type, "_pad",
  270. init_vector_type (int_type, si_pad_size));
  271. }
  272. /* _kill */
  273. type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  274. append_composite_type_field (type, "si_pid", pid_type);
  275. append_composite_type_field (type, "si_uid", uid_type);
  276. append_composite_type_field (sifields_type, "_kill", type);
  277. /* _timer */
  278. type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  279. append_composite_type_field (type, "si_tid", int_type);
  280. append_composite_type_field (type, "si_overrun", int_type);
  281. append_composite_type_field (type, "si_sigval", sigval_type);
  282. append_composite_type_field (sifields_type, "_timer", type);
  283. /* _rt */
  284. type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  285. append_composite_type_field (type, "si_pid", pid_type);
  286. append_composite_type_field (type, "si_uid", uid_type);
  287. append_composite_type_field (type, "si_sigval", sigval_type);
  288. append_composite_type_field (sifields_type, "_rt", type);
  289. /* _sigchld */
  290. type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  291. append_composite_type_field (type, "si_pid", pid_type);
  292. append_composite_type_field (type, "si_uid", uid_type);
  293. append_composite_type_field (type, "si_status", int_type);
  294. append_composite_type_field (type, "si_utime", clock_type);
  295. append_composite_type_field (type, "si_stime", clock_type);
  296. append_composite_type_field (sifields_type, "_sigchld", type);
  297. /* _sigfault */
  298. type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  299. append_composite_type_field (type, "si_addr", void_ptr_type);
  300. /* Additional bound fields for _sigfault in case they were requested. */
  301. if ((extra_fields & LINUX_SIGINFO_FIELD_ADDR_BND) != 0)
  302. {
  303. struct type *sigfault_bnd_fields;
  304. append_composite_type_field (type, "_addr_lsb", short_type);
  305. sigfault_bnd_fields = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  306. append_composite_type_field (sigfault_bnd_fields, "_lower", void_ptr_type);
  307. append_composite_type_field (sigfault_bnd_fields, "_upper", void_ptr_type);
  308. append_composite_type_field (type, "_addr_bnd", sigfault_bnd_fields);
  309. }
  310. append_composite_type_field (sifields_type, "_sigfault", type);
  311. /* _sigpoll */
  312. type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  313. append_composite_type_field (type, "si_band", long_type);
  314. append_composite_type_field (type, "si_fd", int_type);
  315. append_composite_type_field (sifields_type, "_sigpoll", type);
  316. /* _sigsys */
  317. type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  318. append_composite_type_field (type, "_call_addr", void_ptr_type);
  319. append_composite_type_field (type, "_syscall", int_type);
  320. append_composite_type_field (type, "_arch", uint_type);
  321. append_composite_type_field (sifields_type, "_sigsys", type);
  322. /* struct siginfo */
  323. siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  324. siginfo_type->set_name (xstrdup ("siginfo"));
  325. append_composite_type_field (siginfo_type, "si_signo", int_type);
  326. append_composite_type_field (siginfo_type, "si_errno", int_type);
  327. append_composite_type_field (siginfo_type, "si_code", int_type);
  328. append_composite_type_field_aligned (siginfo_type,
  329. "_sifields", sifields_type,
  330. TYPE_LENGTH (long_type));
  331. linux_gdbarch_data->siginfo_type = siginfo_type;
  332. return siginfo_type;
  333. }
  334. /* This function is suitable for architectures that don't
  335. extend/override the standard siginfo structure. */
  336. static struct type *
  337. linux_get_siginfo_type (struct gdbarch *gdbarch)
  338. {
  339. return linux_get_siginfo_type_with_fields (gdbarch, 0);
  340. }
  341. /* Return true if the target is running on uClinux instead of normal
  342. Linux kernel. */
  343. int
  344. linux_is_uclinux (void)
  345. {
  346. CORE_ADDR dummy;
  347. target_ops *target = current_inferior ()->top_target ();
  348. return (target_auxv_search (target, AT_NULL, &dummy) > 0
  349. && target_auxv_search (target, AT_PAGESZ, &dummy) == 0);
  350. }
  351. static int
  352. linux_has_shared_address_space (struct gdbarch *gdbarch)
  353. {
  354. return linux_is_uclinux ();
  355. }
  356. /* This is how we want PTIDs from core files to be printed. */
  357. static std::string
  358. linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
  359. {
  360. if (ptid.lwp () != 0)
  361. return string_printf ("LWP %ld", ptid.lwp ());
  362. return normal_pid_to_str (ptid);
  363. }
  364. /* Data from one mapping from /proc/PID/maps. */
  365. struct mapping
  366. {
  367. ULONGEST addr;
  368. ULONGEST endaddr;
  369. gdb::string_view permissions;
  370. ULONGEST offset;
  371. gdb::string_view device;
  372. ULONGEST inode;
  373. /* This field is guaranteed to be NULL-terminated, hence it is not a
  374. gdb::string_view. */
  375. const char *filename;
  376. };
  377. /* Service function for corefiles and info proc. */
  378. static mapping
  379. read_mapping (const char *line)
  380. {
  381. struct mapping mapping;
  382. const char *p = line;
  383. mapping.addr = strtoulst (p, &p, 16);
  384. if (*p == '-')
  385. p++;
  386. mapping.endaddr = strtoulst (p, &p, 16);
  387. p = skip_spaces (p);
  388. const char *permissions_start = p;
  389. while (*p && !isspace (*p))
  390. p++;
  391. mapping.permissions = {permissions_start, (size_t) (p - permissions_start)};
  392. mapping.offset = strtoulst (p, &p, 16);
  393. p = skip_spaces (p);
  394. const char *device_start = p;
  395. while (*p && !isspace (*p))
  396. p++;
  397. mapping.device = {device_start, (size_t) (p - device_start)};
  398. mapping.inode = strtoulst (p, &p, 10);
  399. p = skip_spaces (p);
  400. mapping.filename = p;
  401. return mapping;
  402. }
  403. /* Helper function to decode the "VmFlags" field in /proc/PID/smaps.
  404. This function was based on the documentation found on
  405. <Documentation/filesystems/proc.txt>, on the Linux kernel.
  406. Linux kernels before commit
  407. 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have this
  408. field on smaps. */
  409. static void
  410. decode_vmflags (char *p, struct smaps_vmflags *v)
  411. {
  412. char *saveptr = NULL;
  413. const char *s;
  414. v->initialized_p = 1;
  415. p = skip_to_space (p);
  416. p = skip_spaces (p);
  417. for (s = strtok_r (p, " ", &saveptr);
  418. s != NULL;
  419. s = strtok_r (NULL, " ", &saveptr))
  420. {
  421. if (strcmp (s, "io") == 0)
  422. v->io_page = 1;
  423. else if (strcmp (s, "ht") == 0)
  424. v->uses_huge_tlb = 1;
  425. else if (strcmp (s, "dd") == 0)
  426. v->exclude_coredump = 1;
  427. else if (strcmp (s, "sh") == 0)
  428. v->shared_mapping = 1;
  429. else if (strcmp (s, "mt") == 0)
  430. v->memory_tagging = 1;
  431. }
  432. }
  433. /* Regexes used by mapping_is_anonymous_p. Put in a structure because
  434. they're initialized lazily. */
  435. struct mapping_regexes
  436. {
  437. /* Matches "/dev/zero" filenames (with or without the "(deleted)"
  438. string in the end). We know for sure, based on the Linux kernel
  439. code, that memory mappings whose associated filename is
  440. "/dev/zero" are guaranteed to be MAP_ANONYMOUS. */
  441. compiled_regex dev_zero
  442. {"^/dev/zero\\( (deleted)\\)\\?$", REG_NOSUB,
  443. _("Could not compile regex to match /dev/zero filename")};
  444. /* Matches "/SYSV%08x" filenames (with or without the "(deleted)"
  445. string in the end). These filenames refer to shared memory
  446. (shmem), and memory mappings associated with them are
  447. MAP_ANONYMOUS as well. */
  448. compiled_regex shmem_file
  449. {"^/\\?SYSV[0-9a-fA-F]\\{8\\}\\( (deleted)\\)\\?$", REG_NOSUB,
  450. _("Could not compile regex to match shmem filenames")};
  451. /* A heuristic we use to try to mimic the Linux kernel's 'n_link ==
  452. 0' code, which is responsible to decide if it is dealing with a
  453. 'MAP_SHARED | MAP_ANONYMOUS' mapping. In other words, if
  454. FILE_DELETED matches, it does not necessarily mean that we are
  455. dealing with an anonymous shared mapping. However, there is no
  456. easy way to detect this currently, so this is the best
  457. approximation we have.
  458. As a result, GDB will dump readonly pages of deleted executables
  459. when using the default value of coredump_filter (0x33), while the
  460. Linux kernel will not dump those pages. But we can live with
  461. that. */
  462. compiled_regex file_deleted
  463. {" (deleted)$", REG_NOSUB,
  464. _("Could not compile regex to match '<file> (deleted)'")};
  465. };
  466. /* Return 1 if the memory mapping is anonymous, 0 otherwise.
  467. FILENAME is the name of the file present in the first line of the
  468. memory mapping, in the "/proc/PID/smaps" output. For example, if
  469. the first line is:
  470. 7fd0ca877000-7fd0d0da0000 r--p 00000000 fd:02 2100770 /path/to/file
  471. Then FILENAME will be "/path/to/file". */
  472. static int
  473. mapping_is_anonymous_p (const char *filename)
  474. {
  475. static gdb::optional<mapping_regexes> regexes;
  476. static int init_regex_p = 0;
  477. if (!init_regex_p)
  478. {
  479. /* Let's be pessimistic and assume there will be an error while
  480. compiling the regex'es. */
  481. init_regex_p = -1;
  482. regexes.emplace ();
  483. /* If we reached this point, then everything succeeded. */
  484. init_regex_p = 1;
  485. }
  486. if (init_regex_p == -1)
  487. {
  488. const char deleted[] = " (deleted)";
  489. size_t del_len = sizeof (deleted) - 1;
  490. size_t filename_len = strlen (filename);
  491. /* There was an error while compiling the regex'es above. In
  492. order to try to give some reliable information to the caller,
  493. we just try to find the string " (deleted)" in the filename.
  494. If we managed to find it, then we assume the mapping is
  495. anonymous. */
  496. return (filename_len >= del_len
  497. && strcmp (filename + filename_len - del_len, deleted) == 0);
  498. }
  499. if (*filename == '\0'
  500. || regexes->dev_zero.exec (filename, 0, NULL, 0) == 0
  501. || regexes->shmem_file.exec (filename, 0, NULL, 0) == 0
  502. || regexes->file_deleted.exec (filename, 0, NULL, 0) == 0)
  503. return 1;
  504. return 0;
  505. }
  506. /* Return 0 if the memory mapping (which is related to FILTERFLAGS, V,
  507. MAYBE_PRIVATE_P, MAPPING_ANONYMOUS_P, ADDR and OFFSET) should not
  508. be dumped, or greater than 0 if it should.
  509. In a nutshell, this is the logic that we follow in order to decide
  510. if a mapping should be dumped or not.
  511. - If the mapping is associated to a file whose name ends with
  512. " (deleted)", or if the file is "/dev/zero", or if it is
  513. "/SYSV%08x" (shared memory), or if there is no file associated
  514. with it, or if the AnonHugePages: or the Anonymous: fields in the
  515. /proc/PID/smaps have contents, then GDB considers this mapping to
  516. be anonymous. Otherwise, GDB considers this mapping to be a
  517. file-backed mapping (because there will be a file associated with
  518. it).
  519. It is worth mentioning that, from all those checks described
  520. above, the most fragile is the one to see if the file name ends
  521. with " (deleted)". This does not necessarily mean that the
  522. mapping is anonymous, because the deleted file associated with
  523. the mapping may have been a hard link to another file, for
  524. example. The Linux kernel checks to see if "i_nlink == 0", but
  525. GDB cannot easily (and normally) do this check (iff running as
  526. root, it could find the mapping in /proc/PID/map_files/ and
  527. determine whether there still are other hard links to the
  528. inode/file). Therefore, we made a compromise here, and we assume
  529. that if the file name ends with " (deleted)", then the mapping is
  530. indeed anonymous. FWIW, this is something the Linux kernel could
  531. do better: expose this information in a more direct way.
  532. - If we see the flag "sh" in the "VmFlags:" field (in
  533. /proc/PID/smaps), then certainly the memory mapping is shared
  534. (VM_SHARED). If we have access to the VmFlags, and we don't see
  535. the "sh" there, then certainly the mapping is private. However,
  536. Linux kernels before commit
  537. 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have the
  538. "VmFlags:" field; in that case, we use another heuristic: if we
  539. see 'p' in the permission flags, then we assume that the mapping
  540. is private, even though the presence of the 's' flag there would
  541. mean VM_MAYSHARE, which means the mapping could still be private.
  542. This should work OK enough, however.
  543. - Even if, at the end, we decided that we should not dump the
  544. mapping, we still have to check if it is something like an ELF
  545. header (of a DSO or an executable, for example). If it is, and
  546. if the user is interested in dump it, then we should dump it. */
  547. static int
  548. dump_mapping_p (filter_flags filterflags, const struct smaps_vmflags *v,
  549. int maybe_private_p, int mapping_anon_p, int mapping_file_p,
  550. const char *filename, ULONGEST addr, ULONGEST offset)
  551. {
  552. /* Initially, we trust in what we received from our caller. This
  553. value may not be very precise (i.e., it was probably gathered
  554. from the permission line in the /proc/PID/smaps list, which
  555. actually refers to VM_MAYSHARE, and not VM_SHARED), but it is
  556. what we have until we take a look at the "VmFlags:" field
  557. (assuming that the version of the Linux kernel being used
  558. supports it, of course). */
  559. int private_p = maybe_private_p;
  560. int dump_p;
  561. /* We always dump vDSO and vsyscall mappings, because it's likely that
  562. there'll be no file to read the contents from at core load time.
  563. The kernel does the same. */
  564. if (strcmp ("[vdso]", filename) == 0
  565. || strcmp ("[vsyscall]", filename) == 0)
  566. return 1;
  567. if (v->initialized_p)
  568. {
  569. /* We never dump I/O mappings. */
  570. if (v->io_page)
  571. return 0;
  572. /* Check if we should exclude this mapping. */
  573. if (!dump_excluded_mappings && v->exclude_coredump)
  574. return 0;
  575. /* Update our notion of whether this mapping is shared or
  576. private based on a trustworthy value. */
  577. private_p = !v->shared_mapping;
  578. /* HugeTLB checking. */
  579. if (v->uses_huge_tlb)
  580. {
  581. if ((private_p && (filterflags & COREFILTER_HUGETLB_PRIVATE))
  582. || (!private_p && (filterflags & COREFILTER_HUGETLB_SHARED)))
  583. return 1;
  584. return 0;
  585. }
  586. }
  587. if (private_p)
  588. {
  589. if (mapping_anon_p && mapping_file_p)
  590. {
  591. /* This is a special situation. It can happen when we see a
  592. mapping that is file-backed, but that contains anonymous
  593. pages. */
  594. dump_p = ((filterflags & COREFILTER_ANON_PRIVATE) != 0
  595. || (filterflags & COREFILTER_MAPPED_PRIVATE) != 0);
  596. }
  597. else if (mapping_anon_p)
  598. dump_p = (filterflags & COREFILTER_ANON_PRIVATE) != 0;
  599. else
  600. dump_p = (filterflags & COREFILTER_MAPPED_PRIVATE) != 0;
  601. }
  602. else
  603. {
  604. if (mapping_anon_p && mapping_file_p)
  605. {
  606. /* This is a special situation. It can happen when we see a
  607. mapping that is file-backed, but that contains anonymous
  608. pages. */
  609. dump_p = ((filterflags & COREFILTER_ANON_SHARED) != 0
  610. || (filterflags & COREFILTER_MAPPED_SHARED) != 0);
  611. }
  612. else if (mapping_anon_p)
  613. dump_p = (filterflags & COREFILTER_ANON_SHARED) != 0;
  614. else
  615. dump_p = (filterflags & COREFILTER_MAPPED_SHARED) != 0;
  616. }
  617. /* Even if we decided that we shouldn't dump this mapping, we still
  618. have to check whether (a) the user wants us to dump mappings
  619. containing an ELF header, and (b) the mapping in question
  620. contains an ELF header. If (a) and (b) are true, then we should
  621. dump this mapping.
  622. A mapping contains an ELF header if it is a private mapping, its
  623. offset is zero, and its first word is ELFMAG. */
  624. if (!dump_p && private_p && offset == 0
  625. && (filterflags & COREFILTER_ELF_HEADERS) != 0)
  626. {
  627. /* Useful define specifying the size of the ELF magical
  628. header. */
  629. #ifndef SELFMAG
  630. #define SELFMAG 4
  631. #endif
  632. /* Let's check if we have an ELF header. */
  633. gdb_byte h[SELFMAG];
  634. if (target_read_memory (addr, h, SELFMAG) == 0)
  635. {
  636. /* The EI_MAG* and ELFMAG* constants come from
  637. <elf/common.h>. */
  638. if (h[EI_MAG0] == ELFMAG0 && h[EI_MAG1] == ELFMAG1
  639. && h[EI_MAG2] == ELFMAG2 && h[EI_MAG3] == ELFMAG3)
  640. {
  641. /* This mapping contains an ELF header, so we
  642. should dump it. */
  643. dump_p = 1;
  644. }
  645. }
  646. }
  647. return dump_p;
  648. }
  649. /* As above, but return true only when we should dump the NT_FILE
  650. entry. */
  651. static int
  652. dump_note_entry_p (filter_flags filterflags, const struct smaps_vmflags *v,
  653. int maybe_private_p, int mapping_anon_p, int mapping_file_p,
  654. const char *filename, ULONGEST addr, ULONGEST offset)
  655. {
  656. /* vDSO and vsyscall mappings will end up in the core file. Don't
  657. put them in the NT_FILE note. */
  658. if (strcmp ("[vdso]", filename) == 0
  659. || strcmp ("[vsyscall]", filename) == 0)
  660. return 0;
  661. /* Otherwise, any other file-based mapping should be placed in the
  662. note. */
  663. return 1;
  664. }
  665. /* Implement the "info proc" command. */
  666. static void
  667. linux_info_proc (struct gdbarch *gdbarch, const char *args,
  668. enum info_proc_what what)
  669. {
  670. /* A long is used for pid instead of an int to avoid a loss of precision
  671. compiler warning from the output of strtoul. */
  672. long pid;
  673. int cmdline_f = (what == IP_MINIMAL || what == IP_CMDLINE || what == IP_ALL);
  674. int cwd_f = (what == IP_MINIMAL || what == IP_CWD || what == IP_ALL);
  675. int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
  676. int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
  677. int status_f = (what == IP_STATUS || what == IP_ALL);
  678. int stat_f = (what == IP_STAT || what == IP_ALL);
  679. char filename[100];
  680. int target_errno;
  681. if (args && isdigit (args[0]))
  682. {
  683. char *tem;
  684. pid = strtoul (args, &tem, 10);
  685. args = tem;
  686. }
  687. else
  688. {
  689. if (!target_has_execution ())
  690. error (_("No current process: you must name one."));
  691. if (current_inferior ()->fake_pid_p)
  692. error (_("Can't determine the current process's PID: you must name one."));
  693. pid = current_inferior ()->pid;
  694. }
  695. args = skip_spaces (args);
  696. if (args && args[0])
  697. error (_("Too many parameters: %s"), args);
  698. gdb_printf (_("process %ld\n"), pid);
  699. if (cmdline_f)
  700. {
  701. xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
  702. gdb_byte *buffer;
  703. ssize_t len = target_fileio_read_alloc (NULL, filename, &buffer);
  704. if (len > 0)
  705. {
  706. gdb::unique_xmalloc_ptr<char> cmdline ((char *) buffer);
  707. ssize_t pos;
  708. for (pos = 0; pos < len - 1; pos++)
  709. {
  710. if (buffer[pos] == '\0')
  711. buffer[pos] = ' ';
  712. }
  713. buffer[len - 1] = '\0';
  714. gdb_printf ("cmdline = '%s'\n", buffer);
  715. }
  716. else
  717. warning (_("unable to open /proc file '%s'"), filename);
  718. }
  719. if (cwd_f)
  720. {
  721. xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid);
  722. gdb::optional<std::string> contents
  723. = target_fileio_readlink (NULL, filename, &target_errno);
  724. if (contents.has_value ())
  725. gdb_printf ("cwd = '%s'\n", contents->c_str ());
  726. else
  727. warning (_("unable to read link '%s'"), filename);
  728. }
  729. if (exe_f)
  730. {
  731. xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid);
  732. gdb::optional<std::string> contents
  733. = target_fileio_readlink (NULL, filename, &target_errno);
  734. if (contents.has_value ())
  735. gdb_printf ("exe = '%s'\n", contents->c_str ());
  736. else
  737. warning (_("unable to read link '%s'"), filename);
  738. }
  739. if (mappings_f)
  740. {
  741. xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid);
  742. gdb::unique_xmalloc_ptr<char> map
  743. = target_fileio_read_stralloc (NULL, filename);
  744. if (map != NULL)
  745. {
  746. char *line;
  747. gdb_printf (_("Mapped address spaces:\n\n"));
  748. if (gdbarch_addr_bit (gdbarch) == 32)
  749. {
  750. gdb_printf ("\t%10s %10s %10s %10s %s %s\n",
  751. "Start Addr", " End Addr", " Size",
  752. " Offset", "Perms ", "objfile");
  753. }
  754. else
  755. {
  756. gdb_printf (" %18s %18s %10s %10s %s %s\n",
  757. "Start Addr", " End Addr", " Size",
  758. " Offset", "Perms ", "objfile");
  759. }
  760. char *saveptr;
  761. for (line = strtok_r (map.get (), "\n", &saveptr);
  762. line;
  763. line = strtok_r (NULL, "\n", &saveptr))
  764. {
  765. struct mapping m = read_mapping (line);
  766. if (gdbarch_addr_bit (gdbarch) == 32)
  767. {
  768. gdb_printf ("\t%10s %10s %10s %10s %-5.*s %s\n",
  769. paddress (gdbarch, m.addr),
  770. paddress (gdbarch, m.endaddr),
  771. hex_string (m.endaddr - m.addr),
  772. hex_string (m.offset),
  773. (int) m.permissions.size (),
  774. m.permissions.data (),
  775. m.filename);
  776. }
  777. else
  778. {
  779. gdb_printf (" %18s %18s %10s %10s %-5.*s %s\n",
  780. paddress (gdbarch, m.addr),
  781. paddress (gdbarch, m.endaddr),
  782. hex_string (m.endaddr - m.addr),
  783. hex_string (m.offset),
  784. (int) m.permissions.size (),
  785. m.permissions.data (),
  786. m.filename);
  787. }
  788. }
  789. }
  790. else
  791. warning (_("unable to open /proc file '%s'"), filename);
  792. }
  793. if (status_f)
  794. {
  795. xsnprintf (filename, sizeof filename, "/proc/%ld/status", pid);
  796. gdb::unique_xmalloc_ptr<char> status
  797. = target_fileio_read_stralloc (NULL, filename);
  798. if (status)
  799. gdb_puts (status.get ());
  800. else
  801. warning (_("unable to open /proc file '%s'"), filename);
  802. }
  803. if (stat_f)
  804. {
  805. xsnprintf (filename, sizeof filename, "/proc/%ld/stat", pid);
  806. gdb::unique_xmalloc_ptr<char> statstr
  807. = target_fileio_read_stralloc (NULL, filename);
  808. if (statstr)
  809. {
  810. const char *p = statstr.get ();
  811. gdb_printf (_("Process: %s\n"),
  812. pulongest (strtoulst (p, &p, 10)));
  813. p = skip_spaces (p);
  814. if (*p == '(')
  815. {
  816. /* ps command also relies on no trailing fields
  817. ever contain ')'. */
  818. const char *ep = strrchr (p, ')');
  819. if (ep != NULL)
  820. {
  821. gdb_printf ("Exec file: %.*s\n",
  822. (int) (ep - p - 1), p + 1);
  823. p = ep + 1;
  824. }
  825. }
  826. p = skip_spaces (p);
  827. if (*p)
  828. gdb_printf (_("State: %c\n"), *p++);
  829. if (*p)
  830. gdb_printf (_("Parent process: %s\n"),
  831. pulongest (strtoulst (p, &p, 10)));
  832. if (*p)
  833. gdb_printf (_("Process group: %s\n"),
  834. pulongest (strtoulst (p, &p, 10)));
  835. if (*p)
  836. gdb_printf (_("Session id: %s\n"),
  837. pulongest (strtoulst (p, &p, 10)));
  838. if (*p)
  839. gdb_printf (_("TTY: %s\n"),
  840. pulongest (strtoulst (p, &p, 10)));
  841. if (*p)
  842. gdb_printf (_("TTY owner process group: %s\n"),
  843. pulongest (strtoulst (p, &p, 10)));
  844. if (*p)
  845. gdb_printf (_("Flags: %s\n"),
  846. hex_string (strtoulst (p, &p, 10)));
  847. if (*p)
  848. gdb_printf (_("Minor faults (no memory page): %s\n"),
  849. pulongest (strtoulst (p, &p, 10)));
  850. if (*p)
  851. gdb_printf (_("Minor faults, children: %s\n"),
  852. pulongest (strtoulst (p, &p, 10)));
  853. if (*p)
  854. gdb_printf (_("Major faults (memory page faults): %s\n"),
  855. pulongest (strtoulst (p, &p, 10)));
  856. if (*p)
  857. gdb_printf (_("Major faults, children: %s\n"),
  858. pulongest (strtoulst (p, &p, 10)));
  859. if (*p)
  860. gdb_printf (_("utime: %s\n"),
  861. pulongest (strtoulst (p, &p, 10)));
  862. if (*p)
  863. gdb_printf (_("stime: %s\n"),
  864. pulongest (strtoulst (p, &p, 10)));
  865. if (*p)
  866. gdb_printf (_("utime, children: %s\n"),
  867. pulongest (strtoulst (p, &p, 10)));
  868. if (*p)
  869. gdb_printf (_("stime, children: %s\n"),
  870. pulongest (strtoulst (p, &p, 10)));
  871. if (*p)
  872. gdb_printf (_("jiffies remaining in current "
  873. "time slice: %s\n"),
  874. pulongest (strtoulst (p, &p, 10)));
  875. if (*p)
  876. gdb_printf (_("'nice' value: %s\n"),
  877. pulongest (strtoulst (p, &p, 10)));
  878. if (*p)
  879. gdb_printf (_("jiffies until next timeout: %s\n"),
  880. pulongest (strtoulst (p, &p, 10)));
  881. if (*p)
  882. gdb_printf (_("jiffies until next SIGALRM: %s\n"),
  883. pulongest (strtoulst (p, &p, 10)));
  884. if (*p)
  885. gdb_printf (_("start time (jiffies since "
  886. "system boot): %s\n"),
  887. pulongest (strtoulst (p, &p, 10)));
  888. if (*p)
  889. gdb_printf (_("Virtual memory size: %s\n"),
  890. pulongest (strtoulst (p, &p, 10)));
  891. if (*p)
  892. gdb_printf (_("Resident set size: %s\n"),
  893. pulongest (strtoulst (p, &p, 10)));
  894. if (*p)
  895. gdb_printf (_("rlim: %s\n"),
  896. pulongest (strtoulst (p, &p, 10)));
  897. if (*p)
  898. gdb_printf (_("Start of text: %s\n"),
  899. hex_string (strtoulst (p, &p, 10)));
  900. if (*p)
  901. gdb_printf (_("End of text: %s\n"),
  902. hex_string (strtoulst (p, &p, 10)));
  903. if (*p)
  904. gdb_printf (_("Start of stack: %s\n"),
  905. hex_string (strtoulst (p, &p, 10)));
  906. #if 0 /* Don't know how architecture-dependent the rest is...
  907. Anyway the signal bitmap info is available from "status". */
  908. if (*p)
  909. gdb_printf (_("Kernel stack pointer: %s\n"),
  910. hex_string (strtoulst (p, &p, 10)));
  911. if (*p)
  912. gdb_printf (_("Kernel instr pointer: %s\n"),
  913. hex_string (strtoulst (p, &p, 10)));
  914. if (*p)
  915. gdb_printf (_("Pending signals bitmap: %s\n"),
  916. hex_string (strtoulst (p, &p, 10)));
  917. if (*p)
  918. gdb_printf (_("Blocked signals bitmap: %s\n"),
  919. hex_string (strtoulst (p, &p, 10)));
  920. if (*p)
  921. gdb_printf (_("Ignored signals bitmap: %s\n"),
  922. hex_string (strtoulst (p, &p, 10)));
  923. if (*p)
  924. gdb_printf (_("Catched signals bitmap: %s\n"),
  925. hex_string (strtoulst (p, &p, 10)));
  926. if (*p)
  927. gdb_printf (_("wchan (system call): %s\n"),
  928. hex_string (strtoulst (p, &p, 10)));
  929. #endif
  930. }
  931. else
  932. warning (_("unable to open /proc file '%s'"), filename);
  933. }
  934. }
  935. /* Implementation of `gdbarch_read_core_file_mappings', as defined in
  936. gdbarch.h.
  937. This function reads the NT_FILE note (which BFD turns into the
  938. section ".note.linuxcore.file"). The format of this note / section
  939. is described as follows in the Linux kernel sources in
  940. fs/binfmt_elf.c:
  941. long count -- how many files are mapped
  942. long page_size -- units for file_ofs
  943. array of [COUNT] elements of
  944. long start
  945. long end
  946. long file_ofs
  947. followed by COUNT filenames in ASCII: "FILE1" NUL "FILE2" NUL...
  948. CBFD is the BFD of the core file.
  949. PRE_LOOP_CB is the callback function to invoke prior to starting
  950. the loop which processes individual entries. This callback will
  951. only be executed after the note has been examined in enough
  952. detail to verify that it's not malformed in some way.
  953. LOOP_CB is the callback function that will be executed once
  954. for each mapping. */
  955. static void
  956. linux_read_core_file_mappings
  957. (struct gdbarch *gdbarch,
  958. struct bfd *cbfd,
  959. read_core_file_mappings_pre_loop_ftype pre_loop_cb,
  960. read_core_file_mappings_loop_ftype loop_cb)
  961. {
  962. /* Ensure that ULONGEST is big enough for reading 64-bit core files. */
  963. gdb_static_assert (sizeof (ULONGEST) >= 8);
  964. /* It's not required that the NT_FILE note exists, so return silently
  965. if it's not found. Beyond this point though, we'll complain
  966. if problems are found. */
  967. asection *section = bfd_get_section_by_name (cbfd, ".note.linuxcore.file");
  968. if (section == nullptr)
  969. return;
  970. unsigned int addr_size_bits = gdbarch_addr_bit (gdbarch);
  971. unsigned int addr_size = addr_size_bits / 8;
  972. size_t note_size = bfd_section_size (section);
  973. if (note_size < 2 * addr_size)
  974. {
  975. warning (_("malformed core note - too short for header"));
  976. return;
  977. }
  978. gdb::def_vector<gdb_byte> contents (note_size);
  979. if (!bfd_get_section_contents (core_bfd, section, contents.data (),
  980. 0, note_size))
  981. {
  982. warning (_("could not get core note contents"));
  983. return;
  984. }
  985. gdb_byte *descdata = contents.data ();
  986. char *descend = (char *) descdata + note_size;
  987. if (descdata[note_size - 1] != '\0')
  988. {
  989. warning (_("malformed note - does not end with \\0"));
  990. return;
  991. }
  992. ULONGEST count = bfd_get (addr_size_bits, core_bfd, descdata);
  993. descdata += addr_size;
  994. ULONGEST page_size = bfd_get (addr_size_bits, core_bfd, descdata);
  995. descdata += addr_size;
  996. if (note_size < 2 * addr_size + count * 3 * addr_size)
  997. {
  998. warning (_("malformed note - too short for supplied file count"));
  999. return;
  1000. }
  1001. char *filenames = (char *) descdata + count * 3 * addr_size;
  1002. /* Make sure that the correct number of filenames exist. Complain
  1003. if there aren't enough or are too many. */
  1004. char *f = filenames;
  1005. for (int i = 0; i < count; i++)
  1006. {
  1007. if (f >= descend)
  1008. {
  1009. warning (_("malformed note - filename area is too small"));
  1010. return;
  1011. }
  1012. f += strnlen (f, descend - f) + 1;
  1013. }
  1014. /* Complain, but don't return early if the filename area is too big. */
  1015. if (f != descend)
  1016. warning (_("malformed note - filename area is too big"));
  1017. const bfd_build_id *orig_build_id = cbfd->build_id;
  1018. std::unordered_map<ULONGEST, const bfd_build_id *> vma_map;
  1019. /* Search for solib build-ids in the core file. Each time one is found,
  1020. map the start vma of the corresponding elf header to the build-id. */
  1021. for (bfd_section *sec = cbfd->sections; sec != nullptr; sec = sec->next)
  1022. {
  1023. cbfd->build_id = nullptr;
  1024. if (sec->flags & SEC_LOAD
  1025. && (get_elf_backend_data (cbfd)->elf_backend_core_find_build_id
  1026. (cbfd, (bfd_vma) sec->filepos)))
  1027. vma_map[sec->vma] = cbfd->build_id;
  1028. }
  1029. cbfd->build_id = orig_build_id;
  1030. pre_loop_cb (count);
  1031. for (int i = 0; i < count; i++)
  1032. {
  1033. ULONGEST start = bfd_get (addr_size_bits, core_bfd, descdata);
  1034. descdata += addr_size;
  1035. ULONGEST end = bfd_get (addr_size_bits, core_bfd, descdata);
  1036. descdata += addr_size;
  1037. ULONGEST file_ofs
  1038. = bfd_get (addr_size_bits, core_bfd, descdata) * page_size;
  1039. descdata += addr_size;
  1040. char * filename = filenames;
  1041. filenames += strlen ((char *) filenames) + 1;
  1042. const bfd_build_id *build_id = nullptr;
  1043. auto vma_map_it = vma_map.find (start);
  1044. if (vma_map_it != vma_map.end ())
  1045. build_id = vma_map_it->second;
  1046. loop_cb (i, start, end, file_ofs, filename, build_id);
  1047. }
  1048. }
  1049. /* Implement "info proc mappings" for a corefile. */
  1050. static void
  1051. linux_core_info_proc_mappings (struct gdbarch *gdbarch, const char *args)
  1052. {
  1053. linux_read_core_file_mappings (gdbarch, core_bfd,
  1054. [=] (ULONGEST count)
  1055. {
  1056. gdb_printf (_("Mapped address spaces:\n\n"));
  1057. if (gdbarch_addr_bit (gdbarch) == 32)
  1058. {
  1059. gdb_printf ("\t%10s %10s %10s %10s %s\n",
  1060. "Start Addr",
  1061. " End Addr",
  1062. " Size", " Offset", "objfile");
  1063. }
  1064. else
  1065. {
  1066. gdb_printf (" %18s %18s %10s %10s %s\n",
  1067. "Start Addr",
  1068. " End Addr",
  1069. " Size", " Offset", "objfile");
  1070. }
  1071. },
  1072. [=] (int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs,
  1073. const char *filename, const bfd_build_id *build_id)
  1074. {
  1075. if (gdbarch_addr_bit (gdbarch) == 32)
  1076. gdb_printf ("\t%10s %10s %10s %10s %s\n",
  1077. paddress (gdbarch, start),
  1078. paddress (gdbarch, end),
  1079. hex_string (end - start),
  1080. hex_string (file_ofs),
  1081. filename);
  1082. else
  1083. gdb_printf (" %18s %18s %10s %10s %s\n",
  1084. paddress (gdbarch, start),
  1085. paddress (gdbarch, end),
  1086. hex_string (end - start),
  1087. hex_string (file_ofs),
  1088. filename);
  1089. });
  1090. }
  1091. /* Implement "info proc" for a corefile. */
  1092. static void
  1093. linux_core_info_proc (struct gdbarch *gdbarch, const char *args,
  1094. enum info_proc_what what)
  1095. {
  1096. int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
  1097. int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
  1098. if (exe_f)
  1099. {
  1100. const char *exe;
  1101. exe = bfd_core_file_failing_command (core_bfd);
  1102. if (exe != NULL)
  1103. gdb_printf ("exe = '%s'\n", exe);
  1104. else
  1105. warning (_("unable to find command name in core file"));
  1106. }
  1107. if (mappings_f)
  1108. linux_core_info_proc_mappings (gdbarch, args);
  1109. if (!exe_f && !mappings_f)
  1110. error (_("unable to handle request"));
  1111. }
  1112. /* Read siginfo data from the core, if possible. Returns -1 on
  1113. failure. Otherwise, returns the number of bytes read. READBUF,
  1114. OFFSET, and LEN are all as specified by the to_xfer_partial
  1115. interface. */
  1116. static LONGEST
  1117. linux_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
  1118. ULONGEST offset, ULONGEST len)
  1119. {
  1120. thread_section_name section_name (".note.linuxcore.siginfo", inferior_ptid);
  1121. asection *section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
  1122. if (section == NULL)
  1123. return -1;
  1124. if (!bfd_get_section_contents (core_bfd, section, readbuf, offset, len))
  1125. return -1;
  1126. return len;
  1127. }
  1128. typedef int linux_find_memory_region_ftype (ULONGEST vaddr, ULONGEST size,
  1129. ULONGEST offset, ULONGEST inode,
  1130. int read, int write,
  1131. int exec, int modified,
  1132. const char *filename,
  1133. void *data);
  1134. typedef int linux_dump_mapping_p_ftype (filter_flags filterflags,
  1135. const struct smaps_vmflags *v,
  1136. int maybe_private_p,
  1137. int mapping_anon_p,
  1138. int mapping_file_p,
  1139. const char *filename,
  1140. ULONGEST addr,
  1141. ULONGEST offset);
  1142. /* Helper function to parse the contents of /proc/<pid>/smaps into a data
  1143. structure, for easy access.
  1144. DATA is the contents of the smaps file. The parsed contents are stored
  1145. into the SMAPS vector. */
  1146. static std::vector<struct smaps_data>
  1147. parse_smaps_data (const char *data,
  1148. const std::string maps_filename)
  1149. {
  1150. char *line, *t;
  1151. gdb_assert (data != nullptr);
  1152. line = strtok_r ((char *) data, "\n", &t);
  1153. std::vector<struct smaps_data> smaps;
  1154. while (line != NULL)
  1155. {
  1156. struct smaps_vmflags v;
  1157. int read, write, exec, priv;
  1158. int has_anonymous = 0;
  1159. int mapping_anon_p;
  1160. int mapping_file_p;
  1161. memset (&v, 0, sizeof (v));
  1162. struct mapping m = read_mapping (line);
  1163. mapping_anon_p = mapping_is_anonymous_p (m.filename);
  1164. /* If the mapping is not anonymous, then we can consider it
  1165. to be file-backed. These two states (anonymous or
  1166. file-backed) seem to be exclusive, but they can actually
  1167. coexist. For example, if a file-backed mapping has
  1168. "Anonymous:" pages (see more below), then the Linux
  1169. kernel will dump this mapping when the user specified
  1170. that she only wants anonymous mappings in the corefile
  1171. (*even* when she explicitly disabled the dumping of
  1172. file-backed mappings). */
  1173. mapping_file_p = !mapping_anon_p;
  1174. /* Decode permissions. */
  1175. auto has_perm = [&m] (char c)
  1176. { return m.permissions.find (c) != gdb::string_view::npos; };
  1177. read = has_perm ('r');
  1178. write = has_perm ('w');
  1179. exec = has_perm ('x');
  1180. /* 'private' here actually means VM_MAYSHARE, and not
  1181. VM_SHARED. In order to know if a mapping is really
  1182. private or not, we must check the flag "sh" in the
  1183. VmFlags field. This is done by decode_vmflags. However,
  1184. if we are using a Linux kernel released before the commit
  1185. 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10), we will
  1186. not have the VmFlags there. In this case, there is
  1187. really no way to know if we are dealing with VM_SHARED,
  1188. so we just assume that VM_MAYSHARE is enough. */
  1189. priv = has_perm ('p');
  1190. /* Try to detect if region should be dumped by parsing smaps
  1191. counters. */
  1192. for (line = strtok_r (NULL, "\n", &t);
  1193. line != NULL && line[0] >= 'A' && line[0] <= 'Z';
  1194. line = strtok_r (NULL, "\n", &t))
  1195. {
  1196. char keyword[64 + 1];
  1197. if (sscanf (line, "%64s", keyword) != 1)
  1198. {
  1199. warning (_("Error parsing {s,}maps file '%s'"),
  1200. maps_filename.c_str ());
  1201. break;
  1202. }
  1203. if (strcmp (keyword, "Anonymous:") == 0)
  1204. {
  1205. /* Older Linux kernels did not support the
  1206. "Anonymous:" counter. Check it here. */
  1207. has_anonymous = 1;
  1208. }
  1209. else if (strcmp (keyword, "VmFlags:") == 0)
  1210. decode_vmflags (line, &v);
  1211. if (strcmp (keyword, "AnonHugePages:") == 0
  1212. || strcmp (keyword, "Anonymous:") == 0)
  1213. {
  1214. unsigned long number;
  1215. if (sscanf (line, "%*s%lu", &number) != 1)
  1216. {
  1217. warning (_("Error parsing {s,}maps file '%s' number"),
  1218. maps_filename.c_str ());
  1219. break;
  1220. }
  1221. if (number > 0)
  1222. {
  1223. /* Even if we are dealing with a file-backed
  1224. mapping, if it contains anonymous pages we
  1225. consider it to be *also* an anonymous
  1226. mapping, because this is what the Linux
  1227. kernel does:
  1228. // Dump segments that have been written to.
  1229. if (vma->anon_vma && FILTER(ANON_PRIVATE))
  1230. goto whole;
  1231. Note that if the mapping is already marked as
  1232. file-backed (i.e., mapping_file_p is
  1233. non-zero), then this is a special case, and
  1234. this mapping will be dumped either when the
  1235. user wants to dump file-backed *or* anonymous
  1236. mappings. */
  1237. mapping_anon_p = 1;
  1238. }
  1239. }
  1240. }
  1241. /* Save the smaps entry to the vector. */
  1242. struct smaps_data map;
  1243. map.start_address = m.addr;
  1244. map.end_address = m.endaddr;
  1245. map.filename = m.filename;
  1246. map.vmflags = v;
  1247. map.read = read? true : false;
  1248. map.write = write? true : false;
  1249. map.exec = exec? true : false;
  1250. map.priv = priv? true : false;
  1251. map.has_anonymous = has_anonymous;
  1252. map.mapping_anon_p = mapping_anon_p? true : false;
  1253. map.mapping_file_p = mapping_file_p? true : false;
  1254. map.offset = m.offset;
  1255. map.inode = m.inode;
  1256. smaps.emplace_back (map);
  1257. }
  1258. return smaps;
  1259. }
  1260. /* See linux-tdep.h. */
  1261. bool
  1262. linux_address_in_memtag_page (CORE_ADDR address)
  1263. {
  1264. if (current_inferior ()->fake_pid_p)
  1265. return false;
  1266. pid_t pid = current_inferior ()->pid;
  1267. std::string smaps_file = string_printf ("/proc/%d/smaps", pid);
  1268. gdb::unique_xmalloc_ptr<char> data
  1269. = target_fileio_read_stralloc (NULL, smaps_file.c_str ());
  1270. if (data == nullptr)
  1271. return false;
  1272. /* Parse the contents of smaps into a vector. */
  1273. std::vector<struct smaps_data> smaps
  1274. = parse_smaps_data (data.get (), smaps_file);
  1275. for (const smaps_data &map : smaps)
  1276. {
  1277. /* Is the address within [start_address, end_address) in a page
  1278. mapped with memory tagging? */
  1279. if (address >= map.start_address
  1280. && address < map.end_address
  1281. && map.vmflags.memory_tagging)
  1282. return true;
  1283. }
  1284. return false;
  1285. }
  1286. /* List memory regions in the inferior for a corefile. */
  1287. static int
  1288. linux_find_memory_regions_full (struct gdbarch *gdbarch,
  1289. linux_dump_mapping_p_ftype *should_dump_mapping_p,
  1290. linux_find_memory_region_ftype *func,
  1291. void *obfd)
  1292. {
  1293. pid_t pid;
  1294. /* Default dump behavior of coredump_filter (0x33), according to
  1295. Documentation/filesystems/proc.txt from the Linux kernel
  1296. tree. */
  1297. filter_flags filterflags = (COREFILTER_ANON_PRIVATE
  1298. | COREFILTER_ANON_SHARED
  1299. | COREFILTER_ELF_HEADERS
  1300. | COREFILTER_HUGETLB_PRIVATE);
  1301. /* We need to know the real target PID to access /proc. */
  1302. if (current_inferior ()->fake_pid_p)
  1303. return 1;
  1304. pid = current_inferior ()->pid;
  1305. if (use_coredump_filter)
  1306. {
  1307. std::string core_dump_filter_name
  1308. = string_printf ("/proc/%d/coredump_filter", pid);
  1309. gdb::unique_xmalloc_ptr<char> coredumpfilterdata
  1310. = target_fileio_read_stralloc (NULL, core_dump_filter_name.c_str ());
  1311. if (coredumpfilterdata != NULL)
  1312. {
  1313. unsigned int flags;
  1314. sscanf (coredumpfilterdata.get (), "%x", &flags);
  1315. filterflags = (enum filter_flag) flags;
  1316. }
  1317. }
  1318. std::string maps_filename = string_printf ("/proc/%d/smaps", pid);
  1319. gdb::unique_xmalloc_ptr<char> data
  1320. = target_fileio_read_stralloc (NULL, maps_filename.c_str ());
  1321. if (data == NULL)
  1322. {
  1323. /* Older Linux kernels did not support /proc/PID/smaps. */
  1324. maps_filename = string_printf ("/proc/%d/maps", pid);
  1325. data = target_fileio_read_stralloc (NULL, maps_filename.c_str ());
  1326. if (data == nullptr)
  1327. return 1;
  1328. }
  1329. /* Parse the contents of smaps into a vector. */
  1330. std::vector<struct smaps_data> smaps
  1331. = parse_smaps_data (data.get (), maps_filename.c_str ());
  1332. for (const struct smaps_data &map : smaps)
  1333. {
  1334. int should_dump_p = 0;
  1335. if (map.has_anonymous)
  1336. {
  1337. should_dump_p
  1338. = should_dump_mapping_p (filterflags, &map.vmflags,
  1339. map.priv,
  1340. map.mapping_anon_p,
  1341. map.mapping_file_p,
  1342. map.filename.c_str (),
  1343. map.start_address,
  1344. map.offset);
  1345. }
  1346. else
  1347. {
  1348. /* Older Linux kernels did not support the "Anonymous:" counter.
  1349. If it is missing, we can't be sure - dump all the pages. */
  1350. should_dump_p = 1;
  1351. }
  1352. /* Invoke the callback function to create the corefile segment. */
  1353. if (should_dump_p)
  1354. {
  1355. func (map.start_address, map.end_address - map.start_address,
  1356. map.offset, map.inode, map.read, map.write, map.exec,
  1357. 1, /* MODIFIED is true because we want to dump
  1358. the mapping. */
  1359. map.filename.c_str (), obfd);
  1360. }
  1361. }
  1362. return 0;
  1363. }
  1364. /* A structure for passing information through
  1365. linux_find_memory_regions_full. */
  1366. struct linux_find_memory_regions_data
  1367. {
  1368. /* The original callback. */
  1369. find_memory_region_ftype func;
  1370. /* The original datum. */
  1371. void *obfd;
  1372. };
  1373. /* A callback for linux_find_memory_regions that converts between the
  1374. "full"-style callback and find_memory_region_ftype. */
  1375. static int
  1376. linux_find_memory_regions_thunk (ULONGEST vaddr, ULONGEST size,
  1377. ULONGEST offset, ULONGEST inode,
  1378. int read, int write, int exec, int modified,
  1379. const char *filename, void *arg)
  1380. {
  1381. struct linux_find_memory_regions_data *data
  1382. = (struct linux_find_memory_regions_data *) arg;
  1383. return data->func (vaddr, size, read, write, exec, modified, data->obfd);
  1384. }
  1385. /* A variant of linux_find_memory_regions_full that is suitable as the
  1386. gdbarch find_memory_regions method. */
  1387. static int
  1388. linux_find_memory_regions (struct gdbarch *gdbarch,
  1389. find_memory_region_ftype func, void *obfd)
  1390. {
  1391. struct linux_find_memory_regions_data data;
  1392. data.func = func;
  1393. data.obfd = obfd;
  1394. return linux_find_memory_regions_full (gdbarch,
  1395. dump_mapping_p,
  1396. linux_find_memory_regions_thunk,
  1397. &data);
  1398. }
  1399. /* This is used to pass information from
  1400. linux_make_mappings_corefile_notes through
  1401. linux_find_memory_regions_full. */
  1402. struct linux_make_mappings_data
  1403. {
  1404. /* Number of files mapped. */
  1405. ULONGEST file_count;
  1406. /* The obstack for the main part of the data. */
  1407. struct obstack *data_obstack;
  1408. /* The filename obstack. */
  1409. struct obstack *filename_obstack;
  1410. /* The architecture's "long" type. */
  1411. struct type *long_type;
  1412. };
  1413. static linux_find_memory_region_ftype linux_make_mappings_callback;
  1414. /* A callback for linux_find_memory_regions_full that updates the
  1415. mappings data for linux_make_mappings_corefile_notes. */
  1416. static int
  1417. linux_make_mappings_callback (ULONGEST vaddr, ULONGEST size,
  1418. ULONGEST offset, ULONGEST inode,
  1419. int read, int write, int exec, int modified,
  1420. const char *filename, void *data)
  1421. {
  1422. struct linux_make_mappings_data *map_data
  1423. = (struct linux_make_mappings_data *) data;
  1424. gdb_byte buf[sizeof (ULONGEST)];
  1425. if (*filename == '\0' || inode == 0)
  1426. return 0;
  1427. ++map_data->file_count;
  1428. pack_long (buf, map_data->long_type, vaddr);
  1429. obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
  1430. pack_long (buf, map_data->long_type, vaddr + size);
  1431. obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
  1432. pack_long (buf, map_data->long_type, offset);
  1433. obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
  1434. obstack_grow_str0 (map_data->filename_obstack, filename);
  1435. return 0;
  1436. }
  1437. /* Write the file mapping data to the core file, if possible. OBFD is
  1438. the output BFD. NOTE_DATA is the current note data, and NOTE_SIZE
  1439. is a pointer to the note size. Updates NOTE_DATA and NOTE_SIZE. */
  1440. static void
  1441. linux_make_mappings_corefile_notes (struct gdbarch *gdbarch, bfd *obfd,
  1442. gdb::unique_xmalloc_ptr<char> &note_data,
  1443. int *note_size)
  1444. {
  1445. struct linux_make_mappings_data mapping_data;
  1446. struct type *long_type
  1447. = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch), 0, "long");
  1448. gdb_byte buf[sizeof (ULONGEST)];
  1449. auto_obstack data_obstack, filename_obstack;
  1450. mapping_data.file_count = 0;
  1451. mapping_data.data_obstack = &data_obstack;
  1452. mapping_data.filename_obstack = &filename_obstack;
  1453. mapping_data.long_type = long_type;
  1454. /* Reserve space for the count. */
  1455. obstack_blank (&data_obstack, TYPE_LENGTH (long_type));
  1456. /* We always write the page size as 1 since we have no good way to
  1457. determine the correct value. */
  1458. pack_long (buf, long_type, 1);
  1459. obstack_grow (&data_obstack, buf, TYPE_LENGTH (long_type));
  1460. linux_find_memory_regions_full (gdbarch,
  1461. dump_note_entry_p,
  1462. linux_make_mappings_callback,
  1463. &mapping_data);
  1464. if (mapping_data.file_count != 0)
  1465. {
  1466. /* Write the count to the obstack. */
  1467. pack_long ((gdb_byte *) obstack_base (&data_obstack),
  1468. long_type, mapping_data.file_count);
  1469. /* Copy the filenames to the data obstack. */
  1470. int size = obstack_object_size (&filename_obstack);
  1471. obstack_grow (&data_obstack, obstack_base (&filename_obstack),
  1472. size);
  1473. note_data.reset (elfcore_write_file_note (obfd, note_data.release (), note_size,
  1474. obstack_base (&data_obstack),
  1475. obstack_object_size (&data_obstack)));
  1476. }
  1477. }
  1478. /* Fetch the siginfo data for the specified thread, if it exists. If
  1479. there is no data, or we could not read it, return an empty
  1480. buffer. */
  1481. static gdb::byte_vector
  1482. linux_get_siginfo_data (thread_info *thread, struct gdbarch *gdbarch)
  1483. {
  1484. struct type *siginfo_type;
  1485. LONGEST bytes_read;
  1486. if (!gdbarch_get_siginfo_type_p (gdbarch))
  1487. return gdb::byte_vector ();
  1488. scoped_restore_current_thread save_current_thread;
  1489. switch_to_thread (thread);
  1490. siginfo_type = gdbarch_get_siginfo_type (gdbarch);
  1491. gdb::byte_vector buf (TYPE_LENGTH (siginfo_type));
  1492. bytes_read = target_read (current_inferior ()->top_target (),
  1493. TARGET_OBJECT_SIGNAL_INFO, NULL,
  1494. buf.data (), 0, TYPE_LENGTH (siginfo_type));
  1495. if (bytes_read != TYPE_LENGTH (siginfo_type))
  1496. buf.clear ();
  1497. return buf;
  1498. }
  1499. struct linux_corefile_thread_data
  1500. {
  1501. linux_corefile_thread_data (struct gdbarch *gdbarch, bfd *obfd,
  1502. gdb::unique_xmalloc_ptr<char> &note_data,
  1503. int *note_size, gdb_signal stop_signal)
  1504. : gdbarch (gdbarch), obfd (obfd), note_data (note_data),
  1505. note_size (note_size), stop_signal (stop_signal)
  1506. {}
  1507. struct gdbarch *gdbarch;
  1508. bfd *obfd;
  1509. gdb::unique_xmalloc_ptr<char> &note_data;
  1510. int *note_size;
  1511. enum gdb_signal stop_signal;
  1512. };
  1513. /* Records the thread's register state for the corefile note
  1514. section. */
  1515. static void
  1516. linux_corefile_thread (struct thread_info *info,
  1517. struct linux_corefile_thread_data *args)
  1518. {
  1519. gcore_elf_build_thread_register_notes (args->gdbarch, info,
  1520. args->stop_signal,
  1521. args->obfd, &args->note_data,
  1522. args->note_size);
  1523. /* Don't return anything if we got no register information above,
  1524. such a core file is useless. */
  1525. if (args->note_data != NULL)
  1526. {
  1527. gdb::byte_vector siginfo_data
  1528. = linux_get_siginfo_data (info, args->gdbarch);
  1529. if (!siginfo_data.empty ())
  1530. args->note_data.reset (elfcore_write_note (args->obfd,
  1531. args->note_data.release (),
  1532. args->note_size,
  1533. "CORE", NT_SIGINFO,
  1534. siginfo_data.data (),
  1535. siginfo_data.size ()));
  1536. }
  1537. }
  1538. /* Fill the PRPSINFO structure with information about the process being
  1539. debugged. Returns 1 in case of success, 0 for failures. Please note that
  1540. even if the structure cannot be entirely filled (e.g., GDB was unable to
  1541. gather information about the process UID/GID), this function will still
  1542. return 1 since some information was already recorded. It will only return
  1543. 0 iff nothing can be gathered. */
  1544. static int
  1545. linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p)
  1546. {
  1547. /* The filename which we will use to obtain some info about the process.
  1548. We will basically use this to store the `/proc/PID/FILENAME' file. */
  1549. char filename[100];
  1550. /* The basename of the executable. */
  1551. const char *basename;
  1552. /* Temporary buffer. */
  1553. char *tmpstr;
  1554. /* The valid states of a process, according to the Linux kernel. */
  1555. const char valid_states[] = "RSDTZW";
  1556. /* The program state. */
  1557. const char *prog_state;
  1558. /* The state of the process. */
  1559. char pr_sname;
  1560. /* The PID of the program which generated the corefile. */
  1561. pid_t pid;
  1562. /* Process flags. */
  1563. unsigned int pr_flag;
  1564. /* Process nice value. */
  1565. long pr_nice;
  1566. /* The number of fields read by `sscanf'. */
  1567. int n_fields = 0;
  1568. gdb_assert (p != NULL);
  1569. /* Obtaining PID and filename. */
  1570. pid = inferior_ptid.pid ();
  1571. xsnprintf (filename, sizeof (filename), "/proc/%d/cmdline", (int) pid);
  1572. /* The full name of the program which generated the corefile. */
  1573. gdb::unique_xmalloc_ptr<char> fname
  1574. = target_fileio_read_stralloc (NULL, filename);
  1575. if (fname == NULL || fname.get ()[0] == '\0')
  1576. {
  1577. /* No program name was read, so we won't be able to retrieve more
  1578. information about the process. */
  1579. return 0;
  1580. }
  1581. memset (p, 0, sizeof (*p));
  1582. /* Defining the PID. */
  1583. p->pr_pid = pid;
  1584. /* Copying the program name. Only the basename matters. */
  1585. basename = lbasename (fname.get ());
  1586. strncpy (p->pr_fname, basename, sizeof (p->pr_fname) - 1);
  1587. p->pr_fname[sizeof (p->pr_fname) - 1] = '\0';
  1588. const std::string &infargs = current_inferior ()->args ();
  1589. /* The arguments of the program. */
  1590. std::string psargs = fname.get ();
  1591. if (!infargs.empty ())
  1592. psargs += ' ' + infargs;
  1593. strncpy (p->pr_psargs, psargs.c_str (), sizeof (p->pr_psargs) - 1);
  1594. p->pr_psargs[sizeof (p->pr_psargs) - 1] = '\0';
  1595. xsnprintf (filename, sizeof (filename), "/proc/%d/stat", (int) pid);
  1596. /* The contents of `/proc/PID/stat'. */
  1597. gdb::unique_xmalloc_ptr<char> proc_stat_contents
  1598. = target_fileio_read_stralloc (NULL, filename);
  1599. char *proc_stat = proc_stat_contents.get ();
  1600. if (proc_stat == NULL || *proc_stat == '\0')
  1601. {
  1602. /* Despite being unable to read more information about the
  1603. process, we return 1 here because at least we have its
  1604. command line, PID and arguments. */
  1605. return 1;
  1606. }
  1607. /* Ok, we have the stats. It's time to do a little parsing of the
  1608. contents of the buffer, so that we end up reading what we want.
  1609. The following parsing mechanism is strongly based on the
  1610. information generated by the `fs/proc/array.c' file, present in
  1611. the Linux kernel tree. More details about how the information is
  1612. displayed can be obtained by seeing the manpage of proc(5),
  1613. specifically under the entry of `/proc/[pid]/stat'. */
  1614. /* Getting rid of the PID, since we already have it. */
  1615. while (isdigit (*proc_stat))
  1616. ++proc_stat;
  1617. proc_stat = skip_spaces (proc_stat);
  1618. /* ps command also relies on no trailing fields ever contain ')'. */
  1619. proc_stat = strrchr (proc_stat, ')');
  1620. if (proc_stat == NULL)
  1621. return 1;
  1622. proc_stat++;
  1623. proc_stat = skip_spaces (proc_stat);
  1624. n_fields = sscanf (proc_stat,
  1625. "%c" /* Process state. */
  1626. "%d%d%d" /* Parent PID, group ID, session ID. */
  1627. "%*d%*d" /* tty_nr, tpgid (not used). */
  1628. "%u" /* Flags. */
  1629. "%*s%*s%*s%*s" /* minflt, cminflt, majflt,
  1630. cmajflt (not used). */
  1631. "%*s%*s%*s%*s" /* utime, stime, cutime,
  1632. cstime (not used). */
  1633. "%*s" /* Priority (not used). */
  1634. "%ld", /* Nice. */
  1635. &pr_sname,
  1636. &p->pr_ppid, &p->pr_pgrp, &p->pr_sid,
  1637. &pr_flag,
  1638. &pr_nice);
  1639. if (n_fields != 6)
  1640. {
  1641. /* Again, we couldn't read the complementary information about
  1642. the process state. However, we already have minimal
  1643. information, so we just return 1 here. */
  1644. return 1;
  1645. }
  1646. /* Filling the structure fields. */
  1647. prog_state = strchr (valid_states, pr_sname);
  1648. if (prog_state != NULL)
  1649. p->pr_state = prog_state - valid_states;
  1650. else
  1651. {
  1652. /* Zero means "Running". */
  1653. p->pr_state = 0;
  1654. }
  1655. p->pr_sname = p->pr_state > 5 ? '.' : pr_sname;
  1656. p->pr_zomb = p->pr_sname == 'Z';
  1657. p->pr_nice = pr_nice;
  1658. p->pr_flag = pr_flag;
  1659. /* Finally, obtaining the UID and GID. For that, we read and parse the
  1660. contents of the `/proc/PID/status' file. */
  1661. xsnprintf (filename, sizeof (filename), "/proc/%d/status", (int) pid);
  1662. /* The contents of `/proc/PID/status'. */
  1663. gdb::unique_xmalloc_ptr<char> proc_status_contents
  1664. = target_fileio_read_stralloc (NULL, filename);
  1665. char *proc_status = proc_status_contents.get ();
  1666. if (proc_status == NULL || *proc_status == '\0')
  1667. {
  1668. /* Returning 1 since we already have a bunch of information. */
  1669. return 1;
  1670. }
  1671. /* Extracting the UID. */
  1672. tmpstr = strstr (proc_status, "Uid:");
  1673. if (tmpstr != NULL)
  1674. {
  1675. /* Advancing the pointer to the beginning of the UID. */
  1676. tmpstr += sizeof ("Uid:");
  1677. while (*tmpstr != '\0' && !isdigit (*tmpstr))
  1678. ++tmpstr;
  1679. if (isdigit (*tmpstr))
  1680. p->pr_uid = strtol (tmpstr, &tmpstr, 10);
  1681. }
  1682. /* Extracting the GID. */
  1683. tmpstr = strstr (proc_status, "Gid:");
  1684. if (tmpstr != NULL)
  1685. {
  1686. /* Advancing the pointer to the beginning of the GID. */
  1687. tmpstr += sizeof ("Gid:");
  1688. while (*tmpstr != '\0' && !isdigit (*tmpstr))
  1689. ++tmpstr;
  1690. if (isdigit (*tmpstr))
  1691. p->pr_gid = strtol (tmpstr, &tmpstr, 10);
  1692. }
  1693. return 1;
  1694. }
  1695. /* Build the note section for a corefile, and return it in a malloc
  1696. buffer. */
  1697. static gdb::unique_xmalloc_ptr<char>
  1698. linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
  1699. {
  1700. struct elf_internal_linux_prpsinfo prpsinfo;
  1701. gdb::unique_xmalloc_ptr<char> note_data;
  1702. if (! gdbarch_iterate_over_regset_sections_p (gdbarch))
  1703. return NULL;
  1704. if (linux_fill_prpsinfo (&prpsinfo))
  1705. {
  1706. if (gdbarch_ptr_bit (gdbarch) == 64)
  1707. note_data.reset (elfcore_write_linux_prpsinfo64 (obfd,
  1708. note_data.release (),
  1709. note_size, &prpsinfo));
  1710. else
  1711. note_data.reset (elfcore_write_linux_prpsinfo32 (obfd,
  1712. note_data.release (),
  1713. note_size, &prpsinfo));
  1714. }
  1715. /* Thread register information. */
  1716. try
  1717. {
  1718. update_thread_list ();
  1719. }
  1720. catch (const gdb_exception_error &e)
  1721. {
  1722. exception_print (gdb_stderr, e);
  1723. }
  1724. /* Like the kernel, prefer dumping the signalled thread first.
  1725. "First thread" is what tools use to infer the signalled
  1726. thread. */
  1727. thread_info *signalled_thr = gcore_find_signalled_thread ();
  1728. gdb_signal stop_signal;
  1729. if (signalled_thr != nullptr)
  1730. stop_signal = signalled_thr->stop_signal ();
  1731. else
  1732. stop_signal = GDB_SIGNAL_0;
  1733. linux_corefile_thread_data thread_args (gdbarch, obfd, note_data, note_size,
  1734. stop_signal);
  1735. if (signalled_thr != nullptr)
  1736. linux_corefile_thread (signalled_thr, &thread_args);
  1737. for (thread_info *thr : current_inferior ()->non_exited_threads ())
  1738. {
  1739. if (thr == signalled_thr)
  1740. continue;
  1741. linux_corefile_thread (thr, &thread_args);
  1742. }
  1743. if (!note_data)
  1744. return NULL;
  1745. /* Auxillary vector. */
  1746. gdb::optional<gdb::byte_vector> auxv =
  1747. target_read_alloc (current_inferior ()->top_target (),
  1748. TARGET_OBJECT_AUXV, NULL);
  1749. if (auxv && !auxv->empty ())
  1750. {
  1751. note_data.reset (elfcore_write_note (obfd, note_data.release (),
  1752. note_size, "CORE", NT_AUXV,
  1753. auxv->data (), auxv->size ()));
  1754. if (!note_data)
  1755. return NULL;
  1756. }
  1757. /* File mappings. */
  1758. linux_make_mappings_corefile_notes (gdbarch, obfd, note_data, note_size);
  1759. /* Target description. */
  1760. gcore_elf_make_tdesc_note (obfd, &note_data, note_size);
  1761. return note_data;
  1762. }
  1763. /* Implementation of `gdbarch_gdb_signal_from_target', as defined in
  1764. gdbarch.h. This function is not static because it is exported to
  1765. other -tdep files. */
  1766. enum gdb_signal
  1767. linux_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
  1768. {
  1769. switch (signal)
  1770. {
  1771. case 0:
  1772. return GDB_SIGNAL_0;
  1773. case LINUX_SIGHUP:
  1774. return GDB_SIGNAL_HUP;
  1775. case LINUX_SIGINT:
  1776. return GDB_SIGNAL_INT;
  1777. case LINUX_SIGQUIT:
  1778. return GDB_SIGNAL_QUIT;
  1779. case LINUX_SIGILL:
  1780. return GDB_SIGNAL_ILL;
  1781. case LINUX_SIGTRAP:
  1782. return GDB_SIGNAL_TRAP;
  1783. case LINUX_SIGABRT:
  1784. return GDB_SIGNAL_ABRT;
  1785. case LINUX_SIGBUS:
  1786. return GDB_SIGNAL_BUS;
  1787. case LINUX_SIGFPE:
  1788. return GDB_SIGNAL_FPE;
  1789. case LINUX_SIGKILL:
  1790. return GDB_SIGNAL_KILL;
  1791. case LINUX_SIGUSR1:
  1792. return GDB_SIGNAL_USR1;
  1793. case LINUX_SIGSEGV:
  1794. return GDB_SIGNAL_SEGV;
  1795. case LINUX_SIGUSR2:
  1796. return GDB_SIGNAL_USR2;
  1797. case LINUX_SIGPIPE:
  1798. return GDB_SIGNAL_PIPE;
  1799. case LINUX_SIGALRM:
  1800. return GDB_SIGNAL_ALRM;
  1801. case LINUX_SIGTERM:
  1802. return GDB_SIGNAL_TERM;
  1803. case LINUX_SIGCHLD:
  1804. return GDB_SIGNAL_CHLD;
  1805. case LINUX_SIGCONT:
  1806. return GDB_SIGNAL_CONT;
  1807. case LINUX_SIGSTOP:
  1808. return GDB_SIGNAL_STOP;
  1809. case LINUX_SIGTSTP:
  1810. return GDB_SIGNAL_TSTP;
  1811. case LINUX_SIGTTIN:
  1812. return GDB_SIGNAL_TTIN;
  1813. case LINUX_SIGTTOU:
  1814. return GDB_SIGNAL_TTOU;
  1815. case LINUX_SIGURG:
  1816. return GDB_SIGNAL_URG;
  1817. case LINUX_SIGXCPU:
  1818. return GDB_SIGNAL_XCPU;
  1819. case LINUX_SIGXFSZ:
  1820. return GDB_SIGNAL_XFSZ;
  1821. case LINUX_SIGVTALRM:
  1822. return GDB_SIGNAL_VTALRM;
  1823. case LINUX_SIGPROF:
  1824. return GDB_SIGNAL_PROF;
  1825. case LINUX_SIGWINCH:
  1826. return GDB_SIGNAL_WINCH;
  1827. /* No way to differentiate between SIGIO and SIGPOLL.
  1828. Therefore, we just handle the first one. */
  1829. case LINUX_SIGIO:
  1830. return GDB_SIGNAL_IO;
  1831. case LINUX_SIGPWR:
  1832. return GDB_SIGNAL_PWR;
  1833. case LINUX_SIGSYS:
  1834. return GDB_SIGNAL_SYS;
  1835. /* SIGRTMIN and SIGRTMAX are not continuous in <gdb/signals.def>,
  1836. therefore we have to handle them here. */
  1837. case LINUX_SIGRTMIN:
  1838. return GDB_SIGNAL_REALTIME_32;
  1839. case LINUX_SIGRTMAX:
  1840. return GDB_SIGNAL_REALTIME_64;
  1841. }
  1842. if (signal >= LINUX_SIGRTMIN + 1 && signal <= LINUX_SIGRTMAX - 1)
  1843. {
  1844. int offset = signal - LINUX_SIGRTMIN + 1;
  1845. return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_33 + offset);
  1846. }
  1847. return GDB_SIGNAL_UNKNOWN;
  1848. }
  1849. /* Implementation of `gdbarch_gdb_signal_to_target', as defined in
  1850. gdbarch.h. This function is not static because it is exported to
  1851. other -tdep files. */
  1852. int
  1853. linux_gdb_signal_to_target (struct gdbarch *gdbarch,
  1854. enum gdb_signal signal)
  1855. {
  1856. switch (signal)
  1857. {
  1858. case GDB_SIGNAL_0:
  1859. return 0;
  1860. case GDB_SIGNAL_HUP:
  1861. return LINUX_SIGHUP;
  1862. case GDB_SIGNAL_INT:
  1863. return LINUX_SIGINT;
  1864. case GDB_SIGNAL_QUIT:
  1865. return LINUX_SIGQUIT;
  1866. case GDB_SIGNAL_ILL:
  1867. return LINUX_SIGILL;
  1868. case GDB_SIGNAL_TRAP:
  1869. return LINUX_SIGTRAP;
  1870. case GDB_SIGNAL_ABRT:
  1871. return LINUX_SIGABRT;
  1872. case GDB_SIGNAL_FPE:
  1873. return LINUX_SIGFPE;
  1874. case GDB_SIGNAL_KILL:
  1875. return LINUX_SIGKILL;
  1876. case GDB_SIGNAL_BUS:
  1877. return LINUX_SIGBUS;
  1878. case GDB_SIGNAL_SEGV:
  1879. return LINUX_SIGSEGV;
  1880. case GDB_SIGNAL_SYS:
  1881. return LINUX_SIGSYS;
  1882. case GDB_SIGNAL_PIPE:
  1883. return LINUX_SIGPIPE;
  1884. case GDB_SIGNAL_ALRM:
  1885. return LINUX_SIGALRM;
  1886. case GDB_SIGNAL_TERM:
  1887. return LINUX_SIGTERM;
  1888. case GDB_SIGNAL_URG:
  1889. return LINUX_SIGURG;
  1890. case GDB_SIGNAL_STOP:
  1891. return LINUX_SIGSTOP;
  1892. case GDB_SIGNAL_TSTP:
  1893. return LINUX_SIGTSTP;
  1894. case GDB_SIGNAL_CONT:
  1895. return LINUX_SIGCONT;
  1896. case GDB_SIGNAL_CHLD:
  1897. return LINUX_SIGCHLD;
  1898. case GDB_SIGNAL_TTIN:
  1899. return LINUX_SIGTTIN;
  1900. case GDB_SIGNAL_TTOU:
  1901. return LINUX_SIGTTOU;
  1902. case GDB_SIGNAL_IO:
  1903. return LINUX_SIGIO;
  1904. case GDB_SIGNAL_XCPU:
  1905. return LINUX_SIGXCPU;
  1906. case GDB_SIGNAL_XFSZ:
  1907. return LINUX_SIGXFSZ;
  1908. case GDB_SIGNAL_VTALRM:
  1909. return LINUX_SIGVTALRM;
  1910. case GDB_SIGNAL_PROF:
  1911. return LINUX_SIGPROF;
  1912. case GDB_SIGNAL_WINCH:
  1913. return LINUX_SIGWINCH;
  1914. case GDB_SIGNAL_USR1:
  1915. return LINUX_SIGUSR1;
  1916. case GDB_SIGNAL_USR2:
  1917. return LINUX_SIGUSR2;
  1918. case GDB_SIGNAL_PWR:
  1919. return LINUX_SIGPWR;
  1920. case GDB_SIGNAL_POLL:
  1921. return LINUX_SIGPOLL;
  1922. /* GDB_SIGNAL_REALTIME_32 is not continuous in <gdb/signals.def>,
  1923. therefore we have to handle it here. */
  1924. case GDB_SIGNAL_REALTIME_32:
  1925. return LINUX_SIGRTMIN;
  1926. /* Same comment applies to _64. */
  1927. case GDB_SIGNAL_REALTIME_64:
  1928. return LINUX_SIGRTMAX;
  1929. }
  1930. /* GDB_SIGNAL_REALTIME_33 to _64 are continuous. */
  1931. if (signal >= GDB_SIGNAL_REALTIME_33
  1932. && signal <= GDB_SIGNAL_REALTIME_63)
  1933. {
  1934. int offset = signal - GDB_SIGNAL_REALTIME_33;
  1935. return LINUX_SIGRTMIN + 1 + offset;
  1936. }
  1937. return -1;
  1938. }
  1939. /* Helper for linux_vsyscall_range that does the real work of finding
  1940. the vsyscall's address range. */
  1941. static int
  1942. linux_vsyscall_range_raw (struct gdbarch *gdbarch, struct mem_range *range)
  1943. {
  1944. char filename[100];
  1945. long pid;
  1946. if (target_auxv_search (current_inferior ()->top_target (),
  1947. AT_SYSINFO_EHDR, &range->start) <= 0)
  1948. return 0;
  1949. /* It doesn't make sense to access the host's /proc when debugging a
  1950. core file. Instead, look for the PT_LOAD segment that matches
  1951. the vDSO. */
  1952. if (!target_has_execution ())
  1953. {
  1954. long phdrs_size;
  1955. int num_phdrs, i;
  1956. phdrs_size = bfd_get_elf_phdr_upper_bound (core_bfd);
  1957. if (phdrs_size == -1)
  1958. return 0;
  1959. gdb::unique_xmalloc_ptr<Elf_Internal_Phdr>
  1960. phdrs ((Elf_Internal_Phdr *) xmalloc (phdrs_size));
  1961. num_phdrs = bfd_get_elf_phdrs (core_bfd, phdrs.get ());
  1962. if (num_phdrs == -1)
  1963. return 0;
  1964. for (i = 0; i < num_phdrs; i++)
  1965. if (phdrs.get ()[i].p_type == PT_LOAD
  1966. && phdrs.get ()[i].p_vaddr == range->start)
  1967. {
  1968. range->length = phdrs.get ()[i].p_memsz;
  1969. return 1;
  1970. }
  1971. return 0;
  1972. }
  1973. /* We need to know the real target PID to access /proc. */
  1974. if (current_inferior ()->fake_pid_p)
  1975. return 0;
  1976. pid = current_inferior ()->pid;
  1977. /* Note that reading /proc/PID/task/PID/maps (1) is much faster than
  1978. reading /proc/PID/maps (2). The later identifies thread stacks
  1979. in the output, which requires scanning every thread in the thread
  1980. group to check whether a VMA is actually a thread's stack. With
  1981. Linux 4.4 on an Intel i7-4810MQ @ 2.80GHz, with an inferior with
  1982. a few thousand threads, (1) takes a few miliseconds, while (2)
  1983. takes several seconds. Also note that "smaps", what we read for
  1984. determining core dump mappings, is even slower than "maps". */
  1985. xsnprintf (filename, sizeof filename, "/proc/%ld/task/%ld/maps", pid, pid);
  1986. gdb::unique_xmalloc_ptr<char> data
  1987. = target_fileio_read_stralloc (NULL, filename);
  1988. if (data != NULL)
  1989. {
  1990. char *line;
  1991. char *saveptr = NULL;
  1992. for (line = strtok_r (data.get (), "\n", &saveptr);
  1993. line != NULL;
  1994. line = strtok_r (NULL, "\n", &saveptr))
  1995. {
  1996. ULONGEST addr, endaddr;
  1997. const char *p = line;
  1998. addr = strtoulst (p, &p, 16);
  1999. if (addr == range->start)
  2000. {
  2001. if (*p == '-')
  2002. p++;
  2003. endaddr = strtoulst (p, &p, 16);
  2004. range->length = endaddr - addr;
  2005. return 1;
  2006. }
  2007. }
  2008. }
  2009. else
  2010. warning (_("unable to open /proc file '%s'"), filename);
  2011. return 0;
  2012. }
  2013. /* Implementation of the "vsyscall_range" gdbarch hook. Handles
  2014. caching, and defers the real work to linux_vsyscall_range_raw. */
  2015. static int
  2016. linux_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
  2017. {
  2018. struct linux_info *info = get_linux_inferior_data (current_inferior ());
  2019. if (info->vsyscall_range_p == 0)
  2020. {
  2021. if (linux_vsyscall_range_raw (gdbarch, &info->vsyscall_range))
  2022. info->vsyscall_range_p = 1;
  2023. else
  2024. info->vsyscall_range_p = -1;
  2025. }
  2026. if (info->vsyscall_range_p < 0)
  2027. return 0;
  2028. *range = info->vsyscall_range;
  2029. return 1;
  2030. }
  2031. /* Symbols for linux_infcall_mmap's ARG_FLAGS; their Linux MAP_* system
  2032. definitions would be dependent on compilation host. */
  2033. #define GDB_MMAP_MAP_PRIVATE 0x02 /* Changes are private. */
  2034. #define GDB_MMAP_MAP_ANONYMOUS 0x20 /* Don't use a file. */
  2035. /* See gdbarch.sh 'infcall_mmap'. */
  2036. static CORE_ADDR
  2037. linux_infcall_mmap (CORE_ADDR size, unsigned prot)
  2038. {
  2039. struct objfile *objf;
  2040. /* Do there still exist any Linux systems without "mmap64"?
  2041. "mmap" uses 64-bit off_t on x86_64 and 32-bit off_t on i386 and x32. */
  2042. struct value *mmap_val = find_function_in_inferior ("mmap64", &objf);
  2043. struct value *addr_val;
  2044. struct gdbarch *gdbarch = objf->arch ();
  2045. CORE_ADDR retval;
  2046. enum
  2047. {
  2048. ARG_ADDR, ARG_LENGTH, ARG_PROT, ARG_FLAGS, ARG_FD, ARG_OFFSET, ARG_LAST
  2049. };
  2050. struct value *arg[ARG_LAST];
  2051. arg[ARG_ADDR] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr,
  2052. 0);
  2053. /* Assuming sizeof (unsigned long) == sizeof (size_t). */
  2054. arg[ARG_LENGTH] = value_from_ulongest
  2055. (builtin_type (gdbarch)->builtin_unsigned_long, size);
  2056. gdb_assert ((prot & ~(GDB_MMAP_PROT_READ | GDB_MMAP_PROT_WRITE
  2057. | GDB_MMAP_PROT_EXEC))
  2058. == 0);
  2059. arg[ARG_PROT] = value_from_longest (builtin_type (gdbarch)->builtin_int, prot);
  2060. arg[ARG_FLAGS] = value_from_longest (builtin_type (gdbarch)->builtin_int,
  2061. GDB_MMAP_MAP_PRIVATE
  2062. | GDB_MMAP_MAP_ANONYMOUS);
  2063. arg[ARG_FD] = value_from_longest (builtin_type (gdbarch)->builtin_int, -1);
  2064. arg[ARG_OFFSET] = value_from_longest (builtin_type (gdbarch)->builtin_int64,
  2065. 0);
  2066. addr_val = call_function_by_hand (mmap_val, NULL, arg);
  2067. retval = value_as_address (addr_val);
  2068. if (retval == (CORE_ADDR) -1)
  2069. error (_("Failed inferior mmap call for %s bytes, errno is changed."),
  2070. pulongest (size));
  2071. return retval;
  2072. }
  2073. /* See gdbarch.sh 'infcall_munmap'. */
  2074. static void
  2075. linux_infcall_munmap (CORE_ADDR addr, CORE_ADDR size)
  2076. {
  2077. struct objfile *objf;
  2078. struct value *munmap_val = find_function_in_inferior ("munmap", &objf);
  2079. struct value *retval_val;
  2080. struct gdbarch *gdbarch = objf->arch ();
  2081. LONGEST retval;
  2082. enum
  2083. {
  2084. ARG_ADDR, ARG_LENGTH, ARG_LAST
  2085. };
  2086. struct value *arg[ARG_LAST];
  2087. arg[ARG_ADDR] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr,
  2088. addr);
  2089. /* Assuming sizeof (unsigned long) == sizeof (size_t). */
  2090. arg[ARG_LENGTH] = value_from_ulongest
  2091. (builtin_type (gdbarch)->builtin_unsigned_long, size);
  2092. retval_val = call_function_by_hand (munmap_val, NULL, arg);
  2093. retval = value_as_long (retval_val);
  2094. if (retval != 0)
  2095. warning (_("Failed inferior munmap call at %s for %s bytes, "
  2096. "errno is changed."),
  2097. hex_string (addr), pulongest (size));
  2098. }
  2099. /* See linux-tdep.h. */
  2100. CORE_ADDR
  2101. linux_displaced_step_location (struct gdbarch *gdbarch)
  2102. {
  2103. CORE_ADDR addr;
  2104. int bp_len;
  2105. /* Determine entry point from target auxiliary vector. This avoids
  2106. the need for symbols. Also, when debugging a stand-alone SPU
  2107. executable, entry_point_address () will point to an SPU
  2108. local-store address and is thus not usable as displaced stepping
  2109. location. The auxiliary vector gets us the PowerPC-side entry
  2110. point address instead. */
  2111. if (target_auxv_search (current_inferior ()->top_target (),
  2112. AT_ENTRY, &addr) <= 0)
  2113. throw_error (NOT_SUPPORTED_ERROR,
  2114. _("Cannot find AT_ENTRY auxiliary vector entry."));
  2115. /* Make certain that the address points at real code, and not a
  2116. function descriptor. */
  2117. addr = gdbarch_convert_from_func_ptr_addr
  2118. (gdbarch, addr, current_inferior ()->top_target ());
  2119. /* Inferior calls also use the entry point as a breakpoint location.
  2120. We don't want displaced stepping to interfere with those
  2121. breakpoints, so leave space. */
  2122. gdbarch_breakpoint_from_pc (gdbarch, &addr, &bp_len);
  2123. addr += bp_len * 2;
  2124. return addr;
  2125. }
  2126. /* See linux-tdep.h. */
  2127. displaced_step_prepare_status
  2128. linux_displaced_step_prepare (gdbarch *arch, thread_info *thread,
  2129. CORE_ADDR &displaced_pc)
  2130. {
  2131. linux_info *per_inferior = get_linux_inferior_data (thread->inf);
  2132. if (!per_inferior->disp_step_bufs.has_value ())
  2133. {
  2134. /* Figure out the location of the buffers. They are contiguous, starting
  2135. at DISP_STEP_BUF_ADDR. They are all of size BUF_LEN. */
  2136. CORE_ADDR disp_step_buf_addr
  2137. = linux_displaced_step_location (thread->inf->gdbarch);
  2138. int buf_len = gdbarch_max_insn_length (arch);
  2139. linux_gdbarch_data *gdbarch_data = get_linux_gdbarch_data (arch);
  2140. gdb_assert (gdbarch_data->num_disp_step_buffers > 0);
  2141. std::vector<CORE_ADDR> buffers;
  2142. for (int i = 0; i < gdbarch_data->num_disp_step_buffers; i++)
  2143. buffers.push_back (disp_step_buf_addr + i * buf_len);
  2144. per_inferior->disp_step_bufs.emplace (buffers);
  2145. }
  2146. return per_inferior->disp_step_bufs->prepare (thread, displaced_pc);
  2147. }
  2148. /* See linux-tdep.h. */
  2149. displaced_step_finish_status
  2150. linux_displaced_step_finish (gdbarch *arch, thread_info *thread, gdb_signal sig)
  2151. {
  2152. linux_info *per_inferior = get_linux_inferior_data (thread->inf);
  2153. gdb_assert (per_inferior->disp_step_bufs.has_value ());
  2154. return per_inferior->disp_step_bufs->finish (arch, thread, sig);
  2155. }
  2156. /* See linux-tdep.h. */
  2157. const displaced_step_copy_insn_closure *
  2158. linux_displaced_step_copy_insn_closure_by_addr (inferior *inf, CORE_ADDR addr)
  2159. {
  2160. linux_info *per_inferior = linux_inferior_data.get (inf);
  2161. if (per_inferior == nullptr
  2162. || !per_inferior->disp_step_bufs.has_value ())
  2163. return nullptr;
  2164. return per_inferior->disp_step_bufs->copy_insn_closure_by_addr (addr);
  2165. }
  2166. /* See linux-tdep.h. */
  2167. void
  2168. linux_displaced_step_restore_all_in_ptid (inferior *parent_inf, ptid_t ptid)
  2169. {
  2170. linux_info *per_inferior = linux_inferior_data.get (parent_inf);
  2171. if (per_inferior == nullptr
  2172. || !per_inferior->disp_step_bufs.has_value ())
  2173. return;
  2174. per_inferior->disp_step_bufs->restore_in_ptid (ptid);
  2175. }
  2176. /* See linux-tdep.h. */
  2177. CORE_ADDR
  2178. linux_get_hwcap (struct target_ops *target)
  2179. {
  2180. CORE_ADDR field;
  2181. if (target_auxv_search (target, AT_HWCAP, &field) != 1)
  2182. return 0;
  2183. return field;
  2184. }
  2185. /* See linux-tdep.h. */
  2186. CORE_ADDR
  2187. linux_get_hwcap2 (struct target_ops *target)
  2188. {
  2189. CORE_ADDR field;
  2190. if (target_auxv_search (target, AT_HWCAP2, &field) != 1)
  2191. return 0;
  2192. return field;
  2193. }
  2194. /* Display whether the gcore command is using the
  2195. /proc/PID/coredump_filter file. */
  2196. static void
  2197. show_use_coredump_filter (struct ui_file *file, int from_tty,
  2198. struct cmd_list_element *c, const char *value)
  2199. {
  2200. gdb_printf (file, _("Use of /proc/PID/coredump_filter file to generate"
  2201. " corefiles is %s.\n"), value);
  2202. }
  2203. /* Display whether the gcore command is dumping mappings marked with
  2204. the VM_DONTDUMP flag. */
  2205. static void
  2206. show_dump_excluded_mappings (struct ui_file *file, int from_tty,
  2207. struct cmd_list_element *c, const char *value)
  2208. {
  2209. gdb_printf (file, _("Dumping of mappings marked with the VM_DONTDUMP"
  2210. " flag is %s.\n"), value);
  2211. }
  2212. /* To be called from the various GDB_OSABI_LINUX handlers for the
  2213. various GNU/Linux architectures and machine types.
  2214. NUM_DISP_STEP_BUFFERS is the number of displaced step buffers to use. If 0,
  2215. displaced stepping is not supported. */
  2216. void
  2217. linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
  2218. int num_disp_step_buffers)
  2219. {
  2220. if (num_disp_step_buffers > 0)
  2221. {
  2222. linux_gdbarch_data *gdbarch_data = get_linux_gdbarch_data (gdbarch);
  2223. gdbarch_data->num_disp_step_buffers = num_disp_step_buffers;
  2224. set_gdbarch_displaced_step_prepare (gdbarch,
  2225. linux_displaced_step_prepare);
  2226. set_gdbarch_displaced_step_finish (gdbarch, linux_displaced_step_finish);
  2227. set_gdbarch_displaced_step_copy_insn_closure_by_addr
  2228. (gdbarch, linux_displaced_step_copy_insn_closure_by_addr);
  2229. set_gdbarch_displaced_step_restore_all_in_ptid
  2230. (gdbarch, linux_displaced_step_restore_all_in_ptid);
  2231. }
  2232. set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str);
  2233. set_gdbarch_info_proc (gdbarch, linux_info_proc);
  2234. set_gdbarch_core_info_proc (gdbarch, linux_core_info_proc);
  2235. set_gdbarch_core_xfer_siginfo (gdbarch, linux_core_xfer_siginfo);
  2236. set_gdbarch_read_core_file_mappings (gdbarch, linux_read_core_file_mappings);
  2237. set_gdbarch_find_memory_regions (gdbarch, linux_find_memory_regions);
  2238. set_gdbarch_make_corefile_notes (gdbarch, linux_make_corefile_notes);
  2239. set_gdbarch_has_shared_address_space (gdbarch,
  2240. linux_has_shared_address_space);
  2241. set_gdbarch_gdb_signal_from_target (gdbarch,
  2242. linux_gdb_signal_from_target);
  2243. set_gdbarch_gdb_signal_to_target (gdbarch,
  2244. linux_gdb_signal_to_target);
  2245. set_gdbarch_vsyscall_range (gdbarch, linux_vsyscall_range);
  2246. set_gdbarch_infcall_mmap (gdbarch, linux_infcall_mmap);
  2247. set_gdbarch_infcall_munmap (gdbarch, linux_infcall_munmap);
  2248. set_gdbarch_get_siginfo_type (gdbarch, linux_get_siginfo_type);
  2249. }
  2250. void _initialize_linux_tdep ();
  2251. void
  2252. _initialize_linux_tdep ()
  2253. {
  2254. linux_gdbarch_data_handle =
  2255. gdbarch_data_register_pre_init (init_linux_gdbarch_data);
  2256. /* Observers used to invalidate the cache when needed. */
  2257. gdb::observers::inferior_exit.attach (invalidate_linux_cache_inf,
  2258. "linux-tdep");
  2259. gdb::observers::inferior_appeared.attach (invalidate_linux_cache_inf,
  2260. "linux-tdep");
  2261. gdb::observers::inferior_execd.attach (invalidate_linux_cache_inf,
  2262. "linux-tdep");
  2263. add_setshow_boolean_cmd ("use-coredump-filter", class_files,
  2264. &use_coredump_filter, _("\
  2265. Set whether gcore should consider /proc/PID/coredump_filter."),
  2266. _("\
  2267. Show whether gcore should consider /proc/PID/coredump_filter."),
  2268. _("\
  2269. Use this command to set whether gcore should consider the contents\n\
  2270. of /proc/PID/coredump_filter when generating the corefile. For more information\n\
  2271. about this file, refer to the manpage of core(5)."),
  2272. NULL, show_use_coredump_filter,
  2273. &setlist, &showlist);
  2274. add_setshow_boolean_cmd ("dump-excluded-mappings", class_files,
  2275. &dump_excluded_mappings, _("\
  2276. Set whether gcore should dump mappings marked with the VM_DONTDUMP flag."),
  2277. _("\
  2278. Show whether gcore should dump mappings marked with the VM_DONTDUMP flag."),
  2279. _("\
  2280. Use this command to set whether gcore should dump mappings marked with the\n\
  2281. VM_DONTDUMP flag (\"dd\" in /proc/PID/smaps) when generating the corefile. For\n\
  2282. more information about this file, refer to the manpage of proc(5) and core(5)."),
  2283. NULL, show_dump_excluded_mappings,
  2284. &setlist, &showlist);
  2285. }
  2286. /* Fetch (and possibly build) an appropriate `link_map_offsets' for
  2287. ILP32/LP64 Linux systems which don't have the r_ldsomap field. */
  2288. link_map_offsets *
  2289. linux_ilp32_fetch_link_map_offsets ()
  2290. {
  2291. static link_map_offsets lmo;
  2292. static link_map_offsets *lmp = nullptr;
  2293. if (lmp == nullptr)
  2294. {
  2295. lmp = &lmo;
  2296. lmo.r_version_offset = 0;
  2297. lmo.r_version_size = 4;
  2298. lmo.r_map_offset = 4;
  2299. lmo.r_brk_offset = 8;
  2300. lmo.r_ldsomap_offset = -1;
  2301. /* Everything we need is in the first 20 bytes. */
  2302. lmo.link_map_size = 20;
  2303. lmo.l_addr_offset = 0;
  2304. lmo.l_name_offset = 4;
  2305. lmo.l_ld_offset = 8;
  2306. lmo.l_next_offset = 12;
  2307. lmo.l_prev_offset = 16;
  2308. }
  2309. return lmp;
  2310. }
  2311. link_map_offsets *
  2312. linux_lp64_fetch_link_map_offsets ()
  2313. {
  2314. static link_map_offsets lmo;
  2315. static link_map_offsets *lmp = nullptr;
  2316. if (lmp == nullptr)
  2317. {
  2318. lmp = &lmo;
  2319. lmo.r_version_offset = 0;
  2320. lmo.r_version_size = 4;
  2321. lmo.r_map_offset = 8;
  2322. lmo.r_brk_offset = 16;
  2323. lmo.r_ldsomap_offset = -1;
  2324. /* Everything we need is in the first 40 bytes. */
  2325. lmo.link_map_size = 40;
  2326. lmo.l_addr_offset = 0;
  2327. lmo.l_name_offset = 8;
  2328. lmo.l_ld_offset = 16;
  2329. lmo.l_next_offset = 24;
  2330. lmo.l_prev_offset = 32;
  2331. }
  2332. return lmp;
  2333. }