Programing

GDB : 변수가 같은 값이면 중단

lottogame 2020. 10. 10. 09:30
반응형

GDB : 변수가 같은 값이면 중단


변수가 내가 설정 한 값과 같을 때 GDB를 중단 점으로 설정하고 싶습니다.이 예제를 시도해 보았습니다.

#include <stdio.h>
main()
{ 
     int i = 0;
     for(i=0;i<7;++i)
        printf("%d\n", i);

     return 0;
}

GDB의 출력 :

(gdb) break if ((int)i == 5)
No default breakpoint address now.
(gdb) run
Starting program: /home/SIFE/run 
0
1
2
3
4
5
6

Program exited normally.
(gdb)

보시다시피, GDB는 중단 점을 만들지 않았습니다. GDB에서 가능합니까?


중단 점 안에 중첩 된 감시 점 외에도 'filename : line_number'에 단일 중단 점을 설정하고 조건을 사용할 수도 있습니다. 나는 때때로 그것이 더 쉽다는 것을 안다.

(gdb) break iter.c:6 if i == 5
Breakpoint 2 at 0x4004dc: file iter.c, line 6.
(gdb) c
Continuing.
0
1
2
3
4

Breakpoint 2, main () at iter.c:6
6           printf("%d\n", i);

나처럼 줄 번호 변경에 질리면 레이블을 추가 한 다음 다음과 같이 레이블에 중단 점을 설정할 수 있습니다.

#include <stdio.h>
main()
{ 
     int i = 0;
     for(i=0;i<7;++i) {
       looping:
        printf("%d\n", i);
     }
     return 0;
}

(gdb) break main:looping if i == 5

이를 위해 감시 점 (코드 대신 데이터의 중단 점)을 사용할 수 있습니다.

을 사용하여 시작할 수 있습니다 watch i.
그런 다음 사용하여 조건을 설정하십시오.condition <breakpoint num> i == 5

다음을 사용하여 중단 점 번호를 얻을 수 있습니다. info watch


먼저 적절한 플래그를 사용하여 코드를 컴파일하여 코드로 디버그 할 수 있어야합니다.

$ gcc -Wall -g -ggdb -o ex1 ex1.c

then just run you code with your favourite debugger

$ gdb ./ex1

show me the code.

(gdb) list
1   #include <stdio.h>
2   int main(void)
3   { 
4     int i = 0;
5     for(i=0;i<7;++i)
6       printf("%d\n", i);
7   
8     return 0;
9   }

break on lines 5 and looks if i == 5.

(gdb) b 5
Breakpoint 1 at 0x4004fb: file ex1.c, line 5.
(gdb) rwatch i if i==5
Hardware read watchpoint 5: i

checking breakpoints

(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004004fb in main at ex1.c:5
    breakpoint already hit 1 time
5       read watchpoint keep y                      i
    stop only if i==5

running the program

(gdb) c
Continuing.
0
1
2
3
4
Hardware read watchpoint 5: i

Value = 5
0x0000000000400523 in main () at ex1.c:5
5     for(i=0;i<7;++i)

There are hardware and software watchpoints. They are for reading and for writing a variable. You need to consult a tutorial:

http://www.unknownroad.com/rtfm/gdbtut/gdbwatch.html

To set a watchpoint, first you need to break the code into a place where the varianle i is present in the environment, and set the watchpoint.

watch command is used to set a watchpoit for writing, while rwatch for reading, and awatch for reading/writing.

참고URL : https://stackoverflow.com/questions/14390256/gdb-break-if-variable-equal-value

반응형