<<戻る

2004年9月3日

コマンドの再確認

 

man [section] target

ex)

man --help GNUコマンドは--helpオプションでどのようなコマンドオプションがあるか確認できる。

man -aw man manについてどのセクションにあるか検索

man -aw socket // socketについてどのセクションにあるか検索

man man // manコマンドについてのドキュメントを表示

man 7 socket // socketについてセクション7のドキュメントを表示

 

nm オブジェクトファイルからシンボルをリストする。

ex)

$ nm hello
080493d0 D _DYNAMIC
080494ac D _GLOBAL_OFFSET_TABLE_
080483ac R _IO_stdin_used
         w _Jv_RegisterClasses
0804949c d __CTOR_END__
08049498 d __CTOR_LIST__
080494a4 d __DTOR_END__
080494a0 d __DTOR_LIST__
080493cc d __EH_FRAME_BEGIN__
080493cc d __FRAME_END__
080494a8 d __JCR_END__
080494a8 d __JCR_LIST__
080494c4 A __bss_start
080493c0 D __data_start
08048368 t __do_global_ctors_aux
080482c0 t __do_global_dtors_aux
080493c4 D __dso_handle
         w __gmon_start__
0804835c T __libc_csu_fini
08048350 T __libc_csu_init
         U __libc_start_main@@GLIBC_2.0
080494c4 A _edata
080494c8 A _end
0804838c T _fini
080483a8 R _fp_hw
08048230 T _init
08048278 T _start
0804829c t call_gmon_start
080494c4 b completed.1
080493c0 W data_start
080482fc t frame_dummy
08048328 T main
080493c8 d p.0
         U printf@@GLIBC_2.0
 

C RunTimeを探す

$ nm -g /usr/lib/crt1.o
00000004 R _IO_stdin_used
00000000 D __data_start
         U __libc_csu_fini
         U __libc_csu_init
         U __libc_start_main
00000000 R _fp_hw
00000000 T _start
00000000 W data_start
         U main
 

 

readelf - Display infomation about ELF files

$ readelf -S hello
There are 34 section headers, starting at offset 0x1abc:

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 080480f4 0000f4 000013 00 A 0 0 1
[ 2] .note.ABI-tag NOTE 08048108 000108 000020 00 A 0 0 4
[ 3] .hash HASH 08048128 000128 000028 04 A 4 0 4
[ 4] .dynsym DYNSYM 08048150 000150 000050 10 A 5 1 4
[ 5] .dynstr STRTAB 080481a0 0001a0 00004c 00 A 0 0 1
[ 6] .gnu.version VERSYM 080481ec 0001ec 00000a 02 A 4 0 2
[ 7] .gnu.version_r VERNEED 080481f8 0001f8 000020 00 A 5 1 4
[ 8] .rel.dyn REL 08048218 000218 000008 08 A 4 0 4
[ 9] .rel.plt REL 08048220 000220 000010 08 A 4 b 4
[10] .init PROGBITS 08048230 000230 000018 00 AX 0 0 4
[11] .plt PROGBITS 08048248 000248 000030 04 AX 0 0 4
[12] .text PROGBITS 08048278 000278 000114 00 AX 0 0 4
[13] .fini PROGBITS 0804838c 00038c 00001c 00 AX 0 0 4
[14] .rodata PROGBITS 080483a8 0003a8 000015 00 A 0 0 4
[15] .data PROGBITS 080493c0 0003c0 00000c 00 WA 0 0 4
[16] .eh_frame PROGBITS 080493cc 0003cc 000004 00 WA 0 0 4
[17] .dynamic DYNAMIC 080493d0 0003d0 0000c8 08 WA 5 0 4
[18] .ctors PROGBITS 08049498 000498 000008 00 WA 0 0 4
[19] .dtors PROGBITS 080494a0 0004a0 000008 00 WA 0 0 4
[20] .jcr PROGBITS 080494a8 0004a8 000004 00 WA 0 0 4
[21] .got PROGBITS 080494ac 0004ac 000018 04 WA 0 0 4
[22] .bss NOBITS 080494c4 0004c4 000004 00 WA 0 0 4
[23] .comment PROGBITS 00000000 0004c4 000132 00 0 0 1
[24] .debug_aranges PROGBITS 00000000 0005f8 000058 00 0 0 8
[25] .debug_pubnames PROGBITS 00000000 000650 000025 00 0 0 1
[26] .debug_info PROGBITS 00000000 000675 0009a0 00 0 0 1
[27] .debug_abbrev PROGBITS 00000000 001015 000120 00 0 0 1
[28] .debug_line PROGBITS 00000000 001135 000188 00 0 0 1
[29] .debug_frame PROGBITS 00000000 0012c0 000014 00 0 0 4
[30] .debug_str PROGBITS 00000000 0012d4 0006ba 01 MS 0 0 1
[31] .shstrtab STRTAB 00000000 00198e 00012b 00 0 0 1
[32] .symtab SYMTAB 00000000 00200c 0004a0 10 33 36 4
[33] .strtab STRTAB 00000000 0024ac 0001ea 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)

 

