Re: [PATCH] [native x86 GNU/Linux] Access debug register mirror from the corresponding inferior.

Pedro Alves
2013-02-11T21:10:05Z

On 02/07/2013 04:59 PM, Jan Kratochvil wrote:
> On Thu, 07 Feb 2013 17:33:39 +0100, Pedro Alves wrote:
>> There's one wrinkle though, and one which we already handle somewhat.
>> When detaching the fork child that we're not interested in debugging
>> (set detach-on-fork off / follow-fork parent), we don't even create an
> set detach-on-fork on
>> inferior for that fork child, so there's no place to get the struct
>> i386_debug_reg_state from, as that's stored in the inferior.
>>
>> I thought of more than one way to fix this, and this seemed the
>> simplest - special case the null inferior case.
>>
>> Other options involved creating a about_to_detach/about_to_fork_detach
>> hook;
>>
>> Create a target side "struct process_info", thus decoupling from
>> struct inferior (mildly complicated, lots of mechanical changes across
>> all native targets that do x86 watchpoints, or
>>
>> Always creating an inferior (that has lots of complications).
>
> I tried that in the past and I agree it was not worth it.

I've now gone the 'target side "struct process_info", thus decoupling from
struct inferior' way.

>
>
>> --- a/gdb/amd64-linux-nat.c
>> +++ b/gdb/amd64-linux-nat.c
>> @@ -394,9 +394,22 @@ amd64_linux_prepare_to_resume (struct lwp_info *lwp)
>>
>> if (lwp->arch_private->debug_registers_changed)
>> {
>> - struct i386_debug_reg_state *state = i386_debug_reg_state ();
>> + int pid = ptid_get_pid (lwp->ptid);
>> + struct inferior *inf = find_inferior_pid (pid);
>> + struct i386_debug_reg_state *state;
>> int i;
>>
>> + if (inf == NULL)
>> + {
>> + /* NULL means this is a fork child we're not interested in
>> + debugging being detached. We want to leave it with its
>> + debug registers cleared. */
>> + amd64_linux_dr_set (lwp->ptid, DR_CONTROL, 0);
>> + return;
>> + }
>
> It is already handled by this code which seems to be skipped by this patch.

Whoops, completely forgot this is here.

>
> if (detached_inf_pid != ptid_get_pid (inferior_ptid))
> {
> /* Reinitialize the local cache if INFERIOR_PTID is
> different from the LWP last detached.
>
> Linux kernel before 2.6.33 commit
> 72f674d203cd230426437cdcf7dd6f681dad8b0d
> will inherit hardware debug registers from parent
> on fork/vfork/clone. Newer Linux kernels create such tasks with
> zeroed debug registers.
>
> GDB will remove all breakpoints (and watchpoints) from the forked
> off process. We also need to reset the debug registers in that
> process to be compatible with the older Linux kernels.
>
> Copy the debug registers mirrors into the new process so that all
> breakpoints and watchpoints can be removed together. The debug
> registers mirror will become zeroed in the end before detaching
> the forked off process. */
>
> detached_inf_pid = ptid_get_pid (inferior_ptid);
> detached_inf_data_local = *inf_data;
> }
>
> Also it seems incorrect to me to use 'ptid_get_pid (inferior_ptid)' there when
> the detached LWP may not come from the current inferior, it is expected to be
> the PID of the process remaining under control of GDB.

Right. But prepare_to_resume is called in a lot more cases where we're
not detaching, and we don't switch the current inferior in those either.
I think that way is becoming too fragile.

> I did not try it but what about temporarily switching current inferior?

I think I'd rather try a different approach.

In a nutshell, we decouple the watchpoints code from inferiors, making
it track target processes instead. This way, we can freely keep track
of the watchpoint mirrors for these processes behind the core's
back. Checkpoints also play dirty tricks with swapping the process behind
the inferior, so they get special treatment too in the patch (which just
amounts to calling a new hook). Instead of the old
hack in i386_inferior_data_get, where we returned a copy of the current
inferior's debug registers mirror, as soon as we detect a fork in the target,
we copy the debug register mirror from the parent to the child process.

I don't have an old kernel handy to test, but I stepped through gdb doing
the watchpoint removal in the fork child in the watchpoint-fork test
seeing that the debug registers end up cleared in the child.

I didn't find the need for linux_nat_iterate_watchpoint_lwps. If
we use plain iterate_over_lwps instead, what happens is that
when removing watchpoints, that iterate_over_lwps doesn't actually
iterate over anything, since the fork child is not added to the
lwp list until later, at detach time, in linux_child_follow_fork.
And if we don't iterate over that lwp, we don't mark its debug
registers as needing update. But linux_child_follow_fork takes
care of doing that explicitly:

child_lp = add_lwp (inferior_ptid);
child_lp->stopped = 1;
child_lp->last_resume_kind = resume_stop;
make_cleanup (delete_lwp_cleanup, child_lp);

/* CHILD_LP has new PID, therefore linux_nat_new_thread is not called for it.
See i386_inferior_data_get for the Linux kernel specifics.
Ensure linux_nat_prepare_to_resume will reset the hardware debug
registers. It is done by the linux_nat_new_thread call, which is
being skipped in add_lwp above for the first lwp of a pid. */
gdb_assert (num_lwps (GET_PID (child_lp->ptid)) == 1);
if (linux_nat_new_thread != NULL)
linux_nat_new_thread (child_lp);

if (linux_nat_prepare_to_resume != NULL)
linux_nat_prepare_to_resume (child_lp);
ptrace (PTRACE_DETACH, child_pid, 0, 0);

so unless I'm missing something (quite possible) it ends up all
the same. But, the !detach-on-fork, and the "follow-fork child" paths
should also call linux_nat_new_thread, and they don't presently. It
seems to me in those cases we're not clearing debug regs correctly
when that's needed? Instead of copying that bit that works around
add_lwp bypassing the linux_nat_new_thread call, I thought it'd
be better to add an add_initial_lwp call to be used in the case we
really need to bypass linux_nat_new_thread, and make
add_lwp always call linux_nat_new_thread.

WDYT?

Re-tested on Fedora 17.

gdb/
2013-02-11 Pedro Alves <palves@redhat.com>

* amd64-linux-nat.c (update_debug_registers_callback):
Update comment.
(amd64_linux_dr_set_control, amd64_linux_dr_set_addr): Use
iterate_over_lwps.
(amd64_linux_prepare_to_resume): Pass the lwp's pid to
i386_debug_reg_state.
(amd64_linux_new_fork): New function.
(_initialize_amd64_linux_nat): Install amd64_linux_new_fork as
linux_nat_new_fork hook, and i386_forget_process as
linux_nat_forget_process hook.
* i386-linux-nat.c (update_debug_registers_callback):
Update comment.
(amd64_linux_dr_set_control, amd64_linux_dr_set_addr): Use
iterate_over_lwps.
(amd64_linux_prepare_to_resume): Pass the lwp's pid to
i386_debug_reg_state.
(amd64_linux_new_fork): New function.
(_initialize_amd64_linux_nat): Install i386_linux_new_fork as
linux_nat_new_fork hook, and i386_forget_process as
linux_nat_forget_process hook.
* i386-nat.c (i386_inferior_data, struct i386_inferior_data):
Delete.
(struct i386_process_info): New.
(i386_process_list): New global.
(i386_find_process_pid, i386_add_process, i386_process_info_get):
New functions.
(i386_inferior_data_get): Delete.
(i386_process_info_get): New function.
(i386_debug_reg_state): New parameter 'pid'. Reimplement.
(i386_forget_process): New function.
(i386_cleanup_dregs, i386_update_inferior_debug_regs)
(i386_insert_watchpoint, i386_remove_watchpoint)
(i386_region_ok_for_watchpoint, i386_stopped_data_address)
(i386_insert_hw_breakpoint, i386_remove_hw_breakpoint): Adjust to
pass the current process id to i386_debug_reg_state.
(i386_use_watchpoints): Don't register inferior data.
* i386-nat.h (i386_debug_reg_state): Add new 'pid' parameter, and
adjust comment.
(i386_forget_process): Declare.
* linux-fork.c (delete_fork): Call linux_nat_forget_process.
* linux-nat.c (linux_nat_new_fork, linux_nat_forget_process_hook):
New static globals.
(linux_child_follow_fork): Don't call linux_nat_new_thread here.
(add_initial_lwp): New, factored out from ...
(add_lwp): ... this. Don't check the number of lwps before
calling linux_nat_new_thread.
(linux_nat_iterate_watchpoint_lwps): Delete.
(linux_nat_attach): Use add_initial_lwp instead of add_lwp.
(linux_handle_extended_wait): Call the linux_nat_new_fork hook on
forks and vforks.
(linux_nat_wait_1): Use add_initial_lwp instead of add_lwp for the
initial lwp.
(linux_nat_kill, linux_nat_mourn_inferior): Call
linux_nat_forget_process.
(linux_nat_set_new_fork, linux_nat_set_forget_process)
(linux_nat_forget_process): New functions.
* linux-nat.h (linux_nat_iterate_watchpoint_lwps_ftype): Delete
type.
(linux_nat_iterate_watchpoint_lwps): Delete declaration.
(linux_nat_set_new_fork, linux_nat_set_forget_process)
(linux_nat_forget_process): New declaration.
---

gdb/amd64-linux-nat.c | 52 ++++++++++++++--
gdb/i386-linux-nat.c | 50 ++++++++++++++-
gdb/i386-nat.c | 159 +++++++++++++++++++++++++++----------------------
gdb/i386-nat.h | 11 ++-
gdb/linux-fork.c | 2 +
gdb/linux-nat.c | 149 +++++++++++++++++++++++++++-------------------
gdb/linux-nat.h | 18 ++++--
7 files changed, 290 insertions(+), 151 deletions(-)

diff --git a/gdb/amd64-linux-nat.c b/gdb/amd64-linux-nat.c
index e3e7f05..4d6e4ab 100644
--- a/gdb/amd64-linux-nat.c
+++ b/gdb/amd64-linux-nat.c
@@ -337,8 +337,8 @@ amd64_linux_dr_get_status (void)
return amd64_linux_dr_get (inferior_ptid, DR_STATUS);
}

-/* Callback for linux_nat_iterate_watchpoint_lwps. Update the debug registers
- of LWP. */
+/* Callback for iterate_over_lwps. Update the debug registers of
+ LWP. */

static int
update_debug_registers_callback (struct lwp_info *lwp, void *arg)
@@ -364,7 +364,9 @@ update_debug_registers_callback (struct lwp_info *lwp, void *arg)
static void
amd64_linux_dr_set_control (unsigned long control)
{
- linux_nat_iterate_watchpoint_lwps (update_debug_registers_callback, NULL);
+ ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
+
+ iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
}

/* Set address REGNUM (zero based) to ADDR in all LWPs of the current
@@ -373,9 +375,11 @@ amd64_linux_dr_set_control (unsigned long control)
static void
amd64_linux_dr_set_addr (int regnum, CORE_ADDR addr)
{
+ ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
+
gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);

- linux_nat_iterate_watchpoint_lwps (update_debug_registers_callback, NULL);
+ iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
}

/* Called when resuming a thread.
@@ -394,7 +398,8 @@ amd64_linux_prepare_to_resume (struct lwp_info *lwp)

if (lwp->arch_private->debug_registers_changed)
{
- struct i386_debug_reg_state *state = i386_debug_reg_state ();
+ struct i386_debug_reg_state *state
+ = i386_debug_reg_state (ptid_get_pid (lwp->ptid));
int i;

/* On Linux kernel before 2.6.33 commit
@@ -434,6 +439,41 @@ amd64_linux_new_thread (struct lwp_info *lp)

lp->arch_private = info;
}
+
+/* linux_nat_new_fork hook. */
+
+static void
+amd64_linux_new_fork (struct lwp_info *parent, int child_pid)
+{
+ int parent_pid;
+ struct i386_debug_reg_state *parent_state;
+ struct i386_debug_reg_state *child_state;
+
+ /* NULL means no watchpoint has ever been set in the parent. In
+ that case, there's nothing to do. */
+ if (parent->arch_private == NULL)
+ return;
+
+ /* Linux kernel before 2.6.33 commit
+ 72f674d203cd230426437cdcf7dd6f681dad8b0d
+ will inherit hardware debug registers from parent
+ on fork/vfork/clone. Newer Linux kernels create such tasks with
+ zeroed debug registers.
+
+ GDB core assumes the child inherits the watchpoints/hw
+ breakpoints of the parent, and will remove them all from the
+ forked off process. Copy the debug registers mirrors into the
+ new process so that all breakpoints and watchpoints can be
+ removed together. The debug registers mirror will become zeroed
+ in the end before detaching the forked off process, thus making
+ this compatible with older Linux kernels too. */
+
+ parent_pid = ptid_get_pid (parent->ptid);
+ parent_state = i386_debug_reg_state (parent_pid);
+ child_state = i386_debug_reg_state (child_pid);
+ *child_state = *parent_state;
+}
+
\f

/* This function is called by libthread_db as part of its handling of
@@ -1120,6 +1160,8 @@ _initialize_amd64_linux_nat (void)
/* Register the target. */
linux_nat_add_target (t);
linux_nat_set_new_thread (t, amd64_linux_new_thread);
+ linux_nat_set_new_fork (t, amd64_linux_new_fork);
+ linux_nat_set_forget_process (t, i386_forget_process);
linux_nat_set_siginfo_fixup (t, amd64_linux_siginfo_fixup);
linux_nat_set_prepare_to_resume (t, amd64_linux_prepare_to_resume);
}
diff --git a/gdb/i386-linux-nat.c b/gdb/i386-linux-nat.c
index 20cc032..3c7494a 100644
--- a/gdb/i386-linux-nat.c
+++ b/gdb/i386-linux-nat.c
@@ -708,8 +708,8 @@ i386_linux_dr_get_status (void)
return i386_linux_dr_get (inferior_ptid, DR_STATUS);
}

