zl程序教程

您现在的位置是:首页 >  工具

当前栏目

ubuntu下valgrind编译,应用

应用Ubuntu 编译 valgrind
2023-09-11 14:15:46 时间

1.获取代码

2.解压,配置,编译

    $ tar xvf valgrind-3.17.0.tar.bz2
    $ cd valgrind-3.17.0
    $ ./autogen.sh
    $ ./configure
    $ make
    $ sudo make install
caozilong@caozilong-Vostro-3268:~/Workspace/valgrind/valgrind-3.17.0$ ./autogen.sh 
running: aclocal
running: autoheader
running: automake -a
running: autoconf
caozilong@caozilong-Vostro-3268:~/Workspace/valgrind/valgrind-3.17.0$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether to enable maintainer-specific portions of Makefiles... no
checking whether ln -s works... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking how to run the C preprocessor... gcc -E
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking dependency style of g++... gcc3
checking for ranlib... ranlib
checking for gcc-ranlib... /usr/bin/gcc-ranlib
checking for a sed that does not truncate output... /bin/sed
checking for ar... /usr/bin/ar
checking for gcc-ar... /usr/bin/gcc-ar
checking for perl... /usr/bin/perl
checking for gdb... /usr/bin/gdb
checking dependency style of gcc... gcc3
checking for diff -u... yes
checking for gcc option to accept ISO C99... none needed
checking for a supported version of gcc... ok (7)
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for a supported CPU... ok (x86_64)
checking for a 64-bit only build... no
checking for a 32-bit only build... no
checking for a supported OS... ok (linux-gnu)
checking for the kernel version... 2.6 or later (5.12.0-14.2-liquorix-amd64)
checking for 32 bit build support... no
checking for a supported CPU/OS combination... ok (amd64-linux)
checking for use as an inner Valgrind... no
checking for using the undefined behaviour sanitiser... no
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking features.h usability... yes
checking features.h presence... yes
checking for features.h... yes
checking the glibc version... 2.27 family
checking for AT_FDCWD... yes
checking for stpncpy... yes
checking for PTRACE_GETREGS... yes
checking for CLOCK_MONOTONIC... yes
checking for Elf32_Chdr... yes
checking for Elf64_Chdr... yes
checking for pthread_rwlock_t... yes
checking for PTHREAD_MUTEX_ADAPTIVE_NP... yes
checking for PTHREAD_MUTEX_ERRORCHECK_NP... yes
checking for PTHREAD_MUTEX_RECURSIVE_NP... yes
checking for PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP... yes
checking for pthread_mutex_t.__m_kind... no
checking for pthread_mutex_t.__data.__kind... yes
checking if AT_HWCAP contains the altivec indicator... no
checking if AT_HWCAP contains the vsx indicator... no
checking if AT_HWCAP contains the dfp indicator... no
checking if AT_HWCAP contains the arch_2_05 indicator... no
checking if AT_HWCAP contains the arch_2_06 indicator... no
checking if AT_HWCAP contains the arch_2_07 indicator... no
checking if AT_HWCAP contains the arch_3_00 indicator... no
checking if AT_HWCAP contains the arch_3_1 indicator... no
checking if AT_HWCAP contains the htm indicator... no
checking if AT_HWCAP contains the mma indicator... no
checking that assembler knows ISA 2.07 instructions ... no
checking for Altivec support in the compiler ... no
checking for VSX compiler flag support... no
checking for VSX support in the assembler ... no
checking that assembler knows DFP... no
checking that compiler knows -mhard-dfp switch... no
checking that compiler knows DFP datatypes... yes
checking if compiler accepts the -mhtm flag... no
checking if compiler can find the htm builtins... no
checking that assembler knows ISA 3.00 ... no
checking that assembler knows xscvhpdp ... no
checking that assembler knows ISA 3.1 ... no
checking for pthread_create@GLIBC2.0()... no
checking for dlinfo RTLD_DI_TLS_MODID... yes
checking for eventfd()... yes
checking that C++ compiler can include <thread> header file... yes
checking for struct user_regs_struct... yes
checking for __NR_membarrier... yes
checking if gcc accepts -m32... yes
checking if gcc accepts -m64... yes
checking if gcc accepts -march=octeon... no
checking if gcc accepts -march=octeon2... no
checking if gcc accepts -mmsa... no
checking if gcc is producing mips n32 binaries... no
checking if gcc is producing mips n64 binaries... no
checking if gcc accepts -mmmx... yes
checking if gcc accepts -msse... yes
checking if gcc accepts -mpreferred-stack-boundary=2 -m32... yes
checking if gcc accepts -mlong-double-128... yes
checking for using the link time optimisation... no
checking if gcc accepts -Wmemset-transposed-args... yes
checking if gcc accepts -Wnonnull... yes
checking if gcc accepts -Woverflow... yes
checking if gcc accepts -Wpointer-sign... yes
checking if gcc accepts -Wuninitialized... yes
checking if gcc accepts -Wunused-function... yes
checking if gcc accepts -Wstatic-local-in-inline... no
checking if gcc accepts -Wmismatched-new-delete... no
checking if gcc accepts -Winfinite-recursion... no
checking if gcc accepts -Wwrite-strings... yes
checking if gcc accepts -Wempty-body... yes
checking if gcc accepts -Wformat... yes
checking if gcc accepts -Wformat-signedness... yes
checking if gcc accepts -Wcast-qual... yes
checking if gcc accepts -Wold-style-declaration... yes
checking if gcc accepts -Wignored-qualifiers... yes
checking if gcc accepts -Wmissing-parameter-type... yes
checking if gcc accepts -Wlogical-op... yes
checking if gcc accepts -Wenum-conversion... no
checking if gcc accepts -Wimplicit-fallthrough=2... yes
checking if gcc accepts -Wformat-security... yes
checking if gcc accepts -Wextra or -W... -Wextra
checking if g++ accepts -faligned-new... yes
checking if g++ accepts -fsized-deallocation... yes
checking if g++ supports aligned new and delete... yes
checking if gcc accepts -fno-stack-protector... yes
checking if gcc accepts -finline-functions... yes
checking if gcc accepts -fno-ipa-icf... yes
checking if gcc accepts --param inline-unit-growth... yes
checking if gcc accepts -gdwarf-4 -fdebug-types-section... yes
checking if gcc accepts -g -gz=zlib... yes
checking if gcc accepts -g -gz=zlib-gnu... yes
checking if gcc accepts nested functions... yes
checking if gcc accepts the 'p' constraint in asm statements... yes
checking if gcc accepts -pie... yes
checking if gcc accepts -no-pie... yes
checking if the linker accepts -Wl,-Ttext-segment... yes
configure: ld -Ttext-segment used, no need to strip build-id NOTEs.
checking if ppc32/64 as supports mtocrf/mfocrf... no
checking if ppc32/64 asm supports phased out floating point instructions... no
checking if amd64 assembler supports fxsave64/fxrstor64... yes
checking if x86/amd64 assembler speaks SSE3... yes
checking if x86/amd64 assembler speaks SSSE3... yes
checking if x86/amd64 assembler supports 'pclmulqdq'... yes
checking if x86/amd64 assembler supports 'vpclmulqdq'... yes
checking if x86/amd64 assembler supports FMA4 'vfmaddpd'... yes
checking if x86/amd64 assembler supports 'lzcnt'... yes
checking if x86/amd64 assembler supports 'loopnel'... yes
checking if x86/amd64 assembler supports 'addr32'... yes
checking if x86/amd64 assembler speaks SSE4.2... yes
checking if x86/amd64 assembler speaks AVX... yes
checking if x86/amd64 assembler speaks AVX2... yes
checking if x86/amd64 assembler speaks TSX... yes
checking if x86/amd64 assembler speaks BMI1 and BMI2... yes
checking if x86/amd64 assembler speaks FMA... yes
checking if amd64 assembler knows the MPX instructions... no
checking if amd64 assembler knows the ADX instructions... yes
checking if amd64 assembler knows the RDRAND instruction... yes
checking if amd64 assembler knows the RDSEED instruction... yes
checking if amd64 assembler knows the F16C instructions... yes
checking if x86/amd64 assembler knows the MOVBE insn... yes
checking if gcc supports the ifunc attribute... yes
checking if gcc supports the armv8 crc feature flag... no
checking if gcc supports the armv81 feature flag and assembler supports v8.1 instructions... no
checking if gcc supports the armv82 feature flag and assembler supports v8.2 instructions... no
checking for TLS support... yes
checking asm/unistd.h usability... yes
checking asm/unistd.h presence... yes
checking for asm/unistd.h... yes
checking endian.h usability... yes
checking endian.h presence... yes
checking for endian.h... yes
checking mqueue.h usability... yes
checking mqueue.h presence... yes
checking for mqueue.h... yes
checking sys/endian.h usability... no
checking sys/endian.h presence... no
checking for sys/endian.h... no
checking sys/epoll.h usability... yes
checking sys/epoll.h presence... yes
checking for sys/epoll.h... yes
checking sys/eventfd.h usability... yes
checking sys/eventfd.h presence... yes
checking for sys/eventfd.h... yes
checking sys/klog.h usability... yes
checking sys/klog.h presence... yes
checking for sys/klog.h... yes
checking sys/poll.h usability... yes
checking sys/poll.h presence... yes
checking for sys/poll.h... yes
checking sys/prctl.h usability... yes
checking sys/prctl.h presence... yes
checking for sys/prctl.h... yes
checking sys/signal.h usability... yes
checking sys/signal.h presence... yes
checking for sys/signal.h... yes
checking sys/signalfd.h usability... yes
checking sys/signalfd.h presence... yes
checking for sys/signalfd.h... yes
checking sys/syscall.h usability... yes
checking sys/syscall.h presence... yes
checking for sys/syscall.h... yes
checking sys/sysnvl.h usability... no
checking sys/sysnvl.h presence... no
checking for sys/sysnvl.h... no
checking sys/time.h usability... yes
checking sys/time.h presence... yes
checking for sys/time.h... yes
checking for sys/types.h... (cached) yes
checking if <linux/futex.h> is usable... yes
checking for uid_t in sys/types.h... yes
checking for off_t... yes
checking for size_t... yes
checking whether time.h and sys/time.h may both be included... yes
checking for working memcmp... yes
checking for stdlib.h... (cached) yes
checking for unistd.h... (cached) yes
checking for sys/param.h... yes
checking for getpagesize... yes
checking for working mmap... yes
checking for pthread_create in -lpthread... yes
checking for clock_gettime in -lrt... yes
checking for clock_gettime... yes
checking for epoll_create... yes
checking for epoll_pwait... yes
checking for klogctl... yes
checking for mallinfo... yes
checking for memchr... yes
checking for memfd_create... yes
checking for memset... yes
checking for mkdir... yes
checking for mremap... yes
checking for ppoll... yes
checking for pthread_barrier_init... yes
checking for pthread_condattr_setclock... yes
checking for pthread_mutex_timedlock... yes
checking for pthread_rwlock_timedrdlock... yes
checking for pthread_rwlock_timedwrlock... yes
checking for pthread_spin_lock... yes
checking for pthread_yield... yes
checking for pthread_setname_np... yes
checking for readlinkat... yes
checking for semtimedop... yes
checking for signalfd... yes
checking for sigwaitinfo... yes
checking for strchr... yes
checking for strdup... yes
checking for strpbrk... yes
checking for strrchr... yes
checking for strstr... yes
checking for syscall... yes
checking for utimensat... yes
checking for process_vm_readv... yes
checking for process_vm_writev... yes
checking for copy_file_range... yes
checking for preadv... yes
checking for pwritev... yes
checking for preadv2... yes
checking for pwritev2... yes
checking for mpicc... mpicc
checking primary target for usable MPI2-compliant C compiler and mpi.h... no
checking secondary target for usable MPI2-compliant C compiler and mpi.h... no
checking for boost... no
checking for OpenMP... yes
checking for __builtin_popcount()... yes
checking for __builtin_clz()... yes
checking for __builtin_ctz()... yes
checking if gcc supports __sync_add_and_fetch for the primary target... yes
checking if gcc supports __sync_add_and_fetch on uint64_t for all targets... yes
checking if g++ supports __sync_add_and_fetch... yes
checking if libstdc++ supports annotating shared pointers... yes
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating VEX/Makefile
config.status: creating valgrind.spec
config.status: creating valgrind.pc
config.status: creating glibc-2.X.supp
config.status: creating docs/Makefile
config.status: creating tests/Makefile
config.status: creating tests/vg_regtest
config.status: creating perf/Makefile
config.status: creating perf/vg_perf
config.status: creating gdbserver_tests/Makefile
config.status: creating gdbserver_tests/solaris/Makefile
config.status: creating include/Makefile
config.status: creating auxprogs/Makefile
config.status: creating mpi/Makefile
config.status: creating coregrind/Makefile
config.status: creating memcheck/Makefile
config.status: creating memcheck/tests/Makefile
config.status: creating memcheck/tests/common/Makefile
config.status: creating memcheck/tests/amd64/Makefile
config.status: creating memcheck/tests/x86/Makefile
config.status: creating memcheck/tests/linux/Makefile
config.status: creating memcheck/tests/linux/debuginfod-check.vgtest
config.status: creating memcheck/tests/darwin/Makefile
config.status: creating memcheck/tests/solaris/Makefile
config.status: creating memcheck/tests/amd64-linux/Makefile
config.status: creating memcheck/tests/arm64-linux/Makefile
config.status: creating memcheck/tests/x86-linux/Makefile
config.status: creating memcheck/tests/amd64-solaris/Makefile
config.status: creating memcheck/tests/x86-solaris/Makefile
config.status: creating memcheck/tests/ppc32/Makefile
config.status: creating memcheck/tests/ppc64/Makefile
config.status: creating memcheck/tests/s390x/Makefile
config.status: creating memcheck/tests/mips32/Makefile
config.status: creating memcheck/tests/mips64/Makefile
config.status: creating memcheck/tests/vbit-test/Makefile
config.status: creating cachegrind/Makefile
config.status: creating cachegrind/tests/Makefile
config.status: creating cachegrind/tests/x86/Makefile
config.status: creating cachegrind/cg_annotate
config.status: creating cachegrind/cg_diff
config.status: creating callgrind/Makefile
config.status: creating callgrind/callgrind_annotate
config.status: creating callgrind/callgrind_control
config.status: creating callgrind/tests/Makefile
config.status: creating helgrind/Makefile
config.status: creating helgrind/tests/Makefile
config.status: creating drd/Makefile
config.status: creating drd/scripts/download-and-build-splash2
config.status: creating drd/tests/Makefile
config.status: creating massif/Makefile
config.status: creating massif/tests/Makefile
config.status: creating massif/ms_print
config.status: creating dhat/Makefile
config.status: creating dhat/tests/Makefile
config.status: creating lackey/Makefile
config.status: creating lackey/tests/Makefile
config.status: creating none/Makefile
config.status: creating none/tests/Makefile
config.status: creating none/tests/scripts/Makefile
config.status: creating none/tests/amd64/Makefile
config.status: creating none/tests/ppc32/Makefile
config.status: creating none/tests/ppc64/Makefile
config.status: creating none/tests/x86/Makefile
config.status: creating none/tests/arm/Makefile
config.status: creating none/tests/arm64/Makefile
config.status: creating none/tests/s390x/Makefile
config.status: creating none/tests/mips32/Makefile
config.status: creating none/tests/mips64/Makefile
config.status: creating none/tests/nanomips/Makefile
config.status: creating none/tests/linux/Makefile
config.status: creating none/tests/darwin/Makefile
config.status: creating none/tests/solaris/Makefile
config.status: creating none/tests/amd64-linux/Makefile
config.status: creating none/tests/x86-linux/Makefile
config.status: creating none/tests/amd64-darwin/Makefile
config.status: creating none/tests/x86-darwin/Makefile
config.status: creating none/tests/amd64-solaris/Makefile
config.status: creating none/tests/x86-solaris/Makefile
config.status: creating exp-bbv/Makefile
config.status: creating exp-bbv/tests/Makefile
config.status: creating exp-bbv/tests/x86/Makefile
config.status: creating exp-bbv/tests/x86-linux/Makefile
config.status: creating exp-bbv/tests/amd64-linux/Makefile
config.status: creating exp-bbv/tests/ppc32-linux/Makefile
config.status: creating exp-bbv/tests/arm-linux/Makefile
config.status: creating shared/Makefile
config.status: creating solaris/Makefile
config.status: creating coregrind/link_tool_exe_linux
config.status: creating coregrind/link_tool_exe_darwin
config.status: creating coregrind/link_tool_exe_solaris
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands

         Maximum build arch: amd64
         Primary build arch: amd64
       Secondary build arch: 
                   Build OS: linux
     Link Time Optimisation: no
       Primary build target: AMD64_LINUX
     Secondary build target: 
           Platform variant: vanilla
      Primary -DVGPV string: -DVGPV_amd64_linux_vanilla=1
         Default supp files: xfree-3.supp xfree-4.supp glibc-2.X-drd.supp glibc-2.34567-NPTL-helgrind.supp glibc-2.X.supp 

caozilong@caozilong-Vostro-3268:~/Workspace/valgrind/valgrind-3.17.0$ 

 3.安装:

sudo make install

 安装验证:

4.测试:

检测内存泄漏:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
	void *p = malloc(100);

	if(p == NULL)
	{
		return -1;
	}

	memset(p, 0x00, 100);
	return 0;
}

 检测内存泄漏:

caozilong@caozilong-Vostro-3268:~/Workspace/valgrind/xielou$ valgrind ./a.out 
==21136== Memcheck, a memory error detector
==21136== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==21136== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info
==21136== Command: ./a.out
==21136== 
==21136== 
==21136== HEAP SUMMARY:
==21136==     in use at exit: 100 bytes in 1 blocks
==21136==   total heap usage: 1 allocs, 0 frees, 100 bytes allocated
==21136== 
==21136== LEAK SUMMARY:
==21136==    definitely lost: 100 bytes in 1 blocks
==21136==    indirectly lost: 0 bytes in 0 blocks
==21136==      possibly lost: 0 bytes in 0 blocks
==21136==    still reachable: 0 bytes in 0 blocks
==21136==         suppressed: 0 bytes in 0 blocks
==21136== Rerun with --leak-check=full to see details of leaked memory
==21136== 
==21136== For lists of detected and suppressed errors, rerun with: -s
==21136== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
caozilong@caozilong-Vostro-3268:~/Workspace/valgrind/xielou$ 

 可以看到,valgrind检测到一处明显的内存泄漏:

==21136==    definitely lost: 100 bytes in 1 blocks

对应到程序中,确实发生了泄漏.

查看堆栈利用率:

valgrind --tool=drd --show-stack-usage=yes ./a.out
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

//O_DIRECT        
#define __USE_GNU 1
#include <fcntl.h>
#include <stdint.h>
#include <sys/mman.h>
#include <errno.h>

static void write_test(int fdno)
{
	int origin_len;
	int reallen;
	void *mp;

	int len = 1 * 1024 * 1024;
	origin_len = len;

	len = len + (getpagesize() - len%getpagesize());
	printf("%s line %d, len = %d.\n", __func__, __LINE__, len);
	unsigned char *p = malloc(len + getpagesize());
	if(p == NULL)
	{
		printf("%s line %d, malloc failure.\n", __func__, __LINE__);
		return;
	}

	mp = p;
	unsigned char *q = (unsigned char *)(((unsigned long)p + getpagesize())&(~(getpagesize()-1)));
	p = q;
	printf("q = %p, mp = %p.\n", q, mp);
	memset(p, 0x00, len);

	//lseek(fdno, 0, SEEK_SET);
	//int reallen = read(fdno, p,len);
	//printf("%s line %d, reallen %d read failure.er %s\n", __func__, __LINE__, reallen, strerror(errno));
	//printf("%s line %d, fdno = %d\n", __func__, __LINE__, fdno);

	/*lseek(fdno, 0, SEEK_SET);*/
	reallen = write(fdno, p, origin_len);
	printf("%s line %d, reallen = %d.\n", __func__, __LINE__, reallen);
	/*lseek(fdno, 0, SEEK_SET);*/
	free(mp);
	return;
}