strace - trace system calls and signals

システムコールとシグナルの呼び出しを表示する。つまりどのようにプログラムが動作しているかトレースできる。

$ strace ./hello
execve("./hello", ["./hello"], [/* 35 vars */]) = 0
uname({sys="Linux", node="localhost.localdomain", ...}) = 0
brk(0)                                  = 0x80494c8
open("/etc/ld.so.preload", O_RDONLY)    = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=42850, ...}) = 0
old_mmap(NULL, 42850, PROT_READ, MAP_PRIVATE, 3, 0) = 0x40014000
close(3)                                = 0
open("/lib/i686/libc.so.6", O_RDONLY)   = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0 Z\1B4\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=1288460, ...}) = 0
old_mmap(0x42000000, 1294148, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0x42000000
old_mmap(0x42135000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3, 0x135000) = 0x42135000
old_mmap(0x4213a000, 8004, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x4213a000
close(3)                                = 0
old_mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x4001f000
munmap(0x40014000, 42850)               = 0
fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40014000
write(1, "hello world\n", 12hello world
)           = 12
munmap(0x40014000, 4096)                = 0
_exit(0)                                = ?
 

strings - print the strings of printable characters in files.

一般に、catコマンドで文字化けしてしまうバイナリなどのテキストではないファイルをテキスト形式で表示する.

$ strings hello
/lib/ld-linux.so.2
libc.so.6
printf
_IO_stdin_used
__libc_start_main
__gmon_start__
GLIBC_2.0
PTRh\
QVh(
hello world
 

 

gcc - GNU Compiler Collection

-vはverboseの略称で、gccコマンドの詳細表示

-save-tempsは、プリプロセス、コンパイル、アセンブルの結果ファイルを生成する。

-Wl, はリンカへのオプションを指定するときに使う。プリプロセッサには-Wp, アセンブラには-Wa,が存在する.このオプションはカンマが必要

-Wl,-t トレース

-Wl,--entry=value エントリの変更

-staticは、オブジェクトファイルをstatic linkする。

-oは、オブジェクト名

$ gcc -v -save-temps -Wl,-t -static -o hello-static hello.c
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --host=i386-redhat-linux --with-system-zlib --enable-__cxa_atexit
Thread model: posix
gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
 /usr/lib/gcc-lib/i386-redhat-linux/3.2/cpp0 -lang-c -v -D__GNUC__=3 -D__GNUC_MINOR__=2 -D__GNUC_PATCHLEVEL__=0 -D__GXX_ABI_VERSION=102 -D__ELF__ -Dunix -D__gnu_linux__ -Dlinux -D__ELF__ -D__unix__ -D__gnu_linux__ -D__linux__ -D__unix -D__linux -Asystem=posix -D__NO_INLINE__ -D__STDC_HOSTED__=1 -Acpu=i386 -Amachine=i386 -Di386 -D__i386 -D__i386__ -D__tune_i386__ hello.c hello.i
GNU CPP version 3.2 20020903 (Red Hat Linux 8.0 3.2-7) (cpplib) (i386 Linux/ELF)
ignoring nonexistent directory "/usr/i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc-lib/i386-redhat-linux/3.2/include
 /usr/include
End of search list.
 /usr/lib/gcc-lib/i386-redhat-linux/3.2/cc1 -fpreprocessed hello.i -quiet -dumpbase hello.c -version -o hello.s
GNU CPP version 3.2 20020903 (Red Hat Linux 8.0 3.2-7) (cpplib) (i386 Linux/ELF)
GNU C version 3.2 20020903 (Red Hat Linux 8.0 3.2-7) (i386-redhat-linux)
        compiled by GNU C version 3.2 20020903 (Red Hat Linux 8.0 3.2-7).
 as -V -Qy -o hello.o hello.s
GNU assembler version 2.13.90.0.2 (i386-redhat-linux) using BFD version 2.13.90.0.2 20020802
 /usr/lib/gcc-lib/i386-redhat-linux/3.2/collect2 -m elf_i386 -static -o hello-static /usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../crt1.o /usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../crti.o /usr/lib/gcc-lib/i386-redhat-linux/3.2/crtbeginT.o -L/usr/lib/gcc-lib/i386-redhat-linux/3.2 -L/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../.. -t hello.o -lgcc -lgcc_eh -lc -lgcc -lgcc_eh /usr/lib/gcc-lib/i386-redhat-linux/3.2/crtend.o /usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../crtn.o
/usr/bin/ld: mode elf_i386
/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../crt1.o
/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../crti.o
/usr/lib/gcc-lib/i386-redhat-linux/3.2/crtbeginT.o
hello.o
(/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../libc.a)libc-start.o
(/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../libc.a)check_fds.o
(/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../libc.a)elf-init.o
(/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../libc.a)errno-loc.o
(/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../libc.a)exit.o
(/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../libc.a)cxa_atexit.o
(/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../libc.a)printf.o
(/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../libc.a)libc_fatal.o
(/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../libc.a)stdio.o
(/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../libc.a)malloc.o
..(snip)..
(/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../libc.a)strncase.o
(/usr/lib/gcc-lib/i386-redhat-linux/3.2/libgcc.a)_udivdi3.oS
(/usr/lib/gcc-lib/i386-redhat-linux/3.2/libgcc.a)_umoddi3.oS
/usr/lib/gcc-lib/i386-redhat-linux/3.2/crtend.o
/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../crtn.o
 

static linkであると、/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../libc.a をオブジェクトファイルに組み込んでいるのが分かる。

ここで重要なのがcrt1.oオブジェクトファイルで、これはC RunTimeである。Javaなどと同様ランタイムは存在していてここから始まる。

file - -determin file typs

$ file /usr/lib/libc.a
/usr/lib/libc.a: current ar archive
 

ar - create, modify, and extract from arhcives

使い方はtarコマンドと同様、tarは元々Tape driver ARchivingのために作られた。

$ ar -tvf /usr/lib/libc.a
rw-r--r-- 100/217   1116 Nov  6 05:56 2003 init-first.o
rw-r--r-- 100/217   2052 Nov  6 05:56 2003 libc-start.o
rw-r--r-- 100/217    648 Nov  6 05:56 2003 sysdep.o
rw-r--r-- 100/217   1884 Nov  6 05:56 2003 version.o
rw-r--r-- 100/217   1188 Nov  6 05:56 2003 check_fds.o
rw-r--r-- 100/217    280 Nov  6 05:56 2003 libc-tls.o
rw-r--r-- 100/217    636 Nov  6 05:56 2003 elf-init.o
rw-r--r-- 100/217    528 Nov  6 05:56 2003 dso_handle.o
rw-r--r-- 100/217    486 Nov  6 05:56 2003 errno.o
rw-r--r-- 100/217    680 Nov  6 05:56 2003 errno-loc.o
rw-r--r-- 100/217   1220 Nov  6 05:56 2003 iconv_open.o
rw-r--r-- 100/217   1096 Nov  6 05:56 2003 iconv.o
rw-r--r-- 100/217    640 Nov  6 05:56 2003 iconv_close.o
rw-r--r-- 100/217   3132 Nov  6 05:56 2003 gconv_open.o
...(snip)...
rw-r--r-- 100/217    764 Nov  6 05:56 2003 enbl-secure.o
rw-r--r-- 100/217    704 Nov  6 05:56 2003 dl-profstub.o
rw-r--r-- 100/217   1900 Nov  6 05:56 2003 dl-libc.o
rw-r--r-- 100/217   1612 Nov  6 05:56 2003 dl-sym.o
rw-r--r-- 100/217    664 Nov  6 05:56 2003 dl-tsd.o
 

static link時にはこの/usr/lib/lic.aがプログラムに組み込まれるので、ファイルサイズが肥大化する。k

ld - the GNU linker

オブジェクトのリンクにつかう。リンカスクリプトも表示できる。

$ ld --verbose
GNU ld version 2.13.90.0.2 20020802
  Supported emulations:
   elf_i386
   i386linux
   elf_i386_glibc21
using internal linker script:
==================================================
/* Script for -z combreloc: combine and sort reloc sections */
OUTPUT_FORMAT("elf32-i386", "elf32-i386",
              "elf32-i386")
OUTPUT_ARCH(i386)
ENTRY(_start)
SEARCH_DIR("/usr/i386-redhat-linux/lib"); SEARCH_DIR("/usr/lib"); SEARCH_DIR("/usr/local/lib"); SEARCH_DIR("/lib");
/* Do we need any of these for elf?
   __DYNAMIC = 0;    */
SECTIONS
{
  /* Read-only sections, merged into text segment: */
  . = 0x08048000 + SIZEOF_HEADERS;
  .interp         : { *(.interp) }
  .hash           : { *(.hash) }
  .dynsym         : { *(.dynsym) }
  .dynstr         : { *(.dynstr) }
  .gnu.version    : { *(.gnu.version) }
  .gnu.version_d  : { *(.gnu.version_d) }
  .gnu.version_r  : { *(.gnu.version_r) }
  .rel.dyn        :
    {
      *(.rel.init)
      *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)
      *(.rel.fini)
      *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)
      *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)
      *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)
      *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)
      *(.rel.ctors)
      *(.rel.dtors)
      *(.rel.got)
      *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)
    }
  .rela.dyn       :
    {
      *(.rela.init)
      *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)
      *(.rela.fini)
      *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)
      *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)
      *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)
      *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)
      *(.rela.ctors)
      *(.rela.dtors)
      *(.rela.got)
      *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)
    }
  .rel.plt        : { *(.rel.plt) }
  .rela.plt       : { *(.rela.plt) }
  .init           :
  {
    KEEP (*(.init))
  } =0x90909090
  .plt            : { *(.plt) }
  .text           :
  {
    *(.text .stub .text.* .gnu.linkonce.t.*)
    /* .gnu.warning sections are handled specially by elf32.em.  */
    *(.gnu.warning)
  } =0x90909090
  .fini           :
  {
    KEEP (*(.fini))
  } =0x90909090
  PROVIDE (__etext = .);
  PROVIDE (_etext = .);
  PROVIDE (etext = .);
  .rodata         : { *(.rodata .rodata.* .gnu.linkonce.r.*) }
  .rodata1        : { *(.rodata1) }
  .eh_frame_hdr : { *(.eh_frame_hdr) }
  /* Adjust the address for the data segment.  We want to adjust up to
     the same address within the page on the next page up.  */
  . = DATA_SEGMENT_ALIGN(0x1000, 0x1000);
  /* Ensure the __preinit_array_start label is properly aligned.  We
     could instead move the label definition inside the section, but
     the linker would then create the section even if it turns out to
     be empty, which isn't pretty.  */
  . = ALIGN(32 / 8);
  PROVIDE (__preinit_array_start = .);
  .preinit_array     : { *(.preinit_array) }
  PROVIDE (__preinit_array_end = .);
  PROVIDE (__init_array_start = .);
  .init_array     : { *(.init_array) }
  PROVIDE (__init_array_end = .);
  PROVIDE (__fini_array_start = .);
  .fini_array     : { *(.fini_array) }
  PROVIDE (__fini_array_end = .);
  .data           :
  {
    *(.data .data.* .gnu.linkonce.d.*)
    SORT(CONSTRUCTORS)
  }
  .data1          : { *(.data1) }
  .tdata          : { *(.tdata .tdata.* .gnu.linkonce.td.*) }
  .tbss           : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }
  .eh_frame       : { KEEP (*(.eh_frame)) }
  .gcc_except_table   : { *(.gcc_except_table) }
  .dynamic        : { *(.dynamic) }
  .ctors          :
  {
    /* gcc uses crtbegin.o to find the start of
       the constructors, so we make sure it is
       first.  Because this is a wildcard, it
       doesn't matter if the user does not
       actually link against crtbegin.o; the
       linker won't look for a file to match a
       wildcard.  The wildcard also means that it
       doesn't matter which directory crtbegin.o
       is in.  */
    KEEP (*crtbegin.o(.ctors))
    /* We don't want to include the .ctor section from
       from the crtend.o file until after the sorted ctors.
       The .ctor section from the crtend file contains the
       end of ctors marker and it must be last */
    KEEP (*(EXCLUDE_FILE (*crtend.o ) .ctors))
    KEEP (*(SORT(.ctors.*)))
    KEEP (*(.ctors))
  }
  .dtors          :
  {
    KEEP (*crtbegin.o(.dtors))
    KEEP (*(EXCLUDE_FILE (*crtend.o ) .dtors))
    KEEP (*(SORT(.dtors.*)))
    KEEP (*(.dtors))
  }
  .jcr            : { KEEP (*(.jcr)) }
  .got            : { *(.got.plt) *(.got) }
  _edata = .;
  PROVIDE (edata = .);
  __bss_start = .;
  .bss            :
  {
   *(.dynbss)
   *(.bss .bss.* .gnu.linkonce.b.*)
   *(COMMON)
   /* Align here to ensure that the .bss section occupies space up to
      _end.  Align after .bss to ensure correct alignment even if the
      .bss section disappears because there are no input sections.  */
   . = ALIGN(32 / 8);
  }
  . = ALIGN(32 / 8);
  _end = .;
  PROVIDE (end = .);
  . = DATA_SEGMENT_END (.);
  /* Stabs debugging sections.  */
  .stab          0 : { *(.stab) }
  .stabstr       0 : { *(.stabstr) }
  .stab.excl     0 : { *(.stab.excl) }
  .stab.exclstr  0 : { *(.stab.exclstr) }
  .stab.index    0 : { *(.stab.index) }
  .stab.indexstr 0 : { *(.stab.indexstr) }
  .comment       0 : { *(.comment) }
  /* DWARF debug sections.
     Symbols in the DWARF debugging sections are relative to the beginning
     of the section so we begin them at 0.  */
  /* DWARF 1 */
  .debug          0 : { *(.debug) }
  .line           0 : { *(.line) }
  /* GNU DWARF 1 extensions */
  .debug_srcinfo  0 : { *(.debug_srcinfo) }
  .debug_sfnames  0 : { *(.debug_sfnames) }
  /* DWARF 1.1 and DWARF 2 */
  .debug_aranges  0 : { *(.debug_aranges) }
  .debug_pubnames 0 : { *(.debug_pubnames) }
  /* DWARF 2 */
  .debug_info     0 : { *(.debug_info .gnu.linkonce.wi.*) }
  .debug_abbrev   0 : { *(.debug_abbrev) }
  .debug_line     0 : { *(.debug_line) }
  .debug_frame    0 : { *(.debug_frame) }
  .debug_str      0 : { *(.debug_str) }
  .debug_loc      0 : { *(.debug_loc) }
  .debug_macinfo  0 : { *(.debug_macinfo) }
  /* SGI/MIPS DWARF 2 extensions */
  .debug_weaknames 0 : { *(.debug_weaknames) }
  .debug_funcnames 0 : { *(.debug_funcnames) }
  .debug_typenames 0 : { *(.debug_typenames) }
  .debug_varnames  0 : { *(.debug_varnames) }
}