-/* Callback for linux_nat_iterate_watchpoint_lwps. Update the debug registers
- of LWP. */
+/* Callback for iterate_over_lwps. Update the debug registers of
+ LWP. */

static int
update_debug_registers_callback (struct lwp_info *lwp, void *arg)
@@ -735,7 +735,9 @@ update_debug_registers_callback (struct lwp_info *lwp, void *arg)
static void
i386_linux_dr_set_control (unsigned long control)
{
- linux_nat_iterate_watchpoint_lwps (update_debug_registers_callback, NULL);
+ ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
+
+ iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
}

/* Set address REGNUM (zero based) to ADDR in all LWPs of the current
@@ -748,7 +750,7 @@ i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)

gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);

- linux_nat_iterate_watchpoint_lwps (update_debug_registers_callback, NULL);
+ iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
}

/* Called when resuming a thread.
@@ -767,7 +769,8 @@ i386_linux_prepare_to_resume (struct lwp_info *lwp)

if (lwp->arch_private->debug_registers_changed)
{
- struct i386_debug_reg_state *state = i386_debug_reg_state ();
+ struct i386_debug_reg_state *state
+ = i386_debug_reg_state (ptid_get_pid (lwp->ptid));
int i;

/* See amd64_linux_prepare_to_resume for Linux kernel note on
@@ -803,6 +806,41 @@ i386_linux_new_thread (struct lwp_info *lp)

lp->arch_private = info;
}
+
+/* linux_nat_new_fork hook. */
+
+static void
+i386_linux_new_fork (struct lwp_info *parent, int child_pid)
+{
+ int parent_pid;
+ struct i386_debug_reg_state *parent_state;
+ struct i386_debug_reg_state *child_state;
+
+ /* NULL means no watchpoint has ever been set in the parent. In
+ that case, there's nothing to do. */
+ if (parent->arch_private == NULL)
+ return;
+
+ /* Linux kernel before 2.6.33 commit
+ 72f674d203cd230426437cdcf7dd6f681dad8b0d
+ will inherit hardware debug registers from parent
+ on fork/vfork/clone. Newer Linux kernels create such tasks with
+ zeroed debug registers.
+
+ GDB core assumes the child inherits the watchpoints/hw
+ breakpoints of the parent, and will remove them all from the
+ forked off process. Copy the debug registers mirrors into the
+ new process so that all breakpoints and watchpoints can be
+ removed together. The debug registers mirror will become zeroed
+ in the end before detaching the forked off process, thus making
+ this compatible with older Linux kernels too. */
+
+ parent_pid = ptid_get_pid (parent->ptid);
+ parent_state = i386_debug_reg_state (parent_pid);
+ child_state = i386_debug_reg_state (child_pid);
+ *child_state = *parent_state;
+}
+
\f

/* Called by libthread_db. Returns a pointer to the thread local
@@ -1044,5 +1082,7 @@ _initialize_i386_linux_nat (void)
/* Register the target. */
linux_nat_add_target (t);
linux_nat_set_new_thread (t, i386_linux_new_thread);
+ linux_nat_set_new_fork (t, i386_linux_new_fork);
+ linux_nat_set_forget_process (t, i386_forget_process);
linux_nat_set_prepare_to_resume (t, i386_linux_prepare_to_resume);
}
diff --git a/gdb/i386-nat.c b/gdb/i386-nat.c
index c87e753..2e75ca3 100644
--- a/gdb/i386-nat.c
+++ b/gdb/i386-nat.c
@@ -170,87 +170,102 @@ i386_init_dregs (struct i386_debug_reg_state *state)
state->dr_status_mirror = 0;
}

-/* Per-inferior data key. */
-static const struct inferior_data *i386_inferior_data;
+/* Per-process data. We don't bind this to a per-inferior registry
+ because of targets like x86 GNU/Linux that need to keep track of
+ processes that aren't bound to any inferior (e.g., fork children,
+ checkpoints). */

-/* Per-inferior data. */
-struct i386_inferior_data
+struct i386_process_info
{
- /* Copy of i386 hardware debug registers for performance reasons. */
+ /* Linked list. */
+ struct i386_process_info *next;
+
+ /* The process identifier. */
+ int pid;
+
+ /* Copy of i386 hardware debug registers. */
struct i386_debug_reg_state state;
};

