代码之家  ›  专栏  ›  技术社区  ›  efunneko

当gdb调试核心文件时,是否可以将满是二进制数据的文件加载到gdb中?

  •  3
  • efunneko  · 技术社区  · 16 年前

    我正在使用gdb和核心文件调试崩溃。内存空间的很大一部分被映射到进程中。内存的那部分没有保存到核心文件中。我有一个文件,其中包含了mmap内存中的所有数据。

    我想找到一种方法,以一定的偏移量将该文件中的数据加载到gdb中,以便在该地址空间内显示数据结构。这有可能吗?

    请注意,我已经尝试了' restore '命令,但它仅在调试正在运行的进程时工作。

    也许有一些工具允许核心文件附加额外的数据?我正在试验objcopy,看看是否可以用这个二进制数据扩充核心文件,但是我还没有成功。

    3 回复  |  直到 16 年前
        1
  •  2
  •   efunneko    16 年前

    我唯一能让它工作的方法是修改核心文件本身,使其具有包含新数据的额外程序头/节。

    从理论上讲,我相信objcopy应该能够做到这一点,但是经过大量测试,我还是无法让它工作。相反,我使用了编写修改核心文件的Perl脚本。

    下面是那些陷入类似情况的用户的脚本(请注意,这是针对i386架构上的elf核心文件的脚本):

    #!/usr/bin/perl
    
    my @elfHeader = (
      [ident => 'A16'],
      [e_type => 'v'],
      [e_machine => 'v'],
      [e_version => 'V'],
      [e_entry => 'V'],
      [e_phoff => 'V'],
      [e_shoff => 'V'],
      [e_flags => 'V'],
      [e_ehsize => 'v'],
      [e_phentsize => 'v'],
      [e_phnum => 'v'],
      [e_shentsize => 'v'],
      [e_shnum => 'v'],
      [e_shstrndx => 'v']
    );
    
    my @progHeader = (
      [ptype => 'V'],
      [poffset => 'V'],
      [pvaddr => 'V'],
      [ppaddr => 'V'],
      [pfilesz => 'V'],
      [pmemsz => 'V'],
      [pflags => 'V'],
      [palign => 'V'],
    );
    
    
    my ($core, $dataFile, $outFile) = @ARGV;
    
    main();
    
    
    sub main {
    
      my @stat = stat($core);
      my $coreSize = $stat[7];
    
      @stat = stat($dataFile);
      my $dfSize = $stat[7];
    
      my ($in, $out, $df);
      open($in, "", $outFile) || die("Couldn't open $outFile: $!");
    
      my $buf;
      my $bytes = sysread($in, $buf, 52);
    
      my $hdr = unpackStruct(\@elfHeader, $buf);
    
      # Fix the elf header to have an additional program header
      my $phNum = $hdr->{e_phnum};
      $hdr->{e_phnum}++;
    
      # Fix the header to point to a new location for the program headers (at the end of the file)
      my $phOff = $hdr->{e_phoff};
      $hdr->{e_phoff} = $coreSize;
    
      # Read in the full program header table
      my $phTable;
      sysseek($in, $phOff, 0);
      my $readSize = $hdr->{e_phentsize} * $phNum;
      $bytes = sysread($in, $phTable, $readSize);
    
      # Add an additional entry to the end of the ph table
      my $entry = packStruct(\@progHeader, {ptype => 1, 
                                            poffset => $coreSize + $hdr->{e_phentsize} * $hdr->{e_phnum},
                                            pvaddr => 0x80f95000,
                                            ppaddr => 0,
                                            pfilesz => $dfSize,
                                            pmemsz => $dfSize,
                                            pflags => 7,
                                            palign => 4096});
    
      $phTable .= $entry;
    
      # Form the new elf header
      my $elf = packStruct(\@elfHeader, $hdr);
    
      # Output the new header
      syswrite($out, $elf, length($elf));
    
      # Copy the full core file after the header
      sysseek($in, 52, 0);
      copyData($in, $out, $coreSize - 52);
    
      # Output the new program table
      syswrite($out, $phTable, length($phTable));
    
      # Add the data on the end
      copyData($df, $out, $dfSize);
    
    }
    
    
    sub copyData {
      my ($in, $out, $numBytes) = @_;
    
      my $buf;
    
      while ($numBytes > 0) {
        my $readBytes = sysread($in, $buf, 8192);
        syswrite($out, $buf, $readBytes);
        $numBytes -= $readBytes;
      }
    
    }
    
    
    sub unpackStruct {
      my ($fields, $data) = @_;
    
      my $unpack;
      map {$unpack .= $_->[1]} @{$fields};
    
      my @vals = unpack($unpack, $data);
    
      my %res;
      foreach my $field (@{$fields}) {
        $res{$field->[0]} = shift(@vals);
      }
    
      return \%res;
    
    }
    
    
    sub packStruct {
      my ($fields, $data) = @_;
    
      my $pack;
      map {$pack .= $_->[1]} @{$fields};
    
      my @vals;
      foreach my $field (@{$fields}) {
        push(@vals, $data->{$field->[0]})
      }
    
      my $res = pack($pack, @vals);
    
      return $res;
    
    }
    
        3
  •  0
  •   wallyk    16 年前

    据我所知, gdb 在核心文件或正在运行的进程上工作。您所描述的似乎是一个混合体:运行一个核心文件。我认为这是不可能的,而且 gdb documentation 表示没有其他选择。