Commit | Line | Data |
---|---|---|
0d62e5e8 | 1 | /* Thread management interface, for the remote server for GDB. |
7b6bb8da | 2 | Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
9b254dd1 | 3 | Free Software Foundation, Inc. |
0d62e5e8 DJ |
4 | |
5 | Contributed by MontaVista Software. | |
6 | ||
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
0d62e5e8 DJ |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
0d62e5e8 DJ |
21 | |
22 | #include "server.h" | |
23 | ||
24 | #include "linux-low.h" | |
25 | ||
26 | extern int debug_threads; | |
27 | ||
24a09b5f DJ |
28 | static int thread_db_use_events; |
29 | ||
0050a760 | 30 | #include "gdb_proc_service.h" |
6938fd34 | 31 | #include "gdb_thread_db.h" |
0d62e5e8 | 32 | |
96f15937 | 33 | #ifndef USE_LIBTHREAD_DB_DIRECTLY |
cdbfd419 | 34 | #include <dlfcn.h> |
96f15937 PP |
35 | #endif |
36 | ||
186947f7 | 37 | #include <stdint.h> |
cdbfd419 PP |
38 | #include <limits.h> |
39 | #include <ctype.h> | |
40 | ||
41 | struct thread_db | |
42 | { | |
43 | /* Structure that identifies the child process for the | |
44 | <proc_service.h> interface. */ | |
45 | struct ps_prochandle proc_handle; | |
46 | ||
47 | /* Connection to the libthread_db library. */ | |
48 | td_thragent_t *thread_agent; | |
49 | ||
9836d6ea PA |
50 | /* If this flag has been set, we've already asked GDB for all |
51 | symbols we might need; assume symbol cache misses are | |
52 | failures. */ | |
53 | int all_symbols_looked_up; | |
54 | ||
96f15937 | 55 | #ifndef USE_LIBTHREAD_DB_DIRECTLY |
cdbfd419 PP |
56 | /* Handle of the libthread_db from dlopen. */ |
57 | void *handle; | |
96f15937 | 58 | #endif |
cdbfd419 | 59 | |
f9e39928 PA |
60 | /* Thread creation event breakpoint. The code at this location in |
61 | the child process will be called by the pthread library whenever | |
62 | a new thread is created. By setting a special breakpoint at this | |
63 | location, GDB can detect when a new thread is created. We obtain | |
64 | this location via the td_ta_event_addr call. Note that if the | |
65 | running kernel supports tracing clones, then we don't need to use | |
66 | (and in fact don't use) this magic thread event breakpoint to | |
67 | learn about threads. */ | |
68 | struct breakpoint *td_create_bp; | |
69 | ||
cdbfd419 PP |
70 | /* Addresses of libthread_db functions. */ |
71 | td_err_e (*td_ta_new_p) (struct ps_prochandle * ps, td_thragent_t **ta); | |
72 | td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta, | |
73 | td_event_msg_t *msg); | |
74 | td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta, | |
75 | td_thr_events_t *event); | |
76 | td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta, | |
77 | td_event_e event, td_notify_t *ptr); | |
78 | td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta, lwpid_t lwpid, | |
79 | td_thrhandle_t *th); | |
80 | td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th, | |
81 | td_thrinfo_t *infop); | |
82 | td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th, int event); | |
83 | td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta, | |
84 | td_thr_iter_f *callback, void *cbdata_p, | |
85 | td_thr_state_e state, int ti_pri, | |
86 | sigset_t *ti_sigmask_p, | |
87 | unsigned int ti_user_flags); | |
88 | td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th, | |
00f515da DE |
89 | psaddr_t map_address, |
90 | size_t offset, psaddr_t *address); | |
cdbfd419 PP |
91 | const char ** (*td_symbol_list_p) (void); |
92 | }; | |
93 | ||
94 | static char *libthread_db_search_path; | |
186947f7 | 95 | |
95954743 | 96 | static int find_one_thread (ptid_t); |
0d62e5e8 DJ |
97 | static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data); |
98 | ||
54363045 | 99 | static const char * |
0d62e5e8 DJ |
100 | thread_db_err_str (td_err_e err) |
101 | { | |
102 | static char buf[64]; | |
103 | ||
104 | switch (err) | |
105 | { | |
106 | case TD_OK: | |
107 | return "generic 'call succeeded'"; | |
108 | case TD_ERR: | |
109 | return "generic error"; | |
110 | case TD_NOTHR: | |
111 | return "no thread to satisfy query"; | |
112 | case TD_NOSV: | |
113 | return "no sync handle to satisfy query"; | |
114 | case TD_NOLWP: | |
115 | return "no LWP to satisfy query"; | |
116 | case TD_BADPH: | |
117 | return "invalid process handle"; | |
118 | case TD_BADTH: | |
119 | return "invalid thread handle"; | |
120 | case TD_BADSH: | |
121 | return "invalid synchronization handle"; | |
122 | case TD_BADTA: | |
123 | return "invalid thread agent"; | |
124 | case TD_BADKEY: | |
125 | return "invalid key"; | |
126 | case TD_NOMSG: | |
127 | return "no event message for getmsg"; | |
128 | case TD_NOFPREGS: | |
129 | return "FPU register set not available"; | |
130 | case TD_NOLIBTHREAD: | |
131 | return "application not linked with libthread"; | |
132 | case TD_NOEVENT: | |
133 | return "requested event is not supported"; | |
134 | case TD_NOCAPAB: | |
135 | return "capability not available"; | |
136 | case TD_DBERR: | |
137 | return "debugger service failed"; | |
138 | case TD_NOAPLIC: | |
139 | return "operation not applicable to"; | |
140 | case TD_NOTSD: | |
141 | return "no thread-specific data for this thread"; | |
142 | case TD_MALLOC: | |
143 | return "malloc failed"; | |
144 | case TD_PARTIALREG: | |
145 | return "only part of register set was written/read"; | |
146 | case TD_NOXREGS: | |
147 | return "X register set not available for this thread"; | |
3db0444b DJ |
148 | #ifdef HAVE_TD_VERSION |
149 | case TD_VERSION: | |
150 | return "version mismatch between libthread_db and libpthread"; | |
151 | #endif | |
0d62e5e8 | 152 | default: |
6cebaf6e | 153 | xsnprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err); |
0d62e5e8 DJ |
154 | return buf; |
155 | } | |
156 | } | |
157 | ||
158 | #if 0 | |
159 | static char * | |
160 | thread_db_state_str (td_thr_state_e state) | |
161 | { | |
162 | static char buf[64]; | |
163 | ||
164 | switch (state) | |
165 | { | |
166 | case TD_THR_STOPPED: | |
167 | return "stopped by debugger"; | |
168 | case TD_THR_RUN: | |
169 | return "runnable"; | |
170 | case TD_THR_ACTIVE: | |
171 | return "active"; | |
172 | case TD_THR_ZOMBIE: | |
173 | return "zombie"; | |
174 | case TD_THR_SLEEP: | |
175 | return "sleeping"; | |
176 | case TD_THR_STOPPED_ASLEEP: | |
177 | return "stopped by debugger AND blocked"; | |
178 | default: | |
6cebaf6e | 179 | xsnprintf (buf, sizeof (buf), "unknown thread_db state %d", state); |
0d62e5e8 DJ |
180 | return buf; |
181 | } | |
182 | } | |
183 | #endif | |
184 | ||
b65d95c5 | 185 | static int |
0d62e5e8 DJ |
186 | thread_db_create_event (CORE_ADDR where) |
187 | { | |
188 | td_event_msg_t msg; | |
189 | td_err_e err; | |
54a0b537 | 190 | struct lwp_info *lwp; |
cdbfd419 PP |
191 | struct thread_db *thread_db = current_process ()->private->thread_db; |
192 | ||
193 | if (thread_db->td_ta_event_getmsg_p == NULL) | |
194 | fatal ("unexpected thread_db->td_ta_event_getmsg_p == NULL"); | |
0d62e5e8 DJ |
195 | |
196 | if (debug_threads) | |
197 | fprintf (stderr, "Thread creation event.\n"); | |
198 | ||
0d62e5e8 DJ |
199 | /* FIXME: This assumes we don't get another event. |
200 | In the LinuxThreads implementation, this is safe, | |
201 | because all events come from the manager thread | |
202 | (except for its own creation, of course). */ | |
cdbfd419 | 203 | err = thread_db->td_ta_event_getmsg_p (thread_db->thread_agent, &msg); |
0d62e5e8 DJ |
204 | if (err != TD_OK) |
205 | fprintf (stderr, "thread getmsg err: %s\n", | |
206 | thread_db_err_str (err)); | |
207 | ||
4105de34 DJ |
208 | /* If we do not know about the main thread yet, this would be a good time to |
209 | find it. We need to do this to pick up the main thread before any newly | |
210 | created threads. */ | |
54a0b537 PA |
211 | lwp = get_thread_lwp (current_inferior); |
212 | if (lwp->thread_known == 0) | |
95954743 | 213 | find_one_thread (lwp->head.id); |
4105de34 | 214 | |
0d62e5e8 DJ |
215 | /* msg.event == TD_EVENT_CREATE */ |
216 | ||
217 | find_new_threads_callback (msg.th_p, NULL); | |
b65d95c5 DJ |
218 | |
219 | return 0; | |
0d62e5e8 DJ |
220 | } |
221 | ||
0d62e5e8 | 222 | static int |
f9e39928 | 223 | thread_db_enable_reporting (void) |
0d62e5e8 DJ |
224 | { |
225 | td_thr_events_t events; | |
226 | td_notify_t notify; | |
227 | td_err_e err; | |
cdbfd419 PP |
228 | struct thread_db *thread_db = current_process ()->private->thread_db; |
229 | ||
230 | if (thread_db->td_ta_set_event_p == NULL | |
231 | || thread_db->td_ta_event_addr_p == NULL | |
232 | || thread_db->td_ta_event_getmsg_p == NULL) | |
233 | /* This libthread_db is missing required support. */ | |
234 | return 0; | |
0d62e5e8 DJ |
235 | |
236 | /* Set the process wide mask saying which events we're interested in. */ | |
237 | td_event_emptyset (&events); | |
238 | td_event_addset (&events, TD_CREATE); | |
239 | ||
cdbfd419 | 240 | err = thread_db->td_ta_set_event_p (thread_db->thread_agent, &events); |
0d62e5e8 DJ |
241 | if (err != TD_OK) |
242 | { | |
243 | warning ("Unable to set global thread event mask: %s", | |
1b3f6016 | 244 | thread_db_err_str (err)); |
0d62e5e8 DJ |
245 | return 0; |
246 | } | |
247 | ||
248 | /* Get address for thread creation breakpoint. */ | |
cdbfd419 PP |
249 | err = thread_db->td_ta_event_addr_p (thread_db->thread_agent, TD_CREATE, |
250 | ¬ify); | |
0d62e5e8 DJ |
251 | if (err != TD_OK) |
252 | { | |
253 | warning ("Unable to get location for thread creation breakpoint: %s", | |
254 | thread_db_err_str (err)); | |
255 | return 0; | |
256 | } | |
f9e39928 PA |
257 | thread_db->td_create_bp |
258 | = set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr, | |
259 | thread_db_create_event); | |
0d62e5e8 | 260 | |
0d62e5e8 DJ |
261 | return 1; |
262 | } | |
263 | ||
ae13219e | 264 | static int |
95954743 | 265 | find_one_thread (ptid_t ptid) |
0d62e5e8 | 266 | { |
ae13219e DJ |
267 | td_thrhandle_t th; |
268 | td_thrinfo_t ti; | |
0d62e5e8 DJ |
269 | td_err_e err; |
270 | struct thread_info *inferior; | |
54a0b537 | 271 | struct lwp_info *lwp; |
cdbfd419 | 272 | struct thread_db *thread_db = current_process ()->private->thread_db; |
95954743 | 273 | int lwpid = ptid_get_lwp (ptid); |
0d62e5e8 | 274 | |
95954743 | 275 | inferior = (struct thread_info *) find_inferior_id (&all_threads, ptid); |
54a0b537 PA |
276 | lwp = get_thread_lwp (inferior); |
277 | if (lwp->thread_known) | |
ae13219e DJ |
278 | return 1; |
279 | ||
24a09b5f | 280 | /* Get information about this thread. */ |
cdbfd419 | 281 | err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th); |
ae13219e | 282 | if (err != TD_OK) |
24a09b5f DJ |
283 | error ("Cannot get thread handle for LWP %d: %s", |
284 | lwpid, thread_db_err_str (err)); | |
ae13219e | 285 | |
cdbfd419 | 286 | err = thread_db->td_thr_get_info_p (&th, &ti); |
ae13219e | 287 | if (err != TD_OK) |
24a09b5f DJ |
288 | error ("Cannot get thread info for LWP %d: %s", |
289 | lwpid, thread_db_err_str (err)); | |
ae13219e DJ |
290 | |
291 | if (debug_threads) | |
24a09b5f | 292 | fprintf (stderr, "Found thread %ld (LWP %d)\n", |
ae13219e DJ |
293 | ti.ti_tid, ti.ti_lid); |
294 | ||
95954743 | 295 | if (lwpid != ti.ti_lid) |
24a09b5f DJ |
296 | { |
297 | warning ("PID mismatch! Expected %ld, got %ld", | |
95954743 | 298 | (long) lwpid, (long) ti.ti_lid); |
24a09b5f DJ |
299 | return 0; |
300 | } | |
ae13219e | 301 | |
24a09b5f | 302 | if (thread_db_use_events) |
0d62e5e8 | 303 | { |
cdbfd419 | 304 | err = thread_db->td_thr_event_enable_p (&th, 1); |
ae13219e DJ |
305 | if (err != TD_OK) |
306 | error ("Cannot enable thread event reporting for %d: %s", | |
307 | ti.ti_lid, thread_db_err_str (err)); | |
0d62e5e8 | 308 | } |
ae13219e | 309 | |
24a09b5f DJ |
310 | /* If the new thread ID is zero, a final thread ID will be available |
311 | later. Do not enable thread debugging yet. */ | |
312 | if (ti.ti_tid == 0) | |
313 | return 0; | |
ae13219e | 314 | |
54a0b537 PA |
315 | lwp->thread_known = 1; |
316 | lwp->th = th; | |
ae13219e | 317 | |
ae13219e DJ |
318 | return 1; |
319 | } | |
320 | ||
5f7d1694 PP |
321 | /* Attach a thread. Return true on success. */ |
322 | ||
323 | static int | |
324 | attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p) | |
ae13219e | 325 | { |
54a0b537 | 326 | struct lwp_info *lwp; |
ae13219e | 327 | |
0d62e5e8 DJ |
328 | if (debug_threads) |
329 | fprintf (stderr, "Attaching to thread %ld (LWP %d)\n", | |
330 | ti_p->ti_tid, ti_p->ti_lid); | |
24a09b5f | 331 | linux_attach_lwp (ti_p->ti_lid); |
95954743 PA |
332 | lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid)); |
333 | if (lwp == NULL) | |
0d62e5e8 DJ |
334 | { |
335 | warning ("Could not attach to thread %ld (LWP %d)\n", | |
336 | ti_p->ti_tid, ti_p->ti_lid); | |
5f7d1694 | 337 | return 0; |
0d62e5e8 DJ |
338 | } |
339 | ||
54a0b537 PA |
340 | lwp->thread_known = 1; |
341 | lwp->th = *th_p; | |
24a09b5f DJ |
342 | |
343 | if (thread_db_use_events) | |
344 | { | |
5f7d1694 | 345 | td_err_e err; |
cdbfd419 | 346 | struct thread_db *thread_db = current_process ()->private->thread_db; |
5f7d1694 | 347 | |
cdbfd419 | 348 | err = thread_db->td_thr_event_enable_p (th_p, 1); |
24a09b5f DJ |
349 | if (err != TD_OK) |
350 | error ("Cannot enable thread event reporting for %d: %s", | |
351 | ti_p->ti_lid, thread_db_err_str (err)); | |
352 | } | |
5f7d1694 PP |
353 | |
354 | return 1; | |
355 | } | |
356 | ||
357 | /* Attach thread if we haven't seen it yet. | |
358 | Increment *COUNTER if we have attached a new thread. | |
359 | Return false on failure. */ | |
360 | ||
361 | static int | |
362 | maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p, | |
363 | int *counter) | |
364 | { | |
365 | struct lwp_info *lwp; | |
366 | ||
367 | lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid)); | |
368 | if (lwp != NULL) | |
369 | return 1; | |
370 | ||
371 | if (!attach_thread (th_p, ti_p)) | |
372 | return 0; | |
373 | ||
374 | if (counter != NULL) | |
375 | *counter += 1; | |
376 | ||
377 | return 1; | |
0d62e5e8 DJ |
378 | } |
379 | ||
380 | static int | |
381 | find_new_threads_callback (const td_thrhandle_t *th_p, void *data) | |
382 | { | |
383 | td_thrinfo_t ti; | |
384 | td_err_e err; | |
cdbfd419 | 385 | struct thread_db *thread_db = current_process ()->private->thread_db; |
0d62e5e8 | 386 | |
cdbfd419 | 387 | err = thread_db->td_thr_get_info_p (th_p, &ti); |
0d62e5e8 DJ |
388 | if (err != TD_OK) |
389 | error ("Cannot get thread info: %s", thread_db_err_str (err)); | |
390 | ||
391 | /* Check for zombies. */ | |
392 | if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) | |
393 | return 0; | |
394 | ||
5f7d1694 PP |
395 | if (!maybe_attach_thread (th_p, &ti, (int *) data)) |
396 | { | |
397 | /* Terminate iteration early: we might be looking at stale data in | |
398 | the inferior. The thread_db_find_new_threads will retry. */ | |
399 | return 1; | |
400 | } | |
0d62e5e8 DJ |
401 | |
402 | return 0; | |
403 | } | |
404 | ||
405 | static void | |
406 | thread_db_find_new_threads (void) | |
407 | { | |
408 | td_err_e err; | |
95954743 | 409 | ptid_t ptid = ((struct inferior_list_entry *) current_inferior)->id; |
cdbfd419 | 410 | struct thread_db *thread_db = current_process ()->private->thread_db; |
5f7d1694 | 411 | int loop, iteration; |
0d62e5e8 | 412 | |
ae13219e DJ |
413 | /* This function is only called when we first initialize thread_db. |
414 | First locate the initial thread. If it is not ready for | |
415 | debugging yet, then stop. */ | |
95954743 | 416 | if (find_one_thread (ptid) == 0) |
ae13219e DJ |
417 | return; |
418 | ||
5f7d1694 PP |
419 | /* Require 4 successive iterations which do not find any new threads. |
420 | The 4 is a heuristic: there is an inherent race here, and I have | |
421 | seen that 2 iterations in a row are not always sufficient to | |
422 | "capture" all threads. */ | |
423 | for (loop = 0, iteration = 0; loop < 4; ++loop, ++iteration) | |
424 | { | |
425 | int new_thread_count = 0; | |
426 | ||
427 | /* Iterate over all user-space threads to discover new threads. */ | |
428 | err = thread_db->td_ta_thr_iter_p (thread_db->thread_agent, | |
429 | find_new_threads_callback, | |
430 | &new_thread_count, | |
493e2a69 MS |
431 | TD_THR_ANY_STATE, |
432 | TD_THR_LOWEST_PRIORITY, | |
5f7d1694 PP |
433 | TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); |
434 | if (debug_threads) | |
435 | fprintf (stderr, "Found %d threads in iteration %d.\n", | |
436 | new_thread_count, iteration); | |
437 | ||
438 | if (new_thread_count != 0) | |
439 | { | |
440 | /* Found new threads. Restart iteration from beginning. */ | |
441 | loop = -1; | |
442 | } | |
443 | } | |
0d62e5e8 DJ |
444 | if (err != TD_OK) |
445 | error ("Cannot find new threads: %s", thread_db_err_str (err)); | |
446 | } | |
447 | ||
fd500816 DJ |
448 | /* Cache all future symbols that thread_db might request. We can not |
449 | request symbols at arbitrary states in the remote protocol, only | |
450 | when the client tells us that new symbols are available. So when | |
451 | we load the thread library, make sure to check the entire list. */ | |
452 | ||
453 | static void | |
454 | thread_db_look_up_symbols (void) | |
455 | { | |
cdbfd419 PP |
456 | struct thread_db *thread_db = current_process ()->private->thread_db; |
457 | const char **sym_list; | |
fd500816 DJ |
458 | CORE_ADDR unused; |
459 | ||
cdbfd419 | 460 | for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++) |
9836d6ea PA |
461 | look_up_one_symbol (*sym_list, &unused, 1); |
462 | ||
463 | /* We're not interested in any other libraries loaded after this | |
464 | point, only in symbols in libpthread.so. */ | |
465 | thread_db->all_symbols_looked_up = 1; | |
466 | } | |
467 | ||
468 | int | |
469 | thread_db_look_up_one_symbol (const char *name, CORE_ADDR *addrp) | |
470 | { | |
471 | struct thread_db *thread_db = current_process ()->private->thread_db; | |
472 | int may_ask_gdb = !thread_db->all_symbols_looked_up; | |
473 | ||
474 | /* If we've passed the call to thread_db_look_up_symbols, then | |
475 | anything not in the cache must not exist; we're not interested | |
476 | in any libraries loaded after that point, only in symbols in | |
477 | libpthread.so. It might not be an appropriate time to look | |
478 | up a symbol, e.g. while we're trying to fetch registers. */ | |
479 | return look_up_one_symbol (name, addrp, may_ask_gdb); | |
fd500816 DJ |
480 | } |
481 | ||
dae5f5cf DJ |
482 | int |
483 | thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset, | |
484 | CORE_ADDR load_module, CORE_ADDR *address) | |
485 | { | |
dae5f5cf DJ |
486 | psaddr_t addr; |
487 | td_err_e err; | |
54a0b537 | 488 | struct lwp_info *lwp; |
95954743 | 489 | struct thread_info *saved_inferior; |
cdbfd419 PP |
490 | struct process_info *proc; |
491 | struct thread_db *thread_db; | |
492 | ||
493 | proc = get_thread_process (thread); | |
494 | thread_db = proc->private->thread_db; | |
dae5f5cf | 495 | |
7fe519cb | 496 | /* If the thread layer is not (yet) initialized, fail. */ |
9836d6ea | 497 | if (!thread_db->all_symbols_looked_up) |
7fe519cb UW |
498 | return TD_ERR; |
499 | ||
cdbfd419 PP |
500 | if (thread_db->td_thr_tls_get_addr_p == NULL) |
501 | return -1; | |
502 | ||
54a0b537 PA |
503 | lwp = get_thread_lwp (thread); |
504 | if (!lwp->thread_known) | |
95954743 | 505 | find_one_thread (lwp->head.id); |
54a0b537 | 506 | if (!lwp->thread_known) |
dae5f5cf DJ |
507 | return TD_NOTHR; |
508 | ||
95954743 PA |
509 | saved_inferior = current_inferior; |
510 | current_inferior = thread; | |
186947f7 DJ |
511 | /* Note the cast through uintptr_t: this interface only works if |
512 | a target address fits in a psaddr_t, which is a host pointer. | |
513 | So a 32-bit debugger can not access 64-bit TLS through this. */ | |
cdbfd419 PP |
514 | err = thread_db->td_thr_tls_get_addr_p (&lwp->th, |
515 | (psaddr_t) (uintptr_t) load_module, | |
516 | offset, &addr); | |
95954743 | 517 | current_inferior = saved_inferior; |
dae5f5cf DJ |
518 | if (err == TD_OK) |
519 | { | |
186947f7 | 520 | *address = (CORE_ADDR) (uintptr_t) addr; |
dae5f5cf DJ |
521 | return 0; |
522 | } | |
523 | else | |
524 | return err; | |
cdbfd419 PP |
525 | } |
526 | ||
96f15937 PP |
527 | #ifdef USE_LIBTHREAD_DB_DIRECTLY |
528 | ||
529 | static int | |
530 | thread_db_load_search (void) | |
531 | { | |
532 | td_err_e err; | |
9836d6ea | 533 | struct thread_db *tdb; |
96f15937 PP |
534 | struct process_info *proc = current_process (); |
535 | ||
536 | if (proc->private->thread_db != NULL) | |
537 | fatal ("unexpected: proc->private->thread_db != NULL"); | |
538 | ||
9836d6ea PA |
539 | tdb = xcalloc (1, sizeof (*tdb)); |
540 | proc->private->thread_db = tdb; | |
f9e39928 | 541 | |
9836d6ea | 542 | tdb->td_ta_new_p = &td_ta_new; |
96f15937 PP |
543 | |
544 | /* Attempt to open a connection to the thread library. */ | |
9836d6ea | 545 | err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent); |
96f15937 PP |
546 | if (err != TD_OK) |
547 | { | |
548 | if (debug_threads) | |
549 | fprintf (stderr, "td_ta_new(): %s\n", thread_db_err_str (err)); | |
9836d6ea PA |
550 | free (tdb); |
551 | proc->private->thread_db = NULL; | |
96f15937 PP |
552 | return 0; |
553 | } | |
554 | ||
9836d6ea PA |
555 | tdb->td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr; |
556 | tdb->td_thr_get_info_p = &td_thr_get_info; | |
557 | tdb->td_ta_thr_iter_p = &td_ta_thr_iter; | |
558 | tdb->td_symbol_list_p = &td_symbol_list; | |
96f15937 PP |
559 | |
560 | /* This is required only when thread_db_use_events is on. */ | |
9836d6ea | 561 | tdb->td_thr_event_enable_p = &td_thr_event_enable; |
96f15937 PP |
562 | |
563 | /* These are not essential. */ | |
9836d6ea PA |
564 | tdb->td_ta_event_addr_p = &td_ta_event_addr; |
565 | tdb->td_ta_set_event_p = &td_ta_set_event; | |
566 | tdb->td_ta_event_getmsg_p = &td_ta_event_getmsg; | |
567 | tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr; | |
96f15937 PP |
568 | |
569 | return 1; | |
570 | } | |
571 | ||
572 | #else | |
573 | ||
cdbfd419 PP |
574 | static int |
575 | try_thread_db_load_1 (void *handle) | |
576 | { | |
577 | td_err_e err; | |
9836d6ea | 578 | struct thread_db *tdb; |
cdbfd419 PP |
579 | struct process_info *proc = current_process (); |
580 | ||
581 | if (proc->private->thread_db != NULL) | |
582 | fatal ("unexpected: proc->private->thread_db != NULL"); | |
583 | ||
9836d6ea PA |
584 | tdb = xcalloc (1, sizeof (*tdb)); |
585 | proc->private->thread_db = tdb; | |
f9e39928 | 586 | |
9836d6ea | 587 | tdb->handle = handle; |
cdbfd419 PP |
588 | |
589 | /* Initialize pointers to the dynamic library functions we will use. | |
590 | Essential functions first. */ | |
591 | ||
592 | #define CHK(required, a) \ | |
593 | do \ | |
594 | { \ | |
595 | if ((a) == NULL) \ | |
596 | { \ | |
597 | if (debug_threads) \ | |
598 | fprintf (stderr, "dlsym: %s\n", dlerror ()); \ | |
599 | if (required) \ | |
9836d6ea PA |
600 | { \ |
601 | free (tdb); \ | |
602 | proc->private->thread_db = NULL; \ | |
603 | return 0; \ | |
604 | } \ | |
cdbfd419 PP |
605 | } \ |
606 | } \ | |
607 | while (0) | |
608 | ||
9836d6ea | 609 | CHK (1, tdb->td_ta_new_p = dlsym (handle, "td_ta_new")); |
cdbfd419 PP |
610 | |
611 | /* Attempt to open a connection to the thread library. */ | |
9836d6ea | 612 | err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent); |
cdbfd419 PP |
613 | if (err != TD_OK) |
614 | { | |
615 | if (debug_threads) | |
616 | fprintf (stderr, "td_ta_new(): %s\n", thread_db_err_str (err)); | |
9836d6ea PA |
617 | free (tdb); |
618 | proc->private->thread_db = NULL; | |
cdbfd419 PP |
619 | return 0; |
620 | } | |
621 | ||
9836d6ea PA |
622 | CHK (1, tdb->td_ta_map_lwp2thr_p = dlsym (handle, "td_ta_map_lwp2thr")); |
623 | CHK (1, tdb->td_thr_get_info_p = dlsym (handle, "td_thr_get_info")); | |
624 | CHK (1, tdb->td_ta_thr_iter_p = dlsym (handle, "td_ta_thr_iter")); | |
625 | CHK (1, tdb->td_symbol_list_p = dlsym (handle, "td_symbol_list")); | |
cdbfd419 PP |
626 | |
627 | /* This is required only when thread_db_use_events is on. */ | |
628 | CHK (thread_db_use_events, | |
9836d6ea | 629 | tdb->td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable")); |
cdbfd419 PP |
630 | |
631 | /* These are not essential. */ | |
9836d6ea PA |
632 | CHK (0, tdb->td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr")); |
633 | CHK (0, tdb->td_ta_set_event_p = dlsym (handle, "td_ta_set_event")); | |
634 | CHK (0, tdb->td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg")); | |
635 | CHK (0, tdb->td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr")); | |
cdbfd419 PP |
636 | |
637 | #undef CHK | |
638 | ||
cdbfd419 PP |
639 | return 1; |
640 | } | |
641 | ||
10e86dd7 DE |
642 | #ifdef HAVE_DLADDR |
643 | ||
cdbfd419 PP |
644 | /* Lookup a library in which given symbol resides. |
645 | Note: this is looking in the GDBSERVER process, not in the inferior. | |
646 | Returns library name, or NULL. */ | |
647 | ||
648 | static const char * | |
649 | dladdr_to_soname (const void *addr) | |
650 | { | |
651 | Dl_info info; | |
652 | ||
653 | if (dladdr (addr, &info) != 0) | |
654 | return info.dli_fname; | |
655 | return NULL; | |
656 | } | |
657 | ||
10e86dd7 DE |
658 | #endif |
659 | ||
cdbfd419 PP |
660 | static int |
661 | try_thread_db_load (const char *library) | |
662 | { | |
663 | void *handle; | |
664 | ||
665 | if (debug_threads) | |
666 | fprintf (stderr, "Trying host libthread_db library: %s.\n", | |
667 | library); | |
668 | handle = dlopen (library, RTLD_NOW); | |
669 | if (handle == NULL) | |
670 | { | |
671 | if (debug_threads) | |
672 | fprintf (stderr, "dlopen failed: %s.\n", dlerror ()); | |
673 | return 0; | |
674 | } | |
675 | ||
10e86dd7 | 676 | #ifdef HAVE_DLADDR |
cdbfd419 PP |
677 | if (debug_threads && strchr (library, '/') == NULL) |
678 | { | |
679 | void *td_init; | |
680 | ||
681 | td_init = dlsym (handle, "td_init"); | |
682 | if (td_init != NULL) | |
683 | { | |
684 | const char *const libpath = dladdr_to_soname (td_init); | |
685 | ||
686 | if (libpath != NULL) | |
687 | fprintf (stderr, "Host %s resolved to: %s.\n", | |
688 | library, libpath); | |
689 | } | |
690 | } | |
10e86dd7 | 691 | #endif |
cdbfd419 PP |
692 | |
693 | if (try_thread_db_load_1 (handle)) | |
694 | return 1; | |
695 | ||
696 | /* This library "refused" to work on current inferior. */ | |
697 | dlclose (handle); | |
698 | return 0; | |
699 | } | |
700 | ||
98a5dd13 DE |
701 | /* Handle $sdir in libthread-db-search-path. |
702 | Look for libthread_db in the system dirs, or wherever a plain | |
703 | dlopen(file_without_path) will look. | |
704 | The result is true for success. */ | |
705 | ||
cdbfd419 | 706 | static int |
98a5dd13 DE |
707 | try_thread_db_load_from_sdir (void) |
708 | { | |
709 | return try_thread_db_load (LIBTHREAD_DB_SO); | |
710 | } | |
711 | ||
712 | /* Try to load libthread_db from directory DIR of length DIR_LEN. | |
713 | The result is true for success. */ | |
714 | ||
715 | static int | |
716 | try_thread_db_load_from_dir (const char *dir, size_t dir_len) | |
cdbfd419 PP |
717 | { |
718 | char path[PATH_MAX]; | |
98a5dd13 DE |
719 | |
720 | if (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path)) | |
721 | { | |
722 | char *cp = xmalloc (dir_len + 1); | |
723 | ||
724 | memcpy (cp, dir, dir_len); | |
725 | cp[dir_len] = '\0'; | |
726 | warning (_("libthread-db-search-path component too long," | |
727 | " ignored: %s."), cp); | |
728 | free (cp); | |
729 | return 0; | |
730 | } | |
731 | ||
732 | memcpy (path, dir, dir_len); | |
733 | path[dir_len] = '/'; | |
734 | strcpy (path + dir_len + 1, LIBTHREAD_DB_SO); | |
735 | return try_thread_db_load (path); | |
736 | } | |
737 | ||
738 | /* Search libthread_db_search_path for libthread_db which "agrees" | |
739 | to work on current inferior. | |
740 | The result is true for success. */ | |
741 | ||
742 | static int | |
743 | thread_db_load_search (void) | |
744 | { | |
cdbfd419 PP |
745 | const char *search_path; |
746 | int rc = 0; | |
747 | ||
748 | if (libthread_db_search_path == NULL) | |
749 | libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH); | |
750 | ||
751 | search_path = libthread_db_search_path; | |
752 | while (*search_path) | |
753 | { | |
754 | const char *end = strchr (search_path, ':'); | |
98a5dd13 DE |
755 | const char *this_dir = search_path; |
756 | size_t this_dir_len; | |
757 | ||
cdbfd419 PP |
758 | if (end) |
759 | { | |
98a5dd13 DE |
760 | this_dir_len = end - search_path; |
761 | search_path += this_dir_len + 1; | |
cdbfd419 PP |
762 | } |
763 | else | |
764 | { | |
98a5dd13 DE |
765 | this_dir_len = strlen (this_dir); |
766 | search_path += this_dir_len; | |
767 | } | |
cdbfd419 | 768 | |
98a5dd13 DE |
769 | if (this_dir_len == sizeof ("$pdir") - 1 |
770 | && strncmp (this_dir, "$pdir", this_dir_len) == 0) | |
771 | { | |
772 | /* We don't maintain a list of loaded libraries so we don't know | |
773 | where libpthread lives. We *could* fetch the info, but we don't | |
774 | do that yet. Ignore it. */ | |
775 | } | |
776 | else if (this_dir_len == sizeof ("$sdir") - 1 | |
777 | && strncmp (this_dir, "$sdir", this_dir_len) == 0) | |
778 | { | |
779 | if (try_thread_db_load_from_sdir ()) | |
cdbfd419 | 780 | { |
98a5dd13 | 781 | rc = 1; |
cdbfd419 PP |
782 | break; |
783 | } | |
cdbfd419 | 784 | } |
98a5dd13 | 785 | else |
cdbfd419 | 786 | { |
98a5dd13 DE |
787 | if (try_thread_db_load_from_dir (this_dir, this_dir_len)) |
788 | { | |
789 | rc = 1; | |
790 | break; | |
791 | } | |
cdbfd419 PP |
792 | } |
793 | } | |
cdbfd419 PP |
794 | |
795 | if (debug_threads) | |
796 | fprintf (stderr, "thread_db_load_search returning %d\n", rc); | |
797 | return rc; | |
dae5f5cf DJ |
798 | } |
799 | ||
96f15937 PP |
800 | #endif /* USE_LIBTHREAD_DB_DIRECTLY */ |
801 | ||
0d62e5e8 | 802 | int |
24a09b5f | 803 | thread_db_init (int use_events) |
0d62e5e8 | 804 | { |
95954743 | 805 | struct process_info *proc = current_process (); |
0d62e5e8 | 806 | |
fd500816 DJ |
807 | /* FIXME drow/2004-10-16: This is the "overall process ID", which |
808 | GNU/Linux calls tgid, "thread group ID". When we support | |
809 | attaching to threads, the original thread may not be the correct | |
810 | thread. We would have to get the process ID from /proc for NPTL. | |
811 | For LinuxThreads we could do something similar: follow the chain | |
812 | of parent processes until we find the highest one we're attached | |
813 | to, and use its tgid. | |
814 | ||
815 | This isn't the only place in gdbserver that assumes that the first | |
816 | process in the list is the thread group leader. */ | |
ea025f5f | 817 | |
24a09b5f DJ |
818 | thread_db_use_events = use_events; |
819 | ||
cdbfd419 | 820 | if (thread_db_load_search ()) |
0d62e5e8 | 821 | { |
24a09b5f | 822 | if (use_events && thread_db_enable_reporting () == 0) |
cdbfd419 PP |
823 | { |
824 | /* Keep trying; maybe event reporting will work later. */ | |
8336d594 | 825 | thread_db_mourn (proc); |
cdbfd419 PP |
826 | return 0; |
827 | } | |
0d62e5e8 | 828 | thread_db_find_new_threads (); |
fd500816 | 829 | thread_db_look_up_symbols (); |
0d62e5e8 | 830 | return 1; |
cdbfd419 | 831 | } |
0d62e5e8 | 832 | |
cdbfd419 PP |
833 | return 0; |
834 | } | |
835 | ||
ca5c370d PA |
836 | static int |
837 | any_thread_of (struct inferior_list_entry *entry, void *args) | |
838 | { | |
839 | int *pid_p = args; | |
840 | ||
841 | if (ptid_get_pid (entry->id) == *pid_p) | |
842 | return 1; | |
843 | ||
844 | return 0; | |
845 | } | |
846 | ||
f9e39928 PA |
847 | static void |
848 | switch_to_process (struct process_info *proc) | |
849 | { | |
850 | int pid = pid_of (proc); | |
851 | ||
852 | current_inferior = | |
853 | (struct thread_info *) find_inferior (&all_threads, | |
854 | any_thread_of, &pid); | |
855 | } | |
856 | ||
cdbfd419 PP |
857 | /* Disconnect from libthread_db and free resources. */ |
858 | ||
8336d594 PA |
859 | static void |
860 | disable_thread_event_reporting (struct process_info *proc) | |
cdbfd419 PP |
861 | { |
862 | struct thread_db *thread_db = proc->private->thread_db; | |
863 | if (thread_db) | |
864 | { | |
21e1bee4 PP |
865 | td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta, |
866 | td_thr_events_t *event); | |
867 | ||
fd7dd3e6 | 868 | #ifndef USE_LIBTHREAD_DB_DIRECTLY |
21e1bee4 | 869 | td_ta_clear_event_p = dlsym (thread_db->handle, "td_ta_clear_event"); |
fd7dd3e6 | 870 | #else |
fd7dd3e6 PA |
871 | td_ta_clear_event_p = &td_ta_clear_event; |
872 | #endif | |
873 | ||
8336d594 | 874 | if (td_ta_clear_event_p != NULL) |
21e1bee4 | 875 | { |
f9e39928 | 876 | struct thread_info *saved_inferior = current_inferior; |
21e1bee4 | 877 | td_thr_events_t events; |
8336d594 | 878 | |
f9e39928 | 879 | switch_to_process (proc); |
21e1bee4 | 880 | |
fd7dd3e6 PA |
881 | /* Set the process wide mask saying we aren't interested |
882 | in any events anymore. */ | |
21e1bee4 PP |
883 | td_event_fillset (&events); |
884 | (*td_ta_clear_event_p) (thread_db->thread_agent, &events); | |
8336d594 PA |
885 | |
886 | current_inferior = saved_inferior; | |
21e1bee4 | 887 | } |
8336d594 PA |
888 | } |
889 | } | |
890 | ||
f9e39928 PA |
891 | static void |
892 | remove_thread_event_breakpoints (struct process_info *proc) | |
893 | { | |
894 | struct thread_db *thread_db = proc->private->thread_db; | |
895 | ||
896 | if (thread_db->td_create_bp != NULL) | |
897 | { | |
898 | struct thread_info *saved_inferior = current_inferior; | |
899 | ||
900 | switch_to_process (proc); | |
901 | ||
902 | delete_breakpoint (thread_db->td_create_bp); | |
903 | thread_db->td_create_bp = NULL; | |
904 | ||
905 | current_inferior = saved_inferior; | |
906 | } | |
907 | } | |
908 | ||
8336d594 PA |
909 | void |
910 | thread_db_detach (struct process_info *proc) | |
911 | { | |
f9e39928 PA |
912 | struct thread_db *thread_db = proc->private->thread_db; |
913 | ||
914 | if (thread_db) | |
915 | { | |
916 | disable_thread_event_reporting (proc); | |
917 | remove_thread_event_breakpoints (proc); | |
918 | } | |
8336d594 PA |
919 | } |
920 | ||
921 | /* Disconnect from libthread_db and free resources. */ | |
922 | ||
923 | void | |
924 | thread_db_mourn (struct process_info *proc) | |
925 | { | |
926 | struct thread_db *thread_db = proc->private->thread_db; | |
927 | if (thread_db) | |
928 | { | |
929 | td_err_e (*td_ta_delete_p) (td_thragent_t *); | |
930 | ||
931 | #ifndef USE_LIBTHREAD_DB_DIRECTLY | |
932 | td_ta_delete_p = dlsym (thread_db->handle, "td_ta_delete"); | |
933 | #else | |
934 | td_ta_delete_p = &td_ta_delete; | |
935 | #endif | |
cdbfd419 | 936 | |
cdbfd419 PP |
937 | if (td_ta_delete_p != NULL) |
938 | (*td_ta_delete_p) (thread_db->thread_agent); | |
939 | ||
fd7dd3e6 | 940 | #ifndef USE_LIBTHREAD_DB_DIRECTLY |
cdbfd419 | 941 | dlclose (thread_db->handle); |
96f15937 PP |
942 | #endif /* USE_LIBTHREAD_DB_DIRECTLY */ |
943 | ||
cdbfd419 PP |
944 | free (thread_db); |
945 | proc->private->thread_db = NULL; | |
946 | } | |
947 | } | |
948 | ||
949 | /* Handle "set libthread-db-search-path" monitor command and return 1. | |
950 | For any other command, return 0. */ | |
951 | ||
952 | int | |
953 | thread_db_handle_monitor_command (char *mon) | |
954 | { | |
84e578fb DE |
955 | const char *cmd = "set libthread-db-search-path"; |
956 | size_t cmd_len = strlen (cmd); | |
957 | ||
958 | if (strncmp (mon, cmd, cmd_len) == 0 | |
959 | && (mon[cmd_len] == '\0' | |
960 | || mon[cmd_len] == ' ')) | |
cdbfd419 | 961 | { |
84e578fb | 962 | const char *cp = mon + cmd_len; |
cdbfd419 PP |
963 | |
964 | if (libthread_db_search_path != NULL) | |
965 | free (libthread_db_search_path); | |
966 | ||
967 | /* Skip leading space (if any). */ | |
968 | while (isspace (*cp)) | |
969 | ++cp; | |
970 | ||
84e578fb DE |
971 | if (*cp == '\0') |
972 | cp = LIBTHREAD_DB_SEARCH_PATH; | |
cdbfd419 PP |
973 | libthread_db_search_path = xstrdup (cp); |
974 | ||
975 | monitor_output ("libthread-db-search-path set to `"); | |
976 | monitor_output (libthread_db_search_path); | |
977 | monitor_output ("'\n"); | |
978 | return 1; | |
0d62e5e8 DJ |
979 | } |
980 | ||
cdbfd419 | 981 | /* Tell server.c to perform default processing. */ |
0d62e5e8 DJ |
982 | return 0; |
983 | } |