]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/thread-db.c
4a26be951b26333b191020b7da44874940f99a55
[thirdparty/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;
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 proc = get_thread_process (inferior)->private;
246 err = td_ta_map_lwp2thr (proc->thread_agent, lwpid, &th);
247 if (err != TD_OK)
248 error ("Cannot get thread handle for LWP %d: %s",
249 lwpid, thread_db_err_str (err));
250
251 err = td_thr_get_info (&th, &ti);
252 if (err != TD_OK)
253 error ("Cannot get thread info for LWP %d: %s",
254 lwpid, thread_db_err_str (err));
255
256 if (debug_threads)
257 fprintf (stderr, "Found thread %ld (LWP %d)\n",
258 ti.ti_tid, ti.ti_lid);
259
260 if (lwpid != ti.ti_lid)
261 {
262 warning ("PID mismatch! Expected %ld, got %ld",
263 (long) lwpid, (long) ti.ti_lid);
264 return 0;
265 }
266
267 if (thread_db_use_events)
268 {
269 err = td_thr_event_enable (&th, 1);
270 if (err != TD_OK)
271 error ("Cannot enable thread event reporting for %d: %s",
272 ti.ti_lid, thread_db_err_str (err));
273 }
274
275 /* If the new thread ID is zero, a final thread ID will be available
276 later. Do not enable thread debugging yet. */
277 if (ti.ti_tid == 0)
278 return 0;
279
280 lwp->thread_known = 1;
281 lwp->th = th;
282
283 return 1;
284 }
285
286 static void
287 maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
288 {
289 td_err_e err;
290 struct lwp_info *lwp;
291
292 lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid));
293 if (lwp != NULL)
294 return;
295
296 if (debug_threads)
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));
301 if (lwp == NULL)
302 {
303 warning ("Could not attach to thread %ld (LWP %d)\n",
304 ti_p->ti_tid, ti_p->ti_lid);
305 return;
306 }
307
308 lwp->thread_known = 1;
309 lwp->th = *th_p;
310
311 if (thread_db_use_events)
312 {
313 err = td_thr_event_enable (th_p, 1);
314 if (err != TD_OK)
315 error ("Cannot enable thread event reporting for %d: %s",
316 ti_p->ti_lid, thread_db_err_str (err));
317 }
318 }
319
320 static int
321 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
322 {
323 td_thrinfo_t ti;
324 td_err_e err;
325
326 err = td_thr_get_info (th_p, &ti);
327 if (err != TD_OK)
328 error ("Cannot get thread info: %s", thread_db_err_str (err));
329
330 /* Check for zombies. */
331 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
332 return 0;
333
334 maybe_attach_thread (th_p, &ti);
335
336 return 0;
337 }
338
339 static void
340 thread_db_find_new_threads (void)
341 {
342 td_err_e err;
343 ptid_t ptid = ((struct inferior_list_entry *) current_inferior)->id;
344 struct process_info_private *proc = current_process()->private;
345
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)
350 return;
351
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);
357 if (err != TD_OK)
358 error ("Cannot find new threads: %s", thread_db_err_str (err));
359 }
360
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. */
365
366 static void
367 thread_db_look_up_symbols (void)
368 {
369 const char **sym_list = td_symbol_list ();
370 CORE_ADDR unused;
371
372 for (sym_list = td_symbol_list (); *sym_list; sym_list++)
373 look_up_one_symbol (*sym_list, &unused);
374 }
375
376 int
377 thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
378 CORE_ADDR load_module, CORE_ADDR *address)
379 {
380 #if HAVE_TD_THR_TLS_GET_ADDR
381 psaddr_t addr;
382 td_err_e err;
383 struct lwp_info *lwp;
384 struct thread_info *saved_inferior;
385
386 /* If the thread layer is not (yet) initialized, fail. */
387 if (!get_thread_process (thread)->all_symbols_looked_up)
388 return TD_ERR;
389
390 lwp = get_thread_lwp (thread);
391 if (!lwp->thread_known)
392 find_one_thread (lwp->head.id);
393 if (!lwp->thread_known)
394 return TD_NOTHR;
395
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,
402 offset, &addr);
403 current_inferior = saved_inferior;
404 if (err == TD_OK)
405 {
406 *address = (CORE_ADDR) (uintptr_t) addr;
407 return 0;
408 }
409 else
410 return err;
411 #else
412 return -1;
413 #endif
414 }
415
416 int
417 thread_db_init (int use_events)
418 {
419 int err;
420 struct process_info *proc = current_process ();
421 struct process_info_private *priv = proc->private;
422
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.
430
431 This isn't the only place in gdbserver that assumes that the first
432 process in the list is the thread group leader. */
433
434 thread_db_use_events = use_events;
435
436 err = td_ta_new (&priv->proc_handle, &priv->thread_agent);
437 switch (err)
438 {
439 case TD_NOLIBTHREAD:
440 /* No thread library was detected. */
441 return 0;
442
443 case TD_OK:
444 /* The thread library was detected. */
445
446 if (use_events && thread_db_enable_reporting () == 0)
447 return 0;
448 thread_db_find_new_threads ();
449 thread_db_look_up_symbols ();
450 proc->all_symbols_looked_up = 1;
451 return 1;
452
453 default:
454 warning ("error initializing thread_db library: %s",
455 thread_db_err_str (err));
456 }
457
458 return 0;
459 }