==================================================
 

ld.so (ld-linux.so) dynamic linker / loader

$ /lib/ld-2.3.2.so
Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]
You have invoked `ld.so', the helper program for shared library executables.
This program usually lives in the file `/lib/ld.so', and special directives
in executable files using ELF shared libraries tell the system's program
loader to load the helper program from this file.  This helper program loads
the shared libraries needed by the program executable, prepares the program
to run, and runs it.  You may invoke this helper program directly from the
command line to load and run an ELF executable file; this is like executing
that file itself, but always uses this helper program from the file you
specified, instead of the helper program file specified in the executable
file you run.  This is mostly of use for maintainers to test new versions
of this helper program; chances are you did not intend to run this program.

  --list                list all dependencies and how they are resolved
  --verify              verify that given object really is a dynamically linked
                        object we can handle
  --library-path PATH   use given PATH instead of content of the environment
                        variable LD_LIBRARY_PATH
  --inhibit-rpath LIST  ignore RUNPATH and RPATH information in object names
                        in LIST


$ /lib/ld-linux.so.2
Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]
You have invoked `ld.so', the helper program for shared library executables.
This program usually lives in the file `/lib/ld.so', and special directives
in executable files using ELF shared libraries tell the system's program
loader to load the helper program from this file.  This helper program loads
the shared libraries needed by the program executable, prepares the program
to run, and runs it.  You may invoke this helper program directly from the
command line to load and run an ELF executable file; this is like executing
that file itself, but always uses this helper program from the file you
specified, instead of the helper program file specified in the executable
file you run.  This is mostly of use for maintainers to test new versions
of this helper program; chances are you did not intend to run this program.

  --list                list all dependencies and how they are resolved
  --verify              verify that given object really is a dynamically linked
                        object we can handle
  --library-path PATH   use given PATH instead of content of the environment
                        variable LD_LIBRARY_PATH
  --inhibit-rpath LIST  ignore RUNPATH and RPATH information in object names
                        in LIST
 

