çµ±åéçºç°å¢ï¼IDEï¼ã¯ãã¡ãããemacsãvimããããã¬ã¼ãå é¨ã§å¼ã³åºãã¦ä½¿ããã®ã§ãã¢ããªã±ã¼ã·ã§ã³éçºã§ãããã¬ã¼ãç´æ¥æä½ãããã¨ã¯å°ãªãã¨æãã¾ããã使ããã¨ä¾¿å©ãªã¨ããããã®ã§ãæä½éã®ä½¿ãæ¹ã¯è¦ãã¦ããããã¨ããã§ãã詳ããã¨ããã¯ããã¥ã¢ã«ãè¦ãããæ¤ç´¢ããããçæAIã«è¨ªãããä½ãçãã¦ãããæ代ã§ãããç°¡åãªã¦ã©ã¼ã¯ã¹ã«ã¼ãã§ããç¨åº¦ã®ç¥èãå ¥ãã¦ããã¾ãããããªããããã§ã®ä¾ã¯Cè¨èªã§ãããC++ãFortranã§åºåããã³ã¼ãã§ããããã°ã§ãã¾ãã
- ãããã°å¯¾è±¡ã®ã³ã³ãã¤ã«
- èµ·å
- ã½ã¼ã¹ã³ã¼ãã®è¡¨ç¤º
- ãã¬ã¼ã¯ãã¤ã³ãã®è¨å®
- å®è¡
- GDBã®çµäº
- ã¾ã¨ã
ãããã°å¯¾è±¡ã®ã³ã³ãã¤ã«
main.cã¨sub.cã®2ã¤ã®ã½ã¼ã¹ã³ã¼ããç¨æãã¦ãgdbãåããã¦ã¿ã¾ãããã
main.cã¯ã
#include<stdio.h> #include<stdlib.h> int ssum(int n); int main(int argc, char *argv[]){ int i; for(i = 1; i < argc; i++){ printf("%d\n", ssum(atoi(argv[i]))); } return 0; }
ã¨ãã¾ãã
sub.cã¯ã
int ssum(int n){ int r, i; r = 0; for(i = 1; i <= n; i++){ r += i*i; } return r; }
ã¨ãã¾ãã
ã³ã³ãã¤ã«ã¯ãå¤æ°åãªã©ã®ã·ã³ãã«æ
å ±ãå®è¡ãã¡ã¤ã«ã«å«ãã-gãªãã·ã§ã³ã¨ãæé©åãå¹ããªãããã«ãã-O0ãªãã·ã§ã³ãã¤ãã¦è¡ãã®ãä¸è¬çã§ã*1ã
gcc -o example.exe main.c sub.c -g -O0
ä»åã¯è©¦ãã«ããã®example.exeããããã¬ã¼ã«ããã¦ã¿ã¾ãã
èµ·å
ã³ãã³ãã©ã¤ã³ã§å®è¡ãã¡ã¤ã«æå®
ã·ã§ã«ã§
gdb example.exe
ã¨ãã£ã¦gdbãèµ·åããã°ãexample.exeã®ãããã°ãå¯è½ã«ãªãã¾ã*2ã
ã³ã¢ãã³ããæ®ãã¦ç°å¸¸çµäºããã¨ãã¯*3ãä¾ãã°ã³ã¢ãã³ãã®ãã¡ã¤ã«åãcore.12345ã®ã¨ãã¯ã
gdb example.exe core.12345
ã¨ããã°ãç°å¸¸çµäºæã®ããã»ã¹ã®ç¶æ
ã調ã¹ããã¨ãã§ãã¾ã*4ã
gdbã®ã³ãã³ãã§å®è¡ãã¡ã¤ã«æå®
gdbãèµ·åå¾ã
gdb
gdbã®ã³ãã³ãã§example.exeãèªã¿è¾¼ããã¨ãã§ãã¾ãã
exec-file example.exe symbol-file example.exe
ã·ã³ãã«æ å ±ã¯å¥ã«èªã¿è¾¼ã¾ããå¿ è¦ãããã®ã§ã注æãã¾ãããã
ã³ã¢ãã³ããæ®ãã¦ç°å¸¸çµäºããã¨ãã¯ãä¾ãã°ã³ã¢ãã³ãã®ãã¡ã¤ã«åãcore.12345ã®ã¨ãã¯ã
core-file core.12345
ã¨è¨ãããã«æå®ãã¾ãã
ç°å¢ã®å¤æ´ã¨ç¢ºèª
ä¾ãã°ç°å¢å¤æ°HOMEã®å¤æ´ã¨ç¢ºèªã¯ã以ä¸ã®ããã«ãã¾ãã
set environment HOME = /var/tmp show environment HOME
ä½æ¥ãã£ã¬ã¯ããªã®å¤æ´ã¨ç¢ºèª
ä¾ãã°/var/tmpã«ç§»åããå ´åã¯ã以ä¸ã®ããã«ãã¾ãã
cd /var/tmp pwd
cdã¨ã ããã¤ã¨ãgdbã®èµ·åãã¹ã«ä½æ¥ãã£ã¬ã¯ããªãè¨å®ããã¾ãã
ã½ã¼ã¹ã³ã¼ãã®è¡¨ç¤º
infoã³ãã³ãã§ãã³ã³ãã¤ã«ã«ä½¿ã£ãã½ã¼ã¹ã³ã¼ãã®ãã¡ã¤ã«åã確èªã§ãã¾ããinfoã¯iã«çç¥å¯è½ã§ãã
info sources
listã³ãã³ãã§ããã¡ã¤ã«åã¨è¡çªå·ãããã¯ããã¡ã¤ã«åã¨é¢æ°åãæå®ãã¦ãã½ã¼ã¹ã³ã¼ãã®æå®ç®æåå¾ã表示ããããã¨ãã§ãã¾ããlistã¯lã«çç¥å¯è½ã§ã表示è¡æ°ãå¤ãããå ´åã¯ã第3å¼æ°ã«æ°åãæå®ãã¾ãã
list sub.c:1
é¢æ°åã®æå®ã¯æ¬¡ã®ããã«ãã¾ãã
list sub.c:ssum
å®è¡ãä¸æãã¦å¤æ°ã®ç¶æ ãªã©ã確èªããã¹ãããå®è¡ãã¦ãããã¬ã¼ã¯ãã¤ã³ãã¯ããã¡ã¤ã«åã¨è¡çªå·ãããã¯ããã¡ã¤ã«åã¨é¢æ°åã§è¨å®ããã®ã§ããããã®ç¢ºèªã¯éè¦ã§ãã
ãã¬ã¼ã¯ãã¤ã³ãã®è¨å®
break ãã¡ã¤ã«å:è¡çªå·ããããã¯ããã¡ã¤ã«å:é¢æ°åã§ãã¬ã¼ã¯ãã¤ã³ããè¨å®ãããã¨ãã§ãã¾ãã
sub.cã®ä¸ã®ssumé¢æ°ã§ãã¬ã¼ã¯ãããå ´åã¯ã
break sub.c:ssum
ã¨ãã¾ããbreakã¯bã«çç¥ã§ãã¾ããifæãã¤ãã¦æ¡ä»¶ä»ããã¬ã¼ã¯ãæå®ãããã¨ãã§ãã¾ããä¾ãã°ã
b sub.c:6 if i==2
ã¨è¨ãããã«æ¸ãã¾ããæ¯è¼æ¼ç®åã¯Cè¨èªã¨äºæã®ãã®ã使ãã¾ããã¾ããæååã®æ¯è¼ãæ£è¦è¡¨ç¾ã使ãããå ´åã«ã¯ãgdbç¬èªã®ä¾¿å©é¢æ°ï¼convenience functionsï¼ã使ããã¨ãã§ãã¾ãã
è¨å®ãããã¬ã¼ã¯ãã¤ã³ãã¯ã
info breakpoints
ã§ç¢ºèªãããã¨ãã§ãã¾ãã
ãªãããã¬ã¼ã¯ãã¤ã³ããåé¤ããã¨ãã¯ãinfo表示ã®çªå·ãdeleteã§æå®ãã¾ããdã«çç¥å¯è½ã§ãã
delete 1
çªå·ãåæã«ä¸¦ã¹ã¦ãè¤æ°ã®ãã¬ã¼ã¯ãã¤ã³ãã®åæåé¤ãã§ãã¾ãã
å®è¡
runã§å®è¡ã§ããã³ãã³ãã©ã¤ã³å¼æ°ãåæ§ã«æå®ã§ãã¾ããrunã¯rã«çç¥å¯è½ã§ããä¾ãã°example.exe 3ã®å®è¡ããããã°ããã¨ãã¯ã
run 3
ã¨ãªãã¾ãã
run 3 > output.txt
ã¨ããã¨æ¨æºåºåããã¡ã¤ã«output.txtã«ãã¤ãã§ã¤ãªãã¾ãã
ä»åã®ã³ã¼ãã§ã¯æå³ããªãã§ãããæ¨æºã¨ã©ã¼åºåãã¤ãªãå ´åã¯ã
set args 2> output.txt run 3
ã¨ãã¾ãã
ç¾å¨ä½ç½®ï¼ããã¯ãã¬ã¼ã¹ï¼ã®è¡¨ç¤º
ãã¬ã¼ã¯ãã¤ã³ããªã©ã§å®è¡åæ¢ãã¦ããå ´åãbacktraceï¼btã«çç¥å¯è½ï¼ãwhereã§ç¾å¨ä½ç½®ï¼ããã¯ãã¬ã¼ã¹ï¼ã®è¡¨ç¤ºãã§ãã¾ãã
where
ä¾ãã°ã以ä¸ã®å ´åã§ã¯
main.cã®10è¡æããå¼ã³åºãããé¢æ°ã®ä¸ã®ãsub.cã®4è¡ç®ã§ãããã¨ããããã¾ãã
ããã¯ã¨ã¬ã¼ã¹ã®è¡¨ç¤ºã«çªå·ãã¤ãã¦ãã¾ããããã¬ã¼ã ã®çªå·ã§ããé¢æ°ã®å¼ã³åºãã§å ¥ãç± æ§é ã«ãªã£ã¦ããããã§ããããã¬ã¼ã ã¯ãã®æ§é ã®ã©ã®å±¤ãã表ãã¾ãã
å¤æ°ã®è¡¨ç¤º
ä¾ãã°å¤æ°rã表示ããå ´åã¯ã
print r
ã¨ãã¾ããprintã¯pã«çç¥å¯è½ã§ããé åaããprint a[1]ã¨ä¸é¨åã ã表示ãããã*ptr@3ã§ãã¤ã³ã¿ã¼ptrãæã示ãå ã®é 3ã¤ã表示ããããCè¨èªã«éããããªãã¿ã®printfæã使ããããã¾ãã
printf "r: %d\n", r
ãã¼ã«ã«ã¹ã³ã¼ãã®å¤æ°ãã¾ã¨ãã¦è¡¨ç¤ºããå ´åã¯ã
info local
ã§è¡¨ç¤ºããã¾ããã¾ãã
info args
ã§ãç¾å¨ããé¢æ°ãå¼ã³åºããã¨ãã®å¼æ°ã表示ãã¾ããframeã³ãã³ãã§ãã¬ã¼ã çªå·ãæå®ããããupãããã¯downã§åç §ãããã¬ã¼ã ãå¤ãããã¨ãã§ããinfo localã¨info argsã®å¯¾è±¡ãå¤ããã¾ãã
ãã¡ãã¡printããã«ãã¹ããããã¨ã«å¤æ°ã®ç¶æ ã確èªãããå ´åã¯ãã¦ã©ãããã¤ã³ããè¨å®ãã¾ãã
watch r
ãã¬ã¼ã¯ãã¤ã³ãã¨åæ§ã«info watchpointsã§è¨å®ä¸è¦§ã確èªããdeleteã§çªå·ãæå®ãã¦åé¤ã§ãã¾ããã¾ãã使ã£ã¦ããå¤æ°ãå®ç¾©ããã¦ããã¹ã³ã¼ãããæããå ´åãèªåçã«ãã®ã¦ã©ãããã¤ã³ãã¯è§£é¤ããã¾ãã
å¤æ°ã¸ã®ä»£å ¥
ä¾ãã°å¤æ°rã«7ãä»£å ¥ããã¨ãã¯ã
print r = 7
ã¨ãããã
set variable r = 7
ã¨ãã¾ãã
ã¹ãããå®è¡
step
ã§ã1è¡ã¥ã¤å®è¡ã§ãã¾ããsã«çç¥å¯è½ã§ããé¢æ°ããã£ãå ´åãé¢æ°ã®ä¸ãä¸è¡ã¥ã¤å®è¡ãããã¨ãã¾ãï¼step-inï¼ã
next
ãã1è¡ã¥ã¤å®è¡ããã³ãã³ãã§ãããé¢æ°ã®ä¸èº«ã¯ã¾ã¨ãã¦å®è¡ãã¾ãï¼step-overï¼ãnã«çç¥å¯è½ã§ãã
ä»ã«finishã§ç¾å¨ã®é¢æ°ã®çµäºã¾ã§å®è¡ãåºæ¥ãããã¾ããstep-overããã¤ããã§ãstep-inãã¦ãã¾ã£ãã¨ãã«ä¾¿å©ã§ãã
次ã®ãã¬ã¼ã¯ãã¤ã³ãã¾ã§å®è¡
continue
ã§ã次ã®ãã¬ã¼ã¯ãã¤ã³ãã¾ã§å®è¡ããããæå¾ã¾ã§å®è¡ãã¾ããcã«çç¥å¯è½ã§ããæå¾ã¾ã§å®è¡ããå ´åãrunã§åå®è¡ã§ãã¾ãã
ãããã°ä¸ã®ããã»ã¹ã®ä¸æ
éä¸ã§ãããã°ãã¦ããããã°ã©ã ã®å®è¡ãä¸æãããã¨ãã§ãã¾ãã
kill
gdbã¯åæ¢ããªãã®ã§ããã¬ã¼ã¯ãã¤ã³ãã®è¨å®ãªã©ã¯ãã®ã¾ã¾æ®ãã¾ãã
ã¾ã¨ã
IDEããgdbã使ããã¨ãå¤ãã¨æãã¾ãããã³ãã³ãã©ã¤ã³ã ã¨æä½å°é£ã¨è¨ãããã§ãç¡ãã®ãåããã¾ããgdbã«ã¯ä»ã«ããåæ¢ããã«ã³ãã³ãå®è¡ããããããtracepointsããã·ã°ãã«ãä¾å¤ã«ä»æããããcatchpointsãè¨å®ã§ããã¾ããæ©æ¢°èªã®ã¬ãã«ã§ããããã°ã§ãã¾ããæ´å²ã®é·ããã¼ã«ãªã®ã§ãæ¤ç´¢ãããªãããã°è²ã ã¨è³æãåºã¦ããã®ã§ãå¿ è¦ã«å¿ãã¦ä½¿ã£ã¦ããã¾ãããã
*1:ç¡ãã¨gdbã§ãããã°ä¸è½ã¨è¨ãããã§ã¯ãªãã§ãããå°é£ã«ãªãã¾ãã
*2:å¼æ°ã¯å¾è¿°ããrunã§æå®ããä»ãgdbèµ·åæã«--argsã®å¾ã«æå®å¯è½ã§ãã
*3:OSãã³ã¢ãã³ããæ®ããªãè¨å®ã«ãªã£ã¦ããå ´åãããï¼e.g. Ubuntu で core ファイルが出力されない #gdb - Qiita ï¼ããã®å¤æ´ã«ã¯ç®¡çè 権éãå¿ è¦ã«ãªãã¾ãã
*4:æ¬ç¨¿ã§ã¯å²æãã¾ã