3 Copyright 2001, 2002 Red Hat, Inc.
5 This file is part of RDA, the Red Hat Debug Agent (and library).
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Alternative licenses for RDA may be arranged by contacting Red Hat,
30 #include <thread_db.h>
36 #include "gdbserv-target.h"
38 #include "gdb_proc_service.h"
39 #include "gdbserv-thread-db.h"
41 /* Make lots of noise (debugging output). */
42 int thread_db_noisy = 0;
43 int proc_service_noisy = 0;
46 * A tiny local symbol table.
48 * This is used by ps_pglobal_lookup, and is really just a
49 * local cache of symbols whose values we have obtained from gdb.
51 * Since the cache is expected to be small, and infrequently used,
52 * there is no effort to sort or hash it. Symbols may be added
53 * in an "undefined" state, and then defined later.
60 struct symbol_cache *next;
63 /* The "defined_p" field may have one of the following three values. */
64 enum { UNDEFINED, REQUESTED, DEFINED };
66 /* Function: add_symbol_to_list
67 Add a symbol to the symbol cache. First checks to see if
68 an entry is already in there, and re-uses it if so. This way
69 the cache may be used for symbols awaiting lookup as well as
70 for those that have already been defined by the debugger. */
73 add_symbol_to_list (char *name, paddr_t value, int defined_p)
75 struct symbol_cache *tmp;
77 for (tmp = symbol_list; tmp; tmp = tmp->next)
79 if (strcmp (name, tmp->name) == 0)
81 /* Symbol is already in cache -- set its value and definedness. */
83 if (defined_p == DEFINED)
84 tmp->defined_p = defined_p;
89 /* Symbol is not in cache -- add it. */
90 tmp = malloc (sizeof (struct symbol_cache));
93 tmp->defined_p = defined_p;
94 tmp->name = malloc (strlen (name) + 1);
95 strcpy (tmp->name, name);
97 tmp->next = symbol_list;
101 /* Function: free_symbol_list
102 Empty the symbol cache. */
105 free_symbol_list (void)
107 struct symbol_cache *tmp;
109 for (tmp = symbol_list; tmp; tmp = symbol_list)
111 symbol_list = tmp->next;
117 /* Function: sync_symbol_list
118 Return all "requested" symbols to the "undefined" state
119 (so they can be "requested" again). Called when a new
120 source of symbols becomes available (eg. a new shared object). */
123 sync_symbol_list (void)
125 struct symbol_cache *tmp;
127 for (tmp = symbol_list; tmp; tmp = tmp->next)
128 if (tmp->defined_p == REQUESTED)
129 tmp->defined_p = UNDEFINED;
132 /* Function: lookup_cached_symbol
133 If symbol is defined and cached, return its value in VALUE.
134 Return: 0 if not found, 1 if found. */
137 lookup_cached_symbol (char *name, paddr_t *value)
139 struct symbol_cache *tmp;
141 for (tmp = symbol_list; tmp; tmp = tmp->next)
142 if (strcmp (name, tmp->name) == 0 && tmp->defined_p == DEFINED)
144 *value = tmp->value; /* known and defined */
148 return 0; /* not found */
151 /* Function: next_undefined_symbol
152 Find a symbol in the cache that needs lookup by GDB.
153 On returning a symbol, mark it REQUESTED, so that it won't
154 be requested again until a new source of symbols opens up
155 (eg. a new shared object). */
158 next_undefined_symbol (void)
160 struct symbol_cache *tmp;
161 /* Make a pass thru the list, and return the first symbol that
162 hasn't been either requested or defined. */
163 for (tmp = symbol_list; tmp; tmp = tmp->next)
164 if (tmp->defined_p == UNDEFINED)
166 tmp->defined_p = REQUESTED;
173 * A tiny local thread list.
175 * This local list of threads is used for gdbserv operations that
176 * require a struct gdbserv_thread. Its first use will be to
177 * implement "info threads" for gdb.
180 /* Define the struct gdbserv_thread object. */
182 struct gdbserv_thread {
188 struct gdbserv_thread *next;
191 /* Function: add_thread_to_list
192 Add a thread (provided by libthread_db) to the local list. */
194 static struct gdbserv_thread *
195 add_thread_to_list (td_thrinfo_t *ti)
197 struct gdbserv_thread *new = malloc (sizeof (struct gdbserv_thread));
199 /* First cut -- add to start of list. */
200 memcpy (&new->ti, ti, sizeof (td_thrinfo_t));
201 new->next = thread_list;
206 static struct gdbserv_thread *
207 first_thread_in_list (void)
212 static struct gdbserv_thread *
213 next_thread_in_list (struct gdbserv_thread *thread)
222 delete_thread_from_list (struct gdbserv_thread *thread)
224 struct gdbserv_thread *tmp;
226 for (tmp = thread_list; tmp; tmp = tmp->next)
228 if (tmp->next == thread)
230 tmp->next = tmp->next->next; /* unlink */
231 free (thread); /* discard */
232 return; /* finished */
235 /* Special case -- delete first element of list. */
236 if (thread == thread_list)
238 thread_list = thread->next; /* unlink */
239 free (thread); /* discard */
240 return; /* finished */
242 /* If we reach this point, the thread wasn't in the list. */
246 free_thread_list (void)
248 struct gdbserv_thread *tmp;
250 for (tmp = thread_list; tmp; tmp = thread_list)
252 thread_list = tmp->next;
257 static struct gdbserv_thread *
258 thread_list_lookup_by_tid (thread_t tid)
260 struct gdbserv_thread *tmp;
262 for (tmp = thread_list; tmp; tmp = tmp->next)
263 if (tmp->ti.ti_tid == tid)
269 static struct gdbserv_thread *
270 thread_list_lookup_by_lid (lwpid_t pid)
272 struct gdbserv_thread *tmp;
274 for (tmp = thread_list; tmp; tmp = tmp->next)
275 if (tmp->ti.ti_lid == pid)
281 /* A copy of the next lower layer's target vector, before we modify it. */
282 static struct gdbserv_target parentvec;
284 /* A pointer to the current target vector. */
285 static struct gdbserv_target *currentvec;
288 * proc_service callback functions, called by thread_db.
292 ps_plog (const char *fmt, ...)
294 fprintf (stderr, "<ps_plog: %s>\n", fmt);
298 /* Look up a symbol in GDB's global symbol table.
299 Return the symbol's address.
300 FIXME: it would be more correct to look up the symbol in the context
301 of the LD_OBJECT_NAME provided. However we're probably fairly safe
302 as long as there aren't name conflicts with other libraries. */
305 ps_pglobal_lookup (gdb_ps_prochandle_t ph,
306 const char *ld_object_name, /* the library name */
307 const char *ld_symbol_name, /* the symbol name */
308 paddr_t *ld_symbol_addr) /* return the symbol addr */
312 if (lookup_cached_symbol ((char *) ld_symbol_name, &value) == 0)
314 /* Symbol not in cache -- ask GDB to look it up.
315 Add the symbol to the cache as undefined. */
316 add_symbol_to_list ((char *) ld_symbol_name, 0, UNDEFINED);
321 /* Symbol is in the cache and defined -- return its value. */
322 *ld_symbol_addr = value;
328 /* Connection to the libthread_db library. */
329 static struct ps_prochandle proc_handle;
330 static td_thragent_t *thread_agent = NULL;
332 /* Pointers to the libthread_db functions. */
333 static td_err_e (*td_init_p) (void);
335 static td_err_e (*td_ta_new_p) (struct ps_prochandle *ps,
337 static td_err_e (*td_ta_delete_p) (td_thragent_t *ta);
338 static td_err_e (*td_ta_map_id2thr_p) (const td_thragent_t *ta,
340 td_thrhandle_t *__th);
341 static td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta,
344 static td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta,
345 td_thr_iter_f *callback,
347 td_thr_state_e state,
349 sigset_t *ti_sigmask,
350 unsigned int ti_user_flags);
351 static td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta,
354 static td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta,
355 td_thr_events_t *event);
356 static td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta,
357 td_event_msg_t *msg);
358 static td_err_e (*td_thr_validate_p) (const td_thrhandle_t *th);
359 static td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
360 td_thrinfo_t *infop);
361 static td_err_e (*td_thr_getfpregs_p) (const td_thrhandle_t *th,
363 static td_err_e (*td_thr_getgregs_p) (const td_thrhandle_t *th,
365 static td_err_e (*td_thr_setfpregs_p) (const td_thrhandle_t *th,
366 const FPREGSET_T *fpregs);
367 static td_err_e (*td_thr_setgregs_p) (const td_thrhandle_t *th,
369 static td_err_e (*td_thr_getxregsize_p) (const td_thrhandle_t *th,
371 static td_err_e (*td_thr_getxregs_p) (const td_thrhandle_t *th,
373 static td_err_e (*td_thr_setxregs_p) (const td_thrhandle_t *th,
375 static td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th,
378 /* Function: thread_db_state_str
379 Convert a thread_db state code to a string.
380 If state code is unknown, return an <unknown> message. */
383 thread_db_state_str (td_thr_state_e statecode)
388 case TD_THR_ANY_STATE: return "<any state>";
389 case TD_THR_UNKNOWN: return "<officially unknown>";
390 case TD_THR_STOPPED: return "<stopped>";
391 case TD_THR_RUN: return "<running>";
392 case TD_THR_ACTIVE: return "<active> ";
393 case TD_THR_ZOMBIE: return "<zombie> ";
394 case TD_THR_SLEEP: return "<sleep> ";
395 case TD_THR_STOPPED_ASLEEP: return "<stopped asleep>";
397 sprintf (buf, "<unknown state code %d>", statecode);
403 thread_db_type_str (td_thr_type_e type)
406 case TD_THR_USER: return "<user> ";
407 case TD_THR_SYSTEM: return "<system>";
408 default: return "<unknown>";
412 /* Function: thread_db_err_string
413 Convert a thread_db error code to a string.
414 If errcode is unknown, then return an <unknown> message. */
417 thread_db_err_str (td_err_e errcode)
422 case TD_OK: return "generic 'call succeeded'";
423 case TD_ERR: return "generic error";
424 case TD_NOTHR: return "no thread to satisfy query";
425 case TD_NOSV: return "no sync handle to satisfy query";
426 case TD_NOLWP: return "no lwp to satisfy query";
427 case TD_BADPH: return "invalid process handle";
428 case TD_BADTH: return "invalid thread handle";
429 case TD_BADSH: return "invalid synchronization handle";
430 case TD_BADTA: return "invalid thread agent";
431 case TD_BADKEY: return "invalid key";
432 case TD_NOMSG: return "no event message for getmsg";
433 case TD_NOFPREGS: return "FPU register set not available";
434 case TD_NOLIBTHREAD: return "application not linked with libthread";
435 case TD_NOEVENT: return "requested event is not supported";
436 case TD_NOCAPAB: return "capability not available";
437 case TD_DBERR: return "debugger service failed";
438 case TD_NOAPLIC: return "operation not applicable to";
439 case TD_NOTSD: return "no thread-specific data for this thread";
440 case TD_MALLOC: return "malloc failed";
441 case TD_PARTIALREG: return "only part of register set was written/read";
442 case TD_NOXREGS: return "X register set not available for this thread";
444 sprintf (buf, "unknown thread_db error '%d'", errcode);
449 /* flag which indicates if the map_id2thr cache is valid. See below. */
450 static int thread_db_map_id2thr_cache_valid;
452 /* Function: thread_db_map_id2thr
453 Calling td_ta_map_id2thr() is expensive. This function invokes
454 td_ta_map_id2thr() and caches the value for future reference. The
455 cache may be invalidated by calling thread_db_invalidate_cache().
456 Returns: TD_OK on success, an appropriate error code otherwise. */
459 thread_db_map_id2thr (const td_thragent_t *ta, thread_t pt,
462 static td_thrhandle_t cached_handle;
463 static thread_t input_pt;
465 if (pt == input_pt && thread_db_map_id2thr_cache_valid)
474 status = td_ta_map_id2thr_p (ta, pt, th);
477 thread_db_map_id2thr_cache_valid = 1;
482 thread_db_map_id2thr_cache_valid = 0;
487 /* Invalidate the map_id2thr cache. */
489 thread_db_invalidate_map_id2thr_cache (void)
491 thread_db_map_id2thr_cache_valid = 0;
494 /* The regset cache object. This object keeps track of the most
495 recently fetched or set gregset (of a particular type) and whether
496 or not it needs to still needs to be synchronized with the target. */
499 /* Are the cache contents valid? */
502 /* Does cache need to be flushed? */
505 /* Handle corresponding to cached regset. */
506 td_thrhandle_t handle;
508 /* Size of memory area used to hold regset. */
511 /* Memory area used to hold regset. */
514 /* Functions used to get/set regset. */
515 td_err_e (*getregset) (const td_thrhandle_t *th, void *regset);
516 td_err_e (*setregset) (const td_thrhandle_t *th, const void *regset);
519 /* Declare fpregset and gregset cache objects. */
520 static struct regset_cache fpregset_cache;
521 static struct regset_cache gregset_cache;
523 /* Wrappers for td_thr_getfpregs_p, td_thr_setfpregs_p, td_thr_getgregs_p,
524 and td_thr_setgregs_p. These simply allow us to pass a void * for the
528 td_thr_getfpregs_wrapper (const td_thrhandle_t *th, void *fpregs)
530 return td_thr_getfpregs_p (th, fpregs);
533 static td_err_e td_thr_getgregs_wrapper (const td_thrhandle_t *th, void *gregs)
535 return td_thr_getgregs_p (th, gregs);
538 static td_err_e td_thr_setfpregs_wrapper (const td_thrhandle_t *th,
541 return td_thr_setfpregs_p (th, fpregs);
544 static td_err_e td_thr_setgregs_wrapper (const td_thrhandle_t *th,
547 void * gregs_nonconst = (void *) gregs;
549 return td_thr_setgregs_p (th, gregs_nonconst);
552 /* Initialize a regset cache object. */
554 initialize_regset_cache (struct regset_cache *regset_cache,
555 const int regset_size,
556 void * const regset_buffer,
557 td_err_e (* const getregset) (const td_thrhandle_t *th,
559 td_err_e (* const setregset) (const td_thrhandle_t *th,
562 regset_cache->valid = 0;
563 regset_cache->needs_flush = 0;
564 regset_cache->regset_size = regset_size;
565 regset_cache->regset_buffer = regset_buffer;
566 regset_cache->getregset = getregset;
567 regset_cache->setregset = setregset;
570 /* Initialize the fpregset and gregset cache objects. Space for
571 the regset buffer is statically allocated to avoid calls to malloc(). */
573 initialize_regset_caches (void)
575 static FPREGSET_T fpregset;
576 static GREGSET_T gregset;
578 initialize_regset_cache (&fpregset_cache, sizeof fpregset, &fpregset,
579 td_thr_getfpregs_wrapper, td_thr_setfpregs_wrapper);
580 initialize_regset_cache (&gregset_cache, sizeof gregset, gregset,
581 td_thr_getgregs_wrapper, td_thr_setgregs_wrapper);
584 /* Synchronize a cached regset with the target. */
586 thread_db_flush_regset_cache (struct regset_cache *regset_cache)
588 td_err_e status = TD_OK;
589 if (regset_cache->valid && regset_cache->needs_flush)
591 status = regset_cache->setregset (®set_cache->handle,
592 regset_cache->regset_buffer);
594 regset_cache->valid = 0;
595 regset_cache->needs_flush = 0;
600 /* Synchronize the gregset and fpregset caches with the target. */
602 thread_db_flush_regset_caches (void)
605 td_err_e ret_status = TD_OK;
607 status = thread_db_flush_regset_cache (&fpregset_cache);
611 status = thread_db_flush_regset_cache (&gregset_cache);
618 /* Fetch a regset, using a previously cached copy if possible. */
620 thread_db_get_regset (struct regset_cache *regset_cache,
621 const td_thrhandle_t *th,
624 if (regset_cache->valid
625 && memcmp (®set_cache->handle, th, sizeof *th) == 0)
627 /* Cache is valid and handles match. Copy the cached regset. */
628 memcpy (regset, regset_cache->regset_buffer, regset_cache->regset_size);
635 /* Handles don't match. Write out old cache contents before
636 fetching contents w/ new handle if necessary. */
637 if (regset_cache->valid && regset_cache->needs_flush)
639 status = regset_cache->setregset (®set_cache->handle,
640 regset_cache->regset_buffer);
643 regset_cache->needs_flush = 0;
644 regset_cache->valid = 0;
650 /* Fetch the regset. */
651 status = regset_cache->getregset (th, regset);
654 /* Preserve it in the cache. */
655 regset_cache->needs_flush = 0;
656 regset_cache->valid = 1;
657 memcpy (®set_cache->handle, th, sizeof (*th));
658 memcpy (regset_cache->regset_buffer, regset,
659 regset_cache->regset_size);
662 regset_cache->valid = 0;
667 /* Set a regset deferring synchronization with the target until
670 thread_db_set_regset (struct regset_cache *regset_cache,
671 const td_thrhandle_t *th,
674 td_err_e ret_status = TD_OK;
676 if (regset_cache->valid && regset_cache->needs_flush
677 && memcmp (®set_cache->handle, th, sizeof *th) != 0)
679 /* Cached regset needs to be flushed because handles don't
681 ret_status = thread_db_flush_regset_cache (regset_cache);
684 memcpy (®set_cache->handle, th, sizeof *th);
685 memcpy (regset_cache->regset_buffer, regset, regset_cache->regset_size);
686 regset_cache->valid = 1;
687 regset_cache->needs_flush = 1;
692 /* Mark a regset cache as invalid. */
694 thread_db_invalidate_regset_cache (struct regset_cache *regset_cache)
696 regset_cache->valid = 0;
699 /* Mark the gregset and fpregset caches as invalid. */
701 thread_db_invalidate_regset_caches (void)
703 thread_db_invalidate_regset_cache (&fpregset_cache);
704 thread_db_invalidate_regset_cache (&gregset_cache);
707 /* Invalidate all caches. */
709 thread_db_invalidate_caches (void)
711 thread_db_invalidate_regset_caches ();
712 thread_db_invalidate_map_id2thr_cache ();
715 /* Fetch the floating point registers via the fpregset cache. */
717 thread_db_getfpregs (const td_thrhandle_t *th, FPREGSET_T *fpregset)
719 return thread_db_get_regset (&fpregset_cache, th, fpregset);
722 /* Set the floating point registers via the fpregset cache. */
724 thread_db_setfpregs (const td_thrhandle_t *th, const FPREGSET_T *fpregset)
726 return thread_db_set_regset (&fpregset_cache, th, fpregset);
729 /* Fetch the general purpose registers via the gregset cache. */
731 thread_db_getgregs (const td_thrhandle_t *th, GREGSET_T gregset)
733 return thread_db_get_regset (&gregset_cache, th, gregset);
736 /* Set the general purpose registers via the gregset cache. */
738 thread_db_setgregs (const td_thrhandle_t *th, const GREGSET_T gregset)
740 return thread_db_set_regset (&gregset_cache, th, gregset);
743 /* Call dlsym() to find the address of a symbol. If symbol lookup fails,
744 print the reason to stderr. */
747 lookup_sym (void *dlhandle, char *symbol)
751 addr = dlsym (dlhandle, symbol);
754 fprintf (stderr, "Symbol lookup of %s failed: %s\n",
760 /* Function: thread_db_dlopen
761 Attach to the libthread_db library.
762 This function does all the dynamic library stuff (dlopen, dlsym).
763 Return: -1 for failure, zero for success. */
766 thread_db_dlopen (void)
770 #ifndef LIBTHREAD_DB_SO
771 #define LIBTHREAD_DB_SO "libthread_db.so.1"
774 if ((dlhandle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW)) == NULL)
776 fprintf (stderr, "Unable to open %s: %s\n",
777 LIBTHREAD_DB_SO, dlerror ());
778 return -1; /* fail */
781 /* Initialize pointers to the dynamic library functions we will use.
784 if ((td_init_p = lookup_sym (dlhandle, "td_init")) == NULL)
785 return -1; /* fail */
787 if ((td_ta_new_p = lookup_sym (dlhandle, "td_ta_new")) == NULL)
788 return -1; /* fail */
790 if ((td_ta_delete_p = lookup_sym (dlhandle, "td_ta_delete")) == NULL)
791 return -1; /* fail */
793 if ((td_ta_map_id2thr_p = lookup_sym (dlhandle, "td_ta_map_id2thr")) == NULL)
794 return -1; /* fail */
796 if ((td_ta_map_lwp2thr_p = lookup_sym (dlhandle, "td_ta_map_lwp2thr")) == NULL)
797 return -1; /* fail */
799 if ((td_ta_thr_iter_p = lookup_sym (dlhandle, "td_ta_thr_iter")) == NULL)
800 return -1; /* fail */
802 if ((td_thr_validate_p = lookup_sym (dlhandle, "td_thr_validate")) == NULL)
803 return -1; /* fail */
805 if ((td_thr_get_info_p = lookup_sym (dlhandle, "td_thr_get_info")) == NULL)
806 return -1; /* fail */
808 if ((td_thr_getfpregs_p = lookup_sym (dlhandle, "td_thr_getfpregs")) == NULL)
809 return -1; /* fail */
811 if ((td_thr_getgregs_p = lookup_sym (dlhandle, "td_thr_getgregs")) == NULL)
812 return -1; /* fail */
814 if ((td_thr_setfpregs_p = lookup_sym (dlhandle, "td_thr_setfpregs")) == NULL)
815 return -1; /* fail */
817 if ((td_thr_setgregs_p = lookup_sym (dlhandle, "td_thr_setgregs")) == NULL)
818 return -1; /* fail */
820 /* These are not essential. */
821 td_ta_event_addr_p = dlsym (dlhandle, "td_ta_event_addr");
822 td_ta_set_event_p = dlsym (dlhandle, "td_ta_set_event");
823 td_ta_event_getmsg_p = dlsym (dlhandle, "td_ta_event_getmsg");
824 td_thr_event_enable_p = dlsym (dlhandle, "td_thr_event_enable");
825 td_thr_getxregsize_p = dlsym (dlhandle, "td_thr_getxregsize");
826 td_thr_getxregs_p = dlsym (dlhandle, "td_thr_getxregs");
827 td_thr_setxregs_p = dlsym (dlhandle, "td_thr_setxregs");
829 return 0; /* success */
832 /* Function: thread_db_open
833 Open a channel to the child's thread library.
834 Returns: -1 for failure, 0 for success
836 FIXME: where should we be called from? We will not succeed
837 until the thread shlib is loaded. The call from attach will not
838 succeed even if the target is statically linked, 'cause there's
839 no symbol lookup handshake on attach. Therefore I can't handle
840 a statically linked threaded process. */
843 thread_db_open (struct gdbserv *serv, int pid)
844 { /* FIXME: once we have the serv, we can derive the pid.
845 No, not true -- not when we're called from attach.
846 But then, there isn't much use in the call from attach unles
847 I make GDB respond to symbol callbacks from there somehow. */
850 if (thread_agent == NULL)
852 proc_handle.pid = pid;
853 proc_handle.serv = serv;
855 ret = td_ta_new_p (&proc_handle, &thread_agent);
858 return -1; /* success */
860 else if (thread_db_noisy)
862 fprintf (stderr, "< -- failed, thread_agent = 0x%08x>\n",
863 (long) thread_agent);
865 return 0; /* failure */
867 return -1; /* success */
870 /* Function: thread_db_detach
871 FIXME: gdbserv kills the inferior and exits when gdb detaches.
872 This is the best place I have from which to shut down the
873 thread_db interface, but it's not really where this should
877 thread_db_detach (struct gdbserv *serv, struct gdbserv_target *target)
879 struct child_process *process = gdbserv_target_data (serv);
881 /* FIXME: this isn't really enough, and detach isn't really the
882 right place for this anyway. Do this in exit_program. */
883 td_ta_delete_p (thread_agent);
887 if (process->debug_informational)
888 fprintf (stderr, "<thread_db_detach>\n");
889 if (parentvec.detach)
890 parentvec.detach (serv, target);
894 attach_thread (struct gdbserv_thread *thread)
896 if (thread->ti.ti_lid != 0 &&
897 thread->ti.ti_state != TD_THR_ZOMBIE) /* Don't attach a zombie. */
899 if (attach_lwp (thread->ti.ti_lid) == 0)
900 thread->attached = 1;
902 thread->attached = 0;
906 /* Function: find_new_threads_callback
907 Enter threads into a local thread database. */
910 find_new_threads_callback (const td_thrhandle_t *thandle, void *data)
912 struct gdbserv_thread *thread;
916 if ((ret = td_thr_get_info_p (thandle, &ti)) != TD_OK)
918 fprintf (stderr, "<find_new_threads_callback: get_info failed! %s>\n",
919 thread_db_err_str (ret));
923 /* Enter the thread into a local list
924 (unless it is TD_THR_UNKNOWN, which means its defunct). */
925 if ((thread = thread_list_lookup_by_tid (ti.ti_tid)) == NULL)
927 if (ti.ti_state != TD_THR_UNKNOWN)
929 thread = add_thread_to_list (&ti);
930 /* Now make sure we've attached to it.
931 Skip the main pid (already attached). */
932 if (thread->ti.ti_lid != proc_handle.pid)
934 attach_thread (thread);
940 /* Already in list -- cache new thread info */
941 memcpy (&thread->ti, &ti, sizeof (ti));
947 /* Function: update_thread_list
949 First run td_ta_thr_iter to find all threads.
950 Then walk the list and validate that each thread is still running.
951 If not, prune it from the list. */
954 update_thread_list (void)
956 struct gdbserv_thread *thread, *next;
957 td_thrhandle_t handle;
959 /* First make sure all libthread threads are in the list. */
960 td_ta_thr_iter_p (thread_agent, find_new_threads_callback,
963 TD_THR_LOWEST_PRIORITY,
965 TD_THR_ANY_USER_FLAGS);
967 /* Next, remove any defunct threads from the list. */
968 for (thread = first_thread_in_list ();
972 /* Thread may be deleted, so find its successor first! */
973 next = next_thread_in_list (thread);
975 /* Now ask if thread is still valid, and if not, delete it. */
976 if (thread_db_map_id2thr (thread_agent,
979 || td_thr_validate_p (&handle) != TD_OK)
981 if (thread->ti.ti_state == TD_THR_UNKNOWN)
983 /* Thread is no longer "valid".
984 By the time this happens, it's too late for us to
985 detach from it. Just delete it from the list. */
987 delete_thread_from_list (thread);
993 /* Function: thread_db_thread_next
994 Exported to gdbserv to implement "info threads" request from GDB. */
996 static struct gdbserv_thread *
997 thread_db_thread_next (struct gdbserv *serv, struct gdbserv_thread *thread)
1001 /* First request -- build up thread list using td_ta_thr_iter. */
1002 /* NOTE: this should be unnecessary, once we begin to keep the
1003 list up to date all the time. */
1004 update_thread_list ();
1006 return next_thread_in_list (thread);
1010 /* Function: thread_db_get_gen
1011 Handle 'q' requests:
1016 thread_db_get_gen (struct gdbserv *serv)
1018 struct child_process *process = gdbserv_target_data (serv);
1019 char tempname[1024], *symbol_query;
1020 unsigned long tempval;
1023 if (gdbserv_input_string_match (serv, "Symbol:") >= 0)
1025 /* Message: qSymbol:<optional value>:<optional name hexified>
1027 Reply: qSymbol:<name hexified>
1029 This message from GDB has three possible forms:
1031 1) "qSymbol::" (no value, no name).
1032 This means the start of a symbol query session.
1033 GDB is offering to serve up symbols.
1034 The target should reply with the FIRST symbol whose value
1035 it wants (or "OK" if it doesn't want any).
1037 2) "qSymbol:<value>:<name hexified>
1038 This means "here is the value of the symbol you requested".
1039 The target should reply with the NEXT symbol whose value
1040 it wants (or "OK" if it doesn't want any more).
1042 3) "qSymbol::<name hexified>" (no value)
1043 This means "I have no value for the symbol you requested".
1044 The target should reply with the NEXT symbol whose value
1045 it wants (or "OK" if it doesn't want any more).
1048 if (gdbserv_input_string_match (serv, ":") >= 0)
1050 /* So far we've matched "qSymbol::". We're looking at either
1051 form #1 ("qSymbol::", open a symbol lookup session), or
1052 form #2 ("qSymbol::<name>", a reply that "this symbol is
1055 len = gdbserv_input_bytes (serv, tempname, sizeof (tempname));
1059 /* Form #1, open a new symbol lookup session.
1060 Prepare to request the first symbol in the list. */
1061 sync_symbol_list ();
1065 /* Form #2, this symbol not currently defined.
1066 Nothing to do, since we already have it marked undefined. */
1069 else if (gdbserv_input_hex_ulong (serv, &tempval) >= 0 &&
1070 gdbserv_input_string_match (serv, ":") >= 0 &&
1071 (len = gdbserv_input_bytes (serv, tempname, sizeof (tempname)))
1074 /* Message contains a symbol and a value (form #3). */
1076 tempname[len] = '\0';
1077 add_symbol_to_list (tempname, (paddr_t) tempval, DEFINED);
1078 if (thread_agent != NULL)
1080 /* We now have a new symbol in the cache, which was
1081 requested by the last td_ta_new call. Delete the
1082 current (not-completely-valid) thread agent, so that
1083 a new one will have to be opened. */
1084 td_ta_delete_p (thread_agent);
1085 thread_agent = NULL;
1089 /* Now the reply depends on whether there is another
1090 symbol in need of lookup. */
1091 thread_db_open (serv, process->pid);
1092 if ((symbol_query = next_undefined_symbol ()) == NULL)
1094 gdbserv_output_string (serv, "OK");
1098 gdbserv_output_string (serv, "qSymbol:");
1099 gdbserv_output_bytes (serv, symbol_query, strlen (symbol_query));
1102 else if (parentvec.process_get_gen)
1103 parentvec.process_get_gen (serv);
1106 /* Function: thread_db_set_gen
1107 Handle 'Q' requests:
1111 thread_db_set_gen (struct gdbserv *serv)
1113 if (parentvec.process_set_gen)
1114 parentvec.process_set_gen (serv);
1118 thread_db_thread_id (struct gdbserv *serv,
1119 struct gdbserv_thread *thread,
1120 struct gdbserv_reg *id)
1122 gdbserv_ulonglong_to_reg (serv,
1123 (unsigned long long) thread->ti.ti_tid,
1128 thread_db_thread_lookup_by_id (struct gdbserv *serv,
1129 const struct gdbserv_reg *thread_id,
1130 struct gdbserv_thread **thread)
1134 gdbserv_reg_to_ulong (serv, thread_id, &id);
1135 if (id == 0) /* any thread */
1137 *thread = next_thread_in_list (NULL); /* FIXME curthread? */
1142 *thread = thread_list_lookup_by_tid ((thread_t) id);
1143 if (*thread == NULL) /* bad thread id */
1145 *thread = next_thread_in_list (NULL); /* FIXME curthread? */
1150 return 1; /* success */
1156 thread_db_thread_info (struct gdbserv *serv, struct gdbserv_thread *thread)
1158 char *info = malloc (128);
1160 sprintf (info, "PID %d Type %s State %s",
1162 thread_db_type_str (thread->ti.ti_type),
1163 thread_db_state_str (thread->ti.ti_state));
1167 /* Function: get_target_int_by_name
1168 Read the value of a target integer, given its name and size.
1169 Returns -1 for failure, zero for success. */
1172 get_target_int_by_name (char *name, void *value, int size)
1176 if (ps_pglobal_lookup (&proc_handle, NULL, name, &addr) == PS_OK)
1178 if (ps_pdread (&proc_handle, addr,
1179 (gdb_ps_read_buf_t) value,
1180 (gdb_ps_size_t) size) == PS_OK)
1183 return -1; /* fail */
1186 /* Function: set_target_int_by_name
1187 Read the value of a target integer, given its name and size.
1188 Returns -1 for failure, zero for success. */
1191 set_target_int_by_name (char *name, void *value, int size)
1195 if (ps_pglobal_lookup (&proc_handle, NULL, name, &addr) == PS_OK)
1197 if (ps_pdwrite (&proc_handle, addr,
1198 (gdb_ps_write_buf_t) value,
1199 (gdb_ps_size_t) size) == PS_OK)
1202 return -1; /* fail */
1205 /* Function: get_thread_signals
1206 Obtain the values of the "cancel", "restart" and "debug" signals
1207 used by linux threads, and store them in a set of global variables
1208 for use by check_child_state and friends. */
1210 static int cancel_signal;
1211 static int restart_signal;
1212 static int debug_signal;
1213 static int got_thread_signals;
1216 get_thread_signals (void)
1218 int cancel, restart, debug, debug_flag;
1220 if (!got_thread_signals)
1222 if (get_target_int_by_name ("__pthread_sig_cancel",
1223 &cancel, sizeof (cancel)) == 0 &&
1224 get_target_int_by_name ("__pthread_sig_restart",
1225 &restart, sizeof (restart)) == 0 &&
1226 get_target_int_by_name ("__pthread_sig_debug",
1227 &debug, sizeof (debug)) == 0)
1229 restart_signal = restart;
1230 cancel_signal = cancel;
1231 debug_signal = debug;
1232 got_thread_signals = 1;
1235 set_target_int_by_name ("__pthread_threads_debug",
1236 &debug_flag, sizeof (debug_flag));
1240 /* Function: stop_thread
1241 Use SIGSTOP to force a thread to stop. */
1244 stop_thread (struct gdbserv_thread *thread)
1246 if (thread->ti.ti_lid != 0)
1248 if (stop_lwp (thread->ti.ti_lid) == 0)
1249 thread->stopped = 1;
1251 thread->stopped = 0;
1255 /* Function: stop_all_threads
1256 Use SIGSTOP to make sure all child threads are stopped.
1257 Do not send SIGSTOP to the event thread, or to any
1258 new threads that have just been attached. */
1261 stop_all_threads (struct child_process *process)
1263 struct gdbserv_thread *thread;
1265 for (thread = first_thread_in_list ();
1267 thread = next_thread_in_list (thread))
1269 if (thread->ti.ti_lid == process->pid)
1271 /* HACK mark him signalled. */
1272 thread->stopped = 1;
1273 continue; /* This thread is already stopped. */
1275 /* All threads must be stopped, unles
1276 a) they have only just been attached, or
1277 b) they're already stopped. */
1278 if (!thread->attached && !thread->stopped &&
1279 thread->ti.ti_state != TD_THR_ZOMBIE &&
1280 thread->ti.ti_state != TD_THR_UNKNOWN)
1282 stop_thread (thread);
1287 /* A list of signals that have been prematurely sucked out of the threads.
1288 Because of the complexities of linux threads, we must send SIGSTOP to
1289 every thread, and then call waitpid on the thread to retrieve the
1290 SIGSTOP event. Sometimes another signal is pending on the thread,
1291 and we get that one by mistake. Throw all such signals into this
1292 list, and send them back to their respective threads once we're
1293 finished calling waitpid. */
1295 static struct event_list {
1296 struct gdbserv_thread *thread;
1300 static int pending_events_listsize;
1301 static int pending_events_top;
1303 /* Function: add_pending_event
1304 Helper function for wait_all_threads.
1306 When we call waitpid for each thread (trying to consume the SIGSTOP
1307 events that we sent from stop_all_threads), we sometimes inadvertantly
1308 get other events that we didn't send. We pend these to a list, and
1309 then resend them to the child threads after our own SIGSTOP events
1312 This list will be used to choose which of the possible events
1313 will be returned to the debugger by check_child_status. */
1316 add_pending_event (struct gdbserv_thread *thread, union wait waited)
1318 if (pending_events_top >= pending_events_listsize)
1320 pending_events_listsize += 64;
1322 realloc (pending_events,
1323 pending_events_listsize * sizeof (*pending_events));
1325 pending_events [pending_events_top].thread = thread;
1326 pending_events [pending_events_top].waited = waited;
1327 pending_events [pending_events_top].selected = 0;
1328 pending_events_top ++;
1331 /* Function: select_pending_event
1332 Helper function for wait_all_threads.
1334 Having collected a list of events from various threads,
1335 choose one "favored event" to be returned to the debugger. */
1339 select_pending_event (struct child_process *process)
1342 int num_wifstopped_events = 0;
1345 /* Select the event that will be returned to the debugger. */
1347 /* Selection criterion #0:
1348 If there are no events, don't do anything! (paranoia) */
1349 if (pending_events_top == 0)
1352 /* Selection criterion #1:
1353 If the thread pointer is null, then the thread library is
1354 not in play yet, so this is the only thread and the only event. */
1355 if (pending_events[0].thread == NULL)
1361 /* Selection criterion #2:
1362 Exit and terminate events take priority. */
1363 for (i = 0; i < pending_events_top; i++)
1364 if (WIFEXITED (pending_events[i].waited) ||
1365 WIFSIGNALED (pending_events[i].waited))
1370 /* Selection criterion #3:
1371 Give priority to a stepping SIGTRAP. */
1372 for (i = 0; i < pending_events_top; i++)
1373 if (pending_events[i].thread->stepping &&
1374 WIFSTOPPED (pending_events[i].waited) &&
1375 WSTOPSIG (pending_events[i].waited) == SIGTRAP)
1377 /* We don't actually know whether this sigtrap was the result
1378 of a singlestep, or of executing a trap instruction. But
1379 GDB has a better chance of figuring it out than we do. */
1383 /* Selection criterion #4:
1384 Count the WIFSTOPPED events and choose one at random. */
1385 for (i = 0; i < pending_events_top; i++)
1386 if (WIFSTOPPED (pending_events[i].waited))
1387 num_wifstopped_events ++;
1390 ((num_wifstopped_events * (double) rand ()) / (RAND_MAX + 1.0));
1392 for (i = pending_events_top - 1; i >= 0; i--)
1393 if (WIFSTOPPED (pending_events[i].waited))
1395 if (random_key == --num_wifstopped_events)
1399 else if (WSTOPSIG (pending_events[i].waited) == SIGINT)
1401 goto selected; /* Give preference to SIGINT. */
1405 /* Selection criterion #4 (should never get here):
1406 If all else fails, take the first event in the list. */
1409 selected: /* Got our favored event. */
1410 pending_events[i].selected = 1;
1411 process->event_thread = pending_events[i].thread;
1412 if (pending_events[i].thread)
1413 process->pid = pending_events[i].thread->ti.ti_lid;
1415 handle_waitstatus (process, pending_events[i].waited);
1416 if (thread_db_noisy)
1417 fprintf (stderr, "<select_pending_event: pid %d '%c' %d>\n",
1418 process->pid, process->stop_status, process->stop_signal);
1422 /* Function: send_pending_signals
1423 Helper function for wait_all_threads.
1425 When we call waitpid for each thread (trying to consume the SIGSTOP
1426 events that we sent from stop_all_threads), we sometimes inadvertantly
1427 get other events that we didn't send. We pend these to a list, and
1428 then resend them to the child threads after our own SIGSTOP events
1431 Some events in the list require special treatment:
1432 * One event is "selected" to be returned to the debugger.
1434 * Trap events may represent breakpoints. We can't just resend
1435 the signal. Instead we must arrange for the breakpoint to be
1436 hit again when the thread resumes. */
1439 send_pending_signals (struct child_process *process)
1444 for (i = 0; i < pending_events_top; i++)
1446 if (WIFSTOPPED (pending_events[i].waited) &&
1447 ! pending_events[i].selected)
1449 signum = WSTOPSIG (pending_events[i].waited);
1450 if (signum == SIGTRAP &&
1451 pending_events[i].thread->stepping == 0)
1453 /* Breakpoint. Push it back. */
1454 if (thread_db_noisy)
1455 fprintf (stderr, "<send_pending_events: pushing back SIGTRAP for %d>\n",
1456 pending_events[i].thread->ti.ti_lid);
1457 decr_pc_after_break (process->serv,
1458 pending_events[i].thread->ti.ti_lid);
1460 else /* FIXME we're letting SIGINT go thru as normal */
1462 /* Put the signal back into the child's queue. */
1463 kill (pending_events[i].thread->ti.ti_lid,
1464 WSTOPSIG (pending_events[i].waited));
1468 pending_events_top = 0;
1471 /* Function: wait_all_threads
1472 Use waitpid to close the loop on all threads that have been
1473 attached or SIGSTOP'd. Skip the eventpid -- it's already been waited.
1475 Special considerations:
1476 The debug signal does not go into the event queue,
1477 does not get forwarded to the thread etc. */
1480 wait_all_threads (struct child_process *process)
1482 struct gdbserv_thread *thread;
1486 for (thread = first_thread_in_list ();
1488 thread = next_thread_in_list (thread))
1490 /* Special handling for the thread that has already been waited. */
1491 if (thread->ti.ti_lid == process->pid)
1493 /* HACK mark him waited. */
1498 while ((thread->stopped || thread->attached) &&
1502 ret = waitpid (thread->ti.ti_lid, (int *) &w,
1503 thread->ti.ti_lid == proc_handle.pid ? 0 : __WCLONE);
1506 if (errno == ECHILD)
1507 fprintf (stderr, "<wait_all_threads: %d has disappeared>\n",
1510 fprintf (stderr, "<wait_all_threads: waitpid %d failed, '%s'>\n",
1511 thread->ti.ti_lid, strerror (errno));
1516 add_pending_event (thread, w);
1517 fprintf (stderr, "<wait_all_threads: %d has exited>\n",
1521 if (WIFSIGNALED (w))
1523 add_pending_event (thread, w);
1524 fprintf (stderr, "<wait_all_threads: %d died with signal %d>\n",
1525 thread->ti.ti_lid, WTERMSIG (w));
1528 stopsig = WSTOPSIG (w);
1531 /* This is the one we're looking for.
1532 Mark the thread as 'waited' and move on to the next thread. */
1533 #if 0 /* too noisy! */
1534 if (thread_db_noisy)
1535 fprintf (stderr, "<waitpid (%d, SIGSTOP)>\n", thread->ti.ti_lid);
1540 if (stopsig == debug_signal)
1542 /* This signal does not need to be forwarded. */
1543 if (thread_db_noisy)
1544 fprintf (stderr, "<wait_all_threads: ignoring SIGDEBUG (%d) for %d>\n",
1550 if (thread_db_noisy)
1551 fprintf (stderr, "<wait_all_threads: stash sig %d for %d at 0x%08x>\n",
1552 stopsig, thread->ti.ti_lid,
1553 (unsigned long) debug_get_pc (process->serv,
1554 thread->ti.ti_lid));
1555 add_pending_event (thread, w);
1559 if (!thread->waited) /* Signal was something other than STOP. */
1561 /* Continue the thread so it can stop on the next signal. */
1562 continue_lwp (thread->ti.ti_lid, 0);
1566 select_pending_event (process);
1567 send_pending_signals (process);
1570 /* Function: continue_thread
1571 Send continue to a struct gdbserv_thread. */
1574 continue_thread (struct gdbserv_thread *thread, int signal)
1576 thread_db_flush_regset_caches();
1578 /* Continue thread only if (a) it was just attached, or
1579 (b) we stopped it and waited for it. */
1580 if (thread->ti.ti_lid != 0)
1581 if (thread->attached || (thread->stopped && thread->waited))
1583 continue_lwp (thread->ti.ti_lid, signal);
1584 thread->stopped = thread->attached = thread->waited = 0;
1586 thread_db_invalidate_caches ();
1589 /* Function: continue_all_threads
1590 Send continue to all stopped or attached threads
1591 except the event thread (which will be continued separately). */
1594 continue_all_threads (struct gdbserv *serv)
1596 struct child_process *process = gdbserv_target_data (serv);
1597 struct gdbserv_thread *thread;
1600 for (thread = first_thread_in_list ();
1602 thread = next_thread_in_list (thread))
1604 /* Send any newly attached thread the restart signal. */
1605 if (thread->attached)
1606 continue_thread (thread, restart_signal);
1608 continue_thread (thread, 0);
1612 /* Function: continue_program
1613 Make sure every thread is running, starting with the event thread. */
1616 thread_db_continue_program (struct gdbserv *serv)
1618 struct child_process *process = gdbserv_target_data (serv);
1620 /* Synchronize the regset caches. */
1621 thread_db_flush_regset_caches();
1623 /* First resume the event thread. */
1624 if (process->event_thread)
1625 continue_thread (process->event_thread, process->signal_to_send);
1627 continue_lwp (process->pid, process->signal_to_send);
1629 process->stop_signal = process->stop_status =
1630 process->signal_to_send = 0;
1632 /* Then resume everyone else. */
1633 continue_all_threads (serv);
1634 process->running = 1;
1635 thread_db_invalidate_caches ();
1638 /* Function: singlestep_thread
1639 Send SINGLESTEP to a struct gdbserv_thread. */
1642 singlestep_thread (struct gdbserv *serv,
1643 struct gdbserv_thread *thread,
1646 singlestep_lwp (serv, thread->ti.ti_lid, signal);
1647 thread->stopped = thread->attached = thread->waited = 0;
1648 thread->stepping = 1;
1651 /* Function: singlestep_program
1652 Make sure every thread is runnable, while the event thread gets to
1656 thread_db_singlestep_program (struct gdbserv *serv)
1658 struct child_process *process = gdbserv_target_data (serv);
1660 /* Synchronize the regset caches. */
1661 thread_db_flush_regset_caches();
1663 /* First singlestep the event thread. */
1664 if (process->event_thread)
1665 singlestep_thread (serv, process->event_thread, process->signal_to_send);
1667 singlestep_lwp (serv, process->pid, process->signal_to_send);
1669 process->stop_status = process->stop_signal =
1670 process->signal_to_send = 0;
1672 /* Then resume everyone else. */
1673 continue_all_threads (serv); /* All but the event thread. */
1674 process->running = 1;
1675 thread_db_invalidate_caches ();
1678 /* Function: thread_db_continue_thread
1679 Let a single thread continue, while everyone else waits. */
1682 thread_db_continue_thread (struct gdbserv *serv,
1683 struct gdbserv_thread *thread,
1684 const struct gdbserv_reg *signum)
1686 struct child_process *process = gdbserv_target_data (serv);
1689 /* Synchronize the regset caches. */
1690 thread_db_flush_regset_caches();
1692 /* Handle the signal value. */
1693 if (parentvec.process_signal && signum)
1695 gdbserv_reg_to_ulong (serv, signum, &sig);
1696 parentvec.process_signal (serv, (int) sig);
1699 /* A null thread argument is to be taken as a continue for all. */
1701 thread_db_continue_program (serv);
1704 process->pid = thread->ti.ti_lid; /* thread to be continued */
1705 continue_thread (thread, process->signal_to_send);
1706 process->stop_status = process->stop_signal =
1707 process->signal_to_send = 0;
1708 process->running = 1;
1710 thread_db_invalidate_caches ();
1713 /* Function: singlestep_thread
1714 Let a single thread step, while everyone else waits. */
1717 thread_db_singlestep_thread (struct gdbserv *serv,
1718 struct gdbserv_thread *thread,
1719 const struct gdbserv_reg *signum)
1721 struct child_process *process = gdbserv_target_data (serv);
1724 /* Synchronize the regset caches. */
1725 thread_db_flush_regset_caches();
1727 /* Handle the signal value. */
1728 if (parentvec.process_signal && signum)
1730 gdbserv_reg_to_ulong (serv, signum, &sig);
1731 parentvec.process_signal (serv, (int) sig);
1734 /* A null thread argument is to be taken as a singlestep for all. */
1736 thread_db_singlestep_program (serv);
1739 singlestep_thread (serv, thread, process->signal_to_send);
1740 process->stop_status = process->stop_signal =
1741 process->signal_to_send = 0;
1742 process->running = 1;
1744 thread_db_invalidate_caches ();
1747 /* Function: exit_program
1748 Called by main loop when child exits. */
1751 thread_db_exit_program (struct gdbserv *serv)
1753 /* FIXME: stop and kill all threads. */
1755 /* Shut down the thread_db library interface. */
1756 td_ta_delete_p (thread_agent);
1757 thread_agent = NULL;
1759 /* Discard all cached symbol lookups. */
1760 free_symbol_list ();
1761 /* Discard all cached threads. */
1762 free_thread_list ();
1763 /* Call underlying exit_program method. */
1764 parentvec.exit_program (serv);
1767 /* Function: check_child_state
1769 This function checks for signal events in the running child processes.
1770 It does not block if there is no event in any child, but if there is
1771 an event, it selectively calls other functions that will, if appropriate,
1772 make sure that all the other children are stopped as well.
1774 This is a polling (non-blocking) function, and may be called when
1775 the child is already stopped. */
1778 thread_db_check_child_state (struct child_process *process)
1780 struct gdbserv *serv = process->serv;
1784 /* The "process" is likely to be the parent thread.
1785 We will have to manage a list of threads/pids. */
1787 /* Since this is a polling call, and threads don't all stop at once,
1788 it is possible for a subsequent call to intercept a new wait event
1789 before we've resumed from the previous wait event. Prevent this
1790 with a resume flag. */
1792 if (process->running)
1794 eventpid = waitpid (-1, (int *) &w, WNOHANG);
1795 /* If no event on main thread, check clone threads.
1796 It doesn't matter what event we find first, since we now have
1797 a fair algorithm for choosing which event to handle next. */
1799 eventpid = waitpid (-1, (int *) &w, WNOHANG | __WCLONE);
1801 if (eventpid > 0) /* found an event */
1803 /* Allow underlying target to use the event process by default,
1804 since it is stopped and the others are still running. */
1805 process->pid = eventpid;
1807 handle_waitstatus (process, w);
1809 /* Look for thread exit.
1810 This has to be done now -- if the eventpid has exited, I can't
1811 run update_thread_list because there is no stopped process
1812 thru which I can read memory. I could find another one to
1813 stop, but it's not really worth it. */
1814 if (process->stop_status == 'W')
1816 if (eventpid == proc_handle.pid)
1817 return 1; /* Main thread exited! */
1819 return 0; /* Just a thread exit, don't tell GDB. */
1822 /* FIXME: this debugging output will be removed soon, but
1823 putting it here before the update_thread_list etc. is
1824 bad from the point of view of synchronization. */
1825 handle_waitstatus (process, w);
1826 if (thread_db_noisy)
1827 fprintf (stderr, "<check_child_state: %d got '%c' - %d at 0x%08x>\n",
1828 process->pid, process->stop_status, process->stop_signal,
1829 (unsigned long) debug_get_pc (process->serv, process->pid));
1831 /* Update the thread list. */
1832 update_thread_list ();
1834 /* For now, call get_thread_signals from here (FIXME:) */
1835 get_thread_signals ();
1837 /* Put this child's event into the pending list. */
1838 add_pending_event (thread_list_lookup_by_lid ((lwpid_t) eventpid),
1841 stop_all_threads (process);
1842 wait_all_threads (process);
1843 /* Note: if more than one thread has an event ready to be
1844 handled, wait_all_threads will have chosen one at random. */
1846 if (got_thread_signals && process->stop_status == 'T')
1848 /* Child stopped with a signal.
1849 See if it was one of our special signals. */
1851 if (process->stop_signal == cancel_signal || /* ignore */
1852 process->stop_signal == restart_signal || /* ignore */
1853 process->stop_signal == debug_signal || /* ignore */
1854 process->stop_signal == SIGCHLD) /* ignore */
1856 /* Ignore this signal, restart the child. */
1857 if (thread_db_noisy)
1858 fprintf (stderr, "<check_child_state: ignoring signal %d for %d>\n",
1859 process->stop_signal, process->pid);
1860 if (process->stop_signal == debug_signal)
1862 /* The debug signal arrives under two circumstances:
1863 1) The main thread raises it once, upon the first call
1864 to pthread_create. This lets us detect the manager
1865 thread. The main thread MUST be given the restart
1866 signal when this occurs.
1867 2) The manager thread raises it each time a new
1868 child thread is created. The child thread will be
1869 in sigsuspend, and MUST be sent the restart signal.
1870 However, the manager thread, which raised the debug
1871 signal, does not need to be restarted.
1873 Sending the restart signal to the newly attached
1874 child thread (which is not the event thread) is
1875 handled in continue_all_threads. */
1877 if (process->pid == proc_handle.pid) /* main thread */
1878 process->stop_signal = restart_signal;
1879 else /* not main thread */
1880 process->stop_signal = 0;
1882 process->signal_to_send = process->stop_signal;
1883 currentvec->continue_program (serv);
1887 if (process->stop_status == 'W')
1889 if (process->pid == proc_handle.pid)
1890 return 1; /* Main thread exited! */
1893 currentvec->continue_program (serv);
1894 return 0; /* Just a thread exit, don't tell GDB. */
1898 process->running = 0;
1900 /* This is the place to cancel its 'stepping' flag. */
1901 if (process && process->event_thread)
1902 process->event_thread->stepping = 0;
1904 /* Pass this event back to GDB. */
1905 if (process->debug_backend)
1906 fprintf (stderr, "wait returned '%c' (%d) for %d.\n",
1907 process->stop_status, process->stop_signal, eventpid);
1912 /* NOTE: this function is called in a polling loop, so it
1913 probably (?) should not block. Return when there's no event. */
1917 /* Function: fromtarget_thread_break
1918 Called from the main loop when one of the child processes stops.
1919 Notifies the RDA library and lets it know which thread took the event. */
1922 thread_db_fromtarget_thread_break (struct child_process *process)
1924 int gdb_signal = parentvec.compute_signal (process->serv,
1925 process->stop_signal);
1927 gdbserv_fromtarget_thread_break (process->serv,
1928 process->event_thread,
1932 /* Function: get_thread_reg
1933 Get a register value for a specific thread. */
1936 thread_db_get_thread_reg (struct gdbserv *serv,
1937 struct gdbserv_thread *thread,
1939 struct gdbserv_reg *reg)
1941 struct child_process *process = gdbserv_target_data (serv);
1942 td_thrhandle_t thread_handle;
1944 FPREGSET_T fpregset;
1949 thread = process->event_thread; /* Default to the event thread. */
1951 if (thread_agent == NULL || /* Thread layer not alive yet? */
1952 thread == NULL) /* No thread specified? */
1954 /* Fall back on parentvec non-threaded method. */
1955 if (parentvec.get_reg)
1956 return parentvec.get_reg (serv, regnum, reg);
1958 return -1; /* give up. */
1961 /* Thread_db active, thread_agent valid.
1962 The request goes to the thread_db library.
1963 From there it will be dispatched to ps_lgetregs,
1964 and from there it will be kicked back to the parent. */
1966 if (thread->ti.ti_state == TD_THR_ZOMBIE ||
1967 thread->ti.ti_state == TD_THR_UNKNOWN)
1969 /* This thread is dead! Can't get its registers. */
1973 ret = thread_db_map_id2thr (thread_agent,
1976 if (ret == TD_NOTHR)
1978 /* Thread has exited, no registers. */
1981 else if (ret != TD_OK)
1983 fprintf (stderr, "<<< ERROR get_thread_reg map_id2thr %d >>>\n",
1985 return -1; /* fail */
1988 if (is_fp_reg (regnum))
1990 if (thread_db_getfpregs (&thread_handle, &fpregset) != TD_OK)
1992 /* Failure to get the fpregs isn't necessarily an error.
1993 Assume that the target just doesn't support fpregs. */
1996 /* Now extract the register from the fpregset. */
1997 if (reg_from_fpregset (serv, reg, regnum, &fpregset) < 0)
1999 fprintf (stderr, "<<< ERROR reg_from_fpregset %d %d>>>\n",
2000 thread->ti.ti_tid, regnum);
2004 else if (td_thr_getxregsize_p != NULL
2005 && td_thr_getxregs_p != NULL
2006 && is_extended_reg (regnum))
2011 if (td_thr_getxregsize_p (&thread_handle, &xregsize) != TD_OK)
2013 /* Failure to get the size of the extended regs isn't
2014 necessarily an error. Assume that the target just
2015 doesn't support them. */
2021 /* Another form of not being supported... */
2025 /* Allocate space for the extended registers. */
2026 xregset = alloca (xregsize);
2028 /* Fetch the extended registers. */
2029 if (td_thr_getxregs_p (&thread_handle, xregset) != TD_OK)
2031 /* Failure to get the extended regs isn't necessarily an error.
2032 Assume that the target just doesn't support them. */
2036 /* Now extract the register from the extended regset. */
2037 if (reg_from_xregset (serv, reg, regnum, xregset) < 0)
2039 fprintf (stderr, "<<< ERROR reg_from_xregset %d %d>>>\n",
2040 thread->ti.ti_tid, regnum);
2044 else if (is_gp_reg (regnum)) /* GP reg */
2046 if (thread_db_getgregs (&thread_handle, gregset) != TD_OK)
2048 fprintf (stderr, "<<< ERROR get_thread_reg td_thr_getgregs %d >>>\n",
2050 return -1; /* fail */
2052 /* Now extract the requested register from the gregset. */
2053 if (reg_from_gregset (serv, reg, regnum, gregset) < 0)
2055 fprintf (stderr, "<<< ERROR reg_from_gregset %d %d>>>\n",
2056 thread->ti.ti_tid, regnum);
2057 return -1; /* fail */
2062 /* Register not supported by this target. This shouldn't be
2063 construed as an error though. */
2067 return 0; /* success */
2070 /* Function: set_thread_reg
2071 Set a register value for a specific thread. */
2074 thread_db_set_thread_reg (struct gdbserv *serv,
2075 struct gdbserv_thread *thread,
2077 const struct gdbserv_reg *reg)
2079 struct child_process *process = gdbserv_target_data (serv);
2080 td_thrhandle_t thread_handle;
2081 FPREGSET_T fpregset;
2086 thread = process->event_thread; /* Default to the event thread. */
2088 if (thread_agent == NULL || /* Thread layer not alive yet? */
2089 thread == NULL) /* No thread specified? */
2091 /* Fall back on parentvec non-threaded method. */
2092 if (parentvec.set_reg)
2093 return parentvec.set_reg (serv, regnum, (struct gdbserv_reg *) reg);
2095 return -1; /* give up. */
2098 /* Thread_db active, thread_agent valid.
2099 The request goes to the thread_db library.
2100 From there it will be dispatched to ps_lsetregs,
2101 and from there it will be kicked back to the parent. */
2103 if (thread->ti.ti_state == TD_THR_ZOMBIE ||
2104 thread->ti.ti_state == TD_THR_UNKNOWN)
2106 /* This thread is dead! Can't get its registers. */
2110 ret = thread_db_map_id2thr (thread_agent,
2113 if (ret == TD_NOTHR)
2115 /* Thread has exited, no registers. */
2118 else if (ret != TD_OK)
2120 fprintf (stderr, "<<< ERROR set_thread_reg map_id2thr %d >>>\n",
2122 return -1; /* fail */
2125 if (is_fp_reg (regnum))
2127 /* Get the current fpregset. */
2128 if (thread_db_getfpregs (&thread_handle, &fpregset) != TD_OK)
2130 /* Failing to get the fpregs is not necessarily an error.
2131 Assume it simply means that this target doesn't support
2135 /* Now write the new reg value into the fpregset. */
2136 if (reg_to_fpregset (serv, reg, regnum, &fpregset) < 0)
2138 fprintf (stderr, "<<< ERROR reg_to_fpregset %d %d >>>\n",
2139 thread->ti.ti_tid, regnum);
2140 return -1; /* fail */
2142 /* Now write the fpregset back to the child. */
2143 if (thread_db_setfpregs (&thread_handle, &fpregset) != TD_OK)
2145 fprintf (stderr, "<<< ERROR set_thread_reg td_thr_setfpregs %d>>>\n",
2147 return -1; /* fail */
2150 else if (td_thr_getxregsize_p != NULL
2151 && td_thr_getxregs_p != NULL
2152 && td_thr_setxregs_p != NULL
2153 && is_extended_reg (regnum))
2158 if (td_thr_getxregsize_p (&thread_handle, &xregsize) != TD_OK)
2160 /* Failure to get the size of the extended regs isn't
2161 necessarily an error. Assume that the target just
2162 doesn't support them. */
2168 /* Another form of not being supported... */
2172 /* Allocate space for the extended registers. */
2173 xregset = alloca (xregsize);
2175 /* Fetch the extended registers. */
2176 if (td_thr_getxregs_p (&thread_handle, xregset) != TD_OK)
2178 /* Failure to get the extended regs isn't necessarily an error.
2179 Assume that the target just doesn't support them. */
2182 /* Now write the new reg value into the extended regset. */
2183 if (reg_to_xregset (serv, reg, regnum, xregset) < 0)
2185 fprintf (stderr, "<<< ERROR reg_to_xregset %d %d >>>\n",
2186 thread->ti.ti_tid, regnum);
2187 return -1; /* fail */
2189 /* Now write the extended regset back to the child. */
2190 if (td_thr_setxregs_p (&thread_handle, gregset) != TD_OK)
2192 fprintf (stderr, "<<< ERROR set_thread_reg td_thr_setxregs %d >>>\n",
2194 return -1; /* fail */
2197 else if (is_gp_reg (regnum))
2199 /* First get the current gregset. */
2200 if (thread_db_getgregs (&thread_handle, gregset) != TD_OK)
2202 fprintf (stderr, "<<< ERROR set_thread_reg td_thr_getgregs %d >>>\n",
2204 return -1; /* fail */
2206 /* Now write the new reg value into the gregset. */
2207 if (reg_to_gregset (serv, reg, regnum, gregset) < 0)
2209 fprintf (stderr, "<<< ERROR reg_to_gregset %d %d >>>\n",
2210 thread->ti.ti_tid, regnum);
2211 return -1; /* fail */
2213 /* Now write the gregset back to the child. */
2214 if (thread_db_setgregs (&thread_handle, gregset) != TD_OK)
2216 fprintf (stderr, "<<< ERROR set_thread_reg td_thr_setgregs %d >>>\n",
2218 return -1; /* fail */
2222 return 0; /* success */
2225 /* Function: thread_db_attach
2226 gdbserv target function called upon attaching to gdb.
2227 Return -1 for failure, zero for success.
2228 Note that this has nothing to do with attaching to a running process
2229 (which in fact we don't even know how to do), or a running thread. */
2232 thread_db_attach (struct gdbserv *serv, struct gdbserv_target *target)
2235 struct child_process *process = target->data;
2236 extern struct server_vector gdbserver;
2239 if ((thread_db_dlopen ()) < 0)
2240 return -1; /* fail */
2242 /* Save a copy of the existing target vector before we modify it. */
2243 memcpy (&parentvec, target, sizeof (parentvec));
2244 /* Save a pointer to the actual target vector. */
2245 currentvec = target;
2247 /* Initialize the library. */
2248 if ((ret = td_init_p ()) != TD_OK)
2251 "Cannot initialize libthread_db: %s", thread_db_err_str (ret));
2253 return -1; /* fail */
2256 /* Initialize threadish target methods. */
2257 target->thread_info = thread_db_thread_info;
2258 target->thread_next = thread_db_thread_next;
2259 target->thread_id = thread_db_thread_id;
2260 target->thread_lookup_by_id = thread_db_thread_lookup_by_id;
2261 target->process_set_gen = thread_db_set_gen;
2262 target->process_get_gen = thread_db_get_gen;
2263 target->detach = thread_db_detach;
2265 /* Take over selected target methods. */
2266 target->exit_program = thread_db_exit_program;
2267 target->continue_program = thread_db_continue_program;
2268 target->singlestep_program = thread_db_singlestep_program;
2270 target->continue_thread = thread_db_continue_thread;
2271 target->singlestep_thread = thread_db_singlestep_thread;
2273 /* Take over get_reg / set_reg methods with threaded versions. */
2274 if (target->next_gg_reg != NULL &&
2275 target->reg_format != NULL &&
2276 target->output_reg != NULL &&
2277 target->input_reg != NULL)
2279 target->get_thread_reg = thread_db_get_thread_reg;
2280 target->set_thread_reg = thread_db_set_thread_reg;
2283 fprintf (stderr, "< ERROR attach: GDB will not read thread regs. >>>\n");
2285 /* KLUDGE: Insert some magic symbols into the cached symbol list,
2286 to be looked up later. This is badly wrong -- we should be
2287 obtaining these values thru the thread_db interface. Their names
2288 should not be hard-coded here <sob>. */
2289 add_symbol_to_list ("__pthread_sig_restart", 0, UNDEFINED);
2290 add_symbol_to_list ("__pthread_sig_cancel", 0, UNDEFINED);
2291 add_symbol_to_list ("__pthread_sig_debug", 0, UNDEFINED);
2292 add_symbol_to_list ("__pthread_threads_debug", 0, UNDEFINED);
2294 /* Attempt to open the thread_db interface. This attempt will
2295 most likely fail (unles the child is statically linked). */
2296 thread_db_open (serv, process->pid); /* Don't test return value */
2298 /* Take over the "wait" vector. FIXME global object */
2299 gdbserver.check_child_state = thread_db_check_child_state;
2300 /* Take over the "fromtarget_break" vector. FIXME global object */
2301 gdbserver.fromtarget_break = thread_db_fromtarget_thread_break;
2302 /* FIXME what about terminate and exit? */
2304 /* Set up the regset caches. */
2305 initialize_regset_caches ();
2306 return 0; /* success */