GDB fallback debugging

Source: Internet
Author: User

To join the debugger that you are using GDB7.0 and above and run on a platform that supports reverse debugging, you can debug the program with the following commands:

Reverse-continue

The reverse runner knows that it encounters an event (such as a breakpoint, observer, exception) that can cause the program to break.

Reverse-step

Reverse-run the program to the last source line that was executed.

Reverse-stepi

Reverse running the program to the previous machine instruction

Reverse-next

The reverse runs to the last line of source code that was executed, but does not enter the function.

Reverse-nexti

Run backwards to the previous machine instruction, unless the instruction is used to return a function call, and the entire function will be executed in reverse.

Reverse-finish

The reverse runner returns to the place where the current function is called.

Set exec-direction [forward | reverse]

Set the direction of the program, you can use the usual command step and continue, etc. to perform reverse debugging commands.

The inverse of the above can also be understood to undo the effect of the statement that is run later, back to the previous state.

OK, then let's try to debug backwards.

First, confirm that your platform supports process record playback (processes records and Replay), and when the debugger enables the process record playback function, the debugger records the daemon, which is the difference between the running state of each step of the debug process and the previous run state. It is convenient to go back to the previous step when you need to undo it.

Suppose we have the following C program:

[CPP]View PlainCopyprint?
    1. int main (int argc, const Char *argv[])
    2. {
    3. int a = 0;
    4. A = 1;
    5. A = 2;
    6. return 0;
    7. }

Compile it and add the debug symbols:

[Python]View PlainCopy print?
    1. $ gcc-wall-g A.C

To start Debugging:

[Python]View PlainCopyprint?
    1. $ gdb a.out

Take a look at the source code:

[Python]View PlainCopy  print?
    1. (GDB) L
    2. 1 int main (int argc, const char *argv[])
    3. 2 {
    4. 3 int a = 0;
    5. 4 A = 1;
    6. 5 A = 2;
    7. 6 return 0;
    8. 7}

Next, set a breakpoint on the third line:

[Python]View PlainCopyprint?
    1. (GDB) b 3
    2. Breakpoint 1 at 0x804839a:file A.C, line 3.

Run, the program will stop at the third line:

[Python]View PlainCopy print?
    1. (GDB) R
    2. Starting program:/home/cheryl/a.out
    3. Breakpoint 1, Main (argc=1, argv=0xbffff3e4) at A.C:3
    4. 3 int a = 0;

Setting a monitoring point for variable a allows us to observe:

[Python]View PlainCopyprint?
    1. (GDB) Watch a
    2. Hardware watchpoint 2:a

Start process record playback:

[Python]View PlainCopyprint?
    1. (GDB) record

Now each step of the debugger will record the changes for backtracking. We execute 3 consecutive statements.

[Python]View PlainCopyprint?
  1. (GDB) n
  2. 4 A = 1;
  3. (GDB)
  4. Hardware watchpoint 2:a
  5. Old value = 0
  6. New value = 1
  7. Main (argc=1, argv=0xbffff3e4) at A.C:5
  8. 5 A = 2;
  9. (GDB)
  10. Hardware watchpoint 2:a
  11. Old value = 1
  12. New value = 2
  13. Main (argc=1, argv=0xbffff3e4) at A.C:6
  14. 6 return 0;

As you can see, the value of a is changed from 0 to 1, then to 2, what if you want the program to revert back to its previous state? You can use the Reverse-next command:

[Python]View PlainCopyprint?
  1. (GDB) Reverse-next
  2. Hardware watchpoint 2:a
  3. Old value = 2
  4. New value = 1
  5. Main (argc=1, argv=0xbffff3e4) at A.C:5
  6. 5 A = 2;
  7. (GDB)
  8. Hardware watchpoint 2:a
  9. Old value = 1
  10. New value = 0
  11. Main (argc=1, argv=0xbffff3e4) at A.C:4
  12. 4 A = 1;
  13. (GDB)
  14. No more reverse-execution history.
  15. Main (argc=1, argv=0xbffff3e4) at A.C:3
  16. 3 int a = 0;
  17. (GDB)

So the program goes back to where we started the process of recording playback, and the value of a is returned to its original state in two steps.

If you need to turn off process record playback, you can use the record stop:

[Python]View PlainCopyprint?
    1. (GDB) Record stop
    2. Process record is stoped and all execution logs is deleted.

Reference: "Reverse debugging with GDB"---http://sourceware.org/gdb/wiki/ReverseDebug

GDB fallback debugging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.