diff mbox series

[v3] gdbinit.in: allow to pass function argument explicitly

Message ID 20191114160147.1357721-1-Hi-Angel@yandex.ru
State New
Headers show
Series [v3] gdbinit.in: allow to pass function argument explicitly | expand

Commit Message

Konstantin Kharlamov Nov. 14, 2019, 4:01 p.m. UTC
Generally, people expect functions to accept arguments directly. But
ones defined in gdbinit did not use the argument, which may be confusing
for newcomers. But we can't change behavior to use the argument without
breaking existing users of the gdbinit. Let's fix this by adding a check
for whether a user passed an argument, and either use it or go with
older behavior.

2019-11-14  Konstantin Kharlamov  <Hi-Angel@yandex.ru>

        * gdbinit.in (pp, pr, prl, pt, pct, pgg, pgq, pgs, pge, pmz, ptc, pdn,
        ptn, pdd, prc, pi, pbm, pel, trt): Make use of $arg0 if a user passed it
---

v3: use the way to detect arg0 suggested by Alexander on mailing list

 gcc/gdbinit.in | 57 +++++++++++++++++++++++++++++++++-----------------
 1 file changed, 38 insertions(+), 19 deletions(-)

Comments

Segher Boessenkool Nov. 14, 2019, 8:50 p.m. UTC | #1
Hi!

On Thu, Nov 14, 2019 at 07:01:47PM +0300, Konstantin Kharlamov wrote:
> Generally, people expect functions to accept arguments directly. But
> ones defined in gdbinit did not use the argument, which may be confusing
> for newcomers. But we can't change behavior to use the argument without
> breaking existing users of the gdbinit. Let's fix this by adding a check
> for whether a user passed an argument, and either use it or go with
> older behavior.
> 
> 2019-11-14  Konstantin Kharlamov  <Hi-Angel@yandex.ru>
> 
>         * gdbinit.in (pp, pr, prl, pt, pct, pgg, pgq, pgs, pge, pmz, ptc, pdn,
>         ptn, pdd, prc, pi, pbm, pel, trt): Make use of $arg0 if a user passed it

(Lines in a changelog end in a dot).

I love this...  if it works :-)  How was it tested?  With what GDB versions?


Segher
Konstantin Kharlamov Nov. 14, 2019, 9:17 p.m. UTC | #2
On 14.11.2019 23:50, Segher Boessenkool wrote:
> Hi!
> 
> On Thu, Nov 14, 2019 at 07:01:47PM +0300, Konstantin Kharlamov wrote:
>> Generally, people expect functions to accept arguments directly. But
>> ones defined in gdbinit did not use the argument, which may be confusing
>> for newcomers. But we can't change behavior to use the argument without
>> breaking existing users of the gdbinit. Let's fix this by adding a check
>> for whether a user passed an argument, and either use it or go with
>> older behavior.
>>
>> 2019-11-14  Konstantin Kharlamov  <Hi-Angel@yandex.ru>
>>
>>          * gdbinit.in (pp, pr, prl, pt, pct, pgg, pgq, pgs, pge, pmz, ptc, pdn,
>>          ptn, pdd, prc, pi, pbm, pel, trt): Make use of $arg0 if a user passed it
> 
> (Lines in a changelog end in a dot).
> 
> I love this...  if it works :-)  How was it tested?  With what GDB versions?

I ran GCC under GDB, and set a breakpoint somewhere in the GCC code 
where `stmt` variable is defined (I don't remember the type, ATM I'm 
from another PC). Then I tested 3 things:

1. that `pgg stmt` prints the `stmt` content
2. that `p 0` followed by `pgg` prints nothing (because the underlying 
function checks its argument for being zero)
3. that `p 1` followed by `pgg` crashes.

Tested on Archlinux, gdb version is 8.3.1
Konstantin Kharlamov Nov. 15, 2019, 9:22 a.m. UTC | #3
On Пт, ноя 15, 2019 at 00:17, Konstantin Kharlamov 
<hi-angel@yandex.ru> wrote:
> On 14.11.2019 23:50, Segher Boessenkool wrote:
>> Hi!
>> 
>> On Thu, Nov 14, 2019 at 07:01:47PM +0300, Konstantin Kharlamov wrote:
>>> Generally, people expect functions to accept arguments directly. But
>>> ones defined in gdbinit did not use the argument, which may be 
>>> confusing
>>> for newcomers. But we can't change behavior to use the argument 
>>> without
>>> breaking existing users of the gdbinit. Let's fix this by adding a 
>>> check
>>> for whether a user passed an argument, and either use it or go with
>>> older behavior.
>>> 
>>> 2019-11-14  Konstantin Kharlamov  <Hi-Angel@yandex.ru>
>>> 
>>>          * gdbinit.in (pp, pr, prl, pt, pct, pgg, pgq, pgs, pge, 
>>> pmz, ptc, pdn,
>>>          ptn, pdd, prc, pi, pbm, pel, trt): Make use of $arg0 if a 
>>> user passed it
>> 
>> (Lines in a changelog end in a dot).
>> 
>> I love this...  if it works :-)  How was it tested?  With what GDB 
>> versions?
> 
> I ran GCC under GDB, and set a breakpoint somewhere in the GCC code 
> where `stmt` variable is defined (I don't remember the type, ATM I'm 
> from another PC). Then I tested 3 things:
> 
> 1. that `pgg stmt` prints the `stmt` content
> 2. that `p 0` followed by `pgg` prints nothing (because the 
> underlying function checks its argument for being zero)
> 3. that `p 1` followed by `pgg` crashes.
> 
> Tested on Archlinux, gdb version is 8.3.1

