Skip to content

Commit

Permalink
Add thread after updating gdbarch when exec'ing
Browse files Browse the repository at this point in the history
As mentioned in the previous patch, we should avoid doing register reads
after a process does an exec and before we've updated that inferior's
gdbarch.  Otherwise, we may interpret the registers using the wrong
architecture.  When a process does an exec with "follow-exec-mode new",
a new inferior is added by follow_exec.  The gdbarch of that new
inferior is at first set to some default value, probably specific to the
gdb build (I get "i386" here), which may not be the right one.  It is
updated later by the call to target_find_description.  Before that
point, if we try to read the inferior's registers, we may not interpret
them correctly.  This has been exposed by a failure in
gdb.base/foll-exec-mode.exp after the previous patch, with:

  Remote 'g' packet reply is too long (expected 312 bytes, got 816 bytes)

The call to "add_thread" done just after adding the inferior is
problematic, because it ends up reading the registers (because the ptid
is re-used, we end up doing a switch_to_thread to it, which tries to
update stop_pc).  The registers returned by gdbserver are the x86-64
ones, while we try to interpret them using the "i386" gdbarch.

Postponing the call to add_thread to until the target
description/gdbarch has been updated seems to fix the issue.

As to why this issue was uncovered by the previous patch: what I think
happened before that patch is that since we were updating stop_pc before
switching to the new inferior, we were filling the regcache associated
to the ptid (this worked fine as long as the architectures of the
previous and new process images were the same).  The call to
switch_to_thread then worked, because the register read hit the
regcache.  Now, it triggers a register read, while the gdbarch is not
set correctly, leading to the "reply is too long" error.  If this is
right, it sounds wrong that we delete and re-add a thread with the same
ptid, and are able to access the registers from the deleted thread.
When we delete a thread, should we clear the regcache associated to that
ptid, so that the new thread starts with a fresh/empty regcache?

gdb/ChangeLog:

	* infrun.c (follow_exec): Call add_thread after
	target_find_description.
  • Loading branch information
Simon Marchi committed Sep 5, 2017
1 parent 1bb7c05 commit bf93d7b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions gdb/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2017-09-05 Simon Marchi <[email protected]>

* infrun.c (follow_exec): Call add_thread after
target_find_description.

2017-09-05 Simon Marchi <[email protected]>

* infrun.c (handle_inferior_event_1): When exec'ing, read
Expand Down
6 changes: 5 additions & 1 deletion gdb/infrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,6 @@ follow_exec (ptid_t ptid, char *exec_file_target)

set_current_inferior (inf);
set_current_program_space (inf->pspace);
add_thread (ptid);
}
else
{
Expand Down Expand Up @@ -1243,6 +1242,11 @@ follow_exec (ptid_t ptid, char *exec_file_target)
registers. */
target_find_description ();

/* The add_thread call ends up reading registers, so do it after updating the
target description. */
if (follow_exec_mode_string == follow_exec_mode_new)
add_thread (ptid);

solib_create_inferior_hook (0);

jit_inferior_created_hook ();
Expand Down

0 comments on commit bf93d7b

Please sign in to comment.