Skip to content

Commit

Permalink
fix: Iterate all debug directory entries (#740)
Browse files Browse the repository at this point in the history
Turns out a PE file can have more than one debug directory entry.
We now iterate over all of them, trying to find the one that has a valid
CodeView record that we can use to get a debug_id from.
  • Loading branch information
Swatinem authored Aug 1, 2022
1 parent 666dbc1 commit 4890a30
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions src/modulefinder/sentry_modulefinder_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,48 @@ extract_pdb_info(uintptr_t module_addr, sentry_value_t module)
nt_headers->OptionalHeader.SizeOfImage);
sentry_value_set_by_key(module, "code_id", sentry_value_new_string(id_buf));

uint32_t relative_addr
= nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG]
.VirtualAddress;
IMAGE_DATA_DIRECTORY debug_entry
= nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG];

size_t relative_addr = (size_t)debug_entry.VirtualAddress;
if (!relative_addr) {
return;
}

PIMAGE_DEBUG_DIRECTORY debug_dict
= (PIMAGE_DEBUG_DIRECTORY)(module_addr + relative_addr);
if (!debug_dict || debug_dict->Type != IMAGE_DEBUG_TYPE_CODEVIEW) {
size_t table_size = (size_t)debug_entry.Size;
size_t entry_size = sizeof(IMAGE_DEBUG_DIRECTORY);
if (table_size % entry_size != 0) {
return;
}

struct CodeViewRecord70 *debug_info
= (struct CodeViewRecord70 *)(module_addr
+ debug_dict->AddressOfRawData);
if (!debug_info || debug_info->signature != CV_SIGNATURE) {
return;
}
for (size_t offset = 0; offset < table_size; offset += entry_size) {
PIMAGE_DEBUG_DIRECTORY debug_dict
= (PIMAGE_DEBUG_DIRECTORY)(module_addr + relative_addr + offset);

if (debug_dict->Type != IMAGE_DEBUG_TYPE_CODEVIEW) {
continue;
}

sentry_value_set_by_key(module, "debug_file",
sentry_value_new_string(debug_info->pdb_filename));
struct CodeViewRecord70 *debug_info
= (struct CodeViewRecord70 *)(module_addr
+ debug_dict->AddressOfRawData);
if (debug_info->signature != CV_SIGNATURE) {
continue;
}

sentry_uuid_t debug_id_base
= sentry__uuid_from_native(&debug_info->pdb_signature);
sentry_uuid_as_string(&debug_id_base, id_buf);
id_buf[36] = '-';
snprintf(id_buf + 37, 10, "%x", debug_info->pdb_age);
sentry_value_set_by_key(
module, "debug_id", sentry_value_new_string(id_buf));
sentry_value_set_by_key(module, "debug_file",
sentry_value_new_string(debug_info->pdb_filename));

sentry_uuid_t debug_id_base
= sentry__uuid_from_native(&debug_info->pdb_signature);
sentry_uuid_as_string(&debug_id_base, id_buf);
id_buf[36] = '-';
snprintf(id_buf + 37, 10, "%x", debug_info->pdb_age);
sentry_value_set_by_key(
module, "debug_id", sentry_value_new_string(id_buf));

return;
}
}

static void
Expand Down

0 comments on commit 4890a30

Please sign in to comment.