Ok, I looked in the office: `stmt` has type `gimple`.

And just in case it matters: I didn't edit the code manually, instead 
the modifications were done with a find&replace functional in Emacs.
diff mbox series

Patch

diff --git a/gcc/gdbinit.in b/gcc/gdbinit.in
index a933ddc6141..ae177fd40d5 100644
--- a/gcc/gdbinit.in
+++ b/gcc/gdbinit.in
@@ -17,7 +17,8 @@ 
 # <http://www.gnu.org/licenses/>.
 
 define pp
-call debug ($)
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+call debug ($debug_arg)
 end
 
 document pp
@@ -26,7 +27,8 @@  Works only when an inferior is executing.
 end
 
 define pr
-call debug_rtx ($)
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+call debug_rtx ($debug_arg)
 end
 
 document pr
@@ -35,7 +37,8 @@  Works only when an inferior is executing.
 end
 
 define prl
-call debug_rtx_list ($, debug_rtx_count)
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+call debug_rtx_list ($debug_arg, debug_rtx_count)
 end
 
 document prl
@@ -50,7 +53,8 @@  it using debug_rtx_list. Usage example: set $foo=debug_rtx_find(first, 42)
 end
 
 define pt
-call debug_tree ($)
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+call debug_tree ($debug_arg)
 end
 
 document pt
@@ -59,7 +63,8 @@  Works only when an inferior is executing.
 end
 
 define pct
-call debug_c_tree ($)
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+call debug_c_tree ($debug_arg)
 end
 
 document pct
@@ -68,7 +73,8 @@  Works only when an inferior is executing.
 end
 
 define pgg
-call debug_gimple_stmt ($)
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+call debug_gimple_stmt ($debug_arg)
 end
 
 document pgg
@@ -77,7 +83,8 @@  Works only when an inferior is executing.
 end
 
 define pgq
-call debug_gimple_seq ($)
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+call debug_gimple_seq ($debug_arg)
 end
 
 document pgq
@@ -86,7 +93,8 @@  Works only when an inferior is executing.
 end
 
 define pgs
-call debug_generic_stmt ($)
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+call debug_generic_stmt ($debug_arg)
 end
 
 document pgs
@@ -95,7 +103,8 @@  Works only when an inferior is executing.
 end
 
 define pge
-call debug_generic_expr ($)
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+call debug_generic_expr ($debug_arg)
 end
 
 document pge
@@ -104,7 +113,8 @@  Works only when an inferior is executing.
 end
 
 define pmz
-call mpz_out_str(stderr, 10, $)
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+call mpz_out_str(stderr, 10, $debug_arg)
 end
 
 document pmz
@@ -113,7 +123,8 @@  Works only when an inferior is executing.
 end
 
 define ptc
-output (enum tree_code) $.base.code
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+output (enum tree_code) $debug_arg.base.code
 echo \n
 end
 
@@ -122,7 +133,8 @@  Print the tree-code of the tree node that is $.
 end
 
 define pdn
-output $.decl_minimal.name->identifier.id.str
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+output $debug_arg.decl_minimal.name->identifier.id.str
 echo \n
 end
 
@@ -131,7 +143,8 @@  Print the name of the decl-node that is $.
 end
 
 define ptn
-output $.type.name->decl_minimal.name->identifier.id.str
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+output $debug_arg.type.name->decl_minimal.name->identifier.id.str
 echo \n
 end
 
@@ -140,7 +153,8 @@  Print the name of the type-node that is $.
 end
 
 define pdd
-call debug_dwarf_die ($)
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+call debug_dwarf_die ($debug_arg)
 end
 
 document pdd
@@ -148,7 +162,8 @@  Print the dw_die_ref that is in $.
 end
 
 define prc
-output (enum rtx_code) $.code
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+output (enum rtx_code) $debug_arg.code
 echo \ (
 output $.mode
 echo )\n
@@ -159,7 +174,8 @@  Print the rtx-code and machine mode of the rtx that is $.
 end
 
 define pi
-print $.u.fld[0].rt_rtx@7
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+print $debug_arg.u.fld[0].rt_rtx@7
 end
 
 document pi
@@ -176,7 +192,8 @@  including the global binding level.
 end
 
 define pbm
-call bitmap_print (stderr, $, "", "\n")
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+call bitmap_print (stderr, $debug_arg, "", "\n")
 end
 
 document pbm
@@ -184,7 +201,8 @@  Dump the bitmap that is in $ as a comma-separated list of numbers.
 end
 
 define pel
-output expand_location ($)
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+output expand_location ($debug_arg)
 echo \n
 end
 
@@ -202,7 +220,8 @@  Print current function.
 end
 
 define trt
-print ($.typed.type)
+eval "set $debug_arg = $%s", $argc ? "arg0" : ""
+print ($debug_arg.typed.type)
 end
 
 document trt