おもにLinux上でのデバッグに役にたちそうなツールのまとめ


メモリ調査

valgrind

valgrindにはいくつかのモードがある。デフォルトはmemcheck。

memcheck

メモリリークなどを調べる 使い方

# valgrind <prog>
optionなど
 --log-file=<file>    ログファイル指定
 --leak-check=full    leak checkのレベル(default:summary)
 --show-reachable=yes

massif

ヒープの状態を調べる 使い方

# valgrind --tool=massif <prog>
optionなd
 --depth=10           調べるネストの深さ指定

readelf (,objdump)

プロセス起動時のメモリマッピングを調べる 使い方

# readelf -a <prog>  全情報表示
# readelf -S <prog>  セクション情報表示
# readelf -s <prog>  シンボル情報表示

# readelf -v
GNU readelf 2.15
Copyright 2004 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License.  This program has absolutely no warranty.
# readelf -S mgen
There are 37 section headers, starting at offset 0xaf874:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .interp           PROGBITS        08048134 000134 000013 00   A  0   0  1
  [ 2] .note.ABI-tag     NOTE            08048148 000148 000020 00   A  0   0  4
  [ 3] .hash             HASH            08048168 000168 0004b8 04   A  4   0  4
  [ 4] .dynsym           DYNSYM          08048620 000620 000a90 10   A  5   1  4
  [ 5] .dynstr           STRTAB          080490b0 0010b0 000c92 00   A  0   0  1
  [ 6] .gnu.version      VERSYM          08049d42 001d42 000152 02   A  4   0  2
  [ 7] .gnu.version_r    VERNEED         08049e94 001e94 000100 00   A  5   5  4
  [ 8] .rel.dyn          REL             08049f94 001f94 000290 08   A  4   0  4
  [ 9] .rel.plt          REL             0804a224 002224 000320 08   A  4   b  4
  [10] .init             PROGBITS        0804a544 002544 000019 00  AX  0   0  1
  [11] .plt              PROGBITS        0804a560 002560 000650 04  AX  0   0  4
  [12] .text             PROGBITS        0804abb0 002bb0 016590 00  AX  0   0 16
  [13] .fini             PROGBITS        08061140 019140 000020 00  AX  0   0  1
  [14] .rodata           PROGBITS        08061160 019160 007a51 00   A  0   0 32
  [15] .eh_frame_hdr     PROGBITS        08068bb4 020bb4 0008d4 00   A  0   0  4
  [16] .data             PROGBITS        0806a4a0 0214a0 0005d0 00  WA  0   0 32
  [17] .eh_frame         PROGBITS        0806aa70 021a70 0026bc 00   A  0   0  4
  [18] .gcc_except_table PROGBITS        0806d12c 02412c 000357 00   A  0   0  1
  [19] .dynamic          DYNAMIC         0806d484 024484 0000f0 08  WA  5   0  4
  [20] .ctors            PROGBITS        0806d574 024574 000014 00  WA  0   0  4
  [21] .dtors            PROGBITS        0806d588 024588 000008 00  WA  0   0  4
  [22] .jcr              PROGBITS        0806d590 024590 000004 00  WA  0   0  4
  [23] .got              PROGBITS        0806d594 024594 00029c 04  WA  0   0  4
  [24] .bss              NOBITS          0806d840 024840 004664 00  WA  0   0 32
  [25] .comment          PROGBITS        00000000 024840 0003c8 00      0   0  1
  [26] .debug_aranges    PROGBITS        00000000 024c08 0003d0 00      0   0  8
  [27] .debug_pubnames   PROGBITS        00000000 024fd8 003c50 00      0   0  1
  [28] .debug_info       PROGBITS        00000000 028c28 06bd00 00      0   0  1
  [29] .debug_abbrev     PROGBITS        00000000 094928 005834 00      0   0  1
  [30] .debug_line       PROGBITS        00000000 09a15c 006680 00      0   0  1
  [31] .debug_frame      PROGBITS        00000000 0a07dc 0031e8 00      0   0  4
  [32] .debug_str        PROGBITS        00000000 0a39c4 00bbd4 01  MS  0   0  1
  [33] .debug_ranges     PROGBITS        00000000 0af598 000180 00      0   0  1
  [34] .shstrtab         STRTAB          00000000 0af718 000159 00      0   0  1
  [35] .symtab           SYMTAB          00000000 0afe3c 003350 10     36  c4  4
  [36] .strtab           STRTAB          00000000 0b318c 005702 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)
# readelf -s mgen
Symbol table '.dynsym' contains 169 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 00000000    50 FUNC    GLOBAL DEFAULT  UND freeaddrinfo@GLIBC_2.0 (2)
     2: 0806aa38    20 OBJECT  WEAK   DEFAULT   16 _ZTV9ProtoPipe
     3: 00000000    88 FUNC    GLOBAL DEFAULT  UND ferror@GLIBC_2.0 (2)
     4: 0806a800    20 OBJECT  WEAK   DEFAULT   16 _ZTVN10ProtoTimer13LISTEN
     5: 00000000   153 FUNC    GLOBAL DEFAULT  UND strncat@GLIBC_2.0 (2)
     6: 00000000   359 FUNC    GLOBAL DEFAULT  UND strchr@GLIBC_2.0 (2)
     7: 00000000    88 FUNC    GLOBAL DEFAULT  UND feof@GLIBC_2.0 (2)
     8: 0804a5c0    56 FUNC    GLOBAL DEFAULT  UND __cxa_pure_virtual@CXXABI_1.2 (3)
     9: 0806a6c8    20 OBJECT  WEAK   DEFAULT   16 _ZTVN10ProtoTimer8Listene
                   .
                   .

