#655550 gforth: "see +" hangs (gdb doesn't terminate)

Package:
gforth
Source:
gforth
Description:
GNU Forth Language Environment
Submitter:
"Ph. Marek"
Date:
2021-02-01 00:21:03 UTC
Severity:
normal
#655550#5
Date:
2012-01-12 08:12:39 UTC
From:
To:
Using gforth=0.7.0+ds1-6 and gdb=7.4-1~cvs20111117.2 the "see" word doesn't
work for built-ins, like "+". GDB just hangs, I expect it gets a bad command.

#655550#10
Date:
2012-01-12 17:15:50 UTC
From:
To:
I have seen that yesterday on an AMD64 Ubuntu 11.10 system.  The
reason there was that it is now possible to restrict the ptrace call
such that attaching gdb to a non-child process is no longer possible
(in Gforth, gdb tries to attach to its grandparent process).  gdb produces
an error message (that goes to /dev/null) and waits for your input.

You can work around that by re-enabling the old ptrace capabilities.
Currently the box is down, so I cannot check how I did it, but a
little googling brought up that

echo 0 >/proc/sys/kernel/yama/ptrace_scope

might help.

If this restriction continues to be the default on a significant
number of systems, I guess that the long-term solution is to call the
library that gdb uses for disassembling directly instead of through
gdb.

- anton

#655550#15
Date:
2012-01-12 17:15:50 UTC
From:
To:
I have seen that yesterday on an AMD64 Ubuntu 11.10 system.  The
reason there was that it is now possible to restrict the ptrace call
such that attaching gdb to a non-child process is no longer possible
(in Gforth, gdb tries to attach to its grandparent process).  gdb produces
an error message (that goes to /dev/null) and waits for your input.

You can work around that by re-enabling the old ptrace capabilities.
Currently the box is down, so I cannot check how I did it, but a
little googling brought up that

echo 0 >/proc/sys/kernel/yama/ptrace_scope

might help.

If this restriction continues to be the default on a significant
number of systems, I guess that the long-term solution is to call the
library that gdb uses for disassembling directly instead of through
gdb.

- anton

