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 static int find_one_thread (ptid_t
);
39 static int find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
);
42 thread_db_err_str (td_err_e err
)
49 return "generic 'call succeeded'";
51 return "generic error";
53 return "no thread to satisfy query";
55 return "no sync handle to satisfy query";
57 return "no LWP to satisfy query";
59 return "invalid process handle";
61 return "invalid thread handle";
63 return "invalid synchronization handle";
65 return "invalid thread agent";
69 return "no event message for getmsg";
71 return "FPU register set not available";
73 return "application not linked with libthread";
75 return "requested event is not supported";
77 return "capability not available";
79 return "debugger service failed";
81 return "operation not applicable to";
83 return "no thread-specific data for this thread";
85 return "malloc failed";
87 return "only part of register set was written/read";
89 return "X register set not available for this thread";
90 #ifdef HAVE_TD_VERSION
92 return "version mismatch between libthread_db and libpthread";
95 snprintf (buf
, sizeof (buf
), "unknown thread_db error '%d'", err
);
102 thread_db_state_str (td_thr_state_e state
)
109 return "stopped by debugger";
118 case TD_THR_STOPPED_ASLEEP
:
119 return "stopped by debugger AND blocked";
121 snprintf (buf
, sizeof (buf
), "unknown thread_db state %d", state
);
128 thread_db_create_event (CORE_ADDR where
)
132 struct lwp_info
*lwp
;
133 struct process_info_private
*proc
= current_process()->private;
136 fprintf (stderr
, "Thread creation event.\n");
138 /* FIXME: This assumes we don't get another event.
139 In the LinuxThreads implementation, this is safe,
140 because all events come from the manager thread
141 (except for its own creation, of course). */
142 err
= td_ta_event_getmsg (proc
->thread_agent
, &msg
);
144 fprintf (stderr
, "thread getmsg err: %s\n",
145 thread_db_err_str (err
));
147 /* If we do not know about the main thread yet, this would be a good time to
148 find it. We need to do this to pick up the main thread before any newly
150 lwp
= get_thread_lwp (current_inferior
);
151 if (lwp
->thread_known
== 0)
152 find_one_thread (lwp
->head
.id
);
154 /* msg.event == TD_EVENT_CREATE */
156 find_new_threads_callback (msg
.th_p
, NULL
);
163 thread_db_death_event (CORE_ADDR where
)
166 fprintf (stderr
, "Thread death event.\n");
173 thread_db_enable_reporting ()
175 td_thr_events_t events
;
178 struct process_info_private
*proc
= current_process()->private;
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 (proc
->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 (proc
->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 (proc
->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 (ptid_t ptid
)
234 struct thread_info
*inferior
;
235 struct lwp_info
*lwp
;
236 struct process_info_private
*proc
;
237 int lwpid
= ptid_get_lwp (ptid
);
239 inferior
= (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
240 lwp
= get_thread_lwp (inferior
);
241 if (lwp
->thread_known
)
244 /* Get information about this thread. */
245 proc
= get_thread_process (inferior
)->private;
246 err
= td_ta_map_lwp2thr (proc
->thread_agent
, lwpid
, &th
);
248 error ("Cannot get thread handle for LWP %d: %s",
249 lwpid
, thread_db_err_str (err
));
251 err
= td_thr_get_info (&th
, &ti
);
253 error ("Cannot get thread info for LWP %d: %s",
254 lwpid
, thread_db_err_str (err
));
257 fprintf (stderr
, "Found thread %ld (LWP %d)\n",
258 ti
.ti_tid
, ti
.ti_lid
);
260 if (lwpid
!= ti
.ti_lid
)
262 warning ("PID mismatch! Expected %ld, got %ld",
263 (long) lwpid
, (long) ti
.ti_lid
);
267 if (thread_db_use_events
)
269 err
= td_thr_event_enable (&th
, 1);
271 error ("Cannot enable thread event reporting for %d: %s",
272 ti
.ti_lid
, thread_db_err_str (err
));
275 /* If the new thread ID is zero, a final thread ID will be available
276 later. Do not enable thread debugging yet. */
280 lwp
->thread_known
= 1;
287 maybe_attach_thread (const td_thrhandle_t
*th_p
, td_thrinfo_t
*ti_p
)
290 struct lwp_info
*lwp
;
292 lwp
= find_lwp_pid (pid_to_ptid (ti_p
->ti_lid
));
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 lwp
= find_lwp_pid (pid_to_ptid (ti_p
->ti_lid
));
303 warning ("Could not attach to thread %ld (LWP %d)\n",
304 ti_p
->ti_tid
, ti_p
->ti_lid
);
308 lwp
->thread_known
= 1;
311 if (thread_db_use_events
)
313 err
= td_thr_event_enable (th_p
, 1);
315 error ("Cannot enable thread event reporting for %d: %s",
316 ti_p
->ti_lid
, thread_db_err_str (err
));
321 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
326 err
= td_thr_get_info (th_p
, &ti
);
328 error ("Cannot get thread info: %s", thread_db_err_str (err
));
330 /* Check for zombies. */
331 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
334 maybe_attach_thread (th_p
, &ti
);
340 thread_db_find_new_threads (void)
343 ptid_t ptid
= ((struct inferior_list_entry
*) current_inferior
)->id
;
344 struct process_info_private
*proc
= current_process()->private;
346 /* This function is only called when we first initialize thread_db.
347 First locate the initial thread. If it is not ready for
348 debugging yet, then stop. */
349 if (find_one_thread (ptid
) == 0)
352 /* Iterate over all user-space threads to discover new threads. */
353 err
= td_ta_thr_iter (proc
->thread_agent
,
354 find_new_threads_callback
, NULL
,
355 TD_THR_ANY_STATE
, TD_THR_LOWEST_PRIORITY
,
356 TD_SIGNO_MASK
, TD_THR_ANY_USER_FLAGS
);
358 error ("Cannot find new threads: %s", thread_db_err_str (err
));
361 /* Cache all future symbols that thread_db might request. We can not
362 request symbols at arbitrary states in the remote protocol, only
363 when the client tells us that new symbols are available. So when
364 we load the thread library, make sure to check the entire list. */
367 thread_db_look_up_symbols (void)
369 const char **sym_list
= td_symbol_list ();
372 for (sym_list
= td_symbol_list (); *sym_list
; sym_list
++)
373 look_up_one_symbol (*sym_list
, &unused
);
377 thread_db_get_tls_address (struct thread_info
*thread
, CORE_ADDR offset
,
378 CORE_ADDR load_module
, CORE_ADDR
*address
)
380 #if HAVE_TD_THR_TLS_GET_ADDR
383 struct lwp_info
*lwp
;
384 struct thread_info
*saved_inferior
;
386 /* If the thread layer is not (yet) initialized, fail. */
387 if (!get_thread_process (thread
)->all_symbols_looked_up
)
390 lwp
= get_thread_lwp (thread
);
391 if (!lwp
->thread_known
)
392 find_one_thread (lwp
->head
.id
);
393 if (!lwp
->thread_known
)
396 saved_inferior
= current_inferior
;
397 current_inferior
= thread
;
398 /* Note the cast through uintptr_t: this interface only works if
399 a target address fits in a psaddr_t, which is a host pointer.
400 So a 32-bit debugger can not access 64-bit TLS through this. */
401 err
= td_thr_tls_get_addr (&lwp
->th
, (psaddr_t
) (uintptr_t) load_module
,
403 current_inferior
= saved_inferior
;
406 *address
= (CORE_ADDR
) (uintptr_t) addr
;
417 thread_db_init (int use_events
)
420 struct process_info
*proc
= current_process ();
421 struct process_info_private
*priv
= proc
->private;
423 /* FIXME drow/2004-10-16: This is the "overall process ID", which
424 GNU/Linux calls tgid, "thread group ID". When we support
425 attaching to threads, the original thread may not be the correct
426 thread. We would have to get the process ID from /proc for NPTL.
427 For LinuxThreads we could do something similar: follow the chain
428 of parent processes until we find the highest one we're attached
429 to, and use its tgid.
431 This isn't the only place in gdbserver that assumes that the first
432 process in the list is the thread group leader. */
434 thread_db_use_events
= use_events
;
436 err
= td_ta_new (&priv
->proc_handle
, &priv
->thread_agent
);
440 /* No thread library was detected. */
444 /* The thread library was detected. */
446 if (use_events
&& thread_db_enable_reporting () == 0)
448 thread_db_find_new_threads ();
449 thread_db_look_up_symbols ();
450 proc
->all_symbols_looked_up
= 1;
454 warning ("error initializing thread_db library: %s",
455 thread_db_err_str (err
));