日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

valgrind基础

發布時間:2025/3/21 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 valgrind基础 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

valgrind

官網

原理

官網解釋
Your program is then run on a synthetic CPU provided by the Valgrind core. As new code is executed for the first time, the core hands the code to the selected tool. The tool adds its own instrumentation code to this and hands the result back to the core, which coordinates the continued execution of this instrumented code.

stackoverflow解釋
valgrind provides a virtual processor that executes your application. However, before your application instructions are processed, they are passed to tools (such as memcheck). These tools are kind of like plugins, and they are able to modify your application before it is run on the processor.
The great thing about this approach is that you don't have to modify or relink your program at all to run it in valgrind. It does cause your program to run slower, however valgrind isn't meant to measure performance or run during normal execution of your application, so this isn't really an issue.

安裝

源碼地址

使用如下命令更簡單些

yum install valgrind

參數

# When enabled, Valgrind will trace into sub-processes initiated via the exec system call. # This is necessary for multi-process programs. # Note that Valgrind does trace into the child of a fork (it would be difficult not to, # since fork makes an identical copy of a process), so this option is arguably badly named. # However, most children of fork calls immediately call exec anyway.--trace-children=<yes|no> [default: no]

Memcheck

功能

  • Memory leaks.
  • Using undefined values, i.e. values that have not been initialised, or that have been derived from other undefined values.
  • Accessing memory you shouldn't, e.g. overrunning and underrunning heap blocks, overrunning the top of the stack, and accessing memory after it has been freed.
  • Incorrect freeing of heap memory, such as double-freeing heap blocks, or mismatched use of malloc/new/new[] versus free/delete/delete[]
  • Overlapping src and dst pointers in memcpy and related functions.
  • Passing a fishy (presumably negative) value to the size parameter of a memory allocation function.
  • 使用

    #編譯文件,加參數 -g clang++ -g -Wall memcheck_test.cc -o memcheck_test #--tool=memcheck 參數缺省 #--leak-check=full,內存泄的具體信息 #--log-file=log_file.txt,輸出信息重定向到文件中 valgrind --leak-check=full --log-file=log_file.txt memcheck_test

    Helgrind

    功能

  • Misuses of the POSIX pthreads API
  • Potential deadlocks arising from lock ordering problems
  • Data races -- accessing memory without adequate locking or synchronisation
  • 使用

    valgrind --tool=helgrind memcheck_test

    如何高效實用Helgrind

    鏈接

    callgrind

    功能

    Callgrind is a profiling tool that records the call history among functions in a program's run as a call-graph.

    使用

    valgrind --tool=callgrind --separate-threads=yes ./callgrind_test #函數調用情況總覽 callgrind_annotate callgrind.out.<pid>#結合源文件 #注意.cc文件目錄格式要和上面命令產生的路徑格式一致 callgrind_annotate callgrind.out.<pid> callgrind_test.cc

    使用kcachegrind工具

    yum install kcachegrind yum install graphviz kcachegrind callgrind.out.<pid>

    QA

    鏈接

    總結

    以上是生活随笔為你收集整理的valgrind基础的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。