|
| 1 | +// Just enough of the minidump format to extract module names + debug |
| 2 | +// identifiers so we can download pdbs |
| 3 | + |
| 4 | +const headerMagic = Buffer.from('MDMP').readUInt32LE(0) |
| 5 | + |
| 6 | +if (!Buffer.prototype.readBigUInt64LE) { |
| 7 | + Buffer.prototype.readBigUInt64LE = function(offset) { |
| 8 | + // ESLint doesn't support BigInt yet |
| 9 | + // eslint-disable-next-line |
| 10 | + return BigInt(this.readUInt32LE(offset)) + (BigInt(this.readUInt32LE(offset + 4)) << BigInt(32)) |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +// MDRawHeader |
| 15 | +// https://chromium.googlesource.com/breakpad/breakpad/+/c46151db0ffd1a8dae914e45f1212ef427f61ed3/src/google_breakpad/common/minidump_format.h#252 |
| 16 | +function readHeader (buf) { |
| 17 | + return { |
| 18 | + signature: buf.readUInt32LE(0), |
| 19 | + version: buf.readUInt32LE(4), |
| 20 | + stream_count: buf.readUInt32LE(8), |
| 21 | + stream_directory_rva: buf.readUInt32LE(12), |
| 22 | + checksum: buf.readUInt32LE(16), |
| 23 | + time_date_stamp: buf.readUInt32LE(20), |
| 24 | + flags: buf.readBigUInt64LE(24) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// MDRawDirectory |
| 29 | +// https://chromium.googlesource.com/breakpad/breakpad/+/c46151db0ffd1a8dae914e45f1212ef427f61ed3/src/google_breakpad/common/minidump_format.h#305 |
| 30 | +function readDirectory (buf, rva) { |
| 31 | + return { |
| 32 | + type: buf.readUInt32LE(rva), |
| 33 | + location: readLocationDescriptor(buf, rva + 4) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// MDRawModule |
| 38 | +// https://chromium.googlesource.com/breakpad/breakpad/+/c46151db0ffd1a8dae914e45f1212ef427f61ed3/src/google_breakpad/common/minidump_format.h#386 |
| 39 | +function readRawModule (buf, rva) { |
| 40 | + const module = { |
| 41 | + base_of_image: buf.readBigUInt64LE(rva), |
| 42 | + size_of_image: buf.readUInt32LE(rva + 8), |
| 43 | + checksum: buf.readUInt32LE(rva + 12), |
| 44 | + time_date_stamp: buf.readUInt32LE(rva + 16), |
| 45 | + module_name_rva: buf.readUInt32LE(rva + 20), |
| 46 | + version_info: readVersionInfo(buf, rva + 24), |
| 47 | + cv_record: readCVRecord(buf, readLocationDescriptor(buf, rva + 24 + 13 * 4)), |
| 48 | + misc_record: readLocationDescriptor(buf, rva + 24 + 13 * 4 + 8) |
| 49 | + } |
| 50 | + // https://chromium.googlesource.com/breakpad/breakpad/+/c46151db0ffd1a8dae914e45f1212ef427f61ed3/src/processor/minidump.cc#2255 |
| 51 | + module.version = [ |
| 52 | + module.version_info.file_version_hi >> 16, |
| 53 | + module.version_info.file_version_hi & 0xffff, |
| 54 | + module.version_info.file_version_lo >> 16, |
| 55 | + module.version_info.file_version_lo & 0xffff |
| 56 | + ].join('.') |
| 57 | + module.name = readString(buf, module.module_name_rva) |
| 58 | + return module |
| 59 | +} |
| 60 | + |
| 61 | +// MDVSFixedFileInfo |
| 62 | +// https://chromium.googlesource.com/breakpad/breakpad/+/c46151db0ffd1a8dae914e45f1212ef427f61ed3/src/google_breakpad/common/minidump_format.h#129 |
| 63 | +function readVersionInfo (buf, base) { |
| 64 | + return { |
| 65 | + signature: buf.readUInt32LE(base), |
| 66 | + struct_version: buf.readUInt32LE(base + 4), |
| 67 | + file_version_hi: buf.readUInt32LE(base + 8), |
| 68 | + file_version_lo: buf.readUInt32LE(base + 12), |
| 69 | + product_version_hi: buf.readUInt32LE(base + 16), |
| 70 | + product_version_lo: buf.readUInt32LE(base + 20), |
| 71 | + file_flags_mask: buf.readUInt32LE(base + 24), |
| 72 | + file_flags: buf.readUInt32LE(base + 28), |
| 73 | + file_os: buf.readUInt32LE(base + 32), |
| 74 | + file_type: buf.readUInt32LE(base + 24), |
| 75 | + file_subtype: buf.readUInt32LE(base + 28), |
| 76 | + file_date_hi: buf.readUInt32LE(base + 32), |
| 77 | + file_date_lo: buf.readUInt32LE(base + 36) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// MDLocationDescriptor |
| 82 | +// https://chromium.googlesource.com/breakpad/breakpad/+/c46151db0ffd1a8dae914e45f1212ef427f61ed3/src/google_breakpad/common/minidump_format.h#237 |
| 83 | +function readLocationDescriptor (buf, base) { |
| 84 | + return { |
| 85 | + data_size: buf.readUInt32LE(base), |
| 86 | + rva: buf.readUInt32LE(base + 4) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// MDGUID |
| 91 | +// https://chromium.googlesource.com/breakpad/breakpad/+/c46151db0ffd1a8dae914e45f1212ef427f61ed3/src/google_breakpad/common/minidump_format.h#81 |
| 92 | +function readGUID (buf) { |
| 93 | + return { |
| 94 | + data1: buf.readUInt32LE(0), |
| 95 | + data2: buf.readUInt16LE(4), |
| 96 | + data3: buf.readUInt16LE(6), |
| 97 | + data4: [...buf.subarray(8)] |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// guid_and_age_to_debug_id |
| 102 | +// https://chromium.googlesource.com/breakpad/breakpad/+/c46151db0ffd1a8dae914e45f1212ef427f61ed3/src/processor/minidump.cc#2153 |
| 103 | +function debugIdFromGuidAndAge (guid, age) { |
| 104 | + return [ |
| 105 | + guid.data1.toString(16).padStart(8, '0'), |
| 106 | + guid.data2.toString(16).padStart(4, '0'), |
| 107 | + guid.data3.toString(16).padStart(4, '0'), |
| 108 | + ...guid.data4.map(x => x.toString(16).padStart(2, '0')), |
| 109 | + age.toString(16) |
| 110 | + ].join('').toUpperCase() |
| 111 | +} |
| 112 | + |
| 113 | +// MDCVInfo{PDB70,ELF} |
| 114 | +// https://chromium.googlesource.com/breakpad/breakpad/+/c46151db0ffd1a8dae914e45f1212ef427f61ed3/src/google_breakpad/common/minidump_format.h#426 |
| 115 | +function readCVRecord (buf, { rva, data_size: dataSize }) { |
| 116 | + if (rva === 0) return |
| 117 | + const cv_signature = buf.readUInt32LE(rva) |
| 118 | + if (cv_signature !== 0x53445352 /* SDSR */) { |
| 119 | + const age = buf.readUInt32LE(rva + 4 + 16) |
| 120 | + const guid = readGUID(buf.subarray(rva + 4, rva + 4 + 16)) |
| 121 | + return { |
| 122 | + cv_signature, |
| 123 | + guid, |
| 124 | + age, |
| 125 | + pdb_file_name: buf.subarray(rva + 4 + 16 + 4, rva + dataSize - 1).toString('utf8'), |
| 126 | + debug_file_id: debugIdFromGuidAndAge(guid, age) |
| 127 | + } |
| 128 | + } else { |
| 129 | + return {cv_signature} |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// MDString |
| 134 | +// https://chromium.googlesource.com/breakpad/breakpad/+/c46151db0ffd1a8dae914e45f1212ef427f61ed3/src/google_breakpad/common/minidump_format.h#357 |
| 135 | +function readString (buf, rva) { |
| 136 | + if (rva === 0) return null |
| 137 | + const bytes = buf.readUInt32LE(rva) |
| 138 | + return buf.subarray(rva + 4, rva + 4 + bytes).toString('utf16le') |
| 139 | +} |
| 140 | + |
| 141 | +// MDStreamType |
| 142 | +// https://chromium.googlesource.com/breakpad/breakpad/+/refs/heads/master/src/google_breakpad/common/minidump_format.h#310 |
| 143 | +const streamTypes = { |
| 144 | + MD_MODULE_LIST_STREAM: 4, |
| 145 | +} |
| 146 | + |
| 147 | +const streamTypeProcessors = { |
| 148 | + [streamTypes.MD_MODULE_LIST_STREAM]: (stream, buf) => { |
| 149 | + const numModules = buf.readUInt32LE(stream.location.rva) |
| 150 | + const modules = [] |
| 151 | + const size = 8 + 4 + 4 + 4 + 4 + 13 * 4 + 8 + 8 + 8 + 8 |
| 152 | + const base = stream.location.rva + 4 |
| 153 | + for (let i = 0; i < numModules; i++) { |
| 154 | + modules.push(readRawModule(buf, base + i * size)) |
| 155 | + } |
| 156 | + stream.modules = modules |
| 157 | + return stream |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +module.exports.readMinidump = function readMinidump (buf) { |
| 162 | + const header = readHeader(buf) |
| 163 | + if (header.signature !== headerMagic) { |
| 164 | + throw new Error('not a minidump file') |
| 165 | + } |
| 166 | + |
| 167 | + const streams = [] |
| 168 | + for (let i = 0; i < header.stream_count; i++) { |
| 169 | + const stream = readDirectory(buf, header.stream_directory_rva + i * 12) |
| 170 | + if (stream.type !== 0) { |
| 171 | + streams.push((streamTypeProcessors[stream.type] || (s => s))(stream, buf)) |
| 172 | + } |
| 173 | + } |
| 174 | + return { header, streams } |
| 175 | +} |
| 176 | + |
| 177 | +module.exports.streamTypes = streamTypes |
0 commit comments