1 /* Thread management interface, for the remote server for GDB.
2 Copyright (C) 2002, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4 Contributed by MontaVista Software.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "linux-low.h"
25 extern int debug_threads;
27 static int thread_db_use_events;
29 #ifdef HAVE_THREAD_DB_H
30 #include <thread_db.h>
33 #include "gdb_proc_service.h"
37 /* Structure that identifies the child process for the
38 <proc_service.h> interface. */
39 static struct ps_prochandle proc_handle;
41 /* Connection to the libthread_db library. */
42 static td_thragent_t *thread_agent;
44 static int find_one_thread (int);
45 static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data);
48 thread_db_err_str (td_err_e err)
55 return "generic 'call succeeded'";
57 return "generic error";
59 return "no thread to satisfy query";
61 return "no sync handle to satisfy query";
63 return "no LWP to satisfy query";
65 return "invalid process handle";
67 return "invalid thread handle";
69 return "invalid synchronization handle";
71 return "invalid thread agent";
75 return "no event message for getmsg";
77 return "FPU register set not available";
79 return "application not linked with libthread";
81 return "requested event is not supported";
83 return "capability not available";
85 return "debugger service failed";
87 return "operation not applicable to";
89 return "no thread-specific data for this thread";
91 return "malloc failed";
93 return "only part of register set was written/read";
95 return "X register set not available for this thread";
96 #ifdef HAVE_TD_VERSION
98 return "version mismatch between libthread_db and libpthread";
101 snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
108 thread_db_state_str (td_thr_state_e state)
115 return "stopped by debugger";
124 case TD_THR_STOPPED_ASLEEP:
125 return "stopped by debugger AND blocked";
127 snprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
134 thread_db_create_event (CORE_ADDR where)
138 struct process_info *process;
141 fprintf (stderr, "Thread creation event.\n");
143 /* FIXME: This assumes we don't get another event.
144 In the LinuxThreads implementation, this is safe,
145 because all events come from the manager thread
146 (except for its own creation, of course). */
147 err = td_ta_event_getmsg (thread_agent, &msg);
149 fprintf (stderr, "thread getmsg err: %s\n",
150 thread_db_err_str (err));
152 /* If we do not know about the main thread yet, this would be a good time to
153 find it. We need to do this to pick up the main thread before any newly
155 process = get_thread_process (current_inferior);
156 if (process->thread_known == 0)
157 find_one_thread (process->lwpid);
159 /* msg.event == TD_EVENT_CREATE */
161 find_new_threads_callback (msg.th_p, NULL);
166 thread_db_death_event (CORE_ADDR where)
169 fprintf (stderr, "Thread death event.\n");
174 thread_db_enable_reporting ()
176 td_thr_events_t events;
180 /* Set the process wide mask saying which events we're interested in. */
181 td_event_emptyset (&events);
182 td_event_addset (&events, TD_CREATE);
185 /* This is reported to be broken in glibc 2.1.3. A different approach
186 will be necessary to support that. */
187 td_event_addset (&events, TD_DEATH);
190 err = td_ta_set_event (thread_agent, &events);
193 warning ("Unable to set global thread event mask: %s",
194 thread_db_err_str (err));
198 /* Get address for thread creation breakpoint. */
199 err = td_ta_event_addr (thread_agent, TD_CREATE, ¬ify);
202 warning ("Unable to get location for thread creation breakpoint: %s",
203 thread_db_err_str (err));
206 set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr,
207 thread_db_create_event);
210 /* Don't concern ourselves with reported thread deaths, only
211 with actual thread deaths (via wait). */
213 /* Get address for thread death breakpoint. */
214 err = td_ta_event_addr (thread_agent, TD_DEATH, ¬ify);
217 warning ("Unable to get location for thread death breakpoint: %s",
218 thread_db_err_str (err));
221 set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr,
222 thread_db_death_event);
229 find_one_thread (int lwpid)
234 struct thread_info *inferior;
235 struct process_info *process;
237 inferior = (struct thread_info *) find_inferior_id (&all_threads, lwpid);
238 process = get_thread_process (inferior);
239 if (process->thread_known)
242 /* Get information about this thread. */
243 err = td_ta_map_lwp2thr (thread_agent, process->lwpid, &th);
245 error ("Cannot get thread handle for LWP %d: %s",
246 lwpid, thread_db_err_str (err));
248 err = td_thr_get_info (&th, &ti);
250 error ("Cannot get thread info for LWP %d: %s",
251 lwpid, thread_db_err_str (err));
254 fprintf (stderr, "Found thread %ld (LWP %d)\n",
255 ti.ti_tid, ti.ti_lid);
257 if (process->lwpid != ti.ti_lid)
259 warning ("PID mismatch! Expected %ld, got %ld",
260 (long) process->lwpid, (long) ti.ti_lid);
264 if (thread_db_use_events)
266 err = td_thr_event_enable (&th, 1);
268 error ("Cannot enable thread event reporting for %d: %s",
269 ti.ti_lid, thread_db_err_str (err));
272 /* If the new thread ID is zero, a final thread ID will be available
273 later. Do not enable thread debugging yet. */
277 process->thread_known = 1;
278 process->tid = ti.ti_tid;
285 maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
288 struct thread_info *inferior;
289 struct process_info *process;
291 inferior = (struct thread_info *) find_inferior_id (&all_threads,
293 if (inferior != NULL)
297 fprintf (stderr, "Attaching to thread %ld (LWP %d)\n",
298 ti_p->ti_tid, ti_p->ti_lid);
299 linux_attach_lwp (ti_p->ti_lid);
300 inferior = (struct thread_info *) find_inferior_id (&all_threads,
302 if (inferior == NULL)
304 warning ("Could not attach to thread %ld (LWP %d)\n",
305 ti_p->ti_tid, ti_p->ti_lid);
309 process = inferior_target_data (inferior);
311 process->tid = ti_p->ti_tid;
312 process->thread_known = 1;
315 if (thread_db_use_events)
317 err = td_thr_event_enable (th_p, 1);
319 error ("Cannot enable thread event reporting for %d: %s",
320 ti_p->ti_lid, thread_db_err_str (err));
325 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
330 err = td_thr_get_info (th_p, &ti);
332 error ("Cannot get thread info: %s", thread_db_err_str (err));
334 /* Check for zombies. */
335 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
338 maybe_attach_thread (th_p, &ti);
344 thread_db_find_new_threads (void)
348 /* This function is only called when we first initialize thread_db.
349 First locate the initial thread. If it is not ready for
350 debugging yet, then stop. */
351 if (find_one_thread (all_threads.head->id) == 0)
354 /* Iterate over all user-space threads to discover new threads. */
355 err = td_ta_thr_iter (thread_agent, find_new_threads_callback, NULL,
356 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
357 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
359 error ("Cannot find new threads: %s", thread_db_err_str (err));
362 /* Cache all future symbols that thread_db might request. We can not
363 request symbols at arbitrary states in the remote protocol, only
364 when the client tells us that new symbols are available. So when
365 we load the thread library, make sure to check the entire list. */
368 thread_db_look_up_symbols (void)
370 const char **sym_list = td_symbol_list ();
373 for (sym_list = td_symbol_list (); *sym_list; sym_list++)
374 look_up_one_symbol (*sym_list, &unused);
378 thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
379 CORE_ADDR load_module, CORE_ADDR *address)
381 #if HAVE_TD_THR_TLS_GET_ADDR
384 struct process_info *process;
386 process = get_thread_process (thread);
387 if (!process->thread_known)
388 find_one_thread (process->lwpid);
389 if (!process->thread_known)
392 /* Note the cast through uintptr_t: this interface only works if
393 a target address fits in a psaddr_t, which is a host pointer.
394 So a 32-bit debugger can not access 64-bit TLS through this. */
395 err = td_thr_tls_get_addr (&process->th, (psaddr_t) (uintptr_t) load_module,
399 *address = (CORE_ADDR) (uintptr_t) addr;
410 thread_db_init (int use_events)
414 /* FIXME drow/2004-10-16: This is the "overall process ID", which
415 GNU/Linux calls tgid, "thread group ID". When we support
416 attaching to threads, the original thread may not be the correct
417 thread. We would have to get the process ID from /proc for NPTL.
418 For LinuxThreads we could do something similar: follow the chain
419 of parent processes until we find the highest one we're attached
420 to, and use its tgid.
422 This isn't the only place in gdbserver that assumes that the first
423 process in the list is the thread group leader. */
424 proc_handle.pid = ((struct inferior_list_entry *)current_inferior)->id;
426 /* Allow new symbol lookups. */
427 all_symbols_looked_up = 0;
429 thread_db_use_events = use_events;
431 err = td_ta_new (&proc_handle, &thread_agent);
435 /* No thread library was detected. */
439 /* The thread library was detected. */
441 if (use_events && thread_db_enable_reporting () == 0)
443 thread_db_find_new_threads ();
444 thread_db_look_up_symbols ();
445 all_symbols_looked_up = 1;
449 warning ("error initializing thread_db library: %s",
450 thread_db_err_str (err));