### Start Valgrind and vgdb with TCP/IP port Source: https://valgrind.org/docs/manual/manual-core-adv.html Example of starting a program under Valgrind on a target machine and connecting via vgdb using a TCP/IP port. ```bash # On the target computer, start your program under valgrind using valgrind --vgdb-error=0 prog # and then in another shell, run: vgdb --port=1234 ``` -------------------------------- ### Launch Android SDK manager Source: https://valgrind.org/docs/manual/dist.readme-android_emulator.html Start the Android SDK manager to install required platforms. ```bash android ``` -------------------------------- ### Start Android emulator and push Valgrind Source: https://valgrind.org/docs/manual/dist.readme-android_emulator.html Use adb to verify the emulator connection and push the Valgrind installation to the device. ```bash adb shell # Once the emulator is ready, push your Valgrind to the emulator: adb push Inst / ``` -------------------------------- ### Build and Install a New Tool Source: https://valgrind.org/docs/manual/manual-writing-tools.html Commands to regenerate the build system, configure the installation path, and compile the new tool. ```bash autogen.sh ./configure --prefix=`pwd`/inst make make install ``` -------------------------------- ### Start Valgrind with gdbserver Source: https://valgrind.org/docs/manual/manual-core-adv.html Run Valgrind with --vgdb=yes and --vgdb-error=0 to start the program under Valgrind and wait for GDB to connect. ```bash valgrind --vgdb=yes --vgdb-error=0 prog ``` -------------------------------- ### Extended Callgrind Example Source: https://valgrind.org/docs/manual/cl-format.html A complete example demonstrating function calls between main, func1, and func2 across different files. ```text # callgrind format events: Instructions fl=file1.c fn=main 16 20 cfn=func1 calls=1 50 16 400 cfi=file2.c cfn=func2 calls=3 20 16 400 fn=func1 51 100 cfi=file2.c cfn=func2 calls=2 20 51 300 fl=file2.c fn=func2 20 700 ``` -------------------------------- ### Build and Install Valgrind Source: https://valgrind.org/docs/manual/dist.readme-android.html Commands to compile Valgrind with parallel jobs and install it into a local directory. ```bash make -j4 make -j4 install DESTDIR=`pwd`/Inst ``` -------------------------------- ### Start valgrind-listener Source: https://valgrind.org/docs/manual/faq.html Command to start the valgrind-listener process to receive logging output over a network socket. ```bash valgrind-listener 12345 ``` -------------------------------- ### Start Valgrind and vgdb on Android Source: https://valgrind.org/docs/manual/manual-core-adv.html Execute these commands on the Android system to start the application under Valgrind and initialize the vgdb server. ```bash valgrind --vgdb-error=0 --vgdb=yes prog # and then in another shell, run: vgdb --port=1234 ``` -------------------------------- ### Initialize Android environment directory Source: https://valgrind.org/docs/manual/dist.readme-android_emulator.html Create and navigate to the directory where Android components will be installed. ```bash mkdir android # or any other place you prefer cd android ``` -------------------------------- ### GDB shadow register validity bits example Source: https://valgrind.org/docs/manual/mc-manual.html Shows how to get validity bits of registers using shadow registers when Valgrind is started with --vgdb-shadow-registers=yes. In this x86 example, eax has all bits undefined (0xffffffff) and ebx is fully defined (0x0). ```gdb (gdb) p /x $eaxs1 $9 = 0xffffffff (gdb) p /x $ebxs1 $10 = 0x0 (gdb) ``` -------------------------------- ### Install JDK, SDK, and NDK Source: https://valgrind.org/docs/manual/dist.readme-android_emulator.html Extract the downloaded archives for the JDK, Android SDK, and Android NDK. ```bash # install jdk tar xzf jdk-7u4-linux-i586.tar.gz # install sdk tar xzf android-sdk_r18-linux.tgz # install ndk tar xjf android-ndk-r8-linux-x86.tar.bz2 ``` -------------------------------- ### Leaf Node Output Example Source: https://valgrind.org/docs/manual/dh-manual.html An example of a leaf node report showing memory allocation statistics and stack trace information. Stack frames starting with '^' are inherited from ancestor nodes. ```text PP 1.1.1.1/2 { Total: 31,460,928 bytes (2.32%, 1,565.9/Minstr) in 262,171 blocks (4.41%, 13.05/Minstr), avg size 120 bytes, avg lifetime 986,406,885.05 instrs (4.91% of program duration) Max: 16,779,136 bytes in 65,543 blocks, avg size 256 bytes At t-gmax: 0 bytes (0%) in 0 blocks (0%), avg size 0 bytes At t-end: 0 bytes (0%) in 0 blocks (0%), avg size 0 bytes Reads: 5,964,704 bytes (0.11%, 296.88/Minstr), 0.19/byte Writes: 10,487,200 bytes (0.51%, 521.98/Minstr), 0.33/byte Allocated at { ^1: 0x95CACC9: alloc (alloc.rs:72) ^2: 0x95CACC9: alloc (alloc.rs:148) ^3: 0x95CACC9: reserve_internal (raw_vec.rs:669) ^4: 0x95CACC9: reserve (raw_vec.rs:492) ^5: 0x95CACC9: reserve (vec.rs:460) ^6: 0x95CACC9: push (vec.rs:989) ^7: 0x95CACC9: parse_token_trees_until_close_delim (tokentrees.rs:27) ^8: 0x95CACC9: syntax::parse::lexer::tokentrees::>::parse_token_tree (tokentrees.rs:81) ^9: 0x95CAC39: parse_token_trees_until_close_delim (tokentrees.rs:26) ^10: 0x95CAC39: syntax::parse::lexer::tokentrees::>::parse_token_tree (tokentrees.rs:81) #11: 0x95CAC39: parse_token_trees_until_close_delim (tokentrees.rs:26) #12: 0x95CAC39: syntax::parse::lexer::tokentrees::>::parse_token_tree (tokentrees.rs:81) } } ``` -------------------------------- ### DRD data race report example (valgrind --tool=drd --read-var-info=yes drd/tests/rwlock_race) Source: https://valgrind.org/docs/manual/drd-manual.html Example output from DRD when a data race is detected. The report shows the conflicting load, the call stack, the allocation context, and the other segment's start and end. The process ID is shown in the left column, and the thread ID is given in the first line. The report includes the start address and size of the conflicting access, and the call stack includes file names and line numbers when compiled with debug information. ```text $ valgrind --tool=drd --read-var-info=yes drd/tests/rwlock_race ... ==9466== Thread 3: ==9466== Conflicting load by thread 3 at 0x006020b8 size 4 ==9466== at 0x400B6C: thread_func (rwlock_race.c:29) ==9466== by 0x4C291DF: vg_thread_wrapper (drd_pthread_intercepts.c:186) ==9466== by 0x4E3403F: start_thread (in /lib64/libpthread-2.8.so) ==9466== by 0x53250CC: clone (in /lib64/libc-2.8.so) ==9466== Location 0x6020b8 is 0 bytes inside local var "s_racy" ==9466== declared at rwlock_race.c:18, in frame #0 of thread 3 ==9466== Other segment start (thread 2) ==9466== at 0x4C2847D: pthread_rwlock_rdlock* (drd_pthread_intercepts.c:813) ==9466== by 0x400B6B: thread_func (rwlock_race.c:28) ==9466== by 0x4C291DF: vg_thread_wrapper (drd_pthread_intercepts.c:186) ==9466== by 0x4E3403F: start_thread (in /lib64/libpthread-2.8.so) ==9466== by 0x53250CC: clone (in /lib64/libc-2.8.so) ==9466== Other segment end (thread 2) ==9466== at 0x4C28B54: pthread_rwlock_unlock* (drd_pthread_intercepts.c:912) ==9466== by 0x400B84: thread_func (rwlock_race.c:30) ==9466== by 0x4C291DF: vg_thread_wrapper (drd_pthread_intercepts.c:186) ==9466== by 0x4E3403F: start_thread (in /lib64/libpthread-2.8.so) ==9466== by 0x53250CC: clone (in /lib64/libc-2.8.so) ... ``` -------------------------------- ### Basic Block Vector Output Format Source: https://valgrind.org/docs/manual/bbv-manual.html Example of the BBV output file format where each line starting with 'T' represents an interval containing colon-separated basic block ID and frequency pairs. ```text T:45:1024 :189:99343 T:11:78573 :15:1353 :56:1 T:18:45 :12:135353 :56:78 314:4324263 ``` -------------------------------- ### Name Compression Example Source: https://valgrind.org/docs/manual/cl-format.html Demonstrates using integer IDs to map and reference file and function names to reduce file size. ```text # callgrind format events: Instructions fl=(1) file1.c fn=(1) main 16 20 cfn=(2) func1 calls=1 50 16 400 cfi=(2) file2.c cfn=(3) func2 calls=3 20 16 400 fn=(2) 51 100 cfi=(2) cfn=(3) calls=2 20 51 300 fl=(2) fn=(3) 20 700 ``` -------------------------------- ### Configure Valgrind for MIPS cross-compilation Source: https://valgrind.org/docs/manual/dist.readme-mips.html Example configure command for cross-compiling Valgrind for MIPS. Use --host=mipsel-linux-gnu when building on a non-MIPS platform, and set --prefix to the desired installation directory. ```shell $ ./configure --host=mipsel-linux-gnu --prefix= ``` -------------------------------- ### Example Progress Output Source: https://valgrind.org/docs/manual/manual-core.html Sample output generated when --progress-interval is set to 10, showing real-time metrics like CPU usage and event checks. ```text PROGRESS: U 110s, W 113s, 97.3% CPU, EvC 414.79M, TIn 616.7k, TOut 0.5k, #thr 67 PROGRESS: U 120s, W 124s, 96.8% CPU, EvC 505.27M, TIn 636.6k, TOut 3.0k, #thr 64 PROGRESS: U 130s, W 134s, 97.0% CPU, EvC 574.90M, TIn 657.5k, TOut 3.0k, #thr 63 ``` -------------------------------- ### Install Valgrind from ports Source: https://valgrind.org/docs/manual/dist.readme-freebsd.html Build and install Valgrind from the FreeBSD ports tree. This command compiles and installs the devel/valgrind port. ```shell cd /usr/ports/devel/valgrind && make install clean ```