簡體   English   中英

c 中的簡單 shell - valgrind 顯示排序命令的內存泄漏

[英]simple shell in c - valgrind shows memorry leaks for sort command

我正在 C 中編寫一個簡單的 shell ,它應該最多支持 2 個管道(我知道 n 管道的工具更短,但由於某種原因,這是特定的要求)。

當使用 valgrind 檢查 memory 泄漏時,除非我使用排序命令,否則我沒有問題,例如“ls | sort”輸出以下消息:

==4903== HEAP SUMMARY:
==4903==     in use at exit: 760 bytes in 17 blocks
==4903==   total heap usage: 65 allocs, 48 frees, 8,533,744 bytes allocated
==4903== 
==4903== 8 bytes in 1 blocks are definitely lost in loss record 1 of 6
==4903==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4903==    by 0x11ACFC: ??? (in /usr/bin/sort)
==4903==    by 0x10D72A: ??? (in /usr/bin/sort)
==4903==    by 0x48AE0B2: (below main) (libc-start.c:308)
==4903== 
==4903== 32 bytes in 1 blocks are indirectly lost in loss record 2 of 6
==4903==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4903==    by 0x11ACFC: ??? (in /usr/bin/sort)
==4903==    by 0x1166F6: ??? (in /usr/bin/sort)
==4903==    by 0x10D860: ??? (in /usr/bin/sort)
==4903==    by 0x48AE0B2: (below main) (libc-start.c:308)
==4903== 
==4903== 48 bytes in 12 blocks are still reachable in loss record 3 of 6
==4903==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4903==    by 0x11ACFC: ??? (in /usr/bin/sort)
==4903==    by 0x10C122: ??? (in /usr/bin/sort)
==4903==    by 0x48AE0B2: (below main) (libc-start.c:308)
==4903== 
==4903== 128 bytes in 1 blocks are still reachable in loss record 4 of 6
==4903==    at 0x483B723: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4903==    by 0x483E017: realloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4903==    by 0x11AD69: ??? (in /usr/bin/sort)
==4903==    by 0x10F0F2: ??? (in /usr/bin/sort)
==4903==    by 0x10DA99: ??? (in /usr/bin/sort)
==4903==    by 0x48AE0B2: (below main) (libc-start.c:308)
==4903== 
==4903== 512 bytes in 1 blocks are indirectly lost in loss record 5 of 6
==4903==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4903==    by 0x11ACFC: ??? (in /usr/bin/sort)
==4903==    by 0x10D89D: ??? (in /usr/bin/sort)
==4903==    by 0x48AE0B2: (below main) (libc-start.c:308)
==4903== 
==4903== 576 (32 direct, 544 indirect) bytes in 1 blocks are definitely lost in loss record 6 of 6
==4903==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4903==    by 0x11ACFC: ??? (in /usr/bin/sort)
==4903==    by 0x1166C7: ??? (in /usr/bin/sort)
==4903==    by 0x10D860: ??? (in /usr/bin/sort)
==4903==    by 0x48AE0B2: (below main) (libc-start.c:308)
==4903== 
==4903== LEAK SUMMARY:
==4903==    definitely lost: 40 bytes in 2 blocks
==4903==    indirectly lost: 544 bytes in 2 blocks
==4903==      possibly lost: 0 bytes in 0 blocks
==4903==    still reachable: 176 bytes in 13 blocks
==4903==         suppressed: 0 bytes in 0 blocks
==4903== 
==4903== For lists of detected and suppressed errors, rerun with: -s
==4903== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

以上是 valgrind 運行sort后的 output ,valgrind 運行ls后泄漏總結為:

==4904== LEAK SUMMARY:
==4904==    definitely lost: 0 bytes in 0 blocks
==4904==    indirectly lost: 0 bytes in 0 blocks
==4904==      possibly lost: 0 bytes in 0 blocks
==4904==    still reachable: 20,381 bytes in 9 blocks
==4904==         suppressed: 0 bytes in 0 blocks
==4904== 
==4904== For lists of detected and suppressed errors, rerun with: -s
==4904== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

我可以忍受。

我嘗試了幾個不同的命令,不管有沒有管道 - 再一次, sort是唯一導致泄漏的命令。

當我退出程序時,valgrind 輸出沒有錯誤,也沒有 memory 泄漏,但這可能是由於最后的exit(0)調用。

我的問題是

  1. 可能是sort的 valgrind 問題嗎?
  2. 我怎樣才能進一步調試呢?

我仔細檢查了程序中的每個free通話,並在 web 上尋找類似的問題,但找不到任何東西。

(如有必要,代碼將更新)

更新

感謝Bodo的回答,我發現我的系統(VMware 16 下的 Linux Mint 20) sort確實會導致或至少在 valgrind 中出現 memory 泄漏。 我用了ls | valgrind sort ls | valgrind sort建議以便弄清楚。

valgrind output:

==2966== HEAP SUMMARY:
==2966==     in use at exit: 760 bytes in 17 blocks
==2966==   total heap usage: 65 allocs, 48 frees, 8,533,744 bytes allocated
==2966== 
==2966== LEAK SUMMARY:
==2966==    definitely lost: 40 bytes in 2 blocks
==2966==    indirectly lost: 544 bytes in 2 blocks
==2966==      possibly lost: 0 bytes in 0 blocks
==2966==    still reachable: 176 bytes in 13 blocks
==2966==         suppressed: 0 bytes in 0 blocks

是的, valgrind可以報告/usr/bin/sort程序中的問題。

您的 shell 可能使用fork為要執行的命令創建一個(或多個)進程,然后調用一些exec.. function 來執行指定的程序。 exec function 成功時,該進程不再運行您的 shell 的代碼,而是運行其他程序的代碼。

如果您指示valgrind檢查子進程,它會繼續跟蹤您的 shell 執行的程序,因此它也會報告這些程序中可能出現的錯誤。 當然,這些錯誤與您的 shell 無關。

要確認這一點,您可以比較類似命令行的結果,例如ls | sort ls | sortls | cat ls | cat


確認這一點的另一種方法是使用valgrind獨立於簡單的 shell 運行sort 你可以試試

ls | valgrind sort

如果valgrind的混合 output 和您要檢查的程序令人困惑,您可以使用valgrind的選項--log-file=<filename>選項將其 output 重定向到文件。


正如Jabberwocky的評論中提到的,為避免檢查任何不相關的程序,您可以創建自己的(假)程序,這些程序沒有 memory 泄漏和 memory 訪問錯誤。 要檢查 shell 實現中的錯誤,您的sort替換實際上不必對行進行排序。 唯一重要的是它不會從valgrind產生任何錯誤/警告,並且它的行為可能類似於sortstdin讀取數據並將數據寫入stdout ,可選延遲。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM