esp32emu / tools /bp.mjs
HelloSun's picture
ESP32 emulator update
d8afd6f verified
Raw History Blame Contribute Delete
1.82 kB
// usage: node bp.mjs flash.bin symbol [--n=300] : run until symbol is executed, then print the function sequence leading to it
import fs from 'fs';
import { ESP32 } from '../web/js/soc.js';
import { Syms } from './sym.mjs';
const romDir = new URL('../web/rom/', import.meta.url).pathname;
const args = process.argv.slice(2);
const symFiles = (args.find(a => a.startsWith('--syms=')) || '--syms=/tmp/opencode/hello.syms').slice(7).split(',').concat(['/tmp/opencode/rom.syms']);
const syms = new Syms(symFiles);
const target = args[1];
let taddr = target.startsWith('0x') ? parseInt(target, 16) : syms.a.find(x => x[1] === target)?.[0];
const soc = new ESP32({});
soc.loadRoms(fs.readFileSync(romDir + 'esp32-v3-rom.bin'), fs.readFileSync(romDir + 'esp32-v3-rom-app.bin'));
soc.setFlash(fs.readFileSync(args[0])); soc.enablePsram(args.includes('--psram'));
soc.onUart = (n, b) => process.stdout.write(String.fromCharCode(b));
soc.reset('power');
const c = soc.cpus[0]; c.enableTrace(); c.bpAddr = taddr;
const nth = parseInt((args.find(a => a.startsWith('--nth=')) || '--nth=1').slice(6));
let hits = 0;
for (let k = 0; k < 100000; k++) { soc.run(1000); if (c.bpHit) { c.bpHit = false; if (++hits >= nth) break; c.bpSkip = true; } }
console.log('\nhit', hits, 'at', (c.pc >>> 0).toString(16), syms.name(c.pc), 'time', soc.timeNs / 1e9);
const regs = []; for (let i = 0; i < 16; i++) regs.push((c.ar[(c.wb4 + i) & 63] >>> 0).toString(16)); console.log('regs', regs.join(' '));
const tr = c.getTrace(parseInt((args.find(a => a.startsWith('--n=')) || '--n=60000').slice(4)));
let last = ''; const seq = []; for (const p of tr) { const n = syms.name(p).replace(/\+.*/, ''); if (n !== last) { seq.push(n); last = n; } }
console.log(seq.slice(-parseInt((args.find(a => a.startsWith('--k=')) || '--k=80').slice(4))).join(' > '));