toolkit/crashreporter/test/dumputils.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/crashreporter/test/dumputils.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,97 @@
     1.4 +#include <stdio.h>
     1.5 +
     1.6 +#include "google_breakpad/processor/minidump.h"
     1.7 +#include "nscore.h"
     1.8 +
     1.9 +using namespace google_breakpad;
    1.10 +
    1.11 +// Return true if the specified minidump contains a stream of |stream_type|.
    1.12 +extern "C"
    1.13 +NS_EXPORT bool
    1.14 +DumpHasStream(const char* dump_file, uint32_t stream_type)
    1.15 +{
    1.16 +  Minidump dump(dump_file);
    1.17 +  if (!dump.Read())
    1.18 +    return false;
    1.19 +
    1.20 +  uint32_t length;
    1.21 +  if (!dump.SeekToStreamType(stream_type, &length) || length == 0)
    1.22 +    return false;
    1.23 +
    1.24 +  return true;
    1.25 +}
    1.26 +
    1.27 +// Return true if the specified minidump contains a memory region
    1.28 +// that contains the instruction pointer from the exception record.
    1.29 +extern "C"
    1.30 +NS_EXPORT bool
    1.31 +DumpHasInstructionPointerMemory(const char* dump_file)
    1.32 +{
    1.33 +  Minidump minidump(dump_file);
    1.34 +  if (!minidump.Read())
    1.35 +    return false;
    1.36 +
    1.37 +  MinidumpException* exception = minidump.GetException();
    1.38 +  MinidumpMemoryList* memory_list = minidump.GetMemoryList();
    1.39 +  if (!exception || !memory_list) {
    1.40 +    return false;
    1.41 +  }
    1.42 +
    1.43 +  MinidumpContext* context = exception->GetContext();
    1.44 +  if (!context)
    1.45 +    return false;
    1.46 +
    1.47 +  uint64_t instruction_pointer;
    1.48 +  if (!context->GetInstructionPointer(&instruction_pointer)) {
    1.49 +    return false;
    1.50 +  }
    1.51 +
    1.52 +  MinidumpMemoryRegion* region =
    1.53 +    memory_list->GetMemoryRegionForAddress(instruction_pointer);
    1.54 +  return region != nullptr;
    1.55 +}
    1.56 +
    1.57 +// This function tests for a very specific condition. It finds
    1.58 +// an address in a file, "crash-addr", in the CWD. It checks
    1.59 +// that the minidump has a memory region starting at that
    1.60 +// address. The region must be 32 bytes long and contain the
    1.61 +// values 0 to 31 as bytes, in ascending order.
    1.62 +extern "C"
    1.63 +NS_EXPORT bool
    1.64 +DumpCheckMemory(const char* dump_file)
    1.65 +{
    1.66 +  Minidump dump(dump_file);
    1.67 +  if (!dump.Read())
    1.68 +    return false;
    1.69 +
    1.70 +  MinidumpMemoryList* memory_list = dump.GetMemoryList();
    1.71 +  if (!memory_list) {
    1.72 +    return false;
    1.73 +  }
    1.74 +
    1.75 +  void *addr;
    1.76 +  FILE *fp = fopen("crash-addr", "r");
    1.77 +  if (!fp)
    1.78 +    return false;
    1.79 +  if (fscanf(fp, "%p", &addr) != 1)
    1.80 +    return false;
    1.81 +  fclose(fp);
    1.82 +
    1.83 +  remove("crash-addr");
    1.84 +
    1.85 +  MinidumpMemoryRegion* region =
    1.86 +    memory_list->GetMemoryRegionForAddress(uint64_t(addr));
    1.87 +  if(!region)
    1.88 +    return false;
    1.89 +
    1.90 +  const uint8_t* chars = region->GetMemory();
    1.91 +  if (region->GetSize() != 32)
    1.92 +    return false;
    1.93 +
    1.94 +  for (int i=0; i<32; i++) {
    1.95 +    if (chars[i] != i)
    1.96 +      return false;
    1.97 +  }
    1.98 +
    1.99 +  return true;
   1.100 +}

mercurial