簡體   English   中英

當GDB調試核心文件時,是否可以將充滿二進制數據的文件加載到GDB中?

[英]Is it possible to load a file full of binary data into GDB when GDB is debugging a core file?

我正在使用GDB和核心文件調試崩潰。 大部分內存空間被映射到進程中。 內存的那部分未保存到核心文件中。 我有一個文件,其中包含該mmapped內存中的所有數據。

我想找到一種方法將該文件中的數據加載到某個偏移量的GDB中,這樣我就可以在該地址空間中顯示數據結構。 這可能嗎?

請注意,我在GDB中嘗試了' restore '命令,但它僅在調試正在運行的進程時才有效。

也許有些工具允許核心文件附加額外的數據? 我正在試驗objcopy,看看我是否可以用這個二進制數據擴充核心文件,但我還沒有成功。

我能夠讓它工作的唯一方法是修改核心文件本身,以獲得包含新數據的附加程序頭/部分。

理論上,我相信objcopy應該能夠做到這一點,但經過大量的測試后我無法讓它工作。 相反,我使用編寫修改核心文件的perl腳本。

以下是那些陷入類似情況的腳本(注意這是針對i386 arch上的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;

}

據我所知, gdb可以在核心文件或正在運行的進程上運行。 您似乎要描述的是混合:運行核心文件。 我不認為這是可能的, gdb文檔表明沒有其他選擇。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM