From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19225 invoked by alias); 12 Sep 2011 14:36:28 -0000 Received: (qmail 18669 invoked by uid 22791); 12 Sep 2011 14:36:25 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL,BAYES_00,TW_EG X-Spam-Check-By: sourceware.org Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 12 Sep 2011 14:36:06 +0000 Received: from nat-ies.mentorg.com ([192.94.31.2] helo=EU1-MAIL.mgc.mentorg.com) by relay1.mentorg.com with esmtp id 1R37cD-0007DJ-Hh from pedro_alves@mentor.com for gdb-patches@sourceware.org; Mon, 12 Sep 2011 07:36:05 -0700 Received: from scottsdale.localnet ([172.16.63.104]) by EU1-MAIL.mgc.mentorg.com with Microsoft SMTPSVC(6.0.3790.1830); Mon, 12 Sep 2011 15:36:03 +0100 From: Pedro Alves To: gdb-patches@sourceware.org Subject: Re: [patch 1/2] Displaced stepping across fork/vfork Date: Mon, 12 Sep 2011 14:59:00 -0000 User-Agent: KMail/1.13.6 (Linux/2.6.38-11-generic; KDE/4.7.0; x86_64; ; ) Cc: Yao Qi References: <4E6C04B3.90106@codesourcery.com> In-Reply-To: <4E6C04B3.90106@codesourcery.com> MIME-Version: 1.0 Content-Type: Text/Plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <201109121536.01756.pedro@codesourcery.com> X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2011-09/txt/msg00190.txt.bz2 On Sunday 11 September 2011 01:45:39, Yao Qi wrote: > Hi, > The current displaced stepping doesn't consider the case that we set a=20 > breakpoint on syscall insn, which will cause fork/vfork, and single step= =20 > over it with displaced stepping. >=20 > In this case, syscall insn is executed in scratch pad, and GDB will=20 > receive TARGET_WAITKIND_FORKED and TARGET_WAITKIND_VFORKED before it=20 > cleans up displaced stepping state. The other problem, in this case, is= =20 > that the PC of child process points to the scratch pad instead of=20 > original program location. >=20 > This patch is to address these two problems. When GDB receives=20 > TARGET_WAITKIND_FORKED or TARGET_WAITKIND_VFORKED, it will check whether= =20 > it has been in displaced stepping. If it is, that means GDB executed a=20 > syscall insn in scratch pad, then, we should cleanup the displaced=20 > stepping state, such as clean up scratch pad, and set the PC of child=20 > process to the original location. Thanks. I think other TARGET_WAITKIND_... events can also leave the PC pointing to the scratchpad. E.g., what if you do "catch syscall", and then step over a breakpoint set at the syscall insn? Or "catch exec", and step over the syscall insn of an exec syscall? Do we leave stale displaced stepping state behind in the later case? >=20 > --=20 > Yao (=E9=BD=90=E5=B0=A7) > 0014-disp-step-cross-vfork.patch > gdb/ > * infrun.c (displaced_step_fixup): Move some code ... > (displaced_step_restore): ... here. New function. > (handle_inferior_event): Cleanup displaced stepping state for both > child and parent when get forked or vforked event. > * regcache.c (get_thread_arch_aspace_regcache): New function. > get_thread_arch_regcache (): Call it. > --- > gdb/infrun.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++--= ----- > gdb/regcache.c | 31 +++++++++++++------- > gdb/regcache.h | 3 ++ > 3 files changed, 95 insertions(+), 22 deletions(-) >=20 > diff --git a/gdb/infrun.c b/gdb/infrun.c > index 729f148..2788b08 100644 > --- a/gdb/infrun.c > +++ b/gdb/infrun.c > @@ -1377,6 +1377,23 @@ write_memory_ptid (ptid_t ptid, CORE_ADDR memaddr, > do_cleanups (ptid_cleanup); > } >=20=20 > +/* Restore the contents of the copy area for thread PTID. */ > + > +static void > +displaced_step_restore (struct displaced_step_inferior_state *displaced, > + ptid_t ptid) > +{ > + ULONGEST len =3D gdbarch_max_insn_length (displaced->step_gdbarch); > + > + write_memory_ptid (ptid, displaced->step_copy, > + displaced->step_saved_copy, len); > + if (debug_displaced) > + fprintf_unfiltered (gdb_stdlog, "displaced: restored %s %s\n", > + target_pid_to_str (ptid), > + paddress (displaced->step_gdbarch, > + displaced->step_copy)); > +} > + > static void > displaced_step_fixup (ptid_t event_ptid, enum target_signal signal) > { > @@ -1395,17 +1412,7 @@ displaced_step_fixup (ptid_t event_ptid, enum targ= et_signal signal) >=20=20 > old_cleanups =3D make_cleanup (displaced_step_clear_cleanup, displaced= ); >=20=20 > - /* Restore the contents of the copy area. */ > - { > - ULONGEST len =3D gdbarch_max_insn_length (displaced->step_gdbarch); > - > - write_memory_ptid (displaced->step_ptid, displaced->step_copy, > - displaced->step_saved_copy, len); > - if (debug_displaced) > - fprintf_unfiltered (gdb_stdlog, "displaced: restored %s\n", > - paddress (displaced->step_gdbarch, > - displaced->step_copy)); > - } > + displaced_step_restore (displaced, displaced->step_ptid); >=20=20 > /* Did the instruction complete successfully? */ > if (signal =3D=3D TARGET_SIGNAL_TRAP) > @@ -3407,6 +3414,60 @@ handle_inferior_event (struct execution_control_st= ate *ecs) > if (debug_infrun) > fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_FORKED\= n"); >=20=20 > + /* Check inferior is in displaced stepping. */ /* Check whether the inferior is displaced stepping. */ or: /* Check whether the inferior is doing a displaced step. */ > + { > + struct regcache *regcache =3D get_thread_regcache (ecs->ptid); > + struct gdbarch *gdbarch =3D get_regcache_arch (regcache); > + struct displaced_step_inferior_state *displaced > + =3D get_displaced_stepping_state (ptid_get_pid (ecs->ptid)); > + > + /* If checking displaced stepping is supported, and thread > + ecs->ptid is being in displaced stepping. */ The use_displaced_stepping check is redundant. When displaced stepping isn't supported, you'll always get a NULL DISPLACED. The resulting comment just reads what the code is doing, so I suggest just removing it. Otherwise s/is being in displaced stepping/is displaced stepping/. > + if (use_displaced_stepping (gdbarch) && displaced > + && ptid_equal (displaced->step_ptid, ecs->ptid)) > + { > + struct inferior *parent_inf > + =3D find_inferior_pid (ptid_get_pid (ecs->ptid)); > + struct regcache *child_regcache; > + CORE_ADDR parent_pc; > + > + /* GDB has got TARGET_WAITKIND_FORKED or TARGET_WAITKIND_VFOR= KED, > + indicating that the displaced stepping of syscall instruct= ion > + has been done. Perform cleanup for parent process here. = Note > + that this operation also clean up child process for vfork, also cleans up the child > + because their pages are shared. */ Spurious double-space after because. > + displaced_step_fixup (ecs->ptid, TARGET_SIGNAL_TRAP); > + > + if (ecs->ws.kind =3D=3D TARGET_WAITKIND_FORKED) > + { > + /* Restore scratch pad for child process. */ > + displaced_step_restore (displaced, ecs->ws.value.related_= pid); > + } > + > + /* Since vfork/fork syscall instruction is executed in scratc= h pad, > + so the PC value of child is still within the range of scra= tchpad. > + Set PC of child process back to original place, that is id= entical > + to father's PC value. */ Not the original place --- to where the PC should point after the fork sysc= all. Suggest writing: /* Since the vfork/fork syscall instruction was executed in the = scratch pad, the child's PC is also within the scratchpad. Set the child'= s PC to the parent's PC value, which has already been fixed up. */ > + > + child_regcache > + =3D get_thread_arch_aspace_regcache (ecs->ws.value.related_= pid, > + gdbarch, > + parent_inf->aspace); Ugh, IIUC, you have to use parent_inf->aspace and the parent's gdb (the chi= ld's gdbarch should always be the same as the parent's, but still) here because = the child has not been added to the inferior list yet... Please add a FIXME comment mentioning this. It's ugly, but better than leaving that unjustified. Som= ething like: /* FIXME: we use the parent's aspace here, although we're touching the chil= d, because the child hasn't been added to the inferior list yet at this point. */ > + /* Read PC value of father process. */ s/father/parent > + parent_pc =3D regcache_read_pc (regcache); > + > + if (debug_displaced) > + fprintf_unfiltered (gdb_stdlog, > + "displaced: write child pc from %s to %= s\n", > + paddress (gdbarch, > + regcache_read_pc (child_regca= che)), > + paddress (gdbarch, parent_pc)); > + > + regcache_write_pc (child_regcache, parent_pc); > + > + } > + } > + > if (!ptid_equal (ecs->ptid, inferior_ptid)) > { > context_switch (ecs->ptid); > diff --git a/gdb/regcache.c b/gdb/regcache.c > index 0af93e8..c64bcb8 100644 > --- a/gdb/regcache.c > +++ b/gdb/regcache.c > @@ -450,17 +450,34 @@ struct regcache_list > static struct regcache_list *current_regcache; >=20=20 > struct regcache * > -get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch) > +get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch, > + struct address_space *aspace) > { > struct regcache_list *list; > struct regcache *new_regcache; > - struct address_space *aspace; >=20=20 > for (list =3D current_regcache; list; list =3D list->next) > if (ptid_equal (list->regcache->ptid, ptid) > && get_regcache_arch (list->regcache) =3D=3D gdbarch) > return list->regcache; >=20=20 > + new_regcache =3D regcache_xmalloc_1 (gdbarch, aspace, 0); > + new_regcache->ptid =3D ptid; > + > + list =3D xmalloc (sizeof (struct regcache_list)); > + list->regcache =3D new_regcache; > + list->next =3D current_regcache; > + current_regcache =3D list; > + > + return new_regcache; > +} > + > +struct regcache * > +get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch) > +{ > + Spurious newline. > + struct address_space *aspace; > + > /* For the benefit of "maint print registers" & co when debugging an > executable, allow dumping the regcache even when there is no > thread selected (target_thread_address_space internal-errors if > @@ -471,15 +488,7 @@ get_thread_arch_regcache (ptid_t ptid, struct gdbarc= h *gdbarch) > ? NULL > : target_thread_address_space (ptid)); >=20=20 > - new_regcache =3D regcache_xmalloc_1 (gdbarch, aspace, 0); > - new_regcache->ptid =3D ptid; > - > - list =3D xmalloc (sizeof (struct regcache_list)); > - list->regcache =3D new_regcache; > - list->next =3D current_regcache; > - current_regcache =3D list; > - > - return new_regcache; > + return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace); > } >=20=20 > static ptid_t current_thread_ptid; > diff --git a/gdb/regcache.h b/gdb/regcache.h > index 7f7dc10..5531f39 100644 > --- a/gdb/regcache.h > +++ b/gdb/regcache.h > @@ -28,6 +28,9 @@ struct address_space; > extern struct regcache *get_current_regcache (void); > extern struct regcache *get_thread_regcache (ptid_t ptid); > extern struct regcache *get_thread_arch_regcache (ptid_t, struct gdbarch= *); > +extern struct regcache *get_thread_arch_aspace_regcache (ptid_t, > + struct gdbarch *, > + struct address_s= pace *); >=20=20 > void regcache_xfree (struct regcache *regcache); > struct cleanup *make_cleanup_regcache_xfree (struct regcache *regcache); > --=20 > 1.7.0.4 --=20 Pedro Alves