static void* write_thread1(void *p)
{
	int fdno;

	fdno = open("./fuck.bin", O_DIRECT|O_RDWR|O_CREAT, 0666);
	/*fdno = open("./fuck.bin", O_RDWR|O_CREAT, 0666);*/
	if(fdno < 0)
	{
		printf("%s line %d, open file failure.\n", __func__, __LINE__);
		return NULL;
	}

	fallocate(fdno, 1, 0, 100*1024*1024);
	/*fallocate(fdno, 0, 0, 100*1024*1024);*/

	/*return NULL;*/
	while(1)
	{
		write_test(fdno);
	}

	close(fdno);
	return NULL;
}

static void* write_thread2(void *p)
{
	int fdno;

	fdno = open("./new.bin", O_DIRECT|O_RDWR|O_CREAT, 0666);
	/*fdno = open("./new.bin", O_RDWR|O_CREAT, 0666);*/
	if(fdno < 0)
	{
		printf("%s line %d, open file failure.\n", __func__, __LINE__);
		return NULL;
	}

	fallocate(fdno, 1, 0, 100*1024*1024);
	/*fallocate(fdno, 0, 0, 100*1024*1024);*/

	/*return NULL;*/
	while(1)
	{
		write_test(fdno);
	}

	close(fdno);
	return NULL;
}
 

