Re: [PATCH v2 08/24] Introduce switch_to_inferior_no_thread

Pedro Alves
2019-12-20T18:50:24Z

On 11/7/19 9:14 AM, Paunovic, Aleksandar wrote:
> Shouldn't there be this change as well:
>
> diff --git a/gdb/progspace-and-thread.c b/gdb/progspace-and-thread.c
> index 3c92b5c8e0..f66aabea40 100644
> --- a/gdb/progspace-and-thread.c
> +++ b/gdb/progspace-and-thread.c
> @@ -39,6 +39,5 @@ switch_to_program_space_and_thread (program_space *pspace)
> }
> }
>
> - switch_to_no_thread ();
> - set_current_program_space (pspace);
> + switch_to_inferior_no_thread (inf);
> }
>
> This fixes the case when an inferior has PID = 0.
> The problem is that in the current state GDB would switch to no_thread and also set the program space
> but because the inferior is not switched, potentially an incorrect target would remain.
>
> Here is a sample scenario that exploits this flow:
>
> # On terminal 1, start a gdbserver on a program named foo:
> $ gdbserver :1234 ./foo
>
> # On terminal 2, start gdb on a program named bar. Suppose foo and bar are compiled from foo.c and bar.c.
> They are completely separate. So, bar.c:2 has no meaning for foo.
>
> $ gdb -q ./bar
> Reading symbols from ./bar...
> (gdb) add-inferior
> [New inferior 2]
> Added inferior 2
> (gdb) inferior 2
> [Switching to inferior 2 [<null>] (<noexec>)]
> (gdb) target remote :1234
> ...
> (gdb) set debug remote 2
> (gdb) break bar.c:2
> Sending packet: $Hgp0.0#ad...Packet received: OK
> Sending packet: $m5fa,12#f8...Packet received: E01
> Sending packet: $m5fa,1#c6...Packet received: E01
> Sending packet: $m5fb,3#c9...Packet received: E01
> Sending packet: $m5fe,1#ca...Packet received: E01
> Breakpoint 1 at 0x5fe: file bar.c, line 2.
> (gdb)
>
> Here we have an unnecessary sending of the packets to the gdbserver.
> But with the proposed change in progspace-and-thread.c there is this
>
> (gdb) break bar.c:2
> Breakpoint 1 at 0x5fe: file bar.c, line 2.
> (gdb)
>
> Now there is no sending of the packets to the gdbserver.

Oh wow, thanks much for this. You're right. I'm working on
converting your example above to a testsuite testcase.

Thanks,
Pedro Alves