PS C:\Users\penta> size -h Usage: D:\software\mingw-w64\bin\size.exe [option(s)] [file(s)] Displays the sizes of sections inside binary files If no input file(s) are specified, a.out is assumed The options are: -A|-B --format={sysv|berkeley} Select output style (default is berkeley) -o|-d|-x --radix={8|10|16} Display numbers in octal, decimal or hex -t --totals Display the total sizes (Berkeley only) --common Display total size for *COM* syms --target=<bfdname> Set the binary file format @<file> Read options from <file> -h --help Display this information -v --version Display the program's version
PS D:\Desktop> nm hello.o 0000000000000000 b .bss 0000000000000000 d .ctors 0000000000000000 d .data 0000000000000000 p .pdata 0000000000000000 r .rdata 0000000000000000 r .rdata$.refptr._ZSt4cout 0000000000000000 r .rdata$zzz 0000000000000000 R .refptr._ZSt4cout 0000000000000000 t .text 0000000000000000 r .xdata U __main 000000000000002b t __tcf_0 0000000000000082 t _GLOBAL__sub_I_main 0000000000000046 t _Z41__static_initialization_and_destruction_0ii U _ZNSt8ios_base4InitC1Ev U _ZNSt8ios_base4InitD1Ev U _ZSt4cout 0000000000000000 r _ZStL19piecewise_construct 0000000000000000 b _ZStL8__ioinit U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc U atexit 0000000000000000 T main
PS D:\Desktop\test> nm test.o | findstr "Base" 0000000000000000 r .rdata$_ZTI4Base 0000000000000000 r .rdata$_ZTS4Base 0000000000000000 r .rdata$_ZTV4Base 00000000000000a3 t _GLOBAL__sub_I__ZN4Base5fun_bEv 0000000000000000 T _ZN4Base5fun_bEv 0000000000000026 T _ZN4Base5fun_cEv 0000000000000000 R _ZTI4Base 0000000000000000 R _ZTS4Base 0000000000000000 R _ZTV4Base PS D:\Desktop\test> nm .\libtest.a | findstr "Base" 0000000000000000 r .rdata$_ZTI4Base 0000000000000000 r .rdata$_ZTS4Base 0000000000000000 r .rdata$_ZTV4Base 00000000000000a3 t _GLOBAL__sub_I__ZN4Base5fun_bEv 0000000000000000 T _ZN4Base5fun_bEv 0000000000000026 T _ZN4Base5fun_cEv 0000000000000000 R _ZTI4Base 0000000000000000 R _ZTS4Base 0000000000000000 R _ZTV4Base PS D:\Desktop\test> nm main.o | findstr "Base" 0000000000000000 p .pdata$_ZN4BaseC1Ev 0000000000000000 r .rdata$.refptr._ZTV4Base 0000000000000000 R .refptr._ZTV4Base 0000000000000000 t .text$_ZN4BaseC1Ev 0000000000000000 r .xdata$_ZN4BaseC1Ev U _ZN4Base5fun_bEv 0000000000000000 T _ZN4BaseC1Ev U _ZTV4Base PS D:\Desktop\test> nm test.exe | findstr "Base" 000000000040506c p .pdata$_ZN4BaseC1Ev 0000000000404340 r .rdata$.refptr._ZTV4Base 00000000004044e0 r .rdata$_ZTI4Base 00000000004044f0 r .rdata$_ZTS4Base 0000000000404500 r .rdata$_ZTV4Base 0000000000404340 R .refptr._ZTV4Base 0000000000402d90 t .text$_ZN4BaseC1Ev 0000000000406078 r .xdata$_ZN4BaseC1Ev 0000000000400000 A __ImageBase 00000000004029e0 T _GetPEImageBase 00000000004016a3 t _GLOBAL__sub_I__ZN4Base5fun_bEv 00000000004027b0 T _ValidateImageBase 0000000000402790 t _ValidateImageBase.part.0 0000000000401600 T _ZN4Base5fun_bEv 0000000000401626 T _ZN4Base5fun_cEv 0000000000402d90 T _ZN4BaseC1Ev 00000000004044e0 R _ZTI4Base 00000000004044f0 R _ZTS4Base 0000000000404500 R _ZTV4Base PS D:\Desktop\test>
jam@jam-S1-Pro-Series:~/Desktop/test$ nm test.o U __cxa_atexit U __dso_handle U _GLOBAL_OFFSET_TABLE_ 00000000000000ae t _GLOBAL__sub_I__ZN4Base5fun_bEv 0000000000000058 t _Z41__static_initialization_and_destruction_0ii 0000000000000000 T _ZN4Base5fun_bEv 000000000000002c T _ZN4Base5fun_cEv U _ZNSt8ios_base4InitC1Ev U _ZNSt8ios_base4InitD1Ev U _ZSt4cout 0000000000000000 b _ZStL8__ioinit U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc 0000000000000000 V _ZTI4Base 0000000000000000 V _ZTS4Base 0000000000000000 V _ZTV4Base U _ZTVN10__cxxabiv117__class_type_infoE jam@jam-S1-Pro-Series:~/Desktop/test$ nm libtest.a test.o: U __cxa_atexit U __dso_handle U _GLOBAL_OFFSET_TABLE_ 00000000000000ae t _GLOBAL__sub_I__ZN4Base5fun_bEv 0000000000000058 t _Z41__static_initialization_and_destruction_0ii 0000000000000000 T _ZN4Base5fun_bEv 000000000000002c T _ZN4Base5fun_cEv U _ZNSt8ios_base4InitC1Ev U _ZNSt8ios_base4InitD1Ev U _ZSt4cout 0000000000000000 b _ZStL8__ioinit U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc 0000000000000000 V _ZTI4Base 0000000000000000 V _ZTS4Base 0000000000000000 V _ZTV4Base U _ZTVN10__cxxabiv117__class_type_infoE jam@jam-S1-Pro-Series:~/Desktop/test$ nm main.o U __cxa_atexit U __dso_handle U _GLOBAL_OFFSET_TABLE_ 00000000000000a4 t _GLOBAL__sub_I_main 0000000000000000 T main U __stack_chk_fail 000000000000004e t _Z41__static_initialization_and_destruction_0ii U _ZN4Base5fun_bEv 0000000000000000 W _ZN4BaseC1Ev 0000000000000000 W _ZN4BaseC2Ev 0000000000000000 n _ZN4BaseC5Ev U _ZNSt8ios_base4InitC1Ev U _ZNSt8ios_base4InitD1Ev 0000000000000000 b _ZStL8__ioinit U _ZTV4Base jam@jam-S1-Pro-Series:~/Desktop/test$ nm test 000000000000038c r __abi_tag 0000000000004010 B __bss_start 0000000000004150 b completed.0 U __cxa_atexit@GLIBC_2.2.5 w __cxa_finalize@GLIBC_2.2.5 0000000000004000 D __data_start 0000000000004000 W data_start 00000000000010f0 t deregister_tm_clones 0000000000001160 t __do_global_dtors_aux 0000000000003d68 d __do_global_dtors_aux_fini_array_entry 0000000000004008 D __dso_handle 0000000000003d98 d _DYNAMIC 0000000000004010 D _edata 0000000000004158 B _end 000000000000134c T _fini 00000000000011a0 t frame_dummy 0000000000003d50 d __frame_dummy_init_array_entry 0000000000002210 r __FRAME_END__ 0000000000003f98 d _GLOBAL_OFFSET_TABLE_ 000000000000124d t _GLOBAL__sub_I_main 0000000000001332 t _GLOBAL__sub_I__ZN4Base5fun_bEv w __gmon_start__ 000000000000201c r __GNU_EH_FRAME_HDR 0000000000001000 T _init 0000000000002000 R _IO_stdin_used w _ITM_deregisterTMCloneTable w _ITM_registerTMCloneTable U __libc_start_main@GLIBC_2.34 00000000000011a9 T main 0000000000001120 t register_tm_clones U __stack_chk_fail@GLIBC_2.4 00000000000010c0 T _start 0000000000004010 D __TMC_END__ 00000000000011f7 t _Z41__static_initialization_and_destruction_0ii 00000000000012dc t _Z41__static_initialization_and_destruction_0ii 0000000000001284 T _ZN4Base5fun_bEv 00000000000012b0 T _ZN4Base5fun_cEv 0000000000001266 W _ZN4BaseC1Ev 0000000000001266 W _ZN4BaseC2Ev U _ZNSt8ios_base4InitC1Ev@GLIBCXX_3.4 U _ZNSt8ios_base4InitD1Ev@GLIBCXX_3.4 0000000000004040 B _ZSt4cout@GLIBCXX_3.4 0000000000004151 b _ZStL8__ioinit 0000000000004152 b _ZStL8__ioinit U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@GLIBCXX_3.4 0000000000003d88 V _ZTI4Base 0000000000002013 V _ZTS4Base 0000000000003d70 V _ZTV4Base U _ZTVN10__cxxabiv117__class_type_infoE@CXXABI_1.3 jam@jam-S1-Pro-Series:~/Desktop/test$
jam@jam-S1-Pro-Series:~/Desktop/test$ readelf test.o Usage: readelf <option(s)> elf-file(s) Display information about the contents of ELF format files Options are: -a --all Equivalent to: -h -l -S -s -r -d -V -A -I -h --file-header Display the ELF file header -l --program-headers Display the program headers --segments An alias for --program-headers -S --section-headers Display the sections' header --sections An alias for --section-headers -g --section-groups Display the section groups -t --section-details Display the section details -e --headers Equivalent to: -h -l -S -s --syms Display the symbol table --symbols An alias for --syms --dyn-syms Display the dynamic symbol table --lto-syms Display LTO symbol tables --sym-base=[0|8|10|16] Force base for symbol sizes. The options are mixed (the default), octal, decimal, hexadecimal. -C --demangle[=STYLE] Decode mangled/processed symbol names STYLE can be "none", "auto", "gnu-v3", "java", "gnat", "dlang", "rust" --no-demangle Do not demangle low-level symbol names. (default) --recurse-limit Enable a demangling recursion limit. (default) --no-recurse-limit Disable a demangling recursion limit -U[dlexhi] --unicode=[default|locale|escape|hex|highlight|invalid] Display unicode characters as determined by the current locale (default), escape sequences, "<hex sequences>", highlighted escape sequences, or treat them as invalid and display as "{hex sequences}" -n --notes Display the core notes (if present) -r --relocs Display the relocations (if present) -u --unwind Display the unwind info (if present) -d --dynamic Display the dynamic section (if present) -V --version-info Display the version sections (if present) -A --arch-specific Display architecture specific information (if any) -c --archive-index Display the symbol/file index in an archive -D --use-dynamic Use the dynamic section info when displaying symbols -L --lint|--enable-checks Display warning messages for possible problems -x --hex-dump=<number|name> Dump the contents of section <number|name> as bytes -p --string-dump=<number|name> Dump the contents of section <number|name> as strings -R --relocated-dump=<number|name> Dump the relocated contents of section <number|name> -z --decompress Decompress section before dumping it -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline, f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc, m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline, s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges, U/=trace_info] Display the contents of DWARF debug sections -wk --debug-dump=links Display the contents of sections that link to separate debuginfo files -P --process-links Display the contents of non-debug sections in separate debuginfo files. (Implies -wK) -wK --debug-dump=follow-links Follow links to separate debug info files (default) -wN --debug-dump=no-follow-links Do not follow links to separate debug info files --dwarf-depth=N Do not display DIEs at depth N or greater --dwarf-start=N Display DIEs starting at offset N --ctf=<number|name> Display CTF info from section <number|name> --ctf-parent=<name> Use CTF archive member <name> as the CTF parent --ctf-symbols=<number|name> Use section <number|name> as the CTF external symtab --ctf-strings=<number|name> Use section <number|name> as the CTF external strtab -I --histogram Display histogram of bucket list lengths -W --wide Allow output width to exceed 80 characters -T --silent-truncation If a symbol name is truncated, do not add [...] suffix @<file> Read options from <file> -H --help Display this information -v --version Display the version number of readelf
如上,编译的时候,添加动态库路径到可执行文件中,本来是想在编译动态库的时候修改静态变量的,但发现export的修改,只是针对执行的bash,所以在make执行以后,LD_LIBRARY_PATH又回归原样了,不过本身也不想进行永久修改,所以使用添加编译参数的办法。另外就是在执行make的时候出现,make: 'test' is up to date的提示,这时候需要进行make
clean操作,然后重新make就可以了:
PS D:\Desktop\test> gdb test GNU gdb (GDB) 8.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-w64-mingw32". 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 test...done. (gdb)
好,成功进入,然后设置断点,开始调试:
1 2 3 4 5 6 7 8 9 10 11 12
(gdb) b main.cpp:6 Breakpoint 1 at 0x401639: file main.cpp, line 6. (gdb) r Starting program: D:\Desktop\test\test.exe [New Thread 6392.0x5bd0] [New Thread 6392.0x2630] [New Thread 6392.0x2330] [New Thread 6392.0x418c]
Thread 1 hit Breakpoint 1, main () at main.cpp:6 6 a.fun_b(); (gdb)
Thread 1 hit Breakpoint 1, main () at main.cpp:6 6 a.fun_b(); (gdb) step Base::fun_b (this=0x61fe10) at test.cpp:4 4 cout << "aaa\n"; (gdb)
1 2 3 4 5 6 7 8 9 10 11 12 13
(gdb) continue Continuing. [New Thread 6392.0x19fc] aaa [Thread 6392.0x19fc exited with code 0] [Thread 6392.0x418c exited with code 0] [Thread 6392.0x2330 exited with code 0] [Thread 6392.0x2630 exited with code 0] [Inferior 1 (process 6392) exited normally] (gdb) next The program is not being run. (gdb) quit PS D:\Desktop\test>
(gdb) break main.cpp:6 Breakpoint 1 at 0x401639: file main.cpp, line 6. (gdb) r Starting program: D:\Desktop\test\test.exe [New Thread 20248.0x3174] [New Thread 20248.0x59ac] [New Thread 20248.0xa00] [New Thread 20248.0x4ae0]
Thread 1 hit Breakpoint 1, main () at main.cpp:6 6 a.fun_b(); (gdb) backtrace #0 main () at main.cpp:6 (gdb) step Base::fun_b (this=0x61fe10) at test.cpp:4 4 cout << "aaa\n"; (gdb) backtrace #0 Base::fun_b (this=0x61fe10) at test.cpp:4 #1 0x0000000000401645 in main () at main.cpp:6 (gdb)
Thread 1 hit Breakpoint 2, main () at main.cpp:5 5 Base a; (gdb) print a $2 = {_vptr.Base = 0x10b19a0, a = 16}
打印变量或寄存器值的是print,info则是查看断点信息,下面可以添加了那么些个断点,然后使用delet来永久删除。如果print要打印的信息过长,命令默认显示不全,那就可以对其进行set print element 0设置。下面展示info使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
(gdb) info break Num Type Disp Enb Address What 1 breakpoint keep y 0x0000000000401639 in main() at main.cpp:6 breakpoint already hit 1 time (gdb) break main.cpp:5 Breakpoint 2 at 0x40162d: file main.cpp, line 5. (gdb) info break Num Type Disp Enb Address What 1 breakpoint keep y 0x0000000000401639 in main() at main.cpp:6 breakpoint already hit 1 time 2 breakpoint keep y 0x000000000040162d in main() at main.cpp:5 (gdb) delet breakpoint 1 (gdb) info break Num Type Disp Enb Address What 2 breakpoint keep y 0x000000000040162d in main() at main.cpp:5 (gdb)
jam@jam-S1-Pro-Series:~/Desktop/test$ g++ coredump.cpp -o test coredump.cpp: In function ‘void test()’: coredump.cpp:4:15: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] 4 | char *p = "abaaba"; | ^~~~~~~~ coredump.cpp:5:13: warning: ‘void operator delete(void*, std::size_t)’ called on a pointer to an unallocated object ‘"abaaba"’ [-Wfree-nonheap-object] 5 | delete(p); | ^ coredump.cpp:5:12: note: assigned here 5 | delete(p); | ~^~ jam@jam-S1-Pro-Series:~/Desktop/test$ ./test free(): invalid pointer Aborted (core dumped) jam@jam-S1-Pro-Series:~/Desktop/test$ ls core coredump.cpp test jam@jam-S1-Pro-Series:~/Desktop/test$
jam@jam-S1-Pro-Series:~/Desktop/test$ gdb test core GNU gdb (Ubuntu 12.1-3ubuntu2) 12.1 Copyright (C) 2022 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: <https://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 test... [New LWP 9563] This GDB supports auto-downloading debuginfo from the following URLs: https://debuginfod.ubuntu.com Enable debuginfod for this session? (y or [n]) y Debuginfod has been enabled. To make this setting permanent, add 'set debuginfod enabled on' to .gdbinit. Downloading 6.01 MB separate debug info for /lib/x86_64-linux-gnu/libstdc++.so.6 Downloading 0.17 MB separate debug info for /lib/x86_64-linux-gnu/libgcc_s.so.1 [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Core was generated by `./test'. Program terminated with signal SIGABRT, Aborted. #0 __pthread_kill_implementation (no_tid=0, signo=6, threadid=<optimized out>) at ./nptl/pthread_kill.c:44 Download failed: Invalid argument. Continuing without source file ./nptl/./nptl/pthread_kill.c. 44 ./nptl/pthread_kill.c: No such file or directory.
这就算进入了core的调试界面了,然后输出一下当前堆栈:
1 2 3 4 5 6 7 8 9 10 11 12
(gdb) bt #0 __pthread_kill_implementation (no_tid=0, signo=6, threadid=<optimized out>) at ./nptl/pthread_kill.c:44 #1 __pthread_kill_internal (signo=6, threadid=<optimized out>) at ./nptl/pthread_kill.c:78 #2 __GI___pthread_kill (threadid=<optimized out>, signo=signo@entry=6) at ./nptl/pthread_kill.c:89 #3 0x00007f4b2683bc46 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26 #4 0x00007f4b268227fc in __GI_abort () at ./stdlib/abort.c:79 #5 0x00007f4b268850be in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7f4b269b95dc "%s\n") at ../sysdeps/posix/libc_fatal.c:155 #6 0x00007f4b2689c3fc in malloc_printerr (str=str@entry=0x7f4b269b7234 "free(): invalid pointer") at ./malloc/malloc.c:5660 #7 0x00007f4b2689e26c in _int_free (av=<optimized out>, p=<optimized out>, have_lock=have_lock@entry=0) at ./malloc/malloc.c:4435 #8 0x00007f4b268a09e3 in __GI___libc_free (mem=<optimized out>) at ./malloc/malloc.c:3385 #9 0x0000555e50af21b6 intest () at coredump.cpp:5 #10 0x0000555e50af21c6 in main () at coredump.cpp:9
jam@jam-S1-Pro-Series:~/Desktop/test$ netstat -lnpt | grep helloserver (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp 0 0 0.0.0.0:8090 0.0.0.0:* LISTEN 10362/./helloserver jam@jam-S1-Pro-Series:~/Desktop/test$ netstat -lnpt | grep 8090 (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp 0 0 0.0.0.0:8090 0.0.0.0:* LISTEN 10362/./helloserver jam@jam-S1-Pro-Series:~/Desktop/test$ ps -ef | grep helloserver jam 10362 5746 0 18:11 pts/0 00:00:00 ./helloserver 8090 jam 10580 10363 0 18:15 pts/1 00:00:00 grep --color=auto helloserver jam@jam-S1-Pro-Series:~/Desktop/test$