How can I use “watch” GDB?

前端 未结 3 1946
旧巷少年郎
旧巷少年郎 2020-12-20 15:52

I tried to watch the change of the \"int a\" by the command \"watch a\". However, the program does not stop, when it changes to 12. Why?

 /* FILE: test.c */
         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-20 16:36

    When you want to debug a program you should always build with -O0 -g3 (i take that you are using gcc, if you are not your compiler will probably support other flags to turn down optimization and enable debug information).

    On my system (x86_64 running Gentoo GNU/Linux) i am unable to get at the 'int a = 12' line when i use any optimization greater or equal to -O as the compiler will then apply dead code elimination. (Taken from here, it is the -fdce flag in the -O section)

    Always keep this in mind when you are debugging! Verify your code by either disassembling it with objdump -D or tell your compiler to show you the generated assembly (on gcc with the -S flag)

提交回复
热议问题