gdb/gdbserver/
[external/binutils.git] / gdb / gdbserver / thread-db.c
1 /* Thread management interface, for the remote server for GDB.
2    Copyright (C) 2002, 2004, 2005, 2006
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 2 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, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor,
22    Boston, MA 02110-1301, USA.  */
23
24 #include "server.h"
25
26 #include "linux-low.h"
27
28 extern int debug_threads;
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 /* Structure that identifies the child process for the
39    <proc_service.h> interface.  */
40 static struct ps_prochandle proc_handle;
41
42 /* Connection to the libthread_db library.  */
43 static td_thragent_t *thread_agent;
44
45 static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data);
46
47 static char *
48 thread_db_err_str (td_err_e err)
49 {
50   static char buf[64];
51
52   switch (err)
53     {
54     case TD_OK:
55       return "generic 'call succeeded'";
56     case TD_ERR:
57       return "generic error";
58     case TD_NOTHR:
59       return "no thread to satisfy query";
60     case TD_NOSV:
61       return "no sync handle to satisfy query";
62     case TD_NOLWP:
63       return "no LWP to satisfy query";
64     case TD_BADPH:
65       return "invalid process handle";
66     case TD_BADTH:
67       return "invalid thread handle";
68     case TD_BADSH:
69       return "invalid synchronization handle";
70     case TD_BADTA:
71       return "invalid thread agent";
72     case TD_BADKEY:
73       return "invalid key";
74     case TD_NOMSG:
75       return "no event message for getmsg";
76     case TD_NOFPREGS:
77       return "FPU register set not available";
78     case TD_NOLIBTHREAD:
79       return "application not linked with libthread";
80     case TD_NOEVENT:
81       return "requested event is not supported";
82     case TD_NOCAPAB:
83       return "capability not available";
84     case TD_DBERR:
85       return "debugger service failed";
86     case TD_NOAPLIC:
87       return "operation not applicable to";
88     case TD_NOTSD:
89       return "no thread-specific data for this thread";
90     case TD_MALLOC:
91       return "malloc failed";
92     case TD_PARTIALREG:
93       return "only part of register set was written/read";
94     case TD_NOXREGS:
95       return "X register set not available for this thread";
96 #ifdef HAVE_TD_VERSION
97     case TD_VERSION:
98       return "version mismatch between libthread_db and libpthread";
99 #endif
100     default:
101       snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
102       return buf;
103     }
104 }
105
106 #if 0
107 static char *
108 thread_db_state_str (td_thr_state_e state)
109 {
110   static char buf[64];
111
112   switch (state)
113     {
114     case TD_THR_STOPPED:
115       return "stopped by debugger";
116     case TD_THR_RUN:
117       return "runnable";
118     case TD_THR_ACTIVE:
119       return "active";
120     case TD_THR_ZOMBIE:
121       return "zombie";
122     case TD_THR_SLEEP:
123       return "sleeping";
124     case TD_THR_STOPPED_ASLEEP:
125       return "stopped by debugger AND blocked";
126     default:
127       snprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
128       return buf;
129     }
130 }
131 #endif
132
133 static void
134 thread_db_create_event (CORE_ADDR where)
135 {
136   td_event_msg_t msg;
137   td_err_e err;
138   struct inferior_linux_data *tdata;
139
140   if (debug_threads)
141     fprintf (stderr, "Thread creation event.\n");
142
143   tdata = inferior_target_data (current_inferior);
144
145   /* FIXME: This assumes we don't get another event.
146      In the LinuxThreads implementation, this is safe,
147      because all events come from the manager thread
148      (except for its own creation, of course).  */
149   err = td_ta_event_getmsg (thread_agent, &msg);
150   if (err != TD_OK)
151     fprintf (stderr, "thread getmsg err: %s\n",
152              thread_db_err_str (err));
153
154   /* msg.event == TD_EVENT_CREATE */
155
156   find_new_threads_callback (msg.th_p, NULL);
157 }
158
159 #if 0
160 static void
161 thread_db_death_event (CORE_ADDR where)
162 {
163   if (debug_threads)
164     fprintf (stderr, "Thread death event.\n");
165 }
166 #endif
167
168 static int
169 thread_db_enable_reporting ()
170 {
171   td_thr_events_t events;
172   td_notify_t notify;
173   td_err_e err;
174
175   /* Set the process wide mask saying which events we're interested in.  */
176   td_event_emptyset (&events);
177   td_event_addset (&events, TD_CREATE);
178
179 #if 0
180   /* This is reported to be broken in glibc 2.1.3.  A different approach
181      will be necessary to support that.  */
182   td_event_addset (&events, TD_DEATH);
183 #endif
184
185   err = td_ta_set_event (thread_agent, &events);
186   if (err != TD_OK)
187     {
188       warning ("Unable to set global thread event mask: %s",
189                thread_db_err_str (err));
190       return 0;
191     }
192
193   /* Get address for thread creation breakpoint.  */
194   err = td_ta_event_addr (thread_agent, TD_CREATE, &notify);
195   if (err != TD_OK)
196     {
197       warning ("Unable to get location for thread creation breakpoint: %s",
198                thread_db_err_str (err));
199       return 0;
200     }
201   set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr,
202                      thread_db_create_event);
203
204 #if 0
205   /* Don't concern ourselves with reported thread deaths, only
206      with actual thread deaths (via wait).  */
207
208   /* Get address for thread death breakpoint.  */
209   err = td_ta_event_addr (thread_agent, TD_DEATH, &notify);
210   if (err != TD_OK)
211     {
212       warning ("Unable to get location for thread death breakpoint: %s",
213                thread_db_err_str (err));
214       return;
215     }
216   set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr,
217                      thread_db_death_event);
218 #endif
219
220   return 1;
221 }
222
223 static void
224 maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
225 {
226   td_err_e err;
227   struct thread_info *inferior;
228   struct process_info *process;
229
230   /* If we are attaching to our first thread, things are a little
231      different.  */
232   if (all_threads.head == all_threads.tail)
233     {
234       inferior = (struct thread_info *) all_threads.head;
235       process = get_thread_process (inferior);
236       if (process->thread_known == 0)
237         {
238           /* Switch to indexing the threads list by TID.  */
239           change_inferior_id (&all_threads, ti_p->ti_tid);
240           goto found;
241         }
242     }
243   
244   inferior = (struct thread_info *) find_inferior_id (&all_threads,
245                                                       ti_p->ti_tid);
246   if (inferior != NULL)
247     return;
248
249   if (debug_threads)
250     fprintf (stderr, "Attaching to thread %ld (LWP %d)\n",
251              ti_p->ti_tid, ti_p->ti_lid);
252   linux_attach_lwp (ti_p->ti_lid, ti_p->ti_tid);
253   inferior = (struct thread_info *) find_inferior_id (&all_threads,
254                                                       ti_p->ti_tid);
255   if (inferior == NULL)
256     {
257       warning ("Could not attach to thread %ld (LWP %d)\n",
258                ti_p->ti_tid, ti_p->ti_lid);
259       return;
260     }
261
262   process = inferior_target_data (inferior);
263
264 found:
265   new_thread_notify (ti_p->ti_tid);
266
267   process->tid = ti_p->ti_tid;
268   process->lwpid = ti_p->ti_lid;
269
270   process->thread_known = 1;
271   process->th = *th_p;
272   err = td_thr_event_enable (th_p, 1);
273   if (err != TD_OK)
274     error ("Cannot enable thread event reporting for %d: %s",
275            ti_p->ti_lid, thread_db_err_str (err));
276 }
277
278 static int
279 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
280 {
281   td_thrinfo_t ti;
282   td_err_e err;
283
284   err = td_thr_get_info (th_p, &ti);
285   if (err != TD_OK)
286     error ("Cannot get thread info: %s", thread_db_err_str (err));
287
288   /* Check for zombies.  */
289   if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
290     return 0;
291
292   maybe_attach_thread (th_p, &ti);
293
294   return 0;
295 }
296
297 static void
298 thread_db_find_new_threads (void)
299 {
300   td_err_e err;
301
302   /* Iterate over all user-space threads to discover new threads.  */
303   err = td_ta_thr_iter (thread_agent, find_new_threads_callback, NULL,
304                         TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
305                         TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
306   if (err != TD_OK)
307     error ("Cannot find new threads: %s", thread_db_err_str (err));
308 }
309
310 /* Cache all future symbols that thread_db might request.  We can not
311    request symbols at arbitrary states in the remote protocol, only
312    when the client tells us that new symbols are available.  So when
313    we load the thread library, make sure to check the entire list.  */
314
315 static void
316 thread_db_look_up_symbols (void)
317 {
318   const char **sym_list = td_symbol_list ();
319   CORE_ADDR unused;
320
321   for (sym_list = td_symbol_list (); *sym_list; sym_list++)
322     look_up_one_symbol (*sym_list, &unused);
323 }
324
325 int
326 thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
327                            CORE_ADDR load_module, CORE_ADDR *address)
328 {
329 #if HAVE_TD_THR_TLS_GET_ADDR
330   psaddr_t addr;
331   td_err_e err;
332   struct process_info *process;
333
334   process = get_thread_process (thread);
335   if (!process->thread_known)
336     return TD_NOTHR;
337
338   /* Note the cast through uintptr_t: this interface only works if
339      a target address fits in a psaddr_t, which is a host pointer.
340      So a 32-bit debugger can not access 64-bit TLS through this.  */
341   err = td_thr_tls_get_addr (&process->th, (psaddr_t) (uintptr_t) load_module,
342                              offset, &addr);
343   if (err == TD_OK)
344     {
345       *address = (CORE_ADDR) (uintptr_t) addr;
346       return 0;
347     }
348   else
349     return err;
350 #else
351   return -1;
352 #endif
353 }
354
355 int
356 thread_db_init ()
357 {
358   int err;
359
360   /* FIXME drow/2004-10-16: This is the "overall process ID", which
361      GNU/Linux calls tgid, "thread group ID".  When we support
362      attaching to threads, the original thread may not be the correct
363      thread.  We would have to get the process ID from /proc for NPTL.
364      For LinuxThreads we could do something similar: follow the chain
365      of parent processes until we find the highest one we're attached
366      to, and use its tgid.
367
368      This isn't the only place in gdbserver that assumes that the first
369      process in the list is the thread group leader.  */
370   proc_handle.pid = ((struct inferior_list_entry *)current_inferior)->id;
371
372   /* Allow new symbol lookups.  */
373   all_symbols_looked_up = 0;
374
375   err = td_ta_new (&proc_handle, &thread_agent);
376   switch (err)
377     {
378     case TD_NOLIBTHREAD:
379       /* No thread library was detected.  */
380       return 0;
381
382     case TD_OK:
383       /* The thread library was detected.  */
384
385       if (thread_db_enable_reporting () == 0)
386         return 0;
387       thread_db_find_new_threads ();
388       thread_db_look_up_symbols ();
389       all_symbols_looked_up = 1;
390       return 1;
391
392     default:
393       warning ("error initializing thread_db library: %s",
394                thread_db_err_str (err));
395     }
396
397   return 0;
398 }