ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
emu_trace.c
Go to the documentation of this file.
1
14
15#include "emu_trace.h"
16
17#include <stdio.h>
18
19#include "emu_elf.h"
21
22/* =============================================================================
23 * Function-entry seam helpers -- emulate "return <r0> to LR" and hook one ELF
24 * symbol's entry to a C callback. Shared by the USB host-mode and virtual-
25 * keyboard seams below. The Ethernet frame seam that first introduced them was
26 * retired once board_periph_eth modelled the R-Switch (ESWM/ETHA/RMAC/GWCA)
27 * registers, so the genuine ra8_eth driver now runs the descriptor DMA path.
28 * =============================================================================
29 */
30
32void eth_hook_return(uc_engine* uc, uint32_t r0)
33{
34 uint32_t lr = 0U;
35 (void)uc_reg_read(uc, UC_ARM_REG_LR, &lr);
36 (void)uc_reg_write(uc, UC_ARM_REG_R0, &r0);
37 uint32_t pc = lr & ~1U; /* drop the Thumb bit; the M-class core stays Thumb. */
38 (void)uc_reg_write(uc, UC_ARM_REG_PC, &pc);
39 (void)uc_emu_stop(uc); /* relaunch from the returned PC (chunk contract). */
40}
41
43void eth_seam_hook(uc_engine* uc, const emu_elf_source_t* elf, const char* name, void* cb)
44{
45 const uint32_t addr = elf_sym_addr(elf, name, nullptr);
46 if (addr == 0U) {
47 return;
48 }
49 enum : uint8_t {
50 k_seam_hook_slots = 24U,
51 };
52 static uc_hook s_handles[k_seam_hook_slots];
53 static uint32_t s_handle_n;
54 if (s_handle_n < (uint32_t)(sizeof(s_handles) / sizeof(s_handles[0]))) {
55 (void)uc_hook_add(uc, &s_handles[s_handle_n], UC_HOOK_CODE, cb, nullptr, addr, addr);
56 s_handle_n++;
57 }
58}
59
72RA8_INTERNAL static void
73internal_on_sym_trace(uc_engine* uc, uint64_t address, uint32_t size, void* user)
74{
75 (void)size;
76 uint32_t lr = 0U;
77 (void)uc_reg_read(uc, UC_ARM_REG_LR, &lr);
78 (void)priv_emu_io_errf(" [symtrace] %s @ 0x%08X (LR 0x%08X)\n",
79 (const char*)user,
80 (unsigned)address,
81 lr);
82}
83
98void sym_trace_install(uc_engine* uc,
99 const emu_elf_source_t* elf,
100 const char* const* names,
101 uint32_t count)
102{
103 static uc_hook s_th[k_trace_sym_max];
104 for (uint32_t i = 0U; (i < count) && (i < (uint32_t)k_trace_sym_max); i++) {
105 const uint32_t addr = elf_sym_addr(elf, names[i], nullptr);
106 if (addr == 0U) {
107 (void)priv_emu_io_errf(" [symtrace] %s: symbol not found -- skipped\n", names[i]);
108 continue;
109 }
110 (void)uc_hook_add(uc,
111 &s_th[i],
112 UC_HOOK_CODE,
114 (void*)names[i],
115 addr,
116 addr);
117 (void)priv_emu_io_errf(" [symtrace] %s armed @ 0x%08X\n", names[i], (unsigned)addr);
118 }
119}
ELF32 image services for the board emulator (load / symbols / vectors).
uint32_t elf_sym_addr(const emu_elf_source_t *elf, const char *name, uint32_t *size_out)
Resolve a function symbol's entry address from the ELF .symtab.
Bounded raw-descriptor I/O seam for the RA8 emulator.
emu_io_result_t priv_emu_io_errf(const char *format,...)
Format bounded text and write it to the injected error descriptor.
void sym_trace_install(uc_engine *uc, const emu_elf_source_t *elf, const char *const *names, uint32_t count)
Install a --trace-sym entry hook for every requested symbol present.
Definition emu_trace.c:98
void eth_seam_hook(uc_engine *uc, const emu_elf_source_t *elf, const char *name, void *cb)
Hook one symbol (if present) to cb; record it for the report.
Definition emu_trace.c:43
static RA8_INTERNAL void internal_on_sym_trace(uc_engine *uc, uint64_t address, uint32_t size, void *user)
Perform on sym trace for the emu trace model.
Definition emu_trace.c:73
void eth_hook_return(uc_engine *uc, uint32_t r0)
Emulate "return r0;" from a hooked function: set R0, branch to LR.
Definition emu_trace.c:32
Function-entry seam glue + the –trace-sym instrument.
@ k_trace_sym_max
Max –trace-sym functions per run.
Definition emu_trace.h:45
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
One independently owned immutable raw-descriptor ELF source.
Definition emu_elf.h:91