gdb [file]

 

コマンド 説明 サンプル
help ヘルプ表示 help files
file 実行ファイルの選択 file ./a.out
set args 実行時の引数設定 set args aaa "hello"
show args 引数の表示 show args
run 実行 run
shll [comnad] シェルプロンプトの表示、実行 shll gcc -g ./hello.c
break [行番号] ブレイクポイントの作成 break main, break 20
info breakpoints ブレイクポイント一覧 info breakpoints
delete [番号] ブレイクポイントの削除 delete 3
awatch [variable] 変数の変化を表示 awatch cnt
step 関数前まで進む(step in) s (sのように省略もできる)
next 次の行に進む n
finish 関数が終わるまで進む(step out) fin
until ループをすべてまわす until
continue 次に進む c, continue
info args, info locals スタックフレーム内(今の関数)の変数表示 info args, info locals
info frame スタックフレームの詳細情報 i f, i frame
info local ローカル変数表示 i lo, info local
print/フォーマット 変数名 データの表示

p/x val1 変数val1を16進表示
p/d val1 変数val1を10進表示
p/t val1 変数val1を2進表示
p/a val1 変数val1のアドレス表示
p/c val1 変数val1のchar表示
p/s val1 変数val1のstring表示
p/f val1 変数val1の浮動小数点表示

 

 

