client/runtime/recomp_trace.c (13811B)
1 /* 2 * Bring-up diagnostics hung off RECOMP_ENTER. See recomp_trace.h. 3 * 4 * Every one of these earned its place on a real target, and the notes say 5 * which question each was written to answer -- the shape of the answer is what 6 * makes a diagnostic worth keeping. 7 */ 8 #define WIN32_LEAN_AND_MEAN 9 #include <windows.h> 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <string.h> 13 14 #include "recomp_types.h" 15 #include "recomp_trace.h" 16 17 void (*recomp_trace_extra)(uint32_t va) = NULL; 18 const char* g_cur_import = "(none)"; 19 20 #ifdef RECOMP_TRACE 21 22 uint32_t g_enter_trace[RECOMP_ENTER_SIZE] = {0}; 23 uint32_t g_enter_idx = 0; 24 25 /* 26 * --calltrace FILE: every lifted-function entry, tagged with the thread. 27 * 28 * The entry ring holds only the last RECOMP_ENTER_SIZE, and when a startup 29 * path loops, the interesting call -- the one that failed -- has already 30 * rolled out of it. The file has no such limit. 31 * 32 * BUFFERED, and that matters more than it looks. Unbuffered, on the theory 33 * that a crash must not take the tail with it, a nine-million-call run took 34 * long enough that the trace was never actually taken; with 4 MB of buffer the 35 * same run costs ten seconds. recomp_trace_flush() from the fault handler, and 36 * the flush every exit(3) does, keep the tail anyway. 37 */ 38 static FILE* g_calltrace; 39 40 /* 41 * --firsthit LO HI: the first entry to each distinct function in [LO,HI), 42 * printed in order with the thread that got there. 43 * 44 * A full calltrace answers "what ran" and then asks you to read a hundred 45 * megabytes. The bring-up question is almost always narrower: did execution 46 * ever reach THIS subsystem, and in what order. One bit per 4-byte-aligned 47 * address over the chosen window costs a range check and a bit test per call, 48 * so it can be left on for a whole run. 49 */ 50 static uint32_t g_fh_lo, g_fh_hi; 51 static uint8_t* g_fh_seen; 52 53 /* 54 * --argtrace VA: one line per entry to VA with its first three stack 55 * arguments. 56 * 57 * A --watch line is seven lines of registers and object dump, which is the 58 * wrong shape when the question is "what sequence of ids went through this one 59 * dispatcher". Pointed at a script-function lookup it is a script trace. 60 */ 61 #define TRACE_AT_MAX 4 62 static uint32_t g_at[TRACE_AT_MAX]; 63 static unsigned g_at_n; 64 65 /* 66 * --watch VA: the machine state and the object under ecx, every entry. 67 * 68 * Reading the generated C tells you which member a function dereferences; only 69 * a run tells you what is in it. --watchspan LO HI moves or narrows the window 70 * so a member at +0x234 can be read without a recompile. 71 */ 72 /* 73 * --argobj VA N: at every entry to VA, dump the object ARGUMENT N points at. 74 * 75 * --watch dumps the object under ecx, which is the right thing for a method and 76 * the wrong thing about half the time: the object a function is really deciding 77 * about arrives as an argument. Reading it needed a recompile until this 78 * existed. N is zero-based and counts the stack arguments only, so for a 79 * thiscall the `this` is ecx and N=0 is the first declared parameter. 80 */ 81 /* 82 * --chase VA OFFSETS: follow a pointer chain from ecx, every entry to VA. 83 * 84 * The field that matters is rarely in the object a method is called on. It is 85 * two or three hops away -- `this->device->batch->buffer` -- and each hop's 86 * address is different every run, so --watch and --poison cannot get there: 87 * --watch dumps one object, and --poison needs an address you do not have 88 * until the run is already past the point you wanted to look at. 89 * 90 * OFFSETS is a comma-separated list, and each hop prints its value and the 91 * word at that value, which is the vtable for anything with one -- so 92 * rtti.json names every object on the way down. 93 * 94 * --chase 0x0071E850 0xC,0x98 95 * [chase] 0x0071E850 ecx=11EABFD0 [007D39A4] +0C=11EAC020 [007CF138] 96 * +98=00000000 97 */ 98 #define TRACE_CHASE_MAX 4 99 #define TRACE_CHASE_HOPS 6 100 static struct { uint32_t va; unsigned n; uint32_t off[TRACE_CHASE_HOPS]; } 101 g_chase[TRACE_CHASE_MAX]; 102 static unsigned g_chase_n; 103 104 #define TRACE_ARGOBJ_MAX 4 105 static struct { uint32_t va; unsigned n; } g_argobj[TRACE_ARGOBJ_MAX]; 106 static unsigned g_argobj_n; 107 108 #define TRACE_WATCH_MAX 8 109 static uint32_t g_watch[TRACE_WATCH_MAX]; 110 static unsigned g_watch_n; 111 static int g_wlo = 0, g_whi = 0xA0; 112 113 /* 114 * --poison ADDR [--poisonval V]: report every change of one target dword. 115 * 116 * Written to catch a 64-bit host pointer being stored into target memory by a 117 * shim that left a struct uninitialised: the value turns into 0x00007FFx and 118 * the program dies much later somewhere else. Pairing the report with the 119 * import the machine was last inside names the shim that did it. 120 */ 121 static uint32_t g_poison, g_poison_last, g_poison_val; 122 static int g_poison_hit; 123 124 /* 125 * --poke ADDR VAL VA: write one byte the first time VA is entered. 126 * 127 * Programs turn their own diagnostics off. A retail build can still carry live 128 * assertion and logging machinery gated on a byte that startup clears from an 129 * absent setting; poking the byte back once execution is past the code that 130 * cleared it turns the original diagnostics back on. "Force this flag, but not 131 * until here" is the general shape. 132 * 133 * ponytail: one byte, one site, no restore. Widen it when something needs a 134 * dword or a second poke. 135 */ 136 static uint32_t g_poke_addr, g_poke_at; 137 static uint8_t g_poke_val; 138 static int g_poke_done; 139 140 void recomp_trace_enter(uint32_t va) { 141 if (g_poke_at && !g_poke_done && va == g_poke_at) { 142 g_poke_done = 1; 143 MEM8(g_poke_addr) = g_poke_val; 144 fprintf(stderr, "[poke] MEM8(0x%08X) = %u on entry to 0x%08X\n", 145 g_poke_addr, g_poke_val, va); 146 } 147 /* The ring first, and unconditionally: recomp_dump_trace is what prints a 148 * call path after a fault, and a backtrace that has quietly stopped 149 * recording is worse than none. */ 150 g_enter_trace[g_enter_idx++ & (RECOMP_ENTER_SIZE - 1)] = va; 151 152 if (g_fh_seen && va >= g_fh_lo && va < g_fh_hi) { 153 uint32_t i = (va - g_fh_lo) >> 2; 154 if (!(g_fh_seen[i >> 3] & (1u << (i & 7)))) { 155 g_fh_seen[i >> 3] |= (uint8_t)(1u << (i & 7)); 156 fprintf(stderr, "[first] t%lu 0x%08X\n", GetCurrentThreadId(), va); 157 } 158 } 159 for (unsigned t = 0; t < g_at_n; t++) 160 if (g_at[t] == va) 161 fprintf(stderr, "[args] t%lu %08X %08X %08X %08X\n", 162 GetCurrentThreadId(), va, 163 MEM32(g_esp + 4), MEM32(g_esp + 8), MEM32(g_esp + 12)); 164 /* Thread-tagged, because the trace interleaves the target's own worker 165 * threads with the main one and a flat sequence cannot be read. */ 166 if (g_calltrace) 167 fprintf(g_calltrace, "%lu %08X\n", GetCurrentThreadId(), va); 168 169 if (g_poison) { 170 uint32_t v = MEM32(g_poison); 171 if (v != g_poison_last && g_poison_hit < 400 172 && (!g_poison_val || v == g_poison_val)) { 173 g_poison_hit++; 174 fprintf(stderr, "[poison] 0x%08X: 0x%08X -> 0x%08X on entry to" 175 " 0x%08X (last import %s)\n", 176 g_poison, g_poison_last, v, va, g_cur_import); 177 g_poison_last = v; 178 } 179 } 180 for (unsigned w = 0; w < g_watch_n; w++) { 181 if (g_watch[w] != va) continue; 182 fprintf(stderr, "[watch] 0x%08X ecx=%08X ebx=%08X eax=%08X esi=%08X" 183 " edi=%08X esp=%08X args:", va, g_ecx, g_ebx, g_eax, 184 g_esi, g_edi, g_esp); 185 for (int k = 4; k <= 0x20; k += 4) 186 fprintf(stderr, " %08X", MEM32(g_esp + k)); 187 fprintf(stderr, "\n"); 188 if (g_ecx >= 0x00200000u) 189 for (int row = g_wlo; row < g_whi; row += 0x20) { 190 fprintf(stderr, "[watch] [ecx+%02X]:", row); 191 for (int k = 0; k < 0x20; k += 4) 192 fprintf(stderr, " %08X", MEM32(g_ecx + row + k)); 193 fprintf(stderr, "\n"); 194 } 195 } 196 for (unsigned w = 0; w < g_argobj_n; w++) { 197 if (g_argobj[w].va != va) continue; 198 uint32_t o = MEM32(g_esp + 4 + 4 * g_argobj[w].n); 199 fprintf(stderr, "[argobj] 0x%08X arg%u=%08X", va, g_argobj[w].n, o); 200 if (o < 0x00200000u) { fprintf(stderr, " (not a pointer)\n"); continue; } 201 fprintf(stderr, " vtable=%08X\n", MEM32(o)); 202 for (int row = 0; row < 0x40; row += 0x20) { 203 fprintf(stderr, "[argobj] [+%02X]:", row); 204 for (int k = 0; k < 0x20; k += 4) 205 fprintf(stderr, " %08X", MEM32(o + row + k)); 206 fprintf(stderr, "\n"); 207 } 208 } 209 for (unsigned w = 0; w < g_chase_n; w++) { 210 if (g_chase[w].va != va) continue; 211 uint32_t p = g_ecx; 212 fprintf(stderr, "[chase] 0x%08X ecx=%08X", va, p); 213 if (p >= 0x00200000u) fprintf(stderr, " [%08X]", MEM32(p)); 214 for (unsigned k = 0; k < g_chase[w].n; k++) { 215 if (p < 0x00200000u) { fprintf(stderr, " +%02X=?", g_chase[w].off[k]); break; } 216 p = MEM32(p + g_chase[w].off[k]); 217 fprintf(stderr, " +%02X=%08X", g_chase[w].off[k], p); 218 if (p >= 0x00200000u) fprintf(stderr, " [%08X]", MEM32(p)); 219 } 220 fprintf(stderr, "\n"); 221 } 222 if (recomp_trace_extra) recomp_trace_extra(va); 223 } 224 225 void recomp_dump_trace(const char* why) { 226 fprintf(stderr, "=== entry trace (%s) ===\n", why ? why : ""); 227 int depth = (g_enter_idx < RECOMP_ENTER_SIZE) ? (int)g_enter_idx 228 : RECOMP_ENTER_SIZE; 229 for (int i = depth; i > 0; i--) { 230 uint32_t idx = (g_enter_idx - i) & (RECOMP_ENTER_SIZE - 1); 231 if (g_enter_trace[idx]) 232 fprintf(stderr, " 0x%08X\n", g_enter_trace[idx]); 233 } 234 } 235 236 void recomp_trace_flush(void) { if (g_calltrace) fflush(g_calltrace); } 237 238 #define TRACE_U32(x) ((uint32_t)strtoul((x), NULL, 0)) 239 240 int recomp_trace_arg(int argc, char** argv, int i) { 241 const char* a = argv[i]; 242 if (!strcmp(a, "--calltrace") && i + 1 < argc) { 243 g_calltrace = fopen(argv[i + 1], "w"); 244 if (g_calltrace) setvbuf(g_calltrace, NULL, _IOFBF, 4u << 20); 245 else fprintf(stderr, "[trace] cannot write %s\n", argv[i + 1]); 246 return 2; 247 } 248 if (!strcmp(a, "--firsthit") && i + 2 < argc) { 249 g_fh_lo = TRACE_U32(argv[i + 1]); 250 g_fh_hi = TRACE_U32(argv[i + 2]); 251 if (g_fh_hi > g_fh_lo) 252 g_fh_seen = (uint8_t*)calloc(((g_fh_hi - g_fh_lo) >> 5) + 1, 1); 253 return 3; 254 } 255 if (!strcmp(a, "--argtrace") && i + 1 < argc && g_at_n < TRACE_AT_MAX) { 256 g_at[g_at_n++] = TRACE_U32(argv[i + 1]); 257 return 2; 258 } 259 if (!strcmp(a, "--watch") && i + 1 < argc && g_watch_n < TRACE_WATCH_MAX) { 260 g_watch[g_watch_n++] = TRACE_U32(argv[i + 1]); 261 return 2; 262 } 263 if (!strcmp(a, "--chase") && i + 2 < argc && g_chase_n < TRACE_CHASE_MAX) { 264 g_chase[g_chase_n].va = TRACE_U32(argv[i + 1]); 265 const char* q = argv[i + 2]; 266 unsigned n = 0; 267 while (*q && n < TRACE_CHASE_HOPS) { 268 g_chase[g_chase_n].off[n++] = (uint32_t)strtoul(q, NULL, 0); 269 const char* comma = strchr(q, ','); 270 if (!comma) break; 271 q = comma + 1; 272 } 273 g_chase[g_chase_n].n = n; 274 g_chase_n++; 275 return 3; 276 } 277 if (!strcmp(a, "--argobj") && i + 2 < argc 278 && g_argobj_n < TRACE_ARGOBJ_MAX) { 279 g_argobj[g_argobj_n].va = TRACE_U32(argv[i + 1]); 280 g_argobj[g_argobj_n].n = (unsigned)TRACE_U32(argv[i + 2]); 281 g_argobj_n++; 282 return 3; 283 } 284 if (!strcmp(a, "--watchspan") && i + 2 < argc) { 285 g_wlo = (int)(TRACE_U32(argv[i + 1]) & ~0x1Fu); 286 g_whi = (int)TRACE_U32(argv[i + 2]); 287 return 3; 288 } 289 if (!strcmp(a, "--poison") && i + 1 < argc) { 290 g_poison = TRACE_U32(argv[i + 1]); 291 return 2; 292 } 293 if (!strcmp(a, "--poisonval") && i + 1 < argc) { 294 g_poison_val = TRACE_U32(argv[i + 1]); 295 return 2; 296 } 297 if (!strcmp(a, "--poke") && i + 3 < argc) { 298 g_poke_addr = TRACE_U32(argv[i + 1]); 299 g_poke_val = (uint8_t)TRACE_U32(argv[i + 2]); 300 g_poke_at = TRACE_U32(argv[i + 3]); 301 return 4; 302 } 303 return 0; 304 } 305 306 #else /* built without -DRECOMP_TRACE: the options parse but do nothing */ 307 308 void recomp_dump_trace(const char* why) { (void)why; } 309 void recomp_trace_flush(void) { } 310 311 int recomp_trace_arg(int argc, char** argv, int i) { 312 static const struct { const char* name; int takes; } opts[] = { 313 {"--calltrace", 2}, {"--firsthit", 3}, {"--argtrace", 2}, 314 {"--watch", 2}, {"--watchspan", 3}, {"--argobj", 3}, {"--chase", 3}, 315 {"--poison", 2}, 316 {"--poisonval", 2}, {"--poke", 4}, 317 }; 318 for (unsigned k = 0; k < sizeof opts / sizeof opts[0]; k++) 319 if (!strcmp(argv[i], opts[k].name)) { 320 fprintf(stderr, "[trace] %s needs a build with -DRECOMP_TRACE\n", 321 argv[i]); 322 return (i + opts[k].takes - 1 < argc) ? opts[k].takes : 1; 323 } 324 return 0; 325 } 326 327 #endif 328 329 void recomp_trace_help(void) { 330 fprintf(stderr, 331 " --calltrace FILE every lifted-function entry, thread-tagged\n" 332 " --firsthit LO HI first entry to each function in [LO,HI)\n" 333 " --argtrace VA one line per entry to VA, with 3 arguments\n" 334 " --watch VA registers, arguments and the object at ecx\n" 335 " --watchspan LO HI move the [ecx+..] window --watch dumps\n" 336 " --argobj VA N vtable and first 0x40 bytes of argument N\n" 337 " --chase VA OFFSETS follow a pointer chain from ecx (0xC,0x98)\n" 338 " --poison ADDR report every change of a target dword\n" 339 " --poisonval V ...only when it becomes V\n" 340 " --poke ADDR VAL VA write one byte on first entry to VA\n"); 341 }