0d62e5e807
* gdbserver/thread-db.c: New file. * gdbserver/proc-service.c: New file. * gdbserver/acinclude.m4: New file. * gdbserver/Makefile.in: Add GDBSERVER_LIBS, gdb_proc_service_h, proc-service.o, and thread-db.o. (linux-low.o): Add USE_THREAD_DB. * gdbserver/acconfig.h: Add HAVE_PRGREGSET_T, HAVE_PRFPREGSET_T, HAVE_LWPID_T, HAVE_PSADDR_T, and PRFPREGSET_T_BROKEN. * gdbserver/aclocal.m4: Regenerated. * gdbserver/config.in: Regenerated. * gdbserver/configure: Regenerated. * gdbserver/configure.in: Check for proc_service.h, sys/procfs.h, thread_db.h, and linux/elf.h headrs. Check for lwpid_t, psaddr_t, prgregset_t, prfpregset_t, and PRFPREGSET_T_BROKEN. Introduce srv_thread_depfiles and USE_THREAD_DB. Check for -lthread_db and thread support. * gdbserver/configure.srv: Enable thread_db support for ARM, i386, MIPS, PowerPC, and SuperH. * gdbserver/i387-fp.c: Constify arguments. * gdbserver/i387-fp.h: Likewise. * gdbserver/inferiors.c: (struct thread_info): Renamed from `struct inferior_info'. Remove PID member. Use generic inferior list header. All uses updated. (inferiors, signal_pid): Removed. (all_threads): New variable. (get_thread): Define. (add_inferior_to_list): New function. (for_each_inferior): New function. (change_inferior_id): New function. (add_inferior): Removed. (remove_inferior): New function. (add_thread): New function. (free_one_thread): New function. (remove_thread): New function. (clear_inferiors): Use for_each_inferior and free_one_thread. (find_inferior): New function. (find_inferior_id): New function. (inferior_target_data): Update argument type. (set_inferior_target_data): Likewise. (inferior_regcache_data): Likewise. (set_inferior_regcache_data): Likewise. * gdbserver/linux-low.c (linux_bp_reinsert): Remove. (all_processes, stopping_threads, using_thrads) (struct pending_signals, debug_threads, pid_of): New. (inferior_pid): Replace with macro. (struct inferior_linux_data): Remove. (get_stop_pc, add_process): New functions. (linux_create_inferior): Restore SIGRTMIN+1 before calling exec. Use add_process and add_thread. (linux_attach_lwp): New function, based on old linux_attach. Use add_process and add_thread. Set stop_expected for new threads. (linux_attach): New function. (linux_kill_one_process): New function. (linux_kill): Kill all LWPs. (linux_thread_alive): Use find_inferior_id. (check_removed_breakpoints, status_pending_p): New functions. (linux_wait_for_process): Renamed from linux_wait_for_one_inferior. Update. Use WNOHANG. Wait for cloned processes also. Update process struct for the found process. (linux_wait_for_event): New function. (linux_wait): Use it. Support LWPs. (send_sigstop, wait_for_sigstop, stop_all_processes) (linux_resume_one_process, linux_continue_one_process): New functions. (linux_resume): Support LWPs. (REGISTER_RAW_SIZE): Remove. (fetch_register): Use register_size instead. Call supply_register. (usr_store_inferior_registers): Likewise. Call collect_register. Fix recursive case. (regsets_fetch_inferior_registers): Improve error message. (regsets_store_inferior_registers): Add debugging. (linux_look_up_symbols): Call thread_db_init if USE_THREAD_DB. (unstopped_p, linux_signal_pid): New functions. (linux_target_ops): Add linux_signal_pid. (linux_init_signals): New function. (initialize_low): Call it. Initialize using_threads. * gdbserver/regcache.c (inferior_regcache_data): Add valid flag. (get_regcache): Fetch registers lazily. Add fetch argument and update all callers. (regcache_invalidate_one, regcache_invalidate): New functions. (new_register_cache): Renamed from create_register_cache. Return the new regcache. (free_register_cache): Change argument to a void *. (registers_to_string, registers_from_string): Call get_regcache with fetch flag set. (register_data): Make static. Pass fetch flag to get_regcache. (supply_register): Call get_regcache with fetch flag clear. (collect_register): Call get_regcache with fetch flag set. (collect_register_as_string): New function. * gdbserver/regcache.h: Update. * gdbserver/remote-utils.c (putpkt): Flush after debug output and use stderr. Handle input interrupts while waiting for an ACK. (input_interrupt): Use signal_pid method. (getpkt): Flush after debug output and use stderr. (outreg): Use collect_register_as_string. (new_thread_notify, dead_thread_notify): New functions. (prepare_resume_reply): Check using_threads. Set thread_from_wait and general_thread. (look_up_one_symbol): Flush after debug output. * gdbserver/server.c (step_thread, server_waiting): New variables. (start_inferior): Don't use signal_pid. Update call to mywait. (attach_inferior): Update call to mywait. (handle_query): Handle qfThreadInfo and qsThreadInfo. (main): Don't fetch/store registers explicitly. Use set_desired_inferior. Support proposed ``Hs'' packet. Update calls to mywait. * gdbserver/server.h: Update. (struct inferior_list, struct_inferior_list_entry): New. * gdbserver/target.c (set_desired_inferior): New. (write_inferior_memory): Constify. (mywait): New function. * gdbserver/target.h: Update. (struct target_ops): New signal_pid method. (mywait): Removed macro, added prototype. * gdbserver/linux-low.h (regset_func): Removed. (regset_fill_func, regset_store_func): New. (enum regset_type): New. (struct regset_info): Add type field. Use new operation types. (struct linux_target_ops): stop_pc renamed to get_pc. Add decr_pc_after_break and breakpoint_at. (get_process, get_thread_proess, get_process_thread) (strut process_info, all_processes, linux_attach_lwp) (thread_db_init): New. * gdbserver/linux-arm-low.c (arm_get_pc, arm_set_pc, arm_breakpoint, arm_breakpoint_len, arm_breakpoint_at): New. (the_low_target): Add new members. * gdbserver/linux-i386-low.c (i386_store_gregset, i386_store_fpregset) (i386_store_fpxregset): Constify. (target_regsets): Add new kind identifier. (i386_get_pc): Renamed from i386_stop_pc. Simplify. (i386_set_pc): Add debugging. (i386_breakpoint_at): New function. (the_low_target): Add new members. * gdbserver/linux-mips-low.c (mips_get_pc, mips_set_pc) (mips_breakpoint, mips_breakpoint_len, mips_reinsert_addr) (mips_breakpoint_at): New. (the_low_target): Add new members. * gdbserver/linux-ppc-low.c (ppc_get_pc, ppc_set_pc) (ppc_breakpoint, ppc_breakpoint_len, ppc_breakpoint_at): New. (the_low_target): Add new members. * gdbserver/linux-sh-low.c (sh_get_pc, sh_set_pc) (sh_breakpoint, sh_breakpoint_len, sh_breakpoint_at): New. (the_low_target): Add new members. * gdbserver/linux-x86-64-low.c (target_regsets): Add new kind identifier.
343 lines
8.7 KiB
C
343 lines
8.7 KiB
C
/* Thread management interface, for the remote server for GDB.
|
|
Copyright 2002
|
|
Free Software Foundation, Inc.
|
|
|
|
Contributed by MontaVista Software.
|
|
|
|
This file is part of GDB.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 59 Temple Place - Suite 330,
|
|
Boston, MA 02111-1307, USA. */
|
|
|
|
#include "server.h"
|
|
|
|
#include "linux-low.h"
|
|
|
|
extern int debug_threads;
|
|
|
|
#ifdef HAVE_THREAD_DB_H
|
|
#include <thread_db.h>
|
|
#endif
|
|
|
|
/* Correct for all GNU/Linux targets (for quite some time). */
|
|
#define GDB_GREGSET_T elf_gregset_t
|
|
#define GDB_FPREGSET_T elf_fpregset_t
|
|
|
|
#ifndef HAVE_ELF_FPREGSET_T
|
|
/* Make sure we have said types. Not all platforms bring in <linux/elf.h>
|
|
via <sys/procfs.h>. */
|
|
#ifdef HAVE_LINUX_ELF_H
|
|
#include <linux/elf.h>
|
|
#endif
|
|
#endif
|
|
|
|
#include "../gdb_proc_service.h"
|
|
|
|
/* Structure that identifies the child process for the
|
|
<proc_service.h> interface. */
|
|
static struct ps_prochandle proc_handle;
|
|
|
|
/* Connection to the libthread_db library. */
|
|
static td_thragent_t *thread_agent;
|
|
|
|
static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data);
|
|
|
|
static char *
|
|
thread_db_err_str (td_err_e err)
|
|
{
|
|
static char buf[64];
|
|
|
|
switch (err)
|
|
{
|
|
case TD_OK:
|
|
return "generic 'call succeeded'";
|
|
case TD_ERR:
|
|
return "generic error";
|
|
case TD_NOTHR:
|
|
return "no thread to satisfy query";
|
|
case TD_NOSV:
|
|
return "no sync handle to satisfy query";
|
|
case TD_NOLWP:
|
|
return "no LWP to satisfy query";
|
|
case TD_BADPH:
|
|
return "invalid process handle";
|
|
case TD_BADTH:
|
|
return "invalid thread handle";
|
|
case TD_BADSH:
|
|
return "invalid synchronization handle";
|
|
case TD_BADTA:
|
|
return "invalid thread agent";
|
|
case TD_BADKEY:
|
|
return "invalid key";
|
|
case TD_NOMSG:
|
|
return "no event message for getmsg";
|
|
case TD_NOFPREGS:
|
|
return "FPU register set not available";
|
|
case TD_NOLIBTHREAD:
|
|
return "application not linked with libthread";
|
|
case TD_NOEVENT:
|
|
return "requested event is not supported";
|
|
case TD_NOCAPAB:
|
|
return "capability not available";
|
|
case TD_DBERR:
|
|
return "debugger service failed";
|
|
case TD_NOAPLIC:
|
|
return "operation not applicable to";
|
|
case TD_NOTSD:
|
|
return "no thread-specific data for this thread";
|
|
case TD_MALLOC:
|
|
return "malloc failed";
|
|
case TD_PARTIALREG:
|
|
return "only part of register set was written/read";
|
|
case TD_NOXREGS:
|
|
return "X register set not available for this thread";
|
|
default:
|
|
snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
|
|
return buf;
|
|
}
|
|
}
|
|
|
|
#if 0
|
|
static char *
|
|
thread_db_state_str (td_thr_state_e state)
|
|
{
|
|
static char buf[64];
|
|
|
|
switch (state)
|
|
{
|
|
case TD_THR_STOPPED:
|
|
return "stopped by debugger";
|
|
case TD_THR_RUN:
|
|
return "runnable";
|
|
case TD_THR_ACTIVE:
|
|
return "active";
|
|
case TD_THR_ZOMBIE:
|
|
return "zombie";
|
|
case TD_THR_SLEEP:
|
|
return "sleeping";
|
|
case TD_THR_STOPPED_ASLEEP:
|
|
return "stopped by debugger AND blocked";
|
|
default:
|
|
snprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
|
|
return buf;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
static void
|
|
thread_db_create_event (CORE_ADDR where)
|
|
{
|
|
td_event_msg_t msg;
|
|
td_err_e err;
|
|
struct inferior_linux_data *tdata;
|
|
|
|
if (debug_threads)
|
|
fprintf (stderr, "Thread creation event.\n");
|
|
|
|
tdata = inferior_target_data (current_inferior);
|
|
|
|
/* FIXME: This assumes we don't get another event.
|
|
In the LinuxThreads implementation, this is safe,
|
|
because all events come from the manager thread
|
|
(except for its own creation, of course). */
|
|
err = td_ta_event_getmsg (thread_agent, &msg);
|
|
if (err != TD_OK)
|
|
fprintf (stderr, "thread getmsg err: %s\n",
|
|
thread_db_err_str (err));
|
|
|
|
/* msg.event == TD_EVENT_CREATE */
|
|
|
|
find_new_threads_callback (msg.th_p, NULL);
|
|
}
|
|
|
|
#if 0
|
|
static void
|
|
thread_db_death_event (CORE_ADDR where)
|
|
{
|
|
if (debug_threads)
|
|
fprintf (stderr, "Thread death event.\n");
|
|
}
|
|
#endif
|
|
|
|
static int
|
|
thread_db_enable_reporting ()
|
|
{
|
|
td_thr_events_t events;
|
|
td_notify_t notify;
|
|
td_err_e err;
|
|
|
|
/* Set the process wide mask saying which events we're interested in. */
|
|
td_event_emptyset (&events);
|
|
td_event_addset (&events, TD_CREATE);
|
|
|
|
#if 0
|
|
/* This is reported to be broken in glibc 2.1.3. A different approach
|
|
will be necessary to support that. */
|
|
td_event_addset (&events, TD_DEATH);
|
|
#endif
|
|
|
|
err = td_ta_set_event (thread_agent, &events);
|
|
if (err != TD_OK)
|
|
{
|
|
warning ("Unable to set global thread event mask: %s",
|
|
thread_db_err_str (err));
|
|
return 0;
|
|
}
|
|
|
|
/* Get address for thread creation breakpoint. */
|
|
err = td_ta_event_addr (thread_agent, TD_CREATE, ¬ify);
|
|
if (err != TD_OK)
|
|
{
|
|
warning ("Unable to get location for thread creation breakpoint: %s",
|
|
thread_db_err_str (err));
|
|
return 0;
|
|
}
|
|
set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr,
|
|
thread_db_create_event);
|
|
|
|
#if 0
|
|
/* Don't concern ourselves with reported thread deaths, only
|
|
with actual thread deaths (via wait). */
|
|
|
|
/* Get address for thread death breakpoint. */
|
|
err = td_ta_event_addr (thread_agent, TD_DEATH, ¬ify);
|
|
if (err != TD_OK)
|
|
{
|
|
warning ("Unable to get location for thread death breakpoint: %s",
|
|
thread_db_err_str (err));
|
|
return;
|
|
}
|
|
set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr,
|
|
thread_db_death_event);
|
|
#endif
|
|
|
|
return 1;
|
|
}
|
|
|
|
static void
|
|
maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
|
|
{
|
|
td_err_e err;
|
|
struct thread_info *inferior;
|
|
struct process_info *process;
|
|
|
|
/* If we are attaching to our first thread, things are a little
|
|
different. */
|
|
if (all_threads.head == all_threads.tail)
|
|
{
|
|
inferior = (struct thread_info *) all_threads.head;
|
|
process = get_thread_process (inferior);
|
|
if (process->thread_known == 0)
|
|
{
|
|
/* Switch to indexing the threads list by TID. */
|
|
change_inferior_id (&all_threads, ti_p->ti_tid);
|
|
goto found;
|
|
}
|
|
}
|
|
|
|
inferior = (struct thread_info *) find_inferior_id (&all_threads,
|
|
ti_p->ti_tid);
|
|
if (inferior != NULL)
|
|
return;
|
|
|
|
if (debug_threads)
|
|
fprintf (stderr, "Attaching to thread %ld (LWP %d)\n",
|
|
ti_p->ti_tid, ti_p->ti_lid);
|
|
linux_attach_lwp (ti_p->ti_lid, ti_p->ti_tid);
|
|
inferior = (struct thread_info *) find_inferior_id (&all_threads,
|
|
ti_p->ti_tid);
|
|
if (inferior == NULL)
|
|
{
|
|
warning ("Could not attach to thread %ld (LWP %d)\n",
|
|
ti_p->ti_tid, ti_p->ti_lid);
|
|
return;
|
|
}
|
|
|
|
process = inferior_target_data (inferior);
|
|
|
|
found:
|
|
new_thread_notify (ti_p->ti_tid);
|
|
|
|
process->tid = ti_p->ti_tid;
|
|
process->lwpid = ti_p->ti_lid;
|
|
|
|
process->thread_known = 1;
|
|
err = td_thr_event_enable (th_p, 1);
|
|
if (err != TD_OK)
|
|
error ("Cannot enable thread event reporting for %d: %s",
|
|
ti_p->ti_lid, thread_db_err_str (err));
|
|
}
|
|
|
|
static int
|
|
find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
|
|
{
|
|
td_thrinfo_t ti;
|
|
td_err_e err;
|
|
|
|
err = td_thr_get_info (th_p, &ti);
|
|
if (err != TD_OK)
|
|
error ("Cannot get thread info: %s", thread_db_err_str (err));
|
|
|
|
/* Check for zombies. */
|
|
if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
|
|
return 0;
|
|
|
|
maybe_attach_thread (th_p, &ti);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
thread_db_find_new_threads (void)
|
|
{
|
|
td_err_e err;
|
|
|
|
/* Iterate over all user-space threads to discover new threads. */
|
|
err = td_ta_thr_iter (thread_agent, find_new_threads_callback, NULL,
|
|
TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
|
|
TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
|
|
if (err != TD_OK)
|
|
error ("Cannot find new threads: %s", thread_db_err_str (err));
|
|
}
|
|
|
|
int
|
|
thread_db_init ()
|
|
{
|
|
int err;
|
|
|
|
proc_handle.pid = ((struct inferior_list_entry *)current_inferior)->id;
|
|
|
|
err = td_ta_new (&proc_handle, &thread_agent);
|
|
switch (err)
|
|
{
|
|
case TD_NOLIBTHREAD:
|
|
/* No thread library was detected. */
|
|
return 0;
|
|
|
|
case TD_OK:
|
|
/* The thread library was detected. */
|
|
|
|
if (thread_db_enable_reporting () == 0)
|
|
return 0;
|
|
thread_db_find_new_threads ();
|
|
return 1;
|
|
|
|
default:
|
|
warning ("error initializing thread_db library.");
|
|
}
|
|
|
|
return 0;
|
|
}
|