int main(int argc,char **argv)
{
	pthread_t t1,t2;

	pthread_create(&t1,0,write_thread1,NULL);
	pthread_create(&t2,0,write_thread2,NULL);

	pthread_join(t1,NULL);
	pthread_join(t2,NULL);
	return 0;
}

堆栈利用率的结论:

5.与GDB交互式DEBUG

valgrind --vgdb=yes --vgdb-error=0 ./a.out

caozilong@caozilong-Vostro-3268:~/Workspace/valgrind/xielou$ valgrind --vgdb=yes --vgdb-error=0 ./a.out
==21460== Memcheck, a memory error detector
==21460== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==21460== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info
==21460== Command: ./a.out
==21460== 
==21460== (action at startup) vgdb me ... 
==21460== 
==21460== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==21460==   /path/to/gdb ./a.out
==21460== and then give GDB the following command
==21460==   target remote | /usr/local/libexec/valgrind/../../bin/vgdb --pid=21460
==21460== --pid is optional if only one valgrind process is running
==21460== 

左边的==21460==表示的是PID.

新建一个终端,GDB链接valgrind命令:

$ gdb ./a.out
(gdb) target remote | vgdb
caozilong@caozilong-Vostro-3268:~/Workspace/valgrind/xielou$ gdb a.out 
GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
(gdb) target remote | vgdb
Remote debugging using | vgdb
relaying data between gdb and process 21507
warning: remote target does not support file transfer, attempting to access files from local filesystem.
Reading symbols from /lib64/ld-linux-x86-64.so.2...Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/ld-2.27.so...done.
done.
0x0000000004001090 in _start () from /lib64/ld-linux-x86-64.so.2
(gdb) b main
Breakpoint 1 at 0x108692: file main.c, line 7.
(gdb) c
Continuing.

Breakpoint 1, main () at main.c:7
7		void *p = malloc(100);
(gdb) 

通过valkyrie(安装方法,sudo apt install valkyrie)和valgrind配合,可以将整个分析过程可视化。


即可!


结束!