-/* Per-inferior hook for register_inferior_data_with_cleanup. */
+struct i386_process_info *i386_process_list = NULL;

-static void
-i386_inferior_data_cleanup (struct inferior *inf, void *arg)
+/* Find process data for process PID. */
+
+static struct i386_process_info *
+i386_find_process_pid (int pid)
{
- struct i386_inferior_data *inf_data = arg;
+ struct i386_process_info *inf;

- xfree (inf_data);
+ for (inf = i386_process_list; inf; inf = inf->next)
+ if (inf->pid == pid)
+ return inf;
+
+ return NULL;
}

-/* Get data specific for INFERIOR_PTID LWP. Return special data area
- for processes being detached. */
+/* Add process data for process PID. Returns newly allocated info
+ object. */

-static struct i386_inferior_data *
-i386_inferior_data_get (void)
+static struct i386_process_info *
+i386_add_process (int pid)
{
- struct inferior *inf = current_inferior ();
- struct i386_inferior_data *inf_data;
+ struct i386_process_info *proc;

- inf_data = inferior_data (inf, i386_inferior_data);
- if (inf_data == NULL)
- {
- inf_data = xzalloc (sizeof (*inf_data));
- set_inferior_data (current_inferior (), i386_inferior_data, inf_data);
- }
+ proc = xcalloc (1, sizeof (*proc));
+ proc->pid = pid;

- if (inf->pid != ptid_get_pid (inferior_ptid))
- {
- /* INFERIOR_PTID is being detached from the inferior INF.
- Provide local cache specific for the detached LWP. */
+ proc->next = i386_process_list;
+ i386_process_list = proc;

- static struct i386_inferior_data detached_inf_data_local;
- static int detached_inf_pid = -1;
+ return proc;
+}

- if (detached_inf_pid != ptid_get_pid (inferior_ptid))
- {
- /* Reinitialize the local cache if INFERIOR_PTID is
- different from the LWP last detached.
-
- Linux kernel before 2.6.33 commit
- 72f674d203cd230426437cdcf7dd6f681dad8b0d
- will inherit hardware debug registers from parent
- on fork/vfork/clone. Newer Linux kernels create such tasks with
- zeroed debug registers.
-
- GDB will remove all breakpoints (and watchpoints) from the forked
- off process. We also need to reset the debug registers in that
- process to be compatible with the older Linux kernels.
-
- Copy the debug registers mirrors into the new process so that all
- breakpoints and watchpoints can be removed together. The debug
- registers mirror will become zeroed in the end before detaching
- the forked off process. */
-
- detached_inf_pid = ptid_get_pid (inferior_ptid);
- detached_inf_data_local = *inf_data;
- }
+/* Get data specific info for process PID, creating it if necessary.
+ Never returns NULL. */

- return &detached_inf_data_local;
- }
+static struct i386_process_info *
+i386_process_info_get (int pid)
+{
+ struct i386_process_info *proc;
+
+ proc = i386_find_process_pid (pid);
+ if (proc == NULL)
+ proc = i386_add_process (pid);

- return inf_data;
+ return proc;
}