display/フォーマット 変数名 データの表示、一度設定したら、nextを実行するたびに、その変数が表示される。 フォーマットはprintと同様
list ソースコードの表示(デフォルトでは10行) list 50 60 50-60行を表示
q 終了 q
backtrace スタックフレームの表示 ba
frame [フレーム番号] 現在のframeの表示、フレーム番号への移動 fra, fra 2
set variable 変数名=値 変数に値を設定する。 set variable src = 500

とりあえず、以下のようなサンプルを作って、gdbに触れた方が理解できると思う。コンパイル時にgcc -gオプションをつけなければならない。

[s-okita@localhost KandR]$ cat hello_gdb.c
#include 

void hello_message(char *message)
{
                printf("%s\n",  message);
}

void hello_print()
{
                printf("hello_print\n");
}

void swap(int *s, int *d)
{
                int src;                        /* フレームテスト用 */
                int dest;                       /* フレームテスト用 */

                src = *s;
                dest = *d;

                printf("local src in swap:%d\n", src);
                printf("local dest in swap:%d\n", dest);

                int tmp  = *s;
                *s = *d;
                *d = tmp;
}

int main(int argc, char *argv[])
{
                int src;
                int dest;

                src = 100;
                dest = 200;

                hello_print();
                hello_message("fuga");

                swap(&src, &dest);
                printf("local src in main:%d\n", src);
                printf("local dest in main:%d\n", dest);

                return 0;
}

[s-okita@localhost KandR]$ gcc -g -o hello_gdb hello_gdb.c
[s-okita@localhost KandR]$ gdb
GNU gdb Red Hat Linux (5.2.1-4)
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux".
(gdb)
 

 

objdump

ctags

cflow

indent

C言語のスタイルを整えてイくれるツール