#655550#20
Date:
2021-01-31 18:15:05 UTC
From:
To:
I don't know if this is an appropriate fix but I have found that if gdb
is run in batch mode by gforth with -batch-silent instead of just with
the -q quiet switch then gdb will exit even when the old ptrace
capabilities are not re-enabled.  With -batch-silent and ptrace
restricted (https://www.kernel.org/doc/Documentation/security/Yama.txt),
gforth will display the reason returned from gdb:

see +
Code +
    0x55eca76d:    /tmp/gforthdis.oWWqwv4yAj:4: Error in sourced command
file:
Cannot access memory at address 0x55eca76d
end-code
  ok

With ptrace_scope set to 0, the output appears to be as expected:


see +
Code +
    0x000055ebda5b176d <gforth_engine+5805>:    mov
%r13,0x22538c(%rip)        # 0x55ebda7d6b00 <saved_ip>
    0x000055ebda5b1774 <gforth_engine+5812>:    lea 0x8(%r14),%rax
    0x000055ebda5b1778 <gforth_engine+5816>:    mov (%rax),%rax
    0x000055ebda5b177b <gforth_engine+5819>:    mov %rax,-0x9e8(%rbp)
    0x000055ebda5b1782 <gforth_engine+5826>:    mov (%r14),%rax
    0x000055ebda5b1785 <gforth_engine+5829>:    mov %rax,-0x9e0(%rbp)
    0x000055ebda5b178c <gforth_engine+5836>:    add $0x8,%r14
    0x000055ebda5b1790 <gforth_engine+5840>:    mov -0x9e8(%rbp),%rdx
    0x000055ebda5b1797 <gforth_engine+5847>:    mov -0x9e0(%rbp),%rax
    0x000055ebda5b179e <gforth_engine+5854>:    add %rdx,%rax
    0x000055ebda5b17a1 <gforth_engine+5857>:    mov %rax,-0x9d8(%rbp)
    0x000055ebda5b17a8 <gforth_engine+5864>:    add $0x8,%r13
    0x000055ebda5b17ac <gforth_engine+5868>:    mov -0x9d8(%rbp),%rax
    0x000055ebda5b17b3 <gforth_engine+5875>:    mov %rax,(%r14)
    0x000055ebda5b17b6 <gforth_engine+5878>:    mov %rax,%r15
    0x000055ebda5b17b9 <gforth_engine+5881>:    mov -0x8(%r13),%rbx
    0x000055ebda5b17bd <gforth_engine+5885>:    mov %rbx,%rax
    0x000055ebda5b17c0 <gforth_engine+5888>:    jmpq 0x55ebda5b016d
<gforth_engine+173>
end-code


Might the use of batch mode for gdb be appropriate?


Here is a patch for 0.7.3:
--- gforth-0.7.3+dfsg.orig/dis-gdb.fs +++ gforth-0.7.3+dfsg/dis-gdb.fs @@ -48,7 +48,7 @@ defer gdb-addr-sep-char ( -- c )      addr 0 <<# gdb-addr-sep-char hold # #s 'x hold # #> append-extend-string #>>      addr u + 0 <<# # #s 'x hold # #> append-extend-string #>>      r> base ! cr -    s\" \nset logging off\nquit\n\" >$file2 && gdb -nx -q -p `ps -p $$ -o ppid=` -x $file2 2>/dev/null >/dev/null && rm $file2 && grep -v \"of assembler\" $file && rm $file" append-extend-string +    s\" \nset logging off\nquit\n\" >$file2 && gdb -nx -batch-silent -p `ps -p $$ -o ppid=` -x $file2 2>/dev/null >/dev/null && rm $file2 && grep -v \"of assembler\" $file && rm $file" append-extend-string      2dup (system) 2swap drop free throw throw if         addr u dump      endif ; On Thu, 12 Jan 2012 18:15:50 +0100 Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote: > On Thu, Jan 12, 2012 at 09:12:39AM +0100, Ph. Marek wrote: > > Package: gforth > > Version: 0.7.0+ds1-6 > > Severity: normal > > > > Using gforth=0.7.0+ds1-6 and gdb=7.4-1~cvs20111117.2 the "see" word doesn't > > work for built-ins, like "+". GDB just hangs, I expect it gets a bad command. > > I have seen that yesterday on an AMD64 Ubuntu 11.10 system. The > reason there was that it is now possible to restrict the ptrace call > such that attaching gdb to a non-child process is no longer possible > (in Gforth, gdb tries to attach to its grandparent process). gdb produces > an error message (that goes to /dev/null) and waits for your input. > > You can work around that by re-enabling the old ptrace capabilities. > Currently the box is down, so I cannot check how I did it, but a > little googling brought up that > > echo 0 >/proc/sys/kernel/yama/ptrace_scope > > might help. > > If this restriction continues to be the default on a significant > number of systems, I guess that the long-term solution is to call the > library that gdb uses for disassembling directly instead of through > gdb. > > - anton > > >
#655550#25
Date:
2021-01-31 23:52:58 UTC
From:
To:
have (documented) only -batch, not -batch-silent, but it works better
than -q.  The only shortcoming is that gdb does not produce an error
exit in this case, so Gforth does not fall back to producing a hex
dump, but that's probably not that useful anyway.

I have now applied this change to the Gforth (i.e., upstream) git head.

- anton

Here's the patch:
----------------------
diff --git a/dis-gdb.fs b/dis-gdb.fs
index 7ab88aa..04d0d85 100644
--- a/dis-gdb.fs
+++ b/dis-gdb.fs
@@ -59,9 +59,9 @@ set-current
         2drop ['] dump is discode
         addr u dump exit then
     [ e? os-type s" cygwin" str= ] [IF]
-       s\" \nset logging off\nquit\n\" >$file2 && gdb -nx -q -p `ps -p $$ | grep -v PPID | cut -c 10-17` -x $file2 2>/dev/null >/dev/null && rm $file2 && grep -v \"of assembler\" $file && rm $file"
+       s\" \nset logging off\nquit\n\" >$file2 && gdb -nx -batch -p `ps -p $$ | grep -v PPID | cut -c 10-17` -x $file2 2>/dev/null >/dev/null && rm $file2 && grep -v \"of assembler\" $file && rm $file"
     [ELSE]
-       s\" \nset logging off\nquit\n\" >$file2 && gdb -nx -q -p `ps -p $$ -o ppid=` -x $file2 2>/dev/null >/dev/null && rm $file2 && grep -v \"of assembler\" $file && rm $file"
+       s\" \nset logging off\nquit\n\" >$file2 && gdb -nx -batch -p `ps -p $$ -o ppid=` -x $file2 2>/dev/null >/dev/null && rm $file2 && grep -v \"of assembler\" $file && rm $file"
     [THEN]  append-extend-string
     2dup (system) 2swap drop free throw throw if
        addr u dump