-/* Get debug registers state for INFERIOR_PTID, see
- i386_inferior_data_get. */
+/* Get debug registers state for process PID. */

struct i386_debug_reg_state *
-i386_debug_reg_state (void)
+i386_debug_reg_state (int pid)
{
- return &i386_inferior_data_get ()->state;
+ return &i386_process_info_get (pid)->state;
+}
+
+/* See declaration in i386-nat.h. */
+
+void
+i386_forget_process (int pid)
+{
+ struct i386_process_info *proc, **proc_link;
+
+ proc = i386_process_list;
+ proc_link = &i386_process_list;
+
+ while (proc != NULL)
+ {
+ if (proc->pid == pid)
+ {
+ *proc_link = proc->next;
+
+ xfree (proc);
+ return;
+ }
+
+ proc_link = &proc->next;
+ proc = *proc_link;
+ }
}

/* Whether or not to print the mirrored debug registers. */
@@ -303,7 +318,8 @@ static int i386_handle_nonaligned_watchpoint (struct i386_debug_reg_state *state
void
i386_cleanup_dregs (void)
{
- struct i386_debug_reg_state *state = i386_debug_reg_state ();
+ struct i386_debug_reg_state *state
+ = i386_debug_reg_state (ptid_get_pid (inferior_ptid));

i386_init_dregs (state);
}
@@ -569,7 +585,8 @@ Invalid value %d of operation in i386_handle_nonaligned_watchpoint.\n"),
static void
i386_update_inferior_debug_regs (struct i386_debug_reg_state *new_state)
{
- struct i386_debug_reg_state *state = i386_debug_reg_state ();
+ struct i386_debug_reg_state *state
+ = i386_debug_reg_state (ptid_get_pid (inferior_ptid));
int i;

ALL_DEBUG_REGISTERS (i)
@@ -594,7 +611,8 @@ static int
i386_insert_watchpoint (CORE_ADDR addr, int len, int type,
struct expression *cond)
{
- struct i386_debug_reg_state *state = i386_debug_reg_state ();
+ struct i386_debug_reg_state *state
+ = i386_debug_reg_state (ptid_get_pid (inferior_ptid));
int retval;
/* Work on a local copy of the debug registers, and on success,
commit the change back to the inferior. */
@@ -631,7 +649,8 @@ static int
i386_remove_watchpoint (CORE_ADDR addr, int len, int type,
struct expression *cond)
{
- struct i386_debug_reg_state *state = i386_debug_reg_state ();
+ struct i386_debug_reg_state *state
+ = i386_debug_reg_state (ptid_get_pid (inferior_ptid));
int retval;
/* Work on a local copy of the debug registers, and on success,
commit the change back to the inferior. */
@@ -664,7 +683,8 @@ i386_remove_watchpoint (CORE_ADDR addr, int len, int type,
static int
i386_region_ok_for_watchpoint (CORE_ADDR addr, int len)
{
- struct i386_debug_reg_state *state = i386_debug_reg_state ();
+ struct i386_debug_reg_state *state
+ = i386_debug_reg_state (ptid_get_pid (inferior_ptid));
int nregs;

/* Compute how many aligned watchpoints we would need to cover this
@@ -681,7 +701,8 @@ i386_region_ok_for_watchpoint (CORE_ADDR addr, int len)
static int
i386_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
{
- struct i386_debug_reg_state *state = i386_debug_reg_state ();
+ struct i386_debug_reg_state *state
+ = i386_debug_reg_state (ptid_get_pid (inferior_ptid));
CORE_ADDR addr = 0;
int i;
int rc = 0;
@@ -766,7 +787,8 @@ static int
i386_insert_hw_breakpoint (struct gdbarch *gdbarch,
struct bp_target_info *bp_tgt)
{
- struct i386_debug_reg_state *state = i386_debug_reg_state ();
+ struct i386_debug_reg_state *state
+ = i386_debug_reg_state (ptid_get_pid (inferior_ptid));
unsigned len_rw = i386_length_and_rw_bits (1, hw_execute);
CORE_ADDR addr = bp_tgt->placed_address;
/* Work on a local copy of the debug registers, and on success,
@@ -791,7 +813,8 @@ static int
i386_remove_hw_breakpoint (struct gdbarch *gdbarch,
struct bp_target_info *bp_tgt)
{
- struct i386_debug_reg_state *state = i386_debug_reg_state ();
+ struct i386_debug_reg_state *state
+ = i386_debug_reg_state (ptid_get_pid (inferior_ptid));
unsigned len_rw = i386_length_and_rw_bits (1, hw_execute);
CORE_ADDR addr = bp_tgt->placed_address;
/* Work on a local copy of the debug registers, and on success,
@@ -869,10 +892,6 @@ i386_use_watchpoints (struct target_ops *t)
t->to_remove_watchpoint = i386_remove_watchpoint;
t->to_insert_hw_breakpoint = i386_insert_hw_breakpoint;
t->to_remove_hw_breakpoint = i386_remove_hw_breakpoint;
-
- if (i386_inferior_data == NULL)
- i386_inferior_data
- = register_inferior_data_with_cleanup (NULL, i386_inferior_data_cleanup);
}

void
diff --git a/gdb/i386-nat.h b/gdb/i386-nat.h
index 87e313d..ead1fb0 100644
--- a/gdb/i386-nat.h
+++ b/gdb/i386-nat.h
@@ -110,9 +110,14 @@ extern void i386_set_debug_register_length (int len);

extern void i386_cleanup_dregs (void);

-/* Return a pointer to the the local mirror of the inferior's debug
- registers. */
+/* Return a pointer to the local mirror of the debug registers of
+ process PID. */

-extern struct i386_debug_reg_state *i386_debug_reg_state (void);
+extern struct i386_debug_reg_state *i386_debug_reg_state (int pid);
+
+/* Called whenever GDB is no longer debugging process PID. It deletes
+ data structures that keep track of debug register state. */
+
+extern void i386_forget_process (int pid);

#endif /* I386_NAT_H */
diff --git a/gdb/linux-fork.c b/gdb/linux-fork.c
index 2151401..7ed1803 100644
--- a/gdb/linux-fork.c
+++ b/gdb/linux-fork.c
@@ -122,6 +122,8 @@ delete_fork (ptid_t ptid)

fpprev = NULL;

+ linux_nat_forget_process (ptid_get_pid (ptid));
+
for (fp = fork_list; fp; fpprev = fp, fp = fp->next)
if (ptid_equal (fp->ptid, ptid))
break;
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index 57bca11..2438fa3 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -184,6 +184,13 @@ static struct target_ops linux_ops_saved;
/* The method to call, if any, when a new thread is attached. */
static void (*linux_nat_new_thread) (struct lwp_info *);

+/* The method to call, if any, when a new fork is attached. */
+static void (*linux_nat_new_fork) (struct lwp_info *, int);
+
+/* The method to call, if any, when a process is no longer
+ attached. */
+static void (*linux_nat_forget_process_hook) (int);
+
/* Hook to call prior to resuming a thread. */
static void (*linux_nat_prepare_to_resume) (struct lwp_info *);

@@ -698,15 +705,6 @@ holding the child stopped. Try \"set detach-on-fork\" or \
child_lp->last_resume_kind = resume_stop;
make_cleanup (delete_lwp_cleanup, child_lp);

- /* CHILD_LP has new PID, therefore linux_nat_new_thread is not called for it.
- See i386_inferior_data_get for the Linux kernel specifics.
- Ensure linux_nat_prepare_to_resume will reset the hardware debug
- registers. It is done by the linux_nat_new_thread call, which is
- being skipped in add_lwp above for the first lwp of a pid. */
- gdb_assert (num_lwps (GET_PID (child_lp->ptid)) == 1);
- if (linux_nat_new_thread != NULL)
- linux_nat_new_thread (child_lp);
-
if (linux_nat_prepare_to_resume != NULL)
linux_nat_prepare_to_resume (child_lp);
ptrace (PTRACE_DETACH, child_pid, 0, 0);
@@ -1176,12 +1174,22 @@ purge_lwp_list (int pid)
}
}

-/* Add the LWP specified by PID to the list. Return a pointer to the
- structure describing the new LWP. The LWP should already be stopped
- (with an exception for the very first LWP). */
+/* Add the LWP specified by PTID to the list. PTID is the first LWP
+ in the process. Return a pointer to the structure describing the
+ new LWP.
+
+ This differs from add_lwp in that we don't let the arch specific
+ bits know about this new thread. Current clients of this callback
+ take the opportunity to install watchpoints in the new thread, and
+ we shouldn't do that for the first thread. If we're spawning a
+ child ("run"), the thread executes the shell wrapper first, and we
+ shouldn't touch it until it execs the program we want to debug.
+ For "attach", it'd be okay to call the callback, but it's not
+ necessary, because watchpoints can't yet have been inserted into
+ the inferior. */

static struct lwp_info *
-add_lwp (ptid_t ptid)
+add_initial_lwp (ptid_t ptid)
{
struct lwp_info *lp;

@@ -1200,15 +1208,25 @@ add_lwp (ptid_t ptid)
lp->next = lwp_list;
lwp_list = lp;

+ return lp;
+}
+
+/* Add the LWP specified by PID to the list. Return a pointer to the
+ structure describing the new LWP. The LWP should already be
+ stopped. */
+
+static struct lwp_info *
+add_lwp (ptid_t ptid)
+{
+ struct lwp_info *lp;
+
+ lp = add_initial_lwp (ptid);
+
/* Let the arch specific bits know about this new thread. Current
clients of this callback take the opportunity to install
- watchpoints in the new thread. Don't do this for the first
- thread though. If we're spawning a child ("run"), the thread
- executes the shell wrapper first, and we shouldn't touch it until
- it execs the program we want to debug. For "attach", it'd be
- okay to call the callback, but it's not necessary, because
- watchpoints can't yet have been inserted into the inferior. */
- if (num_lwps (GET_PID (ptid)) > 1 && linux_nat_new_thread != NULL)
+ watchpoints in the new thread. We don't do this for the first
+ thread though. See all_initial_lwp. */
+ if (linux_nat_new_thread != NULL)
linux_nat_new_thread (lp);

return lp;
@@ -1285,45 +1303,6 @@ iterate_over_lwps (ptid_t filter,
return NULL;
}

-/* Iterate like iterate_over_lwps does except when forking-off a child call
- CALLBACK with CALLBACK_DATA specifically only for that new child PID. */
-
-void
-linux_nat_iterate_watchpoint_lwps
- (linux_nat_iterate_watchpoint_lwps_ftype callback, void *callback_data)
-{
- int inferior_pid = ptid_get_pid (inferior_ptid);
- struct inferior *inf = current_inferior ();
-
- if (inf->pid == inferior_pid)
- {
- /* Iterate all the threads of the current inferior. Without specifying
- INFERIOR_PID it would iterate all threads of all inferiors, which is
- inappropriate for watchpoints. */
-
- iterate_over_lwps (pid_to_ptid (inferior_pid), callback, callback_data);
- }
- else
- {
- /* Detaching a new child PID temporarily present in INFERIOR_PID. */
-
- struct lwp_info *child_lp;
- struct cleanup *old_chain;
- pid_t child_pid = GET_PID (inferior_ptid);
- ptid_t child_ptid = ptid_build (child_pid, child_pid, 0);
-
- gdb_assert (find_lwp_pid (child_ptid) == NULL);
- child_lp = add_lwp (child_ptid);
- child_lp->stopped = 1;
- child_lp->last_resume_kind = resume_stop;
- old_chain = make_cleanup (delete_lwp_cleanup, child_lp);
-
- callback (child_lp, callback_data);
-
- do_cleanups (old_chain);
- }
-}
-
/* Update our internal state when changing from one checkpoint to
another indicated by NEW_PTID. We can only switch single-threaded
applications, so we only create one new LWP, and the previous list
@@ -1656,7 +1635,7 @@ linux_nat_attach (struct target_ops *ops, char *args, int from_tty)
thread_change_ptid (inferior_ptid, ptid);

/* Add the initial process as the first LWP to the list. */
- lp = add_lwp (ptid);
+ lp = add_initial_lwp (ptid);

status = linux_nat_post_attach_wait (lp->ptid, 1, &lp->cloned,
&lp->signalled);
@@ -2309,6 +2288,15 @@ linux_handle_extended_wait (struct lwp_info *lp, int status,

ourstatus->value.related_pid = ptid_build (new_pid, new_pid, 0);

+ if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK)
+ {
+ /* The arch-specific native code may need to know about new
+ forks even if those end up never mapped to an
+ inferior. */
+ if (linux_nat_new_fork != NULL)
+ linux_nat_new_fork (lp, new_pid);
+ }
+
if (event == PTRACE_EVENT_FORK
&& linux_fork_checkpointing_p (GET_PID (lp->ptid)))
{
@@ -3489,7 +3477,7 @@ linux_nat_wait_1 (struct target_ops *ops,
BUILD_LWP (GET_PID (inferior_ptid),
GET_PID (inferior_ptid)));

- lp = add_lwp (inferior_ptid);
+ lp = add_initial_lwp (inferior_ptid);
lp->resumed = 1;
}

@@ -4075,6 +4063,10 @@ linux_nat_kill (struct target_ops *ops)
{
ptrace (PT_KILL, PIDGET (last.value.related_pid), 0, 0);
wait (&status);
+
+ /* Let the arch-specific native code know this process is
+ gone. */
+ linux_nat_forget_process (PIDGET (last.value.related_pid));
}

if (forks_exist_p ())
@@ -4103,7 +4095,9 @@ linux_nat_kill (struct target_ops *ops)
static void
linux_nat_mourn_inferior (struct target_ops *ops)
{
- purge_lwp_list (ptid_get_pid (inferior_ptid));
+ int pid = ptid_get_pid (inferior_ptid);
+
+ purge_lwp_list (pid);

if (! forks_exist_p ())
/* Normal case, no other forks available. */
@@ -4113,6 +4107,9 @@ linux_nat_mourn_inferior (struct target_ops *ops)
there are other viable forks to debug. Delete the exiting
one and context-switch to the first available. */
linux_fork_mourn_inferior ();
+
+ /* Let the arch-specific native code know this process is gone. */
+ linux_nat_forget_process (pid);
}

/* Convert a native/host siginfo object, into/from the siginfo in the
@@ -5146,6 +5143,34 @@ linux_nat_set_new_thread (struct target_ops *t,
linux_nat_new_thread = new_thread;
}

+/* See declaration in linux-nat.h. */
+
+void
+linux_nat_set_new_fork (struct target_ops *t,
+ void (*new_fork) (struct lwp_info *, int))
+{
+ /* Save the pointer. */
+ linux_nat_new_fork = new_fork;
+}
+
+/* See declaration in linux-nat.h. */
+
+void
+linux_nat_set_forget_process (struct target_ops *t, void (*fn) (int))
+{
+ /* Save the pointer. */
+ linux_nat_forget_process_hook = fn;
+}
+
+/* See declaration in linux-nat.h. */
+
+void
+linux_nat_forget_process (int pid)
+{
+ if (linux_nat_forget_process_hook != NULL)
+ linux_nat_forget_process_hook (pid);
+}
+
/* Register a method that converts a siginfo object between the layout
that ptrace returns, and the layout in the architecture of the
inferior. */
diff --git a/gdb/linux-nat.h b/gdb/linux-nat.h
index 928721b..3e6f871 100644
--- a/gdb/linux-nat.h
+++ b/gdb/linux-nat.h
@@ -154,12 +154,6 @@ struct lwp_info *iterate_over_lwps (ptid_t filter,
void *),
void *data);

-typedef int (*linux_nat_iterate_watchpoint_lwps_ftype) (struct lwp_info *lwp,
- void *arg);
-
-extern void linux_nat_iterate_watchpoint_lwps
- (linux_nat_iterate_watchpoint_lwps_ftype callback, void *callback_data);
-
/* Create a prototype generic GNU/Linux target. The client can
override it with local methods. */
struct target_ops * linux_target (void);
@@ -176,6 +170,18 @@ void linux_nat_add_target (struct target_ops *);
/* Register a method to call whenever a new thread is attached. */
void linux_nat_set_new_thread (struct target_ops *, void (*) (struct lwp_info *));

+/* Register a method to call whenever a new fork is attached. */
+void linux_nat_set_new_fork (struct target_ops *,
+ void (*) (struct lwp_info *, int));
+
+/* Register a method to call whenever a process is killed or
+ detached. */
+void linux_nat_set_forget_process (struct target_ops *, void (*) (int));
+
+/* Call the method registered with the function above. PID is the
+ process to forget about. */
+void linux_nat_forget_process (int pid);
+
/* Register a method that converts a siginfo object between the layout
that ptrace returns, and the layout in the architecture of the
inferior. */