2009-04-01 Pedro Alves <pedro@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / gdbserver / thread-db.c
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.
4
5 Contributed by MontaVista Software.
6
7 This file is part of GDB.
8
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.
13
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.
18
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/>. */
21
22 #include "server.h"
23
24 #include "linux-low.h"
25
26 extern int debug_threads;
27
28 static int thread_db_use_events;
29
30 #ifdef HAVE_THREAD_DB_H
31 #include <thread_db.h>
32 #endif
33
34 #include "gdb_proc_service.h"
35
36 #include <stdint.h>
37
38 static int find_one_thread (ptid_t);
39 static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data);
40
41 static const char *
42 thread_db_err_str (td_err_e err)
43 {
44 static char buf[64];
45
46 switch (err)
47 {
48 case TD_OK:
49 return "generic 'call succeeded'";
50 case TD_ERR:
51 return "generic error";
52 case TD_NOTHR:
53 return "no thread to satisfy query";
54 case TD_NOSV:
55 return "no sync handle to satisfy query";
56 case TD_NOLWP:
57 return "no LWP to satisfy query";
58 case TD_BADPH:
59 return "invalid process handle";
60 case TD_BADTH:
61 return "invalid thread handle";
62 case TD_BADSH:
63 return "invalid synchronization handle";
64 case TD_BADTA:
65 return "invalid thread agent";
66 case TD_BADKEY:
67 return "invalid key";
68 case TD_NOMSG:
69 return "no event message for getmsg";
70 case TD_NOFPREGS:
71 return "FPU register set not available";
72 case TD_NOLIBTHREAD:
73 return "application not linked with libthread";
74 case TD_NOEVENT:
75 return "requested event is not supported";
76 case TD_NOCAPAB:
77 return "capability not available";
78 case TD_DBERR:
79 return "debugger service failed";
80 case TD_NOAPLIC:
81 return "operation not applicable to";
82 case TD_NOTSD:
83 return "no thread-specific data for this thread";
84 case TD_MALLOC:
85 return "malloc failed";
86 case TD_PARTIALREG:
87 return "only part of register set was written/read";
88 case TD_NOXREGS:
89 return "X register set not available for this thread";
90 #ifdef HAVE_TD_VERSION
91 case TD_VERSION:
92 return "version mismatch between libthread_db and libpthread";
93 #endif
94 default:
95 snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
96 return buf;
97 }
98 }
99
100 #if 0
101 static char *
102 thread_db_state_str (td_thr_state_e state)
103 {
104 static char buf[64];
105
106 switch (state)
107 {
108 case TD_THR_STOPPED:
109 return "stopped by debugger";
110 case TD_THR_RUN:
111 return "runnable";
112 case TD_THR_ACTIVE:
113 return "active";
114 case TD_THR_ZOMBIE:
115 return "zombie";
116 case TD_THR_SLEEP:
117 return "sleeping";
118 case TD_THR_STOPPED_ASLEEP:
119 return "stopped by debugger AND blocked";
120 default:
121 snprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
122 return buf;
123 }
124 }
125 #endif
126
127 static int
128 thread_db_create_event (CORE_ADDR where)
129 {
130 td_event_msg_t msg;
131 td_err_e err;
132 struct lwp_info *lwp;
133 struct process_info_private *proc = current_process()->private;
134
135 if (debug_threads)
136 fprintf (stderr, "Thread creation event.\n");
137
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);
143 if (err != TD_OK)
144 fprintf (stderr, "thread getmsg err: %s\n",
145 thread_db_err_str (err));
146
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
149 created threads. */
150 lwp = get_thread_lwp (current_inferior);
151 if (lwp->thread_known == 0)
152 find_one_thread (lwp->head.id);
153
154 /* msg.event == TD_EVENT_CREATE */
155
156 find_new_threads_callback (msg.th_p, NULL);
157
158 return 0;
159 }
160
161 #if 0
162 static int
163 thread_db_death_event (CORE_ADDR where)
164 {
165 if (debug_threads)
166 fprintf (stderr, "Thread death event.\n");
167
168 return 0;
169 }
170 #endif
171
172 static int
173 thread_db_enable_reporting ()
174 {
175 td_thr_events_t events;
176 td_notify_t notify;
177 td_err_e err;
178 struct process_info_private *proc = current_process()->private;
179
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);
183
184 #if 0
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);
188 #endif
189
190 err = td_ta_set_event (proc->thread_agent, &events);
191 if (err != TD_OK)
192 {
193 warning ("Unable to set global thread event mask: %s",
194 thread_db_err_str (err));
195 return 0;
196 }
197
198 /* Get address for thread creation breakpoint. */
199 err = td_ta_event_addr (proc->thread_agent, TD_CREATE, &notify);
200 if (err != TD_OK)
201 {
202 warning ("Unable to get location for thread creation breakpoint: %s",
203 thread_db_err_str (err));
204 return 0;
205 }
206 set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr,
207 thread_db_create_event);
208
209 #if 0
210 /* Don't concern ourselves with reported thread deaths, only
211 with actual thread deaths (via wait). */
212
213 /* Get address for thread death breakpoint. */
214 err = td_ta_event_addr (proc->thread_agent, TD_DEATH, &notify);
215 if (err != TD_OK)
216 {
217 warning ("Unable to get location for thread death breakpoint: %s",
218 thread_db_err_str (err));
219 return;
220 }
221 set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr,
222 thread_db_death_event);
223 #endif
224
225 return 1;
226 }
227
228 static int
229 find_one_thread (ptid_t ptid)
230 {
231 td_thrhandle_t th;
232 td_thrinfo_t ti;
233 td_err_e err;
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);
238
239 inferior = (struct thread_info *) find_inferior_id (&all_threads, ptid);
240 lwp = get_thread_lwp (inferior);
241 if (lwp->thread_known)
242 return 1;
243
244 /* Get information about this thread. */
245 err = td_ta_map_lwp2thr (proc->thread_agent, lwpid, &th);
246 if (err != TD_OK)
247 error ("Cannot get thread handle for LWP %d: %s",
248 lwpid, thread_db_err_str (err));
249
250 err = td_thr_get_info (&th, &ti);
251 if (err != TD_OK)
252 error ("Cannot get thread info for LWP %d: %s",
253 lwpid, thread_db_err_str (err));
254
255 if (debug_threads)
256 fprintf (stderr, "Found thread %ld (LWP %d)\n",
257 ti.ti_tid, ti.ti_lid);
258
259 if (lwpid != ti.ti_lid)
260 {
261 warning ("PID mismatch! Expected %ld, got %ld",
262 (long) lwpid, (long) ti.ti_lid);
263 return 0;
264 }
265
266 if (thread_db_use_events)
267 {
268 err = td_thr_event_enable (&th, 1);
269 if (err != TD_OK)
270 error ("Cannot enable thread event reporting for %d: %s",
271 ti.ti_lid, thread_db_err_str (err));
272 }
273
274 /* If the new thread ID is zero, a final thread ID will be available
275 later. Do not enable thread debugging yet. */
276 if (ti.ti_tid == 0)
277 return 0;
278
279 lwp->thread_known = 1;
280 lwp->th = th;
281
282 return 1;
283 }
284
285 static void
286 maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
287 {
288 td_err_e err;
289 struct lwp_info *lwp;
290
291 lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid));
292 if (lwp != NULL)
293 return;
294
295 if (debug_threads)
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));
300 if (lwp == NULL)
301 {
302 warning ("Could not attach to thread %ld (LWP %d)\n",
303 ti_p->ti_tid, ti_p->ti_lid);
304 return;
305 }
306
307 lwp->thread_known = 1;
308 lwp->th = *th_p;
309
310 if (thread_db_use_events)
311 {
312 err = td_thr_event_enable (th_p, 1);
313 if (err != TD_OK)
314 error ("Cannot enable thread event reporting for %d: %s",
315 ti_p->ti_lid, thread_db_err_str (err));
316 }
317 }
318
319 static int
320 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
321 {
322 td_thrinfo_t ti;
323 td_err_e err;
324
325 err = td_thr_get_info (th_p, &ti);
326 if (err != TD_OK)
327 error ("Cannot get thread info: %s", thread_db_err_str (err));
328
329 /* Check for zombies. */
330 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
331 return 0;
332
333 maybe_attach_thread (th_p, &ti);
334
335 return 0;
336 }
337
338 static void
339 thread_db_find_new_threads (void)
340 {
341 td_err_e err;
342 ptid_t ptid = ((struct inferior_list_entry *) current_inferior)->id;
343 struct process_info_private *proc = current_process()->private;
344
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)
349 return;
350
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);
356 if (err != TD_OK)
357 error ("Cannot find new threads: %s", thread_db_err_str (err));
358 }
359
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. */
364
365 static void
366 thread_db_look_up_symbols (void)
367 {
368 const char **sym_list = td_symbol_list ();
369 CORE_ADDR unused;
370
371 for (sym_list = td_symbol_list (); *sym_list; sym_list++)
372 look_up_one_symbol (*sym_list, &unused);
373 }
374
375 int
376 thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
377 CORE_ADDR load_module, CORE_ADDR *address)
378 {
379 #if HAVE_TD_THR_TLS_GET_ADDR
380 psaddr_t addr;
381 td_err_e err;
382 struct lwp_info *lwp;
383 struct thread_info *saved_inferior;
384
385 lwp = get_thread_lwp (thread);
386 if (!lwp->thread_known)
387 find_one_thread (lwp->head.id);
388 if (!lwp->thread_known)
389 return TD_NOTHR;
390
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,
397 offset, &addr);
398 current_inferior = saved_inferior;
399 if (err == TD_OK)
400 {
401 *address = (CORE_ADDR) (uintptr_t) addr;
402 return 0;
403 }
404 else
405 return err;
406 #else
407 return -1;
408 #endif
409 }
410
411 int
412 thread_db_init (int use_events)
413 {
414 int err;
415 struct process_info *proc = current_process ();
416 struct process_info_private *priv = proc->private;
417
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.
425
426 This isn't the only place in gdbserver that assumes that the first
427 process in the list is the thread group leader. */
428
429 thread_db_use_events = use_events;
430
431 err = td_ta_new (&priv->proc_handle, &priv->thread_agent);
432 switch (err)
433 {
434 case TD_NOLIBTHREAD:
435 /* No thread library was detected. */
436 return 0;
437
438 case TD_OK:
439 /* The thread library was detected. */
440
441 if (use_events && thread_db_enable_reporting () == 0)
442 return 0;
443 thread_db_find_new_threads ();
444 thread_db_look_up_symbols ();
445 proc->all_symbols_looked_up = 1;
446 return 1;
447
448 default:
449 warning ("error initializing thread_db library: %s",
450 thread_db_err_str (err));
451 }
452
453 return 0;
454 }
This page took 0.039384 seconds and 5 git commands to generate.