How to set a breakpoint in GDB where the function returns?

后端 未结 7 1322
孤独总比滥情好
孤独总比滥情好 2020-11-27 06:10

I have a C++ function which has many return statements at various places. How to set a breakpoint at the return statement where the function actually returns ?

And

相关标签:
7条回答
  • 2020-11-27 06:58

    rr reverse debugging

    Similar to GDB record mentioned at https://stackoverflow.com/a/3649698/895245 , but much more functional as of GDB 7.11 vs rr 4.1.0 in Ubuntu 16.04.

    Notably, it deals with AVX correctly:

    • gdb reverse debugging fails with "Process record does not support instruction 0xf0d at address"
    • "target record-full" in gdb makes "n" command fail on printf with "Process record does not support instruction 0xc5 at address 0x7ffff7dee6e7"?

    which prevents it from working with the default standard library calls.

    Install Ubuntu 16.04:

    sudo apt-get install rr linux-tools-common linux-tools-generic linux-cloud-tools-generic
    sudo cpupower frequency-set -g performance
    

    But also consider compiling from source to get the latest updates, it was not hard.

    Test program:

    int where_return(int i) {
        if (i)
            return 1;
        else
            return 0;
    }
    
    int main(void) {
        where_return(0);
        where_return(1);
    }
    

    compile and run:

    gcc -O0 -ggdb3 -o reverse.out -std=c89 -Wextra reverse.c
    rr record ./reverse.out
    rr replay
    

    Now you are left inside a GDB session, and you can properly reverse debug:

    (rr) break main
    Breakpoint 1 at 0x56057c458619: file a.c, line 9.
    (rr) continue
    Continuing.
    
    Breakpoint 1, main () at a.c:9
    9           where_return(0);
    (rr) step
    where_return (i=0) at a.c:2
    2           if (i)
    (rr) finish
    Run till exit from #0  where_return (i=0) at a.c:2
    main () at a.c:10
    10          where_return(1);
    Value returned is $1 = 0
    (rr) reverse-step
    where_return (i=0) at a.c:6
    6       }
    (rr) reverse-step
    5               return 0;
    

    We are now on the correct return line.

    0 讨论(0)
提交回复
热议问题