Symbol table '.symtab' contains 821 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 08048134     0 SECTION LOCAL  DEFAULT    1
     2: 08048148     0 SECTION LOCAL  DEFAULT    2
                   .
                   .
   208: 0804d84d     0 FUNC    GLOBAL HIDDEN   12 __i686.get_pc_thunk.cx
   209: 0804cdba   140 FUNC    GLOBAL DEFAULT   12 _ZN14MgenStreamSink5Close
   210: 0804d184    58 FUNC    GLOBAL DEFAULT   12 _ZN16MgenStreamSourceC2ER
   211: 08055544   108 FUNC    GLOBAL DEFAULT   12 _ZNK8MgenFlow16GetCurrent
   212: 080544e4   261 FUNC    GLOBAL DEFAULT   12 _ZN8MgenFlowD1Ev
   213: 0806aa38    20 OBJECT  WEAK   DEFAULT   16 _ZTV9ProtoPipe
   214: 0804c2f6   481 FUNC    GLOBAL DEFAULT   12 _ZN7MgenApp18GetInterface
   215: 080543de   261 FUNC    GLOBAL DEFAULT   12 _ZN8MgenFlowD2Ev
   216: 0805f05e   122 FUNC    GLOBAL DEFAULT   12 _ZN15ProtoDispatcher12Res
   217: 080521ec    52 FUNC    GLOBAL DEFAULT   12 _ZN13MgenBaseEvent21GetSt
   218: 0805cd36   184 FUNC    GLOBAL DEFAULT   12 _ZN11ProtoSocket12SetFlow
   219: 08068a12    11 OBJECT  WEAK   DEFAULT   14 _ZTS9ProtoPipe
   220: 00000000    88 FUNC    GLOBAL DEFAULT  UND ferror@@GLIBC_2.0
   221: 0806a8d4     8 OBJECT  WEAK   DEFAULT   16 _ZTI11ProtoSocket

pmap

プロセスにマッピングされているメモリを調べる procps-3.Xに含まれる。 使用法

# pmap <PID>

# pmap -V
pmap (procps version 3.2.7)
# pmap 5686
5686:   /usr/sbin/named -u bind
0f74a000     44K r-x--  /lib/libnss_files-2.3.6.so
0f755000     60K -----  /lib/libnss_files-2.3.6.so
0f764000      4K r----  /lib/libnss_files-2.3.6.so
0f765000      4K rwx--  /lib/libnss_files-2.3.6.so
0f776000     40K r-x--  /lib/libnss_nis-2.3.6.so
0f780000     60K -----  /lib/libnss_nis-2.3.6.so
0f78f000      4K r----  /lib/libnss_nis-2.3.6.so
0f790000      4K rwx--  /lib/libnss_nis-2.3.6.so
0f7a1000     32K r-x--  /lib/libnss_compat-2.3.6.so
0f7a9000     60K -----  /lib/libnss_compat-2.3.6.so
0f7b8000      4K r----  /lib/libnss_compat-2.3.6.so
0f7b9000      4K rwx--  /lib/libnss_compat-2.3.6.so
0f7ca000   1456K r-x--  /usr/lib/libdns.so.21.0.1
0f936000     64K -----  /usr/lib/libdns.so.21.0.1
0f946000     24K rwx--  /usr/lib/libdns.so.21.0.1
0f94c000      8K rwx--    [ anon ]
0f95e000     84K r-x--  /usr/lib/libz.so.1.2.3
0f973000     64K -----  /usr/lib/libz.so.1.2.3
0f983000      4K rwx--  /usr/lib/libz.so.1.2.3
0f994000     12K r-x--  /lib/libdl-2.3.6.so
0f997000     60K -----  /lib/libdl-2.3.6.so
0f9a6000      4K r----  /lib/libdl-2.3.6.so
0f9a7000      4K rwx--  /lib/libdl-2.3.6.so
0f9b8000   1196K r-x--  /lib/libc-2.3.6.so
0fae3000     64K -----  /lib/libc-2.3.6.so
0faf3000     24K r----  /lib/libc-2.3.6.so
0faf9000     16K rwx--  /lib/libc-2.3.6.so
0fafd000      8K rwx--    [ anon ]
0fb0f000     60K r-x--  /lib/libpthread-0.10.so
0fb1e000     60K -----  /lib/libpthread-0.10.so
0fb2d000      4K r----  /lib/libpthread-0.10.so
0fb2e000      4K rwx--  /lib/libpthread-0.10.so
0fb2f000    264K rwx--    [ anon ]
0fb81000     76K r-x--  /lib/libnsl-2.3.6.so
0fb94000     64K -----  /lib/libnsl-2.3.6.so
0fba4000      4K r----  /lib/libnsl-2.3.6.so
0fba5000      4K rwx--  /lib/libnsl-2.3.6.so
0fba6000      8K rwx--    [ anon ]
0fbb8000    320K r-x--  /usr/lib/libisc.so.11.1.1
0fc08000      8K rwx--  /usr/lib/libisc.so.11.1.1
0fc0a000      4K rwx--    [ anon ]
0fc1b000     40K r-x--  /usr/lib/libisccc.so.0.2.2
0fc25000     60K -----  /usr/lib/libisccc.so.0.2.2
0fc34000      4K rwx--  /usr/lib/libisccc.so.0.2.2
0fc45000     76K r-x--  /usr/lib/libisccfg.so.1.0.6
0fc58000     64K -----  /usr/lib/libisccfg.so.1.0.6
0fc68000      8K rwx--  /usr/lib/libisccfg.so.1.0.6
0fc6a000      4K rwx--    [ anon ]
0fc7b000     32K r-x--  /usr/lib/libbind9.so.0.0.7
0fc83000     64K -----  /usr/lib/libbind9.so.0.0.7
0fc93000      4K rwx--  /usr/lib/libbind9.so.0.0.7

Link