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
= current_process()->private;
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 err
= td_ta_map_lwp2thr (proc
->thread_agent
, lwpid
, &th
);
247 error ("Cannot get thread handle for LWP %d: %s",
248 lwpid
, thread_db_err_str (err
));
250 err
= td_thr_get_info (&th
, &ti
);
252 error ("Cannot get thread info for LWP %d: %s",
253 lwpid
, thread_db_err_str (err
));
256 fprintf (stderr
, "Found thread %ld (LWP %d)\n",
257 ti
.ti_tid
, ti
.ti_lid
);
259 if (lwpid
!= ti
.ti_lid
)
261 warning ("PID mismatch! Expected %ld, got %ld",
262 (long) lwpid
, (long) ti
.ti_lid
);
266 if (thread_db_use_events
)
268 err
= td_thr_event_enable (&th
, 1);
270 error ("Cannot enable thread event reporting for %d: %s",
271 ti
.ti_lid
, thread_db_err_str (err
));
274 /* If the new thread ID is zero, a final thread ID will be available
275 later. Do not enable thread debugging yet. */
279 lwp
->thread_known
= 1;
286 maybe_attach_thread (const td_thrhandle_t
*th_p
, td_thrinfo_t
*ti_p
)
289 struct lwp_info
*lwp
;
291 lwp
= find_lwp_pid (pid_to_ptid (ti_p
->ti_lid
));
296 fprintf (stderr
, "Attaching to thread %ld (LWP %d)\n",
297 ti_p
->ti_tid
, ti_p
->ti_lid
);
298 linux_attach_lwp (ti_p
->ti_lid
);
299 lwp
= find_lwp_pid (pid_to_ptid (ti_p
->ti_lid
));
302 warning ("Could not attach to thread %ld (LWP %d)\n",
303 ti_p
->ti_tid
, ti_p
->ti_lid
);
307 lwp
->thread_known
= 1;
310 if (thread_db_use_events
)
312 err
= td_thr_event_enable (th_p
, 1);
314 error ("Cannot enable thread event reporting for %d: %s",
315 ti_p
->ti_lid
, thread_db_err_str (err
));
320 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
325 err
= td_thr_get_info (th_p
, &ti
);
327 error ("Cannot get thread info: %s", thread_db_err_str (err
));
329 /* Check for zombies. */
330 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
333 maybe_attach_thread (th_p
, &ti
);
339 thread_db_find_new_threads (void)
342 ptid_t ptid
= ((struct inferior_list_entry
*) current_inferior
)->id
;
343 struct process_info_private
*proc
= current_process()->private;
345 /* This function is only called when we first initialize thread_db.
346 First locate the initial thread. If it is not ready for
347 debugging yet, then stop. */
348 if (find_one_thread (ptid
) == 0)
351 /* Iterate over all user-space threads to discover new threads. */
352 err
= td_ta_thr_iter (proc
->thread_agent
,
353 find_new_threads_callback
, NULL
,
354 TD_THR_ANY_STATE
, TD_THR_LOWEST_PRIORITY
,
355 TD_SIGNO_MASK
, TD_THR_ANY_USER_FLAGS
);
357 error ("Cannot find new threads: %s", thread_db_err_str (err
));
360 /* Cache all future symbols that thread_db might request. We can not
361 request symbols at arbitrary states in the remote protocol, only
362 when the client tells us that new symbols are available. So when
363 we load the thread library, make sure to check the entire list. */
366 thread_db_look_up_symbols (void)
368 const char **sym_list
= td_symbol_list ();
371 for (sym_list
= td_symbol_list (); *sym_list
; sym_list
++)
372 look_up_one_symbol (*sym_list
, &unused
);
376 thread_db_get_tls_address (struct thread_info
*thread
, CORE_ADDR offset
,
377 CORE_ADDR load_module
, CORE_ADDR
*address
)
379 #if HAVE_TD_THR_TLS_GET_ADDR
382 struct lwp_info
*lwp
;
383 struct thread_info
*saved_inferior
;
385 lwp
= get_thread_lwp (thread
);
386 if (!lwp
->thread_known
)
387 find_one_thread (lwp
->head
.id
);
388 if (!lwp
->thread_known
)
391 saved_inferior
= current_inferior
;
392 current_inferior
= thread
;
393 /* Note the cast through uintptr_t: this interface only works if
394 a target address fits in a psaddr_t, which is a host pointer.
395 So a 32-bit debugger can not access 64-bit TLS through this. */
396 err
= td_thr_tls_get_addr (&lwp
->th
, (psaddr_t
) (uintptr_t) load_module
,
398 current_inferior
= saved_inferior
;
401 *address
= (CORE_ADDR
) (uintptr_t) addr
;
412 thread_db_init (int use_events
)
415 struct process_info
*proc
= current_process ();
416 struct process_info_private
*priv
= proc
->private;
418 /* FIXME drow/2004-10-16: This is the "overall process ID", which
419 GNU/Linux calls tgid, "thread group ID". When we support
420 attaching to threads, the original thread may not be the correct
421 thread. We would have to get the process ID from /proc for NPTL.
422 For LinuxThreads we could do something similar: follow the chain
423 of parent processes until we find the highest one we're attached
424 to, and use its tgid.
426 This isn't the only place in gdbserver that assumes that the first
427 process in the list is the thread group leader. */
429 thread_db_use_events
= use_events
;
431 err
= td_ta_new (&priv
->proc_handle
, &priv
->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 proc
->all_symbols_looked_up
= 1;
449 warning ("error initializing thread_db library: %s",
450 thread_db_err_str (err
));