How do breakpoints work in C++ code?

前端 未结 4 1285
刺人心
刺人心 2020-12-07 16:59

How do breakpoints work in C++ code? Are they special instructions inserted in between some assembler instructions when the code is compiled? Or is there something else in p

4条回答
  •  时光说笑
    2020-12-07 17:32

    AFAIK all debuggers (for whatever compiled language) that allow an unlimited number of breakpoints use a variant of replacing the instruction to be breakpointed with a special value (as described above) and keeping a list of places where these values have been placed.

    When the processor tries to execute one of these special values, an exception is raised, the debugger catches it and checks if the address of the exception is on its list of breakpoints. If it is, the debugger is invoked and the user is given an opportunity to interact. If it is NOT, then the exception is due to something that was in the program from the outset and the debugger lets the exception 'pass' to whatever error handler might be there.

    Note also, that debugging self-modifying code can fail precisely because the debugger momentarily modifies the code itself. (Of course, nobody would ever write self-modifying, now would they? >;-)

    For these reasons, it is important that the debugger be given the opportunity to remove all the breakpoints it sets before terminating the debugging session.

提交回复
热议问题