Add trace function

This commit is contained in:
Mathieu Strypsteen 2024-03-19 10:24:23 +01:00
parent 24848a0280
commit 4d0ab10704
2 changed files with 35 additions and 7 deletions

View file

@ -9,9 +9,6 @@ export default tseslint.config(
parserOptions: { parserOptions: {
project: true project: true
} }
},
rules: {
"@typescript-eslint/no-unused-vars": "off"
} }
}, },
{ {

View file

@ -1,6 +1,6 @@
function allocString(str: string): NativePointer { global.allocString = function allocString(str: string): NativePointer {
return Memory.allocUtf8String(str); return Memory.allocUtf8String(str);
} };
global.backtrace = function backtrace(): DebugSymbol[] { global.backtrace = function backtrace(): DebugSymbol[] {
const context = Process.enumerateThreads()[0].context; const context = Process.enumerateThreads()[0].context;
const backtrace = Thread.backtrace(context); const backtrace = Thread.backtrace(context);
@ -18,7 +18,7 @@ global.pauseAt = function pauseAt(name: string): void {
Thread.sleep(1); Thread.sleep(1);
} }
resume = false; resume = false;
console.log("Resumed"); console.debug("Resumed");
} }
}); });
}; };
@ -33,7 +33,7 @@ global.backtraceAt = function backtraceAt(name: string): void {
const symbols = backtrace.map(address => { const symbols = backtrace.map(address => {
return DebugSymbol.fromAddress(address); return DebugSymbol.fromAddress(address);
}); });
console.log(JSON.stringify(symbols, null, 4)); console.debug(JSON.stringify(symbols, null, 4));
} }
}); });
}; };
@ -42,6 +42,35 @@ global.getFunc = function getFunc(name: string, ret: NativeFunctionReturnType, a
return new NativeFunction(addr, ret, args); return new NativeFunction(addr, ret, args);
}; };
global.readPointer = function readPointer(pointer: NativePointer, type: string): string {
switch (type) {
case "void":
return "void";
case "int":
return pointer.readInt().toString();
case "pointer":
return "\"" + pointer.readUtf8String()! + "\"";
default:
return "undefined";
}
};
global.trace = function trace(name: string, ret_type: string, args_type: string[]): void {
const addr = DebugSymbol.getFunctionByName(name);
Interceptor.attach(addr, {
onEnter(args: InvocationArguments) {
const args_str: string[] = [];
for (let i = 0; i < args_type.length; i++) {
args_str.push(readPointer(args[i], args_type[i]));
}
console.debug(`-> ${name}(${args_str.join(" ")})`);
},
onLeave(retval: InvocationReturnValue) {
console.debug(`<- ${name} = ${readPointer(retval, ret_type)}`);
}
});
};
export { }; export { };
declare global { declare global {
function allocString(str: string): NativePointer; function allocString(str: string): NativePointer;
@ -50,4 +79,6 @@ declare global {
function backtraceAt(name: string): void; function backtraceAt(name: string): void;
function getFunc(name: string, ret: NativeFunctionReturnType, args: NativeFunctionArgumentType[]): NativeFunction<NativeFunctionReturnValue, NativeFunctionArgumentValue[]>; function getFunc(name: string, ret: NativeFunctionReturnType, args: NativeFunctionArgumentType[]): NativeFunction<NativeFunctionReturnValue, NativeFunctionArgumentValue[]>;
function resumeBreakpoints(): void; function resumeBreakpoints(): void;
function readPointer(pointer: NativePointer, type: string): string;
function trace(name: string, ret_str: string, args_str: string[]): void;
} }