| 7312 | |
| 7313 | #ifdef AMIBERRY |
| 7314 | static void debug_backtrace(int max_frames) |
| 7315 | { |
| 7316 | // Frame pointer must be in A5 for this to give a correct backtrace |
| 7317 | |
| 7318 | uaecptr pc = M68K_GETPC; |
| 7319 | uaecptr sp = regs.usp; |
| 7320 | |
| 7321 | console_out_f(_T("Attempting backtrace of %d frames:\n"), max_frames); |
| 7322 | |
| 7323 | // First frame: current PC |
| 7324 | uaecptr frame_pc = pc; |
| 7325 | uaecptr func_address; |
| 7326 | |
| 7327 | bool found_link = false; |
| 7328 | |
| 7329 | // For first frame, use A5 value |
| 7330 | uaecptr current_fp = regs.regs[13]; |
| 7331 | |
| 7332 | // Walk the stack using frame pointers |
| 7333 | int frame; |
| 7334 | for (frame = 0; frame < max_frames; frame++) { |
| 7335 | func_address = frame_pc; |
| 7336 | // Search backwards up to 200 bytes to find LINK |
| 7337 | for (int i = 0; i < 200; i += 2) { |
| 7338 | uaecptr test_addr = func_address - i; |
| 7339 | |
| 7340 | if (!debug_safe_addr(test_addr, 4)) { |
| 7341 | break; |
| 7342 | } |
| 7343 | |
| 7344 | uae_u16 opcode = get_word_debug(test_addr); |
| 7345 | |
| 7346 | // Check for LINK instruction: 0x4e55 followed by any displacement |
| 7347 | if (opcode == 0x4e55) { |
| 7348 | func_address = test_addr; |
| 7349 | found_link = true; |
| 7350 | break; |
| 7351 | } |
| 7352 | } |
| 7353 | |
| 7354 | // Display the frame |
| 7355 | console_out_f(_T("#%d %08X in %08X\n"), frame, frame_pc, func_address); |
| 7356 | |
| 7357 | if (!debug_safe_addr(current_fp, 8)) |
| 7358 | break; |
| 7359 | |
| 7360 | // Get the return address from the stack |
| 7361 | frame_pc = get_long_debug(current_fp + 4); |
| 7362 | |
| 7363 | // Sanity checks |
| 7364 | if (frame_pc == 0 || frame_pc % 2 == 1 || !debug_safe_addr(frame_pc, 4)) { |
| 7365 | break; // Invalid address, stop backtracing |
| 7366 | } |
| 7367 | |
| 7368 | // Move to next frame |
| 7369 | uaecptr prev_fp = get_long_debug(current_fp); |
| 7370 | |
| 7371 | if (prev_fp == 0 || prev_fp == current_fp || !debug_safe_addr(prev_fp, 8)) { |
no test coverage detected