From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) by sourceware.org (Postfix) with ESMTPS id 4886B3857344 for ; Fri, 17 Jun 2022 13:44:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4886B3857344 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f49.google.com with SMTP id s1so5830144wra.9 for ; Fri, 17 Jun 2022 06:44:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=wGTxuH+s6axioV/+Hyk7mnGrFZiHYtuHH73grRQhXSw=; b=v8mc6yIvSfaZvlhRgSNjuxtUrGiBPBJDGP9QkT6tGpKKHf0yOETQMAbrNLYl0IFVsc kQjebMQ9dgea0Lfoi2UBpK2daGtP3lT6h9mDAhY66hkbU5rQh4Uu+umoTv09GuAyaRdI hlIM6TKpC8FMHDEHZZH0KrvYnpF/DFUhepyMCiwXBza/pfGO2O7Ri+2B6/I+BElldsZJ KM8iEL7Q7JD5w1yQj2ZQj0xO6NTWLHLITOg+jRZY7Mm2yovg4MS3Jl+/bzgmcZoaFVY/ e2PyU+7ozQqZvrDWMSs/BEUFDhyIZJPjFibol5WOmY3li5PLSYAcUtmXbkZq/tJYkAPr CPAw== X-Gm-Message-State: AJIora+1tI1TdgGR+ZvrL1JFO7KLji5al6snwRw60qFJJTaLfbWqUDbp vsejmhcjB/44LJJiPWCYcHH3llCMEB0= X-Google-Smtp-Source: AGRyM1u7R0EHuI1NK1pnHHj+M+GqadiMCgaWWpmIIHNembHxp7EH+dsEVdXuQcr1Qm8eQrk9vm4m1w== X-Received: by 2002:a5d:6c64:0:b0:219:ea71:306f with SMTP id r4-20020a5d6c64000000b00219ea71306fmr9282970wrz.223.1655473479131; Fri, 17 Jun 2022 06:44:39 -0700 (PDT) Received: from ?IPV6:2001:8a0:f924:2600:209d:85e2:409e:8726? ([2001:8a0:f924:2600:209d:85e2:409e:8726]) by smtp.gmail.com with ESMTPSA id y14-20020a1c4b0e000000b0039c95b31812sm5486115wma.31.2022.06.17.06.44.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 17 Jun 2022 06:44:37 -0700 (PDT) Message-ID: <61f6b7de-56e4-efab-6683-d31f95e98fbb@palves.net> Date: Fri, 17 Jun 2022 14:44:36 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: Re: [RFC] Change displayed line when execution direction is reversed Content-Language: en-US To: Bruno Larsen , gdb@sourceware.org References: <040e723a-1f8b-3fb2-a076-85664243513f@redhat.com> <1e63f554-b24b-0bfb-e97d-d182ca6a41d2@palves.net> From: Pedro Alves In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00, BODY_8BITS, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 17 Jun 2022 13:44:42 -0000 On 2022-06-17 14:03, Bruno Larsen wrote: > > On 6/17/22 09:07, Pedro Alves wrote: >> On 2022-06-15 13:25, Bruno Larsen via Gdb wrote: >>> Hello all, >>> >>> I was doing some reverse debugging and noticed that setting the execution direction to reverse does not change how GDB displays lines. The problem with this is that the user doesn't see what will be executed if a step is taken, which makes the user experience quite annoying. How would the community feel if GDB printed the previous line, instead of current line, when the execution direction is reversed? >>> >>> Sorry if this is the wrong list. It didn't feel like a bug, and I don't have a patch yet, so this felt like the best place to send. >> >> How do you know which line that would be?  E.g.: >> >>   if (foo) >>     func (); >>   else >>     bar (); >>   qux = 0; // stopped here. >> >> Say you're stopped at the "stopped here" line, and flip execution direction to reverse.  Which line would gdb show as next line? > > I'm not sure of the how yet, but the plan would be to show the correct line (in this case, if foo == true, show func (), otherwise show bar ()). > > My first plan would be to add a way to query which PC should be used to calculate the line, so regular targets just return the current PC and > recorded targets can see the PC of the previous instruction, then use that to identify the source line. But you don't even know whether the user will do "step" or "next", or "stepi". What if the user does stepi, and the instruction we reverse step to is still in the "stopped here" line? > This system sounds easy enough in my head, > but without any testing I can't say for certain that it works (and with the current knowledge, it'd take me some time to test it), so if you can > think of problems this would have, or a better idea, I'm all ears! > One problem is that there's no generic way to ask the target backend "if I reverse the instruction at the current PC, what would the PC be?". Remember that remote targets can also reverse execute themselves. Another problem is that you don't need just the PC -- for printing the variables in scope you need to fully unwind the whole state. >> >> >> Also, showing a different line would result in the current PC (p $pc, info registers) etc. printing an >> address for a line totally unrelated to the line that GDB is displaying, no?  Same for "break" with no >> arguments, etc.  Worse, what happens when you print variables?  The block scope used is the one for the >> current PC, while gdb would display a different source line as current line? > > Let me try and explain with an example GDB session. Say the program is: > >   int main(){ >     int x = 2; >     x += 2; >     x *= x; >     x /= 2; >     return 0; >   } > > Currently, GDB does this: > > (gdb) start > 2           int x = 2; > (gdb) display x > 1: x = 32767 > (gdb) n > 3           x += 2; > 1: x = 2 > (gdb) > 4           x *= x; > 1: x = 4 > (gdb) > 5           x /= 2; > 1: x = 16 > (gdb) > 6           return 0; > 1: x = 8 > (gdb) set exec-direction reverse > (gdb) n > 5           x /= 2; > 1: x = 16 > (gdb) > 4           x *= x; > 1: x = 4 > (gdb) > 3           x += 2; > 1: x = 2 > > As we move forward, we see what the inferior will do next, and we see the value of x at the start of the line. > However, as soon as we move backwards, we are now seeing what we have just done, and the final result of having > done it, i.e. the state at the start of the line. > > If my idea is implemented, the session would look like this > > (gdb) start > 2           int x = 2; > (gdb) display x > 1: x = 32767 > (gdb) n > 3           x += 2; > 1: x = 2 > (gdb) > 4           x *= x; > 1: x = 4 > (gdb) > 5           x /= 2; > 1: x = 16 > (gdb) > 6           return 0; > 1: x = 8 > (gdb) set exec-direction reverse > 5           x /= 2; > (gdb) n > 4           x *= x; > 1: x = 16 > (gdb) > 3           x += 2; > 1: x = 4 > (gdb) > 2           int x = 2; > 1: x = 2 > > Now, going forward works the same, but going backwards, I see what the inferior will undo next (which is the opposite > operation than what is written), and I see the state of the inferior right before undoing the line. This is blatantly assuming that "what the inferior will undo next" is the effect of undoing one source line. But you have no way to knowing that. The user may type "next 2", or "stepi" or some other execution command, breaking the paradigm. And what if you have multiple function calls in the same line? > This would make > the most difference when showing a function call (so I know I need to "step" instead of "next) or when I will lose > state through an assignment operation. You kind of cheated as your example doesn't have a function call. :-) Can you do a similar mock GDB session for the example I pasted earlier? Here again: if (foo) foo (); else bar (); qux = 0; // stopped here. if stopped at the "stopped here" line, and you do "set execution-direction reverse", then would gdb present the next line as the last line of ... (and I'm going to assume you could figure out which branch would be taken and it would be the then branch), the foo() function's source code? (as if you knew the user would do "step", or would you present the next line as line of the "foo()" call, as if you knew the user would do "next" ? > > > The break with no arguments can cause some confusion if the user changes execution direction often. > To make things more clear, we could perhaps print > >   Breakpoint 1 at 0x401186: file 16678.c, end of line 3. > > instead of printing > >   Breakpoint 1 at 0x401186: file 16678.c, line 4. > > so that if the execution direction is reset to forward, the user isn't caught by surprise. > >> >> I don't see off hand how this can work.  Can you detail it? >> > > One final detail, just to make sure you are aware, this change would only apply when the user explicitly uses > "set exec-direction reverse". Nothing would change for "reverse-next" or similar.  Does this make more sense? > No, sorry.