[Bug python/21496] New: Python signal.signal() doesn't restore SIGINT handler correctly.

jules at op59 dot net sourceware-bugzilla@sourceware.org
Tue May 16 19:52:00 GMT 2017


https://sourceware.org/bugzilla/show_bug.cgi?id=21496

            Bug ID: 21496
           Summary: Python signal.signal() doesn't restore SIGINT handler
                    correctly.
           Product: gdb
           Version: HEAD
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: python
          Assignee: unassigned at sourceware dot org
          Reporter: jules at op59 dot net
  Target Milestone: ---

I've seen a curious problem in gdb where modifying and restoring the handler
for SIGINT from within gdb's python, breaks subsequent handling of Ctrl-C in
gdb. Specifically it affects Ctrl-C when used to terminate commands that output
lots of text and page with '---Type <return> to continue, or q <return> to
quit---'.

Here's how to reproduce, using a build of gdb from git.

First get/build an executable who's main() is long enough to require scrolling
when disassembled from within gdb (you may need to reduce the size of your
terminal window for this), and load into gdb:

    > /home/jules/gdb_git_dir/gdb/gdb --data-directory
gdb_git_dir/gdb/data-directory a.out
    GNU gdb (GDB) 8.0.50.20170516-git
    Copyright (C) 2017 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-pc-linux-gnu".
    Type "show configuration" for configuration details.
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>.
    Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.
    For help, type "help".
    Type "apropos word" to search for commands related to "word"...
    Reading symbols from a.out...done.

Do disassemble of a function:

    (gdb) disassemble main
    Dump of assembler code for function main:
       0x0000000000400546 <+0>:     push   %rbp
       0x0000000000400547 <+1>:     mov    %rsp,%rbp
       0x000000000040054a <+4>:     sub    $0x10,%rsp
       0x000000000040054e <+8>:     mov    $0x400634,%edi
       0x0000000000400553 <+13>:    callq  0x400410 <puts@plt>
       0x0000000000400558 <+18>:    movl   $0x0,-0x4(%rbp)
       0x000000000040055f <+25>:    jmp    0x40059c <main+86>
       0x0000000000400561 <+27>:    mov    -0x4(%rbp),%ecx
       0x0000000000400564 <+30>:    mov    $0x55555556,%edx
       0x0000000000400569 <+35>:    mov    %ecx,%eax
       0x000000000040056b <+37>:    imul   %edx
       0x000000000040056d <+39>:    mov    %ecx,%eax
       0x000000000040056f <+41>:    sar    $0x1f,%eax
       0x0000000000400572 <+44>:    sub    %eax,%edx
       0x0000000000400574 <+46>:    mov    %edx,%eax
       0x0000000000400576 <+48>:    mov    %eax,%edx
       0x0000000000400578 <+50>:    add    %edx,%edx
       0x000000000040057a <+52>:    add    %eax,%edx
       0x000000000040057c <+54>:    mov    %ecx,%eax
       0x000000000040057e <+56>:    sub    %edx,%eax
       0x0000000000400580 <+58>:    test   %eax,%eax
       0x0000000000400582 <+60>:    jne    0x400598 <main+82>
    ---Type <return> to continue, or q <return> to quit---

At this point, Ctrl-C correctly interrupts the disassembly:

    ---Type <return> to continue, or q <return> to quit---^CQuit

Now use python to change and restore the handler for SIGINT. We actually
do this twice to verify that we restored the original <built-in function
default_int_handler>.

    (gdb) python
    >import signal
    >a = signal.signal( signal.SIGINT, signal.SIG_IGN)
    >b = signal.signal( signal.SIGINT, a)
    >c = signal.signal( signal.SIGINT, b)
    >d = signal.signal( signal.SIGINT, c)
    >print a, b, c, d
    >end
    <built-in function default_int_handler> 1 <built-in function
default_int_handler> 1
    (gdb) 

Try disassembling again:

    (gdb) disassemble main
    Dump of assembler code for function main:
       0x0000000000400546 <+0>:     push   %rbp
       0x0000000000400547 <+1>:     mov    %rsp,%rbp
       0x000000000040054a <+4>:     sub    $0x10,%rsp
       0x000000000040054e <+8>:     mov    $0x400634,%edi
       0x0000000000400553 <+13>:    callq  0x400410 <puts@plt>
       0x0000000000400558 <+18>:    movl   $0x0,-0x4(%rbp)
       0x000000000040055f <+25>:    jmp    0x40059c <main+86>
       0x0000000000400561 <+27>:    mov    -0x4(%rbp),%ecx
       0x0000000000400564 <+30>:    mov    $0x55555556,%edx
       0x0000000000400569 <+35>:    mov    %ecx,%eax
       0x000000000040056b <+37>:    imul   %edx
       0x000000000040056d <+39>:    mov    %ecx,%eax
       0x000000000040056f <+41>:    sar    $0x1f,%eax
       0x0000000000400572 <+44>:    sub    %eax,%edx
       0x0000000000400574 <+46>:    mov    %edx,%eax
       0x0000000000400576 <+48>:    mov    %eax,%edx
       0x0000000000400578 <+50>:    add    %edx,%edx
       0x000000000040057a <+52>:    add    %eax,%edx
       0x000000000040057c <+54>:    mov    %ecx,%eax
       0x000000000040057e <+56>:    sub    %edx,%eax
       0x0000000000400580 <+58>:    test   %eax,%eax
       0x0000000000400582 <+60>:    jne    0x400598 <main+82>
    ---Type <return> to continue, or q <return> to quit---

This time pressing Ctrl-C doesn't interrupt; instead it gives:

    ---Type <return> to continue, or q <return> to quit---^C

At this point, entering any other key ('o' in this example) actually interrupts
the disassembly, and appears to insert the key into the next line:

    ---Type <return> to continue, or q <return> to quit---^CQuit
    (gdb) o

So it looks like the behaviour of Ctrl-C has been subtly changed by the
change+restore of gdb's SIGINT handler.

With gdb-7.7, the behaviour is slightly worse - entering Ctrl-C has no affect
at all; the only way to terminate the disassembly is to enter q<Return>.

This is perhaps a minor issue for interactive sessions, but it causes problems
when using gdb from scripts, e.g. as part of a test suite.

Thanks for any information about what could be going wrong here.

-- 
You are receiving this mail because:
You are on the CC list for the bug.


More information about the Gdb-prs mailing list