1 /* Thread management interface, for the remote server for GDB.
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
5 Contributed by MontaVista Software.
7 This file is part of GDB.
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
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "linux-low.h"
26 extern int debug_threads;
28 static int thread_db_use_events;
30 #ifdef HAVE_THREAD_DB_H
31 #include <thread_db.h>
34 #include "gdb_proc_service.h"
38 /* Structure that identifies the child process for the
39 <proc_service.h> interface. */
40 static struct ps_prochandle proc_handle;
42 /* Connection to the libthread_db library. */
43 static td_thragent_t *thread_agent;
45 static int find_one_thread (int);
46 static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data);
49 thread_db_err_str (td_err_e err)
56 return "generic 'call succeeded'";
58 return "generic error";
60 return "no thread to satisfy query";
62 return "no sync handle to satisfy query";
64 return "no LWP to satisfy query";
66 return "invalid process handle";
68 return "invalid thread handle";
70 return "invalid synchronization handle";
72 return "invalid thread agent";
76 return "no event message for getmsg";
78 return "FPU register set not available";
80 return "application not linked with libthread";
82 return "requested event is not supported";
84 return "capability not available";
86 return "debugger service failed";
88 return "operation not applicable to";
90 return "no thread-specific data for this thread";
92 return "malloc failed";
94 return "only part of register set was written/read";
96 return "X register set not available for this thread";
97 #ifdef HAVE_TD_VERSION
99 return "version mismatch between libthread_db and libpthread";
102 snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
109 thread_db_state_str (td_thr_state_e state)
116 return "stopped by debugger";
125 case TD_THR_STOPPED_ASLEEP:
126 return "stopped by debugger AND blocked";
128 snprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
135 thread_db_create_event (CORE_ADDR where)
139 struct lwp_info *lwp;
142 fprintf (stderr, "Thread creation event.\n");
144 /* FIXME: This assumes we don't get another event.
145 In the LinuxThreads implementation, this is safe,
146 because all events come from the manager thread
147 (except for its own creation, of course). */
148 err = td_ta_event_getmsg (thread_agent, &msg);
150 fprintf (stderr, "thread getmsg err: %s\n",
151 thread_db_err_str (err));
153 /* If we do not know about the main thread yet, this would be a good time to
154 find it. We need to do this to pick up the main thread before any newly
156 lwp = get_thread_lwp (current_inferior);
157 if (lwp->thread_known == 0)
158 find_one_thread (lwp->lwpid);
160 /* msg.event == TD_EVENT_CREATE */
162 find_new_threads_callback (msg.th_p, NULL);
169 thread_db_death_event (CORE_ADDR where)
172 fprintf (stderr, "Thread death event.\n");
179 thread_db_enable_reporting ()
181 td_thr_events_t events;
185 /* Set the process wide mask saying which events we're interested in. */
186 td_event_emptyset (&events);
187 td_event_addset (&events, TD_CREATE);
190 /* This is reported to be broken in glibc 2.1.3. A different approach
191 will be necessary to support that. */
192 td_event_addset (&events, TD_DEATH);
195 err = td_ta_set_event (thread_agent, &events);
198 warning ("Unable to set global thread event mask: %s",
199 thread_db_err_str (err));
203 /* Get address for thread creation breakpoint. */
204 err = td_ta_event_addr (thread_agent, TD_CREATE, ¬ify);
207 warning ("Unable to get location for thread creation breakpoint: %s",
208 thread_db_err_str (err));
211 set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr,
212 thread_db_create_event);
215 /* Don't concern ourselves with reported thread deaths, only
216 with actual thread deaths (via wait). */
218 /* Get address for thread death breakpoint. */
219 err = td_ta_event_addr (thread_agent, TD_DEATH, ¬ify);
222 warning ("Unable to get location for thread death breakpoint: %s",
223 thread_db_err_str (err));
226 set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr,
227 thread_db_death_event);
234 find_one_thread (int lwpid)
239 struct thread_info *inferior;
240 struct lwp_info *lwp;
242 inferior = (struct thread_info *) find_inferior_id (&all_threads, lwpid);
243 lwp = get_thread_lwp (inferior);
244 if (lwp->thread_known)
247 /* Get information about this thread. */
248 err = td_ta_map_lwp2thr (thread_agent, lwp->lwpid, &th);
250 error ("Cannot get thread handle for LWP %d: %s",
251 lwpid, thread_db_err_str (err));
253 err = td_thr_get_info (&th, &ti);
255 error ("Cannot get thread info for LWP %d: %s",
256 lwpid, thread_db_err_str (err));
259 fprintf (stderr, "Found thread %ld (LWP %d)\n",
260 ti.ti_tid, ti.ti_lid);
262 if (lwp->lwpid != ti.ti_lid)
264 warning ("PID mismatch! Expected %ld, got %ld",
265 (long) lwp->lwpid, (long) ti.ti_lid);
269 if (thread_db_use_events)
271 err = td_thr_event_enable (&th, 1);
273 error ("Cannot enable thread event reporting for %d: %s",
274 ti.ti_lid, thread_db_err_str (err));
277 /* If the new thread ID is zero, a final thread ID will be available
278 later. Do not enable thread debugging yet. */
282 lwp->thread_known = 1;
289 maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
292 struct thread_info *inferior;
293 struct lwp_info *lwp;
295 inferior = (struct thread_info *) find_inferior_id (&all_threads,
297 if (inferior != NULL)
301 fprintf (stderr, "Attaching to thread %ld (LWP %d)\n",
302 ti_p->ti_tid, ti_p->ti_lid);
303 linux_attach_lwp (ti_p->ti_lid);
304 inferior = (struct thread_info *) find_inferior_id (&all_threads,
306 if (inferior == NULL)
308 warning ("Could not attach to thread %ld (LWP %d)\n",
309 ti_p->ti_tid, ti_p->ti_lid);
313 lwp = inferior_target_data (inferior);
315 lwp->thread_known = 1;
318 if (thread_db_use_events)
320 err = td_thr_event_enable (th_p, 1);
322 error ("Cannot enable thread event reporting for %d: %s",
323 ti_p->ti_lid, thread_db_err_str (err));
328 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
333 err = td_thr_get_info (th_p, &ti);
335 error ("Cannot get thread info: %s", thread_db_err_str (err));
337 /* Check for zombies. */
338 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
341 maybe_attach_thread (th_p, &ti);
347 thread_db_find_new_threads (void)
351 /* This function is only called when we first initialize thread_db.
352 First locate the initial thread. If it is not ready for
353 debugging yet, then stop. */
354 if (find_one_thread (all_threads.head->id) == 0)
357 /* Iterate over all user-space threads to discover new threads. */
358 err = td_ta_thr_iter (thread_agent, find_new_threads_callback, NULL,
359 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
360 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
362 error ("Cannot find new threads: %s", thread_db_err_str (err));
365 /* Cache all future symbols that thread_db might request. We can not
366 request symbols at arbitrary states in the remote protocol, only
367 when the client tells us that new symbols are available. So when
368 we load the thread library, make sure to check the entire list. */
371 thread_db_look_up_symbols (void)
373 const char **sym_list = td_symbol_list ();
376 for (sym_list = td_symbol_list (); *sym_list; sym_list++)
377 look_up_one_symbol (*sym_list, &unused);
381 thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
382 CORE_ADDR load_module, CORE_ADDR *address)
384 #if HAVE_TD_THR_TLS_GET_ADDR
387 struct lwp_info *lwp;
389 lwp = get_thread_lwp (thread);
390 if (!lwp->thread_known)
391 find_one_thread (lwp->lwpid);
392 if (!lwp->thread_known)
395 /* Note the cast through uintptr_t: this interface only works if
396 a target address fits in a psaddr_t, which is a host pointer.
397 So a 32-bit debugger can not access 64-bit TLS through this. */
398 err = td_thr_tls_get_addr (&lwp->th, (psaddr_t) (uintptr_t) load_module,
402 *address = (CORE_ADDR) (uintptr_t) addr;
413 thread_db_init (int use_events)
417 /* FIXME drow/2004-10-16: This is the "overall process ID", which
418 GNU/Linux calls tgid, "thread group ID". When we support
419 attaching to threads, the original thread may not be the correct
420 thread. We would have to get the process ID from /proc for NPTL.
421 For LinuxThreads we could do something similar: follow the chain
422 of parent processes until we find the highest one we're attached
423 to, and use its tgid.
425 This isn't the only place in gdbserver that assumes that the first
426 process in the list is the thread group leader. */
427 proc_handle.pid = ((struct inferior_list_entry *)current_inferior)->id;
429 /* Allow new symbol lookups. */
430 all_symbols_looked_up = 0;
432 thread_db_use_events = use_events;
434 err = td_ta_new (&proc_handle, &thread_agent);
438 /* No thread library was detected. */
442 /* The thread library was detected. */
444 if (use_events && thread_db_enable_reporting () == 0)
446 thread_db_find_new_threads ();
447 thread_db_look_up_symbols ();
448 all_symbols_looked_up = 1;
452 warning ("error initializing thread_db library: %s",
453 thread_db_err_str (err));