.githooks/
riscv/
test/
.clang-format
570 B
.gitignore
5 B
.gitsigners
112 B
AGENTS.md
227 B
LICENSE
1.1 KiB
Makefile
3.5 KiB
README
12.5 KiB
STYLE
781 B
abi_host.c
12.2 KiB
abi_linux.c
3.7 KiB
bitmanip.h
5.8 KiB
color.h
567 B
debug_map.c
5.1 KiB
debug_map.h
1.3 KiB
emulator.c
87.3 KiB
host.h
5.5 KiB
host_posix.c
11.2 KiB
hosted.h
5.7 KiB
image_native.c
4.6 KiB
image_rad0.c
2.7 KiB
image_raw.c
2.6 KiB
io.c
1.1 KiB
io.h
444 B
jit.c
99.2 KiB
jit.h
7.8 KiB
machine.c
4.4 KiB
machine.h
2.1 KiB
machine_cpu.c
13.6 KiB
machine_csr.c
4.6 KiB
machine_devices.c
10.8 KiB
machine_fdt.c
8.7 KiB
machine_internal.h
7.4 KiB
muldiv.h
1.0 KiB
profile.c
17.5 KiB
profile.h
1.4 KiB
riscv.c
755 B
riscv.h
4.3 KiB
types.h
1019 B
vector.c
54.0 KiB
vector.h
1.3 KiB
debug_map.c
raw
| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | #include "debug_map.h" |
| 6 | #include "hosted.h" |
| 7 | #include "io.h" |
| 8 | |
| 9 | u8 *debug_read_sidecar(const char *program, const char *suffix, size_t *size) { |
| 10 | size_t length = strlen(program) + strlen(suffix) + 1; |
| 11 | char *path = malloc(length); |
| 12 | if (!path) |
| 13 | bail("cannot allocate sidecar path"); |
| 14 | snprintf(path, length, "%s%s", program, suffix); |
| 15 | FILE *file = fopen(path, "rb"); |
| 16 | free(path); |
| 17 | if (!file) |
| 18 | return NULL; |
| 19 | long length_on_disk = -1; |
| 20 | if (fseek(file, 0, SEEK_END) == 0) |
| 21 | length_on_disk = ftell(file); |
| 22 | u8 *data = NULL; |
| 23 | if (length_on_disk >= 0 && fseek(file, 0, SEEK_SET) == 0) { |
| 24 | *size = (size_t)length_on_disk; |
| 25 | data = malloc(*size ? *size : 1); |
| 26 | if (!data) |
| 27 | bail("cannot allocate sidecar"); |
| 28 | if (fread(data, 1, *size, file) != *size) { |
| 29 | free(data); |
| 30 | data = NULL; |
| 31 | } |
| 32 | } |
| 33 | fclose(file); |
| 34 | return data; |
| 35 | } |
| 36 | |
| 37 | static bool load_paths(struct debug_map *map, size_t size, u32 code_bytes) { |
| 38 | const u8 *data = map->data; |
| 39 | if (size < 4) |
| 40 | return false; |
| 41 | u32 count = decode_u32_le(data); |
| 42 | size_t pos = 4; |
| 43 | if (count > (size - pos) / 4) |
| 44 | return false; |
| 45 | /* Validate lengths and locations before allocating paths. */ |
| 46 | for (u32 i = 0; i < count; i++) { |
| 47 | if (size - pos < 4) |
| 48 | return false; |
| 49 | u32 len = decode_u32_le(data + pos); |
| 50 | pos += 4; |
| 51 | if (!len || len > size - pos || memchr(data + pos, 0, len)) |
| 52 | return false; |
| 53 | pos += len; |
| 54 | } |
| 55 | if ((size - pos) % 12) |
| 56 | return false; |
| 57 | for (size_t i = pos; i < size; i += 12) { |
| 58 | u32 pc = decode_u32_le(data + i); |
| 59 | if (pc > code_bytes || pc % INSTR_SIZE || |
| 60 | decode_u32_le(data + i + 8) >= count) |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | map->path_count = count; |
| 65 | map->count = (size - pos) / 12; |
| 66 | map->paths = calloc(count, sizeof(*map->paths)); |
| 67 | map->entries = calloc(map->count, sizeof(*map->entries)); |
| 68 | if ((count && !map->paths) || (map->count && !map->entries)) |
| 69 | bail("cannot allocate source map"); |
| 70 | pos = 4; |
| 71 | for (u32 i = 0; i < count; i++) { |
| 72 | u32 len = decode_u32_le(data + pos); |
| 73 | pos += 4; |
| 74 | map->paths[i] = malloc((size_t)len + 1); |
| 75 | if (!map->paths[i]) |
| 76 | bail("cannot allocate source path"); |
| 77 | memcpy(map->paths[i], data + pos, len); |
| 78 | map->paths[i][len] = 0; |
| 79 | pos += len; |
| 80 | } |
| 81 | for (size_t i = 0; i < map->count; i++, pos += 12) { |
| 82 | map->entries[i] = |
| 83 | (struct debug_entry){ decode_u32_le(data + pos), |
| 84 | decode_u32_le(data + pos + 4), |
| 85 | map->paths[decode_u32_le(data + pos + 8)] }; |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | static bool load_legacy(struct debug_map *map, size_t size, u32 code_bytes) { |
| 91 | size_t count = 0; |
| 92 | for (size_t pos = 0; pos < size; count++) { |
| 93 | if (size - pos < 10) |
| 94 | return false; |
| 95 | u32 pc = decode_u32_le(map->data + pos); |
| 96 | if (pc > code_bytes || pc % INSTR_SIZE) |
| 97 | return false; |
| 98 | const u8 *path = map->data + pos + 8; |
| 99 | const u8 *end = memchr(path, 0, size - pos - 8); |
| 100 | if (!end || end == path) |
| 101 | return false; |
| 102 | pos = (size_t)(end - map->data) + 1; |
| 103 | } |
| 104 | map->entries = calloc(count, sizeof(*map->entries)); |
| 105 | if (count && !map->entries) |
| 106 | bail("cannot allocate source map"); |
| 107 | map->count = count; |
| 108 | size_t pos = 0; |
| 109 | for (size_t i = 0; i < count; i++) { |
| 110 | const char *path = (const char *)map->data + pos + 8; |
| 111 | map->entries[i] = |
| 112 | (struct debug_entry){ decode_u32_le(map->data + pos), |
| 113 | decode_u32_le(map->data + pos + 4), |
| 114 | path }; |
| 115 | pos += 9 + strlen(path); |
| 116 | } |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | static int compare_pc(const void *a, const void *b) { |
| 121 | u32 left = ((const struct debug_entry *)a)->pc; |
| 122 | u32 right = ((const struct debug_entry *)b)->pc; |
| 123 | return (left > right) - (left < right); |
| 124 | } |
| 125 | |
| 126 | bool debug_map_load( |
| 127 | struct debug_map *map, const char *program, u32 code_bytes |
| 128 | ) { |
| 129 | size_t size = 0; |
| 130 | map->data = debug_read_sidecar(program, ".debug", &size); |
| 131 | if (!map->data) |
| 132 | return true; |
| 133 | if (!load_paths(map, size, code_bytes) && |
| 134 | !load_legacy(map, size, code_bytes)) { |
| 135 | debug_map_free(map); |
| 136 | return false; |
| 137 | } |
| 138 | if (map->count > 1) |
| 139 | qsort(map->entries, map->count, sizeof(*map->entries), compare_pc); |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | const struct debug_entry *debug_map_lookup( |
| 144 | const struct debug_map *map, u32 pc |
| 145 | ) { |
| 146 | size_t start = 0, end = map->count; |
| 147 | while (start < end) { |
| 148 | size_t middle = start + (end - start) / 2; |
| 149 | if (map->entries[middle].pc <= pc) |
| 150 | start = middle + 1; |
| 151 | else |
| 152 | end = middle; |
| 153 | } |
| 154 | return start ? &map->entries[start - 1] : NULL; |
| 155 | } |
| 156 | |
| 157 | void debug_map_free(struct debug_map *map) { |
| 158 | for (size_t i = 0; i < map->path_count; i++) |
| 159 | free(map->paths[i]); |
| 160 | free(map->paths); |
| 161 | free(map->entries); |
| 162 | free(map->data); |
| 163 | *map = (struct debug_map){ 0 }; |
| 164 | } |