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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
25 #include "linux-low.h"
27 extern int debug_threads
;
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_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
);
47 thread_db_err_str (td_err_e err
)
54 return "generic 'call succeeded'";
56 return "generic error";
58 return "no thread to satisfy query";
60 return "no sync handle to satisfy query";
62 return "no LWP to satisfy query";
64 return "invalid process handle";
66 return "invalid thread handle";
68 return "invalid synchronization handle";
70 return "invalid thread agent";
74 return "no event message for getmsg";
76 return "FPU register set not available";
78 return "application not linked with libthread";
80 return "requested event is not supported";
82 return "capability not available";
84 return "debugger service failed";
86 return "operation not applicable to";
88 return "no thread-specific data for this thread";
90 return "malloc failed";
92 return "only part of register set was written/read";
94 return "X register set not available for this thread";
95 #ifdef HAVE_TD_VERSION
97 return "version mismatch between libthread_db and libpthread";
100 snprintf (buf
, sizeof (buf
), "unknown thread_db error '%d'", err
);
107 thread_db_state_str (td_thr_state_e state
)
114 return "stopped by debugger";
123 case TD_THR_STOPPED_ASLEEP
:
124 return "stopped by debugger AND blocked";
126 snprintf (buf
, sizeof (buf
), "unknown thread_db state %d", state
);
133 thread_db_create_event (CORE_ADDR where
)
137 struct inferior_linux_data
*tdata
;
140 fprintf (stderr
, "Thread creation event.\n");
142 tdata
= inferior_target_data (current_inferior
);
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 /* msg.event == TD_EVENT_CREATE */
155 find_new_threads_callback (msg
.th_p
, NULL
);
160 thread_db_death_event (CORE_ADDR where
)
163 fprintf (stderr
, "Thread death event.\n");
168 thread_db_enable_reporting ()
170 td_thr_events_t events
;
174 /* Set the process wide mask saying which events we're interested in. */
175 td_event_emptyset (&events
);
176 td_event_addset (&events
, TD_CREATE
);
179 /* This is reported to be broken in glibc 2.1.3. A different approach
180 will be necessary to support that. */
181 td_event_addset (&events
, TD_DEATH
);
184 err
= td_ta_set_event (thread_agent
, &events
);
187 warning ("Unable to set global thread event mask: %s",
188 thread_db_err_str (err
));
192 /* Get address for thread creation breakpoint. */
193 err
= td_ta_event_addr (thread_agent
, TD_CREATE
, ¬ify
);
196 warning ("Unable to get location for thread creation breakpoint: %s",
197 thread_db_err_str (err
));
200 set_breakpoint_at ((CORE_ADDR
) (unsigned long) notify
.u
.bptaddr
,
201 thread_db_create_event
);
204 /* Don't concern ourselves with reported thread deaths, only
205 with actual thread deaths (via wait). */
207 /* Get address for thread death breakpoint. */
208 err
= td_ta_event_addr (thread_agent
, TD_DEATH
, ¬ify
);
211 warning ("Unable to get location for thread death breakpoint: %s",
212 thread_db_err_str (err
));
215 set_breakpoint_at ((CORE_ADDR
) (unsigned long) notify
.u
.bptaddr
,
216 thread_db_death_event
);
223 maybe_attach_thread (const td_thrhandle_t
*th_p
, td_thrinfo_t
*ti_p
)
226 struct thread_info
*inferior
;
227 struct process_info
*process
;
229 /* If we are attaching to our first thread, things are a little
231 if (all_threads
.head
== all_threads
.tail
)
233 inferior
= (struct thread_info
*) all_threads
.head
;
234 process
= get_thread_process (inferior
);
235 if (process
->thread_known
== 0)
237 /* Switch to indexing the threads list by TID. */
238 change_inferior_id (&all_threads
, ti_p
->ti_tid
);
243 inferior
= (struct thread_info
*) find_inferior_id (&all_threads
,
245 if (inferior
!= NULL
)
249 fprintf (stderr
, "Attaching to thread %ld (LWP %d)\n",
250 ti_p
->ti_tid
, ti_p
->ti_lid
);
251 linux_attach_lwp (ti_p
->ti_lid
, ti_p
->ti_tid
);
252 inferior
= (struct thread_info
*) find_inferior_id (&all_threads
,
254 if (inferior
== NULL
)
256 warning ("Could not attach to thread %ld (LWP %d)\n",
257 ti_p
->ti_tid
, ti_p
->ti_lid
);
261 process
= inferior_target_data (inferior
);
264 new_thread_notify (ti_p
->ti_tid
);
266 process
->tid
= ti_p
->ti_tid
;
267 process
->lwpid
= ti_p
->ti_lid
;
269 process
->thread_known
= 1;
271 err
= td_thr_event_enable (th_p
, 1);
273 error ("Cannot enable thread event reporting for %d: %s",
274 ti_p
->ti_lid
, thread_db_err_str (err
));
278 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
283 err
= td_thr_get_info (th_p
, &ti
);
285 error ("Cannot get thread info: %s", thread_db_err_str (err
));
287 /* Check for zombies. */
288 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
291 maybe_attach_thread (th_p
, &ti
);
297 thread_db_find_new_threads (void)
301 /* Iterate over all user-space threads to discover new threads. */
302 err
= td_ta_thr_iter (thread_agent
, find_new_threads_callback
, NULL
,
303 TD_THR_ANY_STATE
, TD_THR_LOWEST_PRIORITY
,
304 TD_SIGNO_MASK
, TD_THR_ANY_USER_FLAGS
);
306 error ("Cannot find new threads: %s", thread_db_err_str (err
));
309 /* Cache all future symbols that thread_db might request. We can not
310 request symbols at arbitrary states in the remote protocol, only
311 when the client tells us that new symbols are available. So when
312 we load the thread library, make sure to check the entire list. */
315 thread_db_look_up_symbols (void)
317 const char **sym_list
= td_symbol_list ();
320 for (sym_list
= td_symbol_list (); *sym_list
; sym_list
++)
321 look_up_one_symbol (*sym_list
, &unused
);
325 thread_db_get_tls_address (struct thread_info
*thread
, CORE_ADDR offset
,
326 CORE_ADDR load_module
, CORE_ADDR
*address
)
328 #if HAVE_TD_THR_TLS_GET_ADDR
331 struct process_info
*process
;
333 process
= get_thread_process (thread
);
334 if (!process
->thread_known
)
337 /* Note the cast through uintptr_t: this interface only works if
338 a target address fits in a psaddr_t, which is a host pointer.
339 So a 32-bit debugger can not access 64-bit TLS through this. */
340 err
= td_thr_tls_get_addr (&process
->th
, (psaddr_t
) (uintptr_t) load_module
,
344 *address
= (CORE_ADDR
) (uintptr_t) addr
;
359 /* FIXME drow/2004-10-16: This is the "overall process ID", which
360 GNU/Linux calls tgid, "thread group ID". When we support
361 attaching to threads, the original thread may not be the correct
362 thread. We would have to get the process ID from /proc for NPTL.
363 For LinuxThreads we could do something similar: follow the chain
364 of parent processes until we find the highest one we're attached
365 to, and use its tgid.
367 This isn't the only place in gdbserver that assumes that the first
368 process in the list is the thread group leader. */
369 proc_handle
.pid
= ((struct inferior_list_entry
*)current_inferior
)->id
;
371 /* Allow new symbol lookups. */
372 all_symbols_looked_up
= 0;
374 err
= td_ta_new (&proc_handle
, &thread_agent
);
378 /* No thread library was detected. */
382 /* The thread library was detected. */
384 if (thread_db_enable_reporting () == 0)
386 thread_db_find_new_threads ();
387 thread_db_look_up_symbols ();
388 all_symbols_looked_up
= 1;
392 warning ("error initializing thread_db library: %s",
393 thread_db_err_str (err
));