【除錯技巧】一種針對正在執行的程序中途寫值快速除錯的方法
阿新 • • 發佈:2019-02-09
// 程式介面執行上文 #if 1 int test_fd = open ("/tmp/test_file", O_RDWR|O_CREAT); char test_buff[16] = {0}; int test_rs = 0; if (test_rs = read (test_fd, test_buff, sizeof (test_buff)) > 0) { /* 字串strcpy拷貝,數字atoi轉化 */ #if 0 strncpy (want_test_buff, test_buff, sizeof (want_test_value)); printf ("want_test_buff = %s\n", want_test_buff); #else want_test_value = atoi (test_buff); printf ("want_test_value = %d\n", want_test_value); #endif } #endif // 程式介面執行下文
終端視窗一:
前臺執行程式,實時輸出log或者輸出到檔案。
終端視窗二:
# echo "88" >/tmp/test_file
# cat /tmp/test_file
88
# echo "任意想要除錯的值" >/tmp/test_file
從log中觀察該值在後續的執行時變化狀態,實時跟進,實現最簡單的快速除錯多個值或字串。
主要節約的是編譯這個最耗時的時間,事半功倍。