linux-thread-db.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  1. /* libthread_db assisted debugging support, generic parts.
  2. Copyright (C) 1999-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 <dlfcn.h>
  16. #include "gdb_proc_service.h"
  17. #include "nat/gdb_thread_db.h"
  18. #include "gdbsupport/gdb_vecs.h"
  19. #include "bfd.h"
  20. #include "command.h"
  21. #include "gdbcmd.h"
  22. #include "gdbthread.h"
  23. #include "inferior.h"
  24. #include "infrun.h"
  25. #include "symfile.h"
  26. #include "objfiles.h"
  27. #include "target.h"
  28. #include "regcache.h"
  29. #include "solib.h"
  30. #include "solib-svr4.h"
  31. #include "gdbcore.h"
  32. #include "observable.h"
  33. #include "linux-nat.h"
  34. #include "nat/linux-procfs.h"
  35. #include "nat/linux-ptrace.h"
  36. #include "nat/linux-osdata.h"
  37. #include "auto-load.h"
  38. #include "cli/cli-utils.h"
  39. #include <signal.h>
  40. #include <ctype.h>
  41. #include "nat/linux-namespaces.h"
  42. #include <algorithm>
  43. #include "gdbsupport/pathstuff.h"
  44. #include "valprint.h"
  45. #include "cli/cli-style.h"
  46. /* GNU/Linux libthread_db support.
  47. libthread_db is a library, provided along with libpthread.so, which
  48. exposes the internals of the thread library to a debugger. It
  49. allows GDB to find existing threads, new threads as they are
  50. created, thread IDs (usually, the result of pthread_self), and
  51. thread-local variables.
  52. The libthread_db interface originates on Solaris, where it is both
  53. more powerful and more complicated. This implementation only works
  54. for NPTL, the glibc threading library. It assumes that each thread
  55. is permanently assigned to a single light-weight process (LWP). At
  56. some point it also supported the older LinuxThreads library, but it
  57. no longer does.
  58. libthread_db-specific information is stored in the "private" field
  59. of struct thread_info. When the field is NULL we do not yet have
  60. information about the new thread; this could be temporary (created,
  61. but the thread library's data structures do not reflect it yet)
  62. or permanent (created using clone instead of pthread_create).
  63. Process IDs managed by linux-thread-db.c match those used by
  64. linux-nat.c: a common PID for all processes, an LWP ID for each
  65. thread, and no TID. We save the TID in private. Keeping it out
  66. of the ptid_t prevents thread IDs changing when libpthread is
  67. loaded or unloaded. */
  68. static const target_info thread_db_target_info = {
  69. "multi-thread",
  70. N_("multi-threaded child process."),
  71. N_("Threads and pthreads support.")
  72. };
  73. class thread_db_target final : public target_ops
  74. {
  75. public:
  76. const target_info &info () const override
  77. { return thread_db_target_info; }
  78. strata stratum () const override { return thread_stratum; }
  79. void detach (inferior *, int) override;
  80. ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
  81. void resume (ptid_t, int, enum gdb_signal) override;
  82. void mourn_inferior () override;
  83. void follow_exec (inferior *, ptid_t, const char *) override;
  84. void update_thread_list () override;
  85. std::string pid_to_str (ptid_t) override;
  86. CORE_ADDR get_thread_local_address (ptid_t ptid,
  87. CORE_ADDR load_module_addr,
  88. CORE_ADDR offset) override;
  89. const char *extra_thread_info (struct thread_info *) override;
  90. ptid_t get_ada_task_ptid (long lwp, ULONGEST thread) override;
  91. thread_info *thread_handle_to_thread_info (const gdb_byte *thread_handle,
  92. int handle_len,
  93. inferior *inf) override;
  94. gdb::byte_vector thread_info_to_thread_handle (struct thread_info *) override;
  95. };
  96. static std::string libthread_db_search_path = LIBTHREAD_DB_SEARCH_PATH;
  97. /* Set to true if thread_db auto-loading is enabled
  98. by the "set auto-load libthread-db" command. */
  99. static bool auto_load_thread_db = true;
  100. /* Set to true if load-time libthread_db tests have been enabled
  101. by the "maintenance set check-libthread-db" command. */
  102. static bool check_thread_db_on_load = false;
  103. /* "show" command for the auto_load_thread_db configuration variable. */
  104. static void
  105. show_auto_load_thread_db (struct ui_file *file, int from_tty,
  106. struct cmd_list_element *c, const char *value)
  107. {
  108. gdb_printf (file, _("Auto-loading of inferior specific libthread_db "
  109. "is %s.\n"),
  110. value);
  111. }
  112. static void
  113. set_libthread_db_search_path (const char *ignored, int from_tty,
  114. struct cmd_list_element *c)
  115. {
  116. if (libthread_db_search_path.empty ())
  117. libthread_db_search_path = LIBTHREAD_DB_SEARCH_PATH;
  118. }
  119. /* If non-zero, print details of libthread_db processing. */
  120. static unsigned int libthread_db_debug;
  121. static void
  122. show_libthread_db_debug (struct ui_file *file, int from_tty,
  123. struct cmd_list_element *c, const char *value)
  124. {
  125. gdb_printf (file, _("libthread-db debugging is %s.\n"), value);
  126. }
  127. /* If we're running on GNU/Linux, we must explicitly attach to any new
  128. threads. */
  129. /* This module's target vector. */
  130. static thread_db_target the_thread_db_target;
  131. /* Non-zero if we have determined the signals used by the threads
  132. library. */
  133. static int thread_signals;
  134. struct thread_db_info
  135. {
  136. struct thread_db_info *next;
  137. /* The target this thread_db_info is bound to. */
  138. process_stratum_target *process_target;
  139. /* Process id this object refers to. */
  140. int pid;
  141. /* Handle from dlopen for libthread_db.so. */
  142. void *handle;
  143. /* Absolute pathname from gdb_realpath to disk file used for dlopen-ing
  144. HANDLE. It may be NULL for system library. */
  145. char *filename;
  146. /* Structure that identifies the child process for the
  147. <proc_service.h> interface. */
  148. struct ps_prochandle proc_handle;
  149. /* Connection to the libthread_db library. */
  150. td_thragent_t *thread_agent;
  151. /* True if we need to apply the workaround for glibc/BZ5983. When
  152. we catch a PTRACE_O_TRACEFORK, and go query the child's thread
  153. list, nptl_db returns the parent's threads in addition to the new
  154. (single) child thread. If this flag is set, we do extra work to
  155. be able to ignore such stale entries. */
  156. int need_stale_parent_threads_check;
  157. /* Pointers to the libthread_db functions. */
  158. td_init_ftype *td_init_p;
  159. td_ta_new_ftype *td_ta_new_p;
  160. td_ta_delete_ftype *td_ta_delete_p;
  161. td_ta_map_lwp2thr_ftype *td_ta_map_lwp2thr_p;
  162. td_ta_thr_iter_ftype *td_ta_thr_iter_p;
  163. td_thr_get_info_ftype *td_thr_get_info_p;
  164. td_thr_tls_get_addr_ftype *td_thr_tls_get_addr_p;
  165. td_thr_tlsbase_ftype *td_thr_tlsbase_p;
  166. };
  167. /* List of known processes using thread_db, and the required
  168. bookkeeping. */
  169. static thread_db_info *thread_db_list;
  170. static void thread_db_find_new_threads_1 (thread_info *stopped);
  171. static void thread_db_find_new_threads_2 (thread_info *stopped,
  172. bool until_no_new);
  173. static void check_thread_signals (void);
  174. static struct thread_info *record_thread
  175. (struct thread_db_info *info, struct thread_info *tp,
  176. ptid_t ptid, const td_thrhandle_t *th_p, const td_thrinfo_t *ti_p);
  177. /* Add the current inferior to the list of processes using libpthread.
  178. Return a pointer to the newly allocated object that was added to
  179. THREAD_DB_LIST. HANDLE is the handle returned by dlopen'ing
  180. LIBTHREAD_DB_SO. */
  181. static struct thread_db_info *
  182. add_thread_db_info (void *handle)
  183. {
  184. struct thread_db_info *info = XCNEW (struct thread_db_info);
  185. info->process_target = current_inferior ()->process_target ();
  186. info->pid = inferior_ptid.pid ();
  187. info->handle = handle;
  188. /* The workaround works by reading from /proc/pid/status, so it is
  189. disabled for core files. */
  190. if (target_has_execution ())
  191. info->need_stale_parent_threads_check = 1;
  192. info->next = thread_db_list;
  193. thread_db_list = info;
  194. return info;
  195. }
  196. /* Return the thread_db_info object representing the bookkeeping
  197. related to process PID, if any; NULL otherwise. */
  198. static struct thread_db_info *
  199. get_thread_db_info (process_stratum_target *targ, int pid)
  200. {
  201. struct thread_db_info *info;
  202. for (info = thread_db_list; info; info = info->next)
  203. if (targ == info->process_target && pid == info->pid)
  204. return info;
  205. return NULL;
  206. }
  207. static const char *thread_db_err_str (td_err_e err);
  208. /* When PID has exited or has been detached, we no longer want to keep
  209. track of it as using libpthread. Call this function to discard
  210. thread_db related info related to PID. Note that this closes
  211. LIBTHREAD_DB_SO's dlopen'ed handle. */
  212. static void
  213. delete_thread_db_info (process_stratum_target *targ, int pid)
  214. {
  215. struct thread_db_info *info, *info_prev;
  216. info_prev = NULL;
  217. for (info = thread_db_list; info; info_prev = info, info = info->next)
  218. if (targ == info->process_target && pid == info->pid)
  219. break;
  220. if (info == NULL)
  221. return;
  222. if (info->thread_agent != NULL && info->td_ta_delete_p != NULL)
  223. {
  224. td_err_e err = info->td_ta_delete_p (info->thread_agent);
  225. if (err != TD_OK)
  226. warning (_("Cannot deregister process %d from libthread_db: %s"),
  227. pid, thread_db_err_str (err));
  228. info->thread_agent = NULL;
  229. }
  230. if (info->handle != NULL)
  231. dlclose (info->handle);
  232. xfree (info->filename);
  233. if (info_prev)
  234. info_prev->next = info->next;
  235. else
  236. thread_db_list = info->next;
  237. xfree (info);
  238. }
  239. /* Use "struct private_thread_info" to cache thread state. This is
  240. a substantial optimization. */
  241. struct thread_db_thread_info : public private_thread_info
  242. {
  243. /* Flag set when we see a TD_DEATH event for this thread. */
  244. bool dying = false;
  245. /* Cached thread state. */
  246. td_thrhandle_t th {};
  247. thread_t tid {};
  248. };
  249. static thread_db_thread_info *
  250. get_thread_db_thread_info (thread_info *thread)
  251. {
  252. return static_cast<thread_db_thread_info *> (thread->priv.get ());
  253. }
  254. static const char *
  255. thread_db_err_str (td_err_e err)
  256. {
  257. static char buf[64];
  258. switch (err)
  259. {
  260. case TD_OK:
  261. return "generic 'call succeeded'";
  262. case TD_ERR:
  263. return "generic error";
  264. case TD_NOTHR:
  265. return "no thread to satisfy query";
  266. case TD_NOSV:
  267. return "no sync handle to satisfy query";
  268. case TD_NOLWP:
  269. return "no LWP to satisfy query";
  270. case TD_BADPH:
  271. return "invalid process handle";
  272. case TD_BADTH:
  273. return "invalid thread handle";
  274. case TD_BADSH:
  275. return "invalid synchronization handle";
  276. case TD_BADTA:
  277. return "invalid thread agent";
  278. case TD_BADKEY:
  279. return "invalid key";
  280. case TD_NOMSG:
  281. return "no event message for getmsg";
  282. case TD_NOFPREGS:
  283. return "FPU register set not available";
  284. case TD_NOLIBTHREAD:
  285. return "application not linked with libthread";
  286. case TD_NOEVENT:
  287. return "requested event is not supported";
  288. case TD_NOCAPAB:
  289. return "capability not available";
  290. case TD_DBERR:
  291. return "debugger service failed";
  292. case TD_NOAPLIC:
  293. return "operation not applicable to";
  294. case TD_NOTSD:
  295. return "no thread-specific data for this thread";
  296. case TD_MALLOC:
  297. return "malloc failed";
  298. case TD_PARTIALREG:
  299. return "only part of register set was written/read";
  300. case TD_NOXREGS:
  301. return "X register set not available for this thread";
  302. #ifdef THREAD_DB_HAS_TD_NOTALLOC
  303. case TD_NOTALLOC:
  304. return "thread has not yet allocated TLS for given module";
  305. #endif
  306. #ifdef THREAD_DB_HAS_TD_VERSION
  307. case TD_VERSION:
  308. return "versions of libpthread and libthread_db do not match";
  309. #endif
  310. #ifdef THREAD_DB_HAS_TD_NOTLS
  311. case TD_NOTLS:
  312. return "there is no TLS segment in the given module";
  313. #endif
  314. default:
  315. snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
  316. return buf;
  317. }
  318. }
  319. /* Fetch the user-level thread id of PTID. STOPPED is a stopped
  320. thread that we can use to access memory. */
  321. static struct thread_info *
  322. thread_from_lwp (thread_info *stopped, ptid_t ptid)
  323. {
  324. td_thrhandle_t th;
  325. td_thrinfo_t ti;
  326. td_err_e err;
  327. struct thread_db_info *info;
  328. struct thread_info *tp;
  329. /* Just in case td_ta_map_lwp2thr doesn't initialize it completely. */
  330. th.th_unique = 0;
  331. /* This ptid comes from linux-nat.c, which should always fill in the
  332. LWP. */
  333. gdb_assert (ptid.lwp () != 0);
  334. info = get_thread_db_info (stopped->inf->process_target (), ptid.pid ());
  335. /* Access an lwp we know is stopped. */
  336. info->proc_handle.thread = stopped;
  337. err = info->td_ta_map_lwp2thr_p (info->thread_agent, ptid.lwp (),
  338. &th);
  339. if (err != TD_OK)
  340. error (_("Cannot find user-level thread for LWP %ld: %s"),
  341. ptid.lwp (), thread_db_err_str (err));
  342. err = info->td_thr_get_info_p (&th, &ti);
  343. if (err != TD_OK)
  344. error (_("thread_get_info_callback: cannot get thread info: %s"),
  345. thread_db_err_str (err));
  346. /* Fill the cache. */
  347. tp = find_thread_ptid (stopped->inf->process_target (), ptid);
  348. return record_thread (info, tp, ptid, &th, &ti);
  349. }
  350. /* See linux-nat.h. */
  351. int
  352. thread_db_notice_clone (ptid_t parent, ptid_t child)
  353. {
  354. struct thread_db_info *info;
  355. info = get_thread_db_info (linux_target, child.pid ());
  356. if (info == NULL)
  357. return 0;
  358. thread_info *stopped = find_thread_ptid (linux_target, parent);
  359. thread_from_lwp (stopped, child);
  360. /* If we do not know about the main thread's pthread info yet, this
  361. would be a good time to find it. */
  362. thread_from_lwp (stopped, parent);
  363. return 1;
  364. }
  365. static void *
  366. verbose_dlsym (void *handle, const char *name)
  367. {
  368. void *sym = dlsym (handle, name);
  369. if (sym == NULL)
  370. warning (_("Symbol \"%s\" not found in libthread_db: %s"),
  371. name, dlerror ());
  372. return sym;
  373. }
  374. /* Verify inferior's '\0'-terminated symbol VER_SYMBOL starts with "%d.%d" and
  375. return 1 if this version is lower (and not equal) to
  376. VER_MAJOR_MIN.VER_MINOR_MIN. Return 0 in all other cases. */
  377. static int
  378. inferior_has_bug (const char *ver_symbol, int ver_major_min, int ver_minor_min)
  379. {
  380. struct bound_minimal_symbol version_msym;
  381. CORE_ADDR version_addr;
  382. int got, retval = 0;
  383. version_msym = lookup_minimal_symbol (ver_symbol, NULL, NULL);
  384. if (version_msym.minsym == NULL)
  385. return 0;
  386. version_addr = BMSYMBOL_VALUE_ADDRESS (version_msym);
  387. gdb::unique_xmalloc_ptr<char> version
  388. = target_read_string (version_addr, 32, &got);
  389. if (version != nullptr
  390. && memchr (version.get (), 0, got) == version.get () + got - 1)
  391. {
  392. int major, minor;
  393. retval = (sscanf (version.get (), "%d.%d", &major, &minor) == 2
  394. && (major < ver_major_min
  395. || (major == ver_major_min && minor < ver_minor_min)));
  396. }
  397. return retval;
  398. }
  399. /* Similar as thread_db_find_new_threads_1, but try to silently ignore errors
  400. if appropriate.
  401. Return 1 if the caller should abort libthread_db initialization. Return 0
  402. otherwise. */
  403. static int
  404. thread_db_find_new_threads_silently (thread_info *stopped)
  405. {
  406. try
  407. {
  408. thread_db_find_new_threads_2 (stopped, true);
  409. }
  410. catch (const gdb_exception_error &except)
  411. {
  412. if (libthread_db_debug)
  413. exception_fprintf (gdb_stdlog, except,
  414. "Warning: thread_db_find_new_threads_silently: ");
  415. /* There is a bug fixed between nptl 2.6.1 and 2.7 by
  416. commit 7d9d8bd18906fdd17364f372b160d7ab896ce909
  417. where calls to td_thr_get_info fail with TD_ERR for statically linked
  418. executables if td_thr_get_info is called before glibc has initialized
  419. itself.
  420. If the nptl bug is NOT present in the inferior and still thread_db
  421. reports an error return 1. It means the inferior has corrupted thread
  422. list and GDB should fall back only to LWPs.
  423. If the nptl bug is present in the inferior return 0 to silently ignore
  424. such errors, and let gdb enumerate threads again later. In such case
  425. GDB cannot properly display LWPs if the inferior thread list is
  426. corrupted. For core files it does not apply, no 'later enumeration'
  427. is possible. */
  428. if (!target_has_execution () || !inferior_has_bug ("nptl_version", 2, 7))
  429. {
  430. exception_fprintf (gdb_stderr, except,
  431. _("Warning: couldn't activate thread debugging "
  432. "using libthread_db: "));
  433. return 1;
  434. }
  435. }
  436. return 0;
  437. }
  438. /* Lookup a library in which given symbol resides.
  439. Note: this is looking in GDB process, not in the inferior.
  440. Returns library name, or NULL. */
  441. static const char *
  442. dladdr_to_soname (const void *addr)
  443. {
  444. Dl_info info;
  445. if (dladdr (addr, &info) != 0)
  446. return info.dli_fname;
  447. return NULL;
  448. }
  449. /* State for check_thread_db_callback. */
  450. struct check_thread_db_info
  451. {
  452. /* The libthread_db under test. */
  453. struct thread_db_info *info;
  454. /* True if progress should be logged. */
  455. bool log_progress;
  456. /* True if the callback was called. */
  457. bool threads_seen;
  458. /* Name of last libthread_db function called. */
  459. const char *last_call;
  460. /* Value returned by last libthread_db call. */
  461. td_err_e last_result;
  462. };
  463. static struct check_thread_db_info *tdb_testinfo;
  464. /* Callback for check_thread_db. */
  465. static int
  466. check_thread_db_callback (const td_thrhandle_t *th, void *arg)
  467. {
  468. gdb_assert (tdb_testinfo != NULL);
  469. tdb_testinfo->threads_seen = true;
  470. #define LOG(fmt, args...) \
  471. do \
  472. { \
  473. if (tdb_testinfo->log_progress) \
  474. { \
  475. debug_printf (fmt, ## args); \
  476. gdb_flush (gdb_stdlog); \
  477. } \
  478. } \
  479. while (0)
  480. #define CHECK_1(expr, args...) \
  481. do \
  482. { \
  483. if (!(expr)) \
  484. { \
  485. LOG (" ... FAIL!\n"); \
  486. error (args); \
  487. } \
  488. } \
  489. while (0)
  490. #define CHECK(expr) \
  491. CHECK_1 (expr, "(%s) == false", #expr)
  492. #define CALL_UNCHECKED(func, args...) \
  493. do \
  494. { \
  495. tdb_testinfo->last_call = #func; \
  496. tdb_testinfo->last_result \
  497. = tdb_testinfo->info->func ## _p (args); \
  498. } \
  499. while (0)
  500. #define CHECK_CALL() \
  501. CHECK_1 (tdb_testinfo->last_result == TD_OK, \
  502. _("%s failed: %s"), \
  503. tdb_testinfo->last_call, \
  504. thread_db_err_str (tdb_testinfo->last_result)) \
  505. #define CALL(func, args...) \
  506. do \
  507. { \
  508. CALL_UNCHECKED (func, args); \
  509. CHECK_CALL (); \
  510. } \
  511. while (0)
  512. LOG (" Got thread");
  513. /* Check td_ta_thr_iter passed consistent arguments. */
  514. CHECK (th != NULL);
  515. CHECK (arg == (void *) tdb_testinfo);
  516. CHECK (th->th_ta_p == tdb_testinfo->info->thread_agent);
  517. LOG (" %s", core_addr_to_string_nz ((CORE_ADDR) th->th_unique));
  518. /* Check td_thr_get_info. */
  519. td_thrinfo_t ti;
  520. CALL (td_thr_get_info, th, &ti);
  521. LOG (" => %d", ti.ti_lid);
  522. CHECK (ti.ti_ta_p == th->th_ta_p);
  523. CHECK (ti.ti_tid == (thread_t) th->th_unique);
  524. /* Check td_ta_map_lwp2thr. */
  525. td_thrhandle_t th2;
  526. memset (&th2, 23, sizeof (td_thrhandle_t));
  527. CALL_UNCHECKED (td_ta_map_lwp2thr, th->th_ta_p, ti.ti_lid, &th2);
  528. if (tdb_testinfo->last_result == TD_ERR && !target_has_execution ())
  529. {
  530. /* Some platforms require execution for td_ta_map_lwp2thr. */
  531. LOG (_("; can't map_lwp2thr"));
  532. }
  533. else
  534. {
  535. CHECK_CALL ();
  536. LOG (" => %s", core_addr_to_string_nz ((CORE_ADDR) th2.th_unique));
  537. CHECK (memcmp (th, &th2, sizeof (td_thrhandle_t)) == 0);
  538. }
  539. /* Attempt TLS access. Assuming errno is TLS, this calls
  540. thread_db_get_thread_local_address, which in turn calls
  541. td_thr_tls_get_addr for live inferiors or td_thr_tlsbase
  542. for core files. This test is skipped if the thread has
  543. not been recorded; proceeding in that case would result
  544. in the test having the side-effect of noticing threads
  545. which seems wrong.
  546. Note that in glibc's libthread_db td_thr_tls_get_addr is
  547. a thin wrapper around td_thr_tlsbase; this check always
  548. hits the bulk of the code.
  549. Note also that we don't actually check any libthread_db
  550. calls are made, we just assume they were; future changes
  551. to how GDB accesses TLS could result in this passing
  552. without exercising the calls it's supposed to. */
  553. ptid_t ptid = ptid_t (tdb_testinfo->info->pid, ti.ti_lid);
  554. thread_info *thread_info = find_thread_ptid (linux_target, ptid);
  555. if (thread_info != NULL && thread_info->priv != NULL)
  556. {
  557. LOG ("; errno");
  558. scoped_restore_current_thread restore_current_thread;
  559. switch_to_thread (thread_info);
  560. expression_up expr = parse_expression ("(int) errno");
  561. struct value *val = evaluate_expression (expr.get ());
  562. if (tdb_testinfo->log_progress)
  563. {
  564. struct value_print_options opts;
  565. get_user_print_options (&opts);
  566. LOG (" = ");
  567. value_print (val, gdb_stdlog, &opts);
  568. }
  569. }
  570. LOG (" ... OK\n");
  571. #undef LOG
  572. #undef CHECK_1
  573. #undef CHECK
  574. #undef CALL_UNCHECKED
  575. #undef CHECK_CALL
  576. #undef CALL
  577. return 0;
  578. }
  579. /* Run integrity checks on the dlopen()ed libthread_db described by
  580. INFO. Returns true on success, displays a warning and returns
  581. false on failure. Logs progress messages to gdb_stdlog during
  582. the test if LOG_PROGRESS is true. */
  583. static bool
  584. check_thread_db (struct thread_db_info *info, bool log_progress)
  585. {
  586. bool test_passed = true;
  587. if (log_progress)
  588. debug_printf (_("Running libthread_db integrity checks:\n"));
  589. /* GDB avoids using td_ta_thr_iter wherever possible (see comment
  590. in try_thread_db_load_1 below) so in order to test it we may
  591. have to locate it ourselves. */
  592. td_ta_thr_iter_ftype *td_ta_thr_iter_p = info->td_ta_thr_iter_p;
  593. if (td_ta_thr_iter_p == NULL)
  594. {
  595. void *thr_iter = verbose_dlsym (info->handle, "td_ta_thr_iter");
  596. if (thr_iter == NULL)
  597. return 0;
  598. td_ta_thr_iter_p = (td_ta_thr_iter_ftype *) thr_iter;
  599. }
  600. /* Set up the test state we share with the callback. */
  601. gdb_assert (tdb_testinfo == NULL);
  602. struct check_thread_db_info tdb_testinfo_buf;
  603. tdb_testinfo = &tdb_testinfo_buf;
  604. memset (tdb_testinfo, 0, sizeof (struct check_thread_db_info));
  605. tdb_testinfo->info = info;
  606. tdb_testinfo->log_progress = log_progress;
  607. /* td_ta_thr_iter shouldn't be used on running processes. Note that
  608. it's possible the inferior will stop midway through modifying one
  609. of its thread lists, in which case the check will spuriously
  610. fail. */
  611. linux_stop_and_wait_all_lwps ();
  612. try
  613. {
  614. td_err_e err = td_ta_thr_iter_p (info->thread_agent,
  615. check_thread_db_callback,
  616. tdb_testinfo,
  617. TD_THR_ANY_STATE,
  618. TD_THR_LOWEST_PRIORITY,
  619. TD_SIGNO_MASK,
  620. TD_THR_ANY_USER_FLAGS);
  621. if (err != TD_OK)
  622. error (_("td_ta_thr_iter failed: %s"), thread_db_err_str (err));
  623. if (!tdb_testinfo->threads_seen)
  624. error (_("no threads seen"));
  625. }
  626. catch (const gdb_exception_error &except)
  627. {
  628. if (warning_pre_print)
  629. gdb_puts (warning_pre_print, gdb_stderr);
  630. exception_fprintf (gdb_stderr, except,
  631. _("libthread_db integrity checks failed: "));
  632. test_passed = false;
  633. }
  634. if (test_passed && log_progress)
  635. debug_printf (_("libthread_db integrity checks passed.\n"));
  636. tdb_testinfo = NULL;
  637. linux_unstop_all_lwps ();
  638. return test_passed;
  639. }
  640. /* Predicate which tests whether objfile OBJ refers to the library
  641. containing pthread related symbols. Historically, this library has
  642. been named in such a way that looking for "libpthread" in the name
  643. was sufficient to identify it. As of glibc-2.34, the C library
  644. (libc) contains the thread library symbols. Therefore we check
  645. that the name matches a possible thread library, but we also check
  646. that it contains at least one of the symbols (pthread_create) that
  647. we'd expect to find in the thread library. */
  648. static bool
  649. libpthread_objfile_p (objfile *obj)
  650. {
  651. return (libpthread_name_p (objfile_name (obj))
  652. && lookup_minimal_symbol ("pthread_create",
  653. NULL,
  654. obj).minsym != NULL);
  655. }
  656. /* Attempt to initialize dlopen()ed libthread_db, described by INFO.
  657. Return true on success.
  658. Failure could happen if libthread_db does not have symbols we expect,
  659. or when it refuses to work with the current inferior (e.g. due to
  660. version mismatch between libthread_db and libpthread). */
  661. static bool
  662. try_thread_db_load_1 (struct thread_db_info *info)
  663. {
  664. td_err_e err;
  665. /* Initialize pointers to the dynamic library functions we will use.
  666. Essential functions first. */
  667. #define TDB_VERBOSE_DLSYM(info, func) \
  668. info->func ## _p = (func ## _ftype *) verbose_dlsym (info->handle, #func)
  669. #define TDB_DLSYM(info, func) \
  670. info->func ## _p = (func ## _ftype *) dlsym (info->handle, #func)
  671. #define CHK(a) \
  672. do \
  673. { \
  674. if ((a) == NULL) \
  675. return false; \
  676. } while (0)
  677. CHK (TDB_VERBOSE_DLSYM (info, td_init));
  678. err = info->td_init_p ();
  679. if (err != TD_OK)
  680. {
  681. warning (_("Cannot initialize libthread_db: %s"),
  682. thread_db_err_str (err));
  683. return false;
  684. }
  685. CHK (TDB_VERBOSE_DLSYM (info, td_ta_new));
  686. /* Initialize the structure that identifies the child process. */
  687. info->proc_handle.thread = inferior_thread ();
  688. /* Now attempt to open a connection to the thread library. */
  689. err = info->td_ta_new_p (&info->proc_handle, &info->thread_agent);
  690. if (err != TD_OK)
  691. {
  692. if (libthread_db_debug)
  693. gdb_printf (gdb_stdlog, _("td_ta_new failed: %s\n"),
  694. thread_db_err_str (err));
  695. else
  696. switch (err)
  697. {
  698. case TD_NOLIBTHREAD:
  699. #ifdef THREAD_DB_HAS_TD_VERSION
  700. case TD_VERSION:
  701. #endif
  702. /* The errors above are not unexpected and silently ignored:
  703. they just mean we haven't found correct version of
  704. libthread_db yet. */
  705. break;
  706. default:
  707. warning (_("td_ta_new failed: %s"), thread_db_err_str (err));
  708. }
  709. return false;
  710. }
  711. /* These are essential. */
  712. CHK (TDB_VERBOSE_DLSYM (info, td_ta_map_lwp2thr));
  713. CHK (TDB_VERBOSE_DLSYM (info, td_thr_get_info));
  714. /* These are not essential. */
  715. TDB_DLSYM (info, td_thr_tls_get_addr);
  716. TDB_DLSYM (info, td_thr_tlsbase);
  717. TDB_DLSYM (info, td_ta_delete);
  718. /* It's best to avoid td_ta_thr_iter if possible. That walks data
  719. structures in the inferior's address space that may be corrupted,
  720. or, if the target is running, may change while we walk them. If
  721. there's execution (and /proc is mounted), then we're already
  722. attached to all LWPs. Use thread_from_lwp, which uses
  723. td_ta_map_lwp2thr instead, which does not walk the thread list.
  724. td_ta_map_lwp2thr uses ps_get_thread_area, but we can't use that
  725. currently on core targets, as it uses ptrace directly. */
  726. if (target_has_execution ()
  727. && linux_proc_task_list_dir_exists (inferior_ptid.pid ()))
  728. info->td_ta_thr_iter_p = NULL;
  729. else
  730. CHK (TDB_VERBOSE_DLSYM (info, td_ta_thr_iter));
  731. #undef TDB_VERBOSE_DLSYM
  732. #undef TDB_DLSYM
  733. #undef CHK
  734. /* Run integrity checks if requested. */
  735. if (check_thread_db_on_load)
  736. {
  737. if (!check_thread_db (info, libthread_db_debug))
  738. return false;
  739. }
  740. if (info->td_ta_thr_iter_p == NULL)
  741. {
  742. int pid = inferior_ptid.pid ();
  743. thread_info *curr_thread = inferior_thread ();
  744. linux_stop_and_wait_all_lwps ();
  745. for (const lwp_info *lp : all_lwps ())
  746. if (lp->ptid.pid () == pid)
  747. thread_from_lwp (curr_thread, lp->ptid);
  748. linux_unstop_all_lwps ();
  749. }
  750. else if (thread_db_find_new_threads_silently (inferior_thread ()) != 0)
  751. {
  752. /* Even if libthread_db initializes, if the thread list is
  753. corrupted, we'd not manage to list any threads. Better reject this
  754. thread_db, and fall back to at least listing LWPs. */
  755. return false;
  756. }
  757. gdb_printf (_("[Thread debugging using libthread_db enabled]\n"));
  758. if (!libthread_db_search_path.empty () || libthread_db_debug)
  759. {
  760. const char *library;
  761. library = dladdr_to_soname ((const void *) *info->td_ta_new_p);
  762. if (library == NULL)
  763. library = LIBTHREAD_DB_SO;
  764. gdb_printf (_("Using host libthread_db library \"%ps\".\n"),
  765. styled_string (file_name_style.style (), library));
  766. }
  767. /* The thread library was detected. Activate the thread_db target
  768. for this process. */
  769. current_inferior ()->push_target (&the_thread_db_target);
  770. return true;
  771. }
  772. /* Attempt to use LIBRARY as libthread_db. LIBRARY could be absolute,
  773. relative, or just LIBTHREAD_DB. */
  774. static bool
  775. try_thread_db_load (const char *library, bool check_auto_load_safe)
  776. {
  777. void *handle;
  778. struct thread_db_info *info;
  779. if (libthread_db_debug)
  780. gdb_printf (gdb_stdlog,
  781. _("Trying host libthread_db library: %s.\n"),
  782. library);
  783. if (check_auto_load_safe)
  784. {
  785. if (access (library, R_OK) != 0)
  786. {
  787. /* Do not print warnings by file_is_auto_load_safe if the library does
  788. not exist at this place. */
  789. if (libthread_db_debug)
  790. gdb_printf (gdb_stdlog, _("open failed: %s.\n"),
  791. safe_strerror (errno));
  792. return false;
  793. }
  794. auto_load_debug_printf
  795. ("Loading libthread-db library \"%s\" from explicit directory.",
  796. library);
  797. if (!file_is_auto_load_safe (library))
  798. return false;
  799. }
  800. handle = dlopen (library, RTLD_NOW);
  801. if (handle == NULL)
  802. {
  803. if (libthread_db_debug)
  804. gdb_printf (gdb_stdlog, _("dlopen failed: %s.\n"), dlerror ());
  805. return false;
  806. }
  807. if (libthread_db_debug && strchr (library, '/') == NULL)
  808. {
  809. void *td_init;
  810. td_init = dlsym (handle, "td_init");
  811. if (td_init != NULL)
  812. {
  813. const char *const libpath = dladdr_to_soname (td_init);
  814. if (libpath != NULL)
  815. gdb_printf (gdb_stdlog, _("Host %s resolved to: %s.\n"),
  816. library, libpath);
  817. }
  818. }
  819. info = add_thread_db_info (handle);
  820. /* Do not save system library name, that one is always trusted. */
  821. if (strchr (library, '/') != NULL)
  822. info->filename = gdb_realpath (library).release ();
  823. try
  824. {
  825. if (try_thread_db_load_1 (info))
  826. return true;
  827. }
  828. catch (const gdb_exception_error &except)
  829. {
  830. if (libthread_db_debug)
  831. exception_fprintf (gdb_stdlog, except,
  832. "Warning: While trying to load libthread_db: ");
  833. }
  834. /* This library "refused" to work on current inferior. */
  835. delete_thread_db_info (current_inferior ()->process_target (),
  836. inferior_ptid.pid ());
  837. return false;
  838. }
  839. /* Subroutine of try_thread_db_load_from_pdir to simplify it.
  840. Try loading libthread_db in directory(OBJ)/SUBDIR.
  841. SUBDIR may be NULL. It may also be something like "../lib64".
  842. The result is true for success. */
  843. static bool
  844. try_thread_db_load_from_pdir_1 (struct objfile *obj, const char *subdir)
  845. {
  846. const char *obj_name = objfile_name (obj);
  847. if (obj_name[0] != '/')
  848. {
  849. warning (_("Expected absolute pathname for libpthread in the"
  850. " inferior, but got %ps."),
  851. styled_string (file_name_style.style (), obj_name));
  852. return false;
  853. }
  854. std::string path = obj_name;
  855. size_t cp = path.rfind ('/');
  856. /* This should at minimum hit the first character. */
  857. gdb_assert (cp != std::string::npos);
  858. path.resize (cp + 1);
  859. if (subdir != NULL)
  860. path = path + subdir + "/";
  861. path += LIBTHREAD_DB_SO;
  862. return try_thread_db_load (path.c_str (), true);
  863. }
  864. /* Handle $pdir in libthread-db-search-path.
  865. Look for libthread_db in directory(libpthread)/SUBDIR.
  866. SUBDIR may be NULL. It may also be something like "../lib64".
  867. The result is true for success. */
  868. static bool
  869. try_thread_db_load_from_pdir (const char *subdir)
  870. {
  871. if (!auto_load_thread_db)
  872. return false;
  873. for (objfile *obj : current_program_space->objfiles ())
  874. if (libpthread_objfile_p (obj))
  875. {
  876. if (try_thread_db_load_from_pdir_1 (obj, subdir))
  877. return true;
  878. /* We may have found the separate-debug-info version of
  879. libpthread, and it may live in a directory without a matching
  880. libthread_db. */
  881. if (obj->separate_debug_objfile_backlink != NULL)
  882. return try_thread_db_load_from_pdir_1 (obj->separate_debug_objfile_backlink,
  883. subdir);
  884. return false;
  885. }
  886. return false;
  887. }
  888. /* Handle $sdir in libthread-db-search-path.
  889. Look for libthread_db in the system dirs, or wherever a plain
  890. dlopen(file_without_path) will look.
  891. The result is true for success. */
  892. static bool
  893. try_thread_db_load_from_sdir (void)
  894. {
  895. return try_thread_db_load (LIBTHREAD_DB_SO, false);
  896. }
  897. /* Try to load libthread_db from directory DIR of length DIR_LEN.
  898. The result is true for success. */
  899. static bool
  900. try_thread_db_load_from_dir (const char *dir, size_t dir_len)
  901. {
  902. if (!auto_load_thread_db)
  903. return false;
  904. std::string path = std::string (dir, dir_len) + "/" + LIBTHREAD_DB_SO;
  905. return try_thread_db_load (path.c_str (), true);
  906. }
  907. /* Search libthread_db_search_path for libthread_db which "agrees"
  908. to work on current inferior.
  909. The result is true for success. */
  910. static bool
  911. thread_db_load_search (void)
  912. {
  913. bool rc = false;
  914. std::vector<gdb::unique_xmalloc_ptr<char>> dir_vec
  915. = dirnames_to_char_ptr_vec (libthread_db_search_path.c_str ());
  916. for (const gdb::unique_xmalloc_ptr<char> &this_dir_up : dir_vec)
  917. {
  918. const char *this_dir = this_dir_up.get ();
  919. const int pdir_len = sizeof ("$pdir") - 1;
  920. size_t this_dir_len;
  921. this_dir_len = strlen (this_dir);
  922. if (strncmp (this_dir, "$pdir", pdir_len) == 0
  923. && (this_dir[pdir_len] == '\0'
  924. || this_dir[pdir_len] == '/'))
  925. {
  926. const char *subdir = NULL;
  927. std::string subdir_holder;
  928. if (this_dir[pdir_len] == '/')
  929. {
  930. subdir_holder = std::string (this_dir + pdir_len + 1);
  931. subdir = subdir_holder.c_str ();
  932. }
  933. rc = try_thread_db_load_from_pdir (subdir);
  934. if (rc)
  935. break;
  936. }
  937. else if (strcmp (this_dir, "$sdir") == 0)
  938. {
  939. if (try_thread_db_load_from_sdir ())
  940. {
  941. rc = 1;
  942. break;
  943. }
  944. }
  945. else
  946. {
  947. if (try_thread_db_load_from_dir (this_dir, this_dir_len))
  948. {
  949. rc = 1;
  950. break;
  951. }
  952. }
  953. }
  954. if (libthread_db_debug)
  955. gdb_printf (gdb_stdlog,
  956. _("thread_db_load_search returning %d\n"), rc);
  957. return rc;
  958. }
  959. /* Return true if the inferior has a libpthread. */
  960. static bool
  961. has_libpthread (void)
  962. {
  963. for (objfile *obj : current_program_space->objfiles ())
  964. if (libpthread_objfile_p (obj))
  965. return true;
  966. return false;
  967. }
  968. /* Attempt to load and initialize libthread_db.
  969. Return 1 on success. */
  970. static bool
  971. thread_db_load (void)
  972. {
  973. inferior *inf = current_inferior ();
  974. /* When attaching / handling fork child, don't try loading libthread_db
  975. until we know about all shared libraries. */
  976. if (inf->in_initial_library_scan)
  977. return false;
  978. thread_db_info *info = get_thread_db_info (inf->process_target (),
  979. inferior_ptid.pid ());
  980. if (info != NULL)
  981. return true;
  982. /* Don't attempt to use thread_db on executables not running
  983. yet. */
  984. if (!target_has_registers ())
  985. return false;
  986. /* Don't attempt to use thread_db for remote targets. */
  987. if (!(target_can_run () || core_bfd))
  988. return false;
  989. if (thread_db_load_search ())
  990. return true;
  991. /* We couldn't find a libthread_db.
  992. If the inferior has a libpthread warn the user. */
  993. if (has_libpthread ())
  994. {
  995. warning (_("Unable to find libthread_db matching inferior's thread"
  996. " library, thread debugging will not be available."));
  997. return false;
  998. }
  999. /* Either this executable isn't using libpthread at all, or it is
  1000. statically linked. Since we can't easily distinguish these two cases,
  1001. no warning is issued. */
  1002. return false;
  1003. }
  1004. static void
  1005. check_thread_signals (void)
  1006. {
  1007. if (!thread_signals)
  1008. {
  1009. int i;
  1010. for (i = 0; i < lin_thread_get_thread_signal_num (); i++)
  1011. {
  1012. int sig = lin_thread_get_thread_signal (i);
  1013. signal_stop_update (gdb_signal_from_host (sig), 0);
  1014. signal_print_update (gdb_signal_from_host (sig), 0);
  1015. thread_signals = 1;
  1016. }
  1017. }
  1018. }
  1019. /* Check whether thread_db is usable. This function is called when
  1020. an inferior is created (or otherwise acquired, e.g. attached to)
  1021. and when new shared libraries are loaded into a running process. */
  1022. static void
  1023. check_for_thread_db (void)
  1024. {
  1025. /* Do nothing if we couldn't load libthread_db.so.1. */
  1026. if (!thread_db_load ())
  1027. return;
  1028. }
  1029. /* This function is called via the new_objfile observer. */
  1030. static void
  1031. thread_db_new_objfile (struct objfile *objfile)
  1032. {
  1033. /* This observer must always be called with inferior_ptid set
  1034. correctly. */
  1035. if (objfile != NULL
  1036. /* libpthread with separate debug info has its debug info file already
  1037. loaded (and notified without successful thread_db initialization)
  1038. the time gdb::observers::new_objfile.notify is called for the library itself.
  1039. Static executables have their separate debug info loaded already
  1040. before the inferior has started. */
  1041. && objfile->separate_debug_objfile_backlink == NULL
  1042. /* Only check for thread_db if we loaded libpthread,
  1043. or if this is the main symbol file.
  1044. We need to check OBJF_MAINLINE to handle the case of debugging
  1045. a statically linked executable AND the symbol file is specified AFTER
  1046. the exec file is loaded (e.g., gdb -c core ; file foo).
  1047. For dynamically linked executables, libpthread can be near the end
  1048. of the list of shared libraries to load, and in an app of several
  1049. thousand shared libraries, this can otherwise be painful. */
  1050. && ((objfile->flags & OBJF_MAINLINE) != 0
  1051. || libpthread_objfile_p (objfile)))
  1052. check_for_thread_db ();
  1053. }
  1054. static void
  1055. check_pid_namespace_match (inferior *inf)
  1056. {
  1057. /* Check is only relevant for local targets targets. */
  1058. if (target_can_run ())
  1059. {
  1060. /* If the child is in a different PID namespace, its idea of its
  1061. PID will differ from our idea of its PID. When we scan the
  1062. child's thread list, we'll mistakenly think it has no threads
  1063. since the thread PID fields won't match the PID we give to
  1064. libthread_db. */
  1065. if (!linux_ns_same (inf->pid, LINUX_NS_PID))
  1066. {
  1067. warning (_ ("Target and debugger are in different PID "
  1068. "namespaces; thread lists and other data are "
  1069. "likely unreliable. "
  1070. "Connect to gdbserver inside the container."));
  1071. }
  1072. }
  1073. }
  1074. /* This function is called via the inferior_created observer.
  1075. This handles the case of debugging statically linked executables. */
  1076. static void
  1077. thread_db_inferior_created (inferior *inf)
  1078. {
  1079. check_pid_namespace_match (inf);
  1080. check_for_thread_db ();
  1081. }
  1082. /* Update the thread's state (what's displayed in "info threads"),
  1083. from libthread_db thread state information. */
  1084. static void
  1085. update_thread_state (thread_db_thread_info *priv,
  1086. const td_thrinfo_t *ti_p)
  1087. {
  1088. priv->dying = (ti_p->ti_state == TD_THR_UNKNOWN
  1089. || ti_p->ti_state == TD_THR_ZOMBIE);
  1090. }
  1091. /* Record a new thread in GDB's thread list. Creates the thread's
  1092. private info. If TP is NULL or TP is marked as having exited,
  1093. creates a new thread. Otherwise, uses TP. */
  1094. static struct thread_info *
  1095. record_thread (struct thread_db_info *info,
  1096. struct thread_info *tp,
  1097. ptid_t ptid, const td_thrhandle_t *th_p,
  1098. const td_thrinfo_t *ti_p)
  1099. {
  1100. /* A thread ID of zero may mean the thread library has not
  1101. initialized yet. Leave private == NULL until the thread library
  1102. has initialized. */
  1103. if (ti_p->ti_tid == 0)
  1104. return tp;
  1105. /* Construct the thread's private data. */
  1106. thread_db_thread_info *priv = new thread_db_thread_info;
  1107. priv->th = *th_p;
  1108. priv->tid = ti_p->ti_tid;
  1109. update_thread_state (priv, ti_p);
  1110. /* Add the thread to GDB's thread list. If we already know about a
  1111. thread with this PTID, but it's marked exited, then the kernel
  1112. reused the tid of an old thread. */
  1113. if (tp == NULL || tp->state == THREAD_EXITED)
  1114. tp = add_thread_with_info (info->process_target, ptid, priv);
  1115. else
  1116. tp->priv.reset (priv);
  1117. if (target_has_execution ())
  1118. check_thread_signals ();
  1119. return tp;
  1120. }
  1121. void
  1122. thread_db_target::detach (inferior *inf, int from_tty)
  1123. {
  1124. delete_thread_db_info (inf->process_target (), inf->pid);
  1125. beneath ()->detach (inf, from_tty);
  1126. /* NOTE: From this point on, inferior_ptid is null_ptid. */
  1127. /* Detach the thread_db target from this inferior. */
  1128. inf->unpush_target (this);
  1129. }
  1130. ptid_t
  1131. thread_db_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
  1132. target_wait_flags options)
  1133. {
  1134. struct thread_db_info *info;
  1135. process_stratum_target *beneath
  1136. = as_process_stratum_target (this->beneath ());
  1137. ptid = beneath->wait (ptid, ourstatus, options);
  1138. switch (ourstatus->kind ())
  1139. {
  1140. case TARGET_WAITKIND_IGNORE:
  1141. case TARGET_WAITKIND_EXITED:
  1142. case TARGET_WAITKIND_THREAD_EXITED:
  1143. case TARGET_WAITKIND_SIGNALLED:
  1144. case TARGET_WAITKIND_EXECD:
  1145. return ptid;
  1146. }
  1147. info = get_thread_db_info (beneath, ptid.pid ());
  1148. /* If this process isn't using thread_db, we're done. */
  1149. if (info == NULL)
  1150. return ptid;
  1151. /* Fill in the thread's user-level thread id and status. */
  1152. thread_from_lwp (find_thread_ptid (beneath, ptid), ptid);
  1153. return ptid;
  1154. }
  1155. void
  1156. thread_db_target::mourn_inferior ()
  1157. {
  1158. process_stratum_target *target_beneath
  1159. = as_process_stratum_target (this->beneath ());
  1160. delete_thread_db_info (target_beneath, inferior_ptid.pid ());
  1161. target_beneath->mourn_inferior ();
  1162. /* Detach the thread_db target from this inferior. */
  1163. current_inferior ()->unpush_target (this);
  1164. }
  1165. void
  1166. thread_db_target::follow_exec (inferior *follow_inf, ptid_t ptid,
  1167. const char *execd_pathname)
  1168. {
  1169. process_stratum_target *beneath
  1170. = as_process_stratum_target (this->beneath ());
  1171. delete_thread_db_info (beneath, ptid.pid ());
  1172. current_inferior ()->unpush_target (this);
  1173. beneath->follow_exec (follow_inf, ptid, execd_pathname);
  1174. }
  1175. struct callback_data
  1176. {
  1177. struct thread_db_info *info;
  1178. int new_threads;
  1179. };
  1180. static int
  1181. find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
  1182. {
  1183. td_thrinfo_t ti;
  1184. td_err_e err;
  1185. struct thread_info *tp;
  1186. struct callback_data *cb_data = (struct callback_data *) data;
  1187. struct thread_db_info *info = cb_data->info;
  1188. err = info->td_thr_get_info_p (th_p, &ti);
  1189. if (err != TD_OK)
  1190. error (_("find_new_threads_callback: cannot get thread info: %s"),
  1191. thread_db_err_str (err));
  1192. if (ti.ti_lid == -1)
  1193. {
  1194. /* A thread with kernel thread ID -1 is either a thread that
  1195. exited and was joined, or a thread that is being created but
  1196. hasn't started yet, and that is reusing the tcb/stack of a
  1197. thread that previously exited and was joined. (glibc marks
  1198. terminated and joined threads with kernel thread ID -1. See
  1199. glibc PR17707. */
  1200. if (libthread_db_debug)
  1201. gdb_printf (gdb_stdlog,
  1202. "thread_db: skipping exited and "
  1203. "joined thread (0x%lx)\n",
  1204. (unsigned long) ti.ti_tid);
  1205. return 0;
  1206. }
  1207. if (ti.ti_tid == 0)
  1208. {
  1209. /* A thread ID of zero means that this is the main thread, but
  1210. glibc has not yet initialized thread-local storage and the
  1211. pthread library. We do not know what the thread's TID will
  1212. be yet. */
  1213. /* In that case, we're not stopped in a fork syscall and don't
  1214. need this glibc bug workaround. */
  1215. info->need_stale_parent_threads_check = 0;
  1216. return 0;
  1217. }
  1218. /* Ignore stale parent threads, caused by glibc/BZ5983. This is a
  1219. bit expensive, as it needs to open /proc/pid/status, so try to
  1220. avoid doing the work if we know we don't have to. */
  1221. if (info->need_stale_parent_threads_check)
  1222. {
  1223. int tgid = linux_proc_get_tgid (ti.ti_lid);
  1224. if (tgid != -1 && tgid != info->pid)
  1225. return 0;
  1226. }
  1227. ptid_t ptid (info->pid, ti.ti_lid);
  1228. tp = find_thread_ptid (info->process_target, ptid);
  1229. if (tp == NULL || tp->priv == NULL)
  1230. record_thread (info, tp, ptid, th_p, &ti);
  1231. return 0;
  1232. }
  1233. /* Helper for thread_db_find_new_threads_2.
  1234. Returns number of new threads found. */
  1235. static int
  1236. find_new_threads_once (struct thread_db_info *info, int iteration,
  1237. td_err_e *errp)
  1238. {
  1239. struct callback_data data;
  1240. td_err_e err = TD_ERR;
  1241. data.info = info;
  1242. data.new_threads = 0;
  1243. /* See comment in thread_db_update_thread_list. */
  1244. gdb_assert (info->td_ta_thr_iter_p != NULL);
  1245. try
  1246. {
  1247. /* Iterate over all user-space threads to discover new threads. */
  1248. err = info->td_ta_thr_iter_p (info->thread_agent,
  1249. find_new_threads_callback,
  1250. &data,
  1251. TD_THR_ANY_STATE,
  1252. TD_THR_LOWEST_PRIORITY,
  1253. TD_SIGNO_MASK,
  1254. TD_THR_ANY_USER_FLAGS);
  1255. }
  1256. catch (const gdb_exception_error &except)
  1257. {
  1258. if (libthread_db_debug)
  1259. {
  1260. exception_fprintf (gdb_stdlog, except,
  1261. "Warning: find_new_threads_once: ");
  1262. }
  1263. }
  1264. if (libthread_db_debug)
  1265. {
  1266. gdb_printf (gdb_stdlog,
  1267. _("Found %d new threads in iteration %d.\n"),
  1268. data.new_threads, iteration);
  1269. }
  1270. if (errp != NULL)
  1271. *errp = err;
  1272. return data.new_threads;
  1273. }
  1274. /* Search for new threads, accessing memory through stopped thread
  1275. PTID. If UNTIL_NO_NEW is true, repeat searching until several
  1276. searches in a row do not discover any new threads. */
  1277. static void
  1278. thread_db_find_new_threads_2 (thread_info *stopped, bool until_no_new)
  1279. {
  1280. td_err_e err = TD_OK;
  1281. struct thread_db_info *info;
  1282. int i, loop;
  1283. info = get_thread_db_info (stopped->inf->process_target (),
  1284. stopped->ptid.pid ());
  1285. /* Access an lwp we know is stopped. */
  1286. info->proc_handle.thread = stopped;
  1287. if (until_no_new)
  1288. {
  1289. /* Require 4 successive iterations which do not find any new threads.
  1290. The 4 is a heuristic: there is an inherent race here, and I have
  1291. seen that 2 iterations in a row are not always sufficient to
  1292. "capture" all threads. */
  1293. for (i = 0, loop = 0; loop < 4 && err == TD_OK; ++i, ++loop)
  1294. if (find_new_threads_once (info, i, &err) != 0)
  1295. {
  1296. /* Found some new threads. Restart the loop from beginning. */
  1297. loop = -1;
  1298. }
  1299. }
  1300. else
  1301. find_new_threads_once (info, 0, &err);
  1302. if (err != TD_OK)
  1303. error (_("Cannot find new threads: %s"), thread_db_err_str (err));
  1304. }
  1305. static void
  1306. thread_db_find_new_threads_1 (thread_info *stopped)
  1307. {
  1308. thread_db_find_new_threads_2 (stopped, 0);
  1309. }
  1310. /* Implement the to_update_thread_list target method for this
  1311. target. */
  1312. void
  1313. thread_db_target::update_thread_list ()
  1314. {
  1315. struct thread_db_info *info;
  1316. prune_threads ();
  1317. for (inferior *inf : all_inferiors ())
  1318. {
  1319. if (inf->pid == 0)
  1320. continue;
  1321. info = get_thread_db_info (inf->process_target (), inf->pid);
  1322. if (info == NULL)
  1323. continue;
  1324. thread_info *thread = any_live_thread_of_inferior (inf);
  1325. if (thread == NULL || thread->executing ())
  1326. continue;
  1327. /* It's best to avoid td_ta_thr_iter if possible. That walks
  1328. data structures in the inferior's address space that may be
  1329. corrupted, or, if the target is running, the list may change
  1330. while we walk it. In the latter case, it's possible that a
  1331. thread exits just at the exact time that causes GDB to get
  1332. stuck in an infinite loop. To avoid pausing all threads
  1333. whenever the core wants to refresh the thread list, we
  1334. instead use thread_from_lwp immediately when we see an LWP
  1335. stop. That uses thread_db entry points that do not walk
  1336. libpthread's thread list, so should be safe, as well as more
  1337. efficient. */
  1338. if (thread->inf->has_execution ())
  1339. continue;
  1340. thread_db_find_new_threads_1 (thread);
  1341. }
  1342. /* Give the beneath target a chance to do extra processing. */
  1343. this->beneath ()->update_thread_list ();
  1344. }
  1345. std::string
  1346. thread_db_target::pid_to_str (ptid_t ptid)
  1347. {
  1348. thread_info *thread_info = find_thread_ptid (current_inferior (), ptid);
  1349. if (thread_info != NULL && thread_info->priv != NULL)
  1350. {
  1351. thread_db_thread_info *priv = get_thread_db_thread_info (thread_info);
  1352. return string_printf ("Thread 0x%lx (LWP %ld)",
  1353. (unsigned long) priv->tid, ptid.lwp ());
  1354. }
  1355. return beneath ()->pid_to_str (ptid);
  1356. }
  1357. /* Return a string describing the state of the thread specified by
  1358. INFO. */
  1359. const char *
  1360. thread_db_target::extra_thread_info (thread_info *info)
  1361. {
  1362. if (info->priv == NULL)
  1363. return NULL;
  1364. thread_db_thread_info *priv = get_thread_db_thread_info (info);
  1365. if (priv->dying)
  1366. return "Exiting";
  1367. return NULL;
  1368. }
  1369. /* Return pointer to the thread_info struct which corresponds to
  1370. THREAD_HANDLE (having length HANDLE_LEN). */
  1371. thread_info *
  1372. thread_db_target::thread_handle_to_thread_info (const gdb_byte *thread_handle,
  1373. int handle_len,
  1374. inferior *inf)
  1375. {
  1376. thread_t handle_tid;
  1377. /* When debugging a 32-bit target from a 64-bit host, handle_len
  1378. will be 4 and sizeof (handle_tid) will be 8. This requires
  1379. a different cast than the more straightforward case where
  1380. the sizes are the same.
  1381. Use "--target_board unix/-m32" from a native x86_64 linux build
  1382. to test the 32/64-bit case. */
  1383. if (handle_len == 4 && sizeof (handle_tid) == 8)
  1384. handle_tid = (thread_t) * (const uint32_t *) thread_handle;
  1385. else if (handle_len == sizeof (handle_tid))
  1386. handle_tid = * (const thread_t *) thread_handle;
  1387. else
  1388. error (_("Thread handle size mismatch: %d vs %zu (from libthread_db)"),
  1389. handle_len, sizeof (handle_tid));
  1390. for (thread_info *tp : inf->non_exited_threads ())
  1391. {
  1392. thread_db_thread_info *priv = get_thread_db_thread_info (tp);
  1393. if (priv != NULL && handle_tid == priv->tid)
  1394. return tp;
  1395. }
  1396. return NULL;
  1397. }
  1398. /* Return the thread handle associated the thread_info pointer TP. */
  1399. gdb::byte_vector
  1400. thread_db_target::thread_info_to_thread_handle (struct thread_info *tp)
  1401. {
  1402. thread_db_thread_info *priv = get_thread_db_thread_info (tp);
  1403. if (priv == NULL)
  1404. return gdb::byte_vector ();
  1405. int handle_size = sizeof (priv->tid);
  1406. gdb::byte_vector rv (handle_size);
  1407. memcpy (rv.data (), &priv->tid, handle_size);
  1408. return rv;
  1409. }
  1410. /* Get the address of the thread local variable in load module LM which
  1411. is stored at OFFSET within the thread local storage for thread PTID. */
  1412. CORE_ADDR
  1413. thread_db_target::get_thread_local_address (ptid_t ptid,
  1414. CORE_ADDR lm,
  1415. CORE_ADDR offset)
  1416. {
  1417. struct thread_info *thread_info;
  1418. process_stratum_target *beneath
  1419. = as_process_stratum_target (this->beneath ());
  1420. /* Find the matching thread. */
  1421. thread_info = find_thread_ptid (beneath, ptid);
  1422. /* We may not have discovered the thread yet. */
  1423. if (thread_info != NULL && thread_info->priv == NULL)
  1424. thread_info = thread_from_lwp (thread_info, ptid);
  1425. if (thread_info != NULL && thread_info->priv != NULL)
  1426. {
  1427. td_err_e err;
  1428. psaddr_t address;
  1429. thread_db_info *info = get_thread_db_info (beneath, ptid.pid ());
  1430. thread_db_thread_info *priv = get_thread_db_thread_info (thread_info);
  1431. /* Finally, get the address of the variable. */
  1432. if (lm != 0)
  1433. {
  1434. /* glibc doesn't provide the needed interface. */
  1435. if (!info->td_thr_tls_get_addr_p)
  1436. throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR,
  1437. _("No TLS library support"));
  1438. /* Note the cast through uintptr_t: this interface only works if
  1439. a target address fits in a psaddr_t, which is a host pointer.
  1440. So a 32-bit debugger can not access 64-bit TLS through this. */
  1441. err = info->td_thr_tls_get_addr_p (&priv->th,
  1442. (psaddr_t)(uintptr_t) lm,
  1443. offset, &address);
  1444. }
  1445. else
  1446. {
  1447. /* If glibc doesn't provide the needed interface throw an error
  1448. that LM is zero - normally cases it should not be. */
  1449. if (!info->td_thr_tlsbase_p)
  1450. throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
  1451. _("TLS load module not found"));
  1452. /* This code path handles the case of -static -pthread executables:
  1453. https://sourceware.org/ml/libc-help/2014-03/msg00024.html
  1454. For older GNU libc r_debug.r_map is NULL. For GNU libc after
  1455. PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
  1456. The constant number 1 depends on GNU __libc_setup_tls
  1457. initialization of l_tls_modid to 1. */
  1458. err = info->td_thr_tlsbase_p (&priv->th, 1, &address);
  1459. address = (char *) address + offset;
  1460. }
  1461. #ifdef THREAD_DB_HAS_TD_NOTALLOC
  1462. /* The memory hasn't been allocated, yet. */
  1463. if (err == TD_NOTALLOC)
  1464. /* Now, if libthread_db provided the initialization image's
  1465. address, we *could* try to build a non-lvalue value from
  1466. the initialization image. */
  1467. throw_error (TLS_NOT_ALLOCATED_YET_ERROR,
  1468. _("TLS not allocated yet"));
  1469. #endif
  1470. /* Something else went wrong. */
  1471. if (err != TD_OK)
  1472. throw_error (TLS_GENERIC_ERROR,
  1473. (("%s")), thread_db_err_str (err));
  1474. /* Cast assuming host == target. Joy. */
  1475. /* Do proper sign extension for the target. */
  1476. gdb_assert (current_program_space->exec_bfd ());
  1477. return (bfd_get_sign_extend_vma (current_program_space->exec_bfd ()) > 0
  1478. ? (CORE_ADDR) (intptr_t) address
  1479. : (CORE_ADDR) (uintptr_t) address);
  1480. }
  1481. return beneath->get_thread_local_address (ptid, lm, offset);
  1482. }
  1483. /* Implement the to_get_ada_task_ptid target method for this target. */
  1484. ptid_t
  1485. thread_db_target::get_ada_task_ptid (long lwp, ULONGEST thread)
  1486. {
  1487. /* NPTL uses a 1:1 model, so the LWP id suffices. */
  1488. return ptid_t (inferior_ptid.pid (), lwp);
  1489. }
  1490. void
  1491. thread_db_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
  1492. {
  1493. process_stratum_target *beneath
  1494. = as_process_stratum_target (this->beneath ());
  1495. thread_db_info *info
  1496. = get_thread_db_info (beneath, (ptid == minus_one_ptid
  1497. ? inferior_ptid.pid ()
  1498. : ptid.pid ()));
  1499. /* This workaround is only needed for child fork lwps stopped in a
  1500. PTRACE_O_TRACEFORK event. When the inferior is resumed, the
  1501. workaround can be disabled. */
  1502. if (info)
  1503. info->need_stale_parent_threads_check = 0;
  1504. beneath->resume (ptid, step, signo);
  1505. }
  1506. /* std::sort helper function for info_auto_load_libthread_db, sort the
  1507. thread_db_info pointers primarily by their FILENAME and secondarily by their
  1508. PID, both in ascending order. */
  1509. static bool
  1510. info_auto_load_libthread_db_compare (const struct thread_db_info *a,
  1511. const struct thread_db_info *b)
  1512. {
  1513. int retval;
  1514. retval = strcmp (a->filename, b->filename);
  1515. if (retval)
  1516. return retval < 0;
  1517. return a->pid < b->pid;
  1518. }
  1519. /* Implement 'info auto-load libthread-db'. */
  1520. static void
  1521. info_auto_load_libthread_db (const char *args, int from_tty)
  1522. {
  1523. struct ui_out *uiout = current_uiout;
  1524. const char *cs = args ? args : "";
  1525. struct thread_db_info *info;
  1526. unsigned unique_filenames;
  1527. size_t max_filename_len, pids_len;
  1528. int i;
  1529. cs = skip_spaces (cs);
  1530. if (*cs)
  1531. error (_("'info auto-load libthread-db' does not accept any parameters"));
  1532. std::vector<struct thread_db_info *> array;
  1533. for (info = thread_db_list; info; info = info->next)
  1534. if (info->filename != NULL)
  1535. array.push_back (info);
  1536. /* Sort ARRAY by filenames and PIDs. */
  1537. std::sort (array.begin (), array.end (),
  1538. info_auto_load_libthread_db_compare);
  1539. /* Calculate the number of unique filenames (rows) and the maximum string
  1540. length of PIDs list for the unique filenames (columns). */
  1541. unique_filenames = 0;
  1542. max_filename_len = 0;
  1543. pids_len = 0;
  1544. for (i = 0; i < array.size (); i++)
  1545. {
  1546. int pid = array[i]->pid;
  1547. size_t this_pid_len;
  1548. for (this_pid_len = 0; pid != 0; pid /= 10)
  1549. this_pid_len++;
  1550. if (i == 0 || strcmp (array[i - 1]->filename, array[i]->filename) != 0)
  1551. {
  1552. unique_filenames++;
  1553. max_filename_len = std::max (max_filename_len,
  1554. strlen (array[i]->filename));
  1555. if (i > 0)
  1556. pids_len -= strlen (", ");
  1557. pids_len = 0;
  1558. }
  1559. pids_len += this_pid_len + strlen (", ");
  1560. }
  1561. if (i)
  1562. pids_len -= strlen (", ");
  1563. /* Table header shifted right by preceding "libthread-db: " would not match
  1564. its columns. */
  1565. if (array.size () > 0 && args == auto_load_info_scripts_pattern_nl)
  1566. uiout->text ("\n");
  1567. {
  1568. ui_out_emit_table table_emitter (uiout, 2, unique_filenames,
  1569. "LinuxThreadDbTable");
  1570. uiout->table_header (max_filename_len, ui_left, "filename", "Filename");
  1571. uiout->table_header (pids_len, ui_left, "PIDs", "Pids");
  1572. uiout->table_body ();
  1573. /* Note I is incremented inside the cycle, not at its end. */
  1574. for (i = 0; i < array.size ();)
  1575. {
  1576. ui_out_emit_tuple tuple_emitter (uiout, NULL);
  1577. info = array[i];
  1578. uiout->field_string ("filename", info->filename,
  1579. file_name_style.style ());
  1580. std::string pids;
  1581. while (i < array.size () && strcmp (info->filename,
  1582. array[i]->filename) == 0)
  1583. {
  1584. if (!pids.empty ())
  1585. pids += ", ";
  1586. string_appendf (pids, "%u", array[i]->pid);
  1587. i++;
  1588. }
  1589. uiout->field_string ("pids", pids);
  1590. uiout->text ("\n");
  1591. }
  1592. }
  1593. if (array.empty ())
  1594. uiout->message (_("No auto-loaded libthread-db.\n"));
  1595. }
  1596. /* Implement 'maintenance check libthread-db'. */
  1597. static void
  1598. maintenance_check_libthread_db (const char *args, int from_tty)
  1599. {
  1600. int inferior_pid = inferior_ptid.pid ();
  1601. struct thread_db_info *info;
  1602. if (inferior_pid == 0)
  1603. error (_("No inferior running"));
  1604. info = get_thread_db_info (current_inferior ()->process_target (),
  1605. inferior_pid);
  1606. if (info == NULL)
  1607. error (_("No libthread_db loaded"));
  1608. check_thread_db (info, true);
  1609. }
  1610. void _initialize_thread_db ();
  1611. void
  1612. _initialize_thread_db ()
  1613. {
  1614. /* Defer loading of libthread_db.so until inferior is running.
  1615. This allows gdb to load correct libthread_db for a given
  1616. executable -- there could be multiple versions of glibc,
  1617. and until there is a running inferior, we can't tell which
  1618. libthread_db is the correct one to load. */
  1619. add_setshow_optional_filename_cmd ("libthread-db-search-path",
  1620. class_support,
  1621. &libthread_db_search_path, _("\
  1622. Set search path for libthread_db."), _("\
  1623. Show the current search path or libthread_db."), _("\
  1624. This path is used to search for libthread_db to be loaded into \
  1625. gdb itself.\n\
  1626. Its value is a colon (':') separate list of directories to search.\n\
  1627. Setting the search path to an empty list resets it to its default value."),
  1628. set_libthread_db_search_path,
  1629. NULL,
  1630. &setlist, &showlist);
  1631. add_setshow_zuinteger_cmd ("libthread-db", class_maintenance,
  1632. &libthread_db_debug, _("\
  1633. Set libthread-db debugging."), _("\
  1634. Show libthread-db debugging."), _("\
  1635. When non-zero, libthread-db debugging is enabled."),
  1636. NULL,
  1637. show_libthread_db_debug,
  1638. &setdebuglist, &showdebuglist);
  1639. add_setshow_boolean_cmd ("libthread-db", class_support,
  1640. &auto_load_thread_db, _("\
  1641. Enable or disable auto-loading of inferior specific libthread_db."), _("\
  1642. Show whether auto-loading inferior specific libthread_db is enabled."), _("\
  1643. If enabled, libthread_db will be searched in 'set libthread-db-search-path'\n\
  1644. locations to load libthread_db compatible with the inferior.\n\
  1645. Standard system libthread_db still gets loaded even with this option off.\n\
  1646. This option has security implications for untrusted inferiors."),
  1647. NULL, show_auto_load_thread_db,
  1648. auto_load_set_cmdlist_get (),
  1649. auto_load_show_cmdlist_get ());
  1650. add_cmd ("libthread-db", class_info, info_auto_load_libthread_db,
  1651. _("Print the list of loaded inferior specific libthread_db.\n\
  1652. Usage: info auto-load libthread-db"),
  1653. auto_load_info_cmdlist_get ());
  1654. add_cmd ("libthread-db", class_maintenance,
  1655. maintenance_check_libthread_db, _("\
  1656. Run integrity checks on the current inferior's libthread_db."),
  1657. &maintenancechecklist);
  1658. add_setshow_boolean_cmd ("check-libthread-db",
  1659. class_maintenance,
  1660. &check_thread_db_on_load, _("\
  1661. Set whether to check libthread_db at load time."), _("\
  1662. Show whether to check libthread_db at load time."), _("\
  1663. If enabled GDB will run integrity checks on inferior specific libthread_db\n\
  1664. as they are loaded."),
  1665. NULL,
  1666. NULL,
  1667. &maintenance_set_cmdlist,
  1668. &maintenance_show_cmdlist);
  1669. /* Add ourselves to objfile event chain. */
  1670. gdb::observers::new_objfile.attach (thread_db_new_objfile, "linux-thread-db");
  1671. /* Add ourselves to inferior_created event chain.
  1672. This is needed to handle debugging statically linked programs where
  1673. the new_objfile observer won't get called for libpthread. */
  1674. gdb::observers::inferior_created.attach (thread_db_inferior_created,
  1675. "linux-thread-db");
  1676. }