Skip to content

Commit

Permalink
New Disassembler API
Browse files Browse the repository at this point in the history
  • Loading branch information
dd86k committed Mar 8, 2024
1 parent 50c849f commit d8d4bb3
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 134 deletions.
8 changes: 4 additions & 4 deletions app/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ immutable setting_platform_t[] platforms = [
// Syntaxes

struct setting_syntax_t {
AdbgDasmSyntax val;
AdbgDisSyntax val;
const(char)* opt, desc;
}
immutable setting_syntax_t[] syntaxes = [
{ AdbgDasmSyntax.att, "att", "AT&T syntax" },
{ AdbgDasmSyntax.intel, "intel", "Intel syntax" },
{ AdbgDisSyntax.att, "att", "AT&T syntax" },
{ AdbgDisSyntax.intel, "intel", "Intel syntax" },
];

//
Expand Down Expand Up @@ -84,7 +84,7 @@ struct settings_t {
int dump_options; /// Dumper options
long dump_base_address; /// Dumper base address (org)
AdbgMachine machine; /// Disassembler: Target machine
AdbgDasmSyntax syntax; /// Disassembler: Syntax
AdbgDisSyntax syntax; /// Disassembler: Syntax
}

/// Global variables. Helps keeping track of app variables.
Expand Down
18 changes: 7 additions & 11 deletions app/dumper.d
Original file line number Diff line number Diff line change
Expand Up @@ -416,20 +416,16 @@ int dump_disassemble_object(ref Dumper dump, adbg_object_t *o,

int dump_disassemble(ref Dumper dump, AdbgMachine machine,
void* data, ulong size, ulong base_address) {
adbg_disassembler_t *dasm = cast(adbg_disassembler_t*)malloc(adbg_disassembler_t.sizeof);
if (dasm == null)
quitext(ErrSource.crt);
scope(exit) free(dasm);

if (adbg_dasm_open(dasm, machine))
adbg_disassembler_t *dis = adbg_dis_open(machine);
if (dis == null)
quitext(ErrSource.adbg);
scope(exit) adbg_dasm_close(dasm);
scope(exit) adbg_dis_close(dis);

if (globals.syntax)
adbg_dasm_options(dasm, AdbgDasmOption.syntax, globals.syntax, 0);
adbg_dis_options(dis, AdbgDisOpt.syntax, globals.syntax, 0);

adbg_opcode_t op = void;
adbg_dasm_start(dasm, data, cast(size_t)size, base_address);
adbg_dis_start(dis, data, cast(size_t)size, base_address);

// stats mode
if (dump.selected_disasm_stats()) {
Expand All @@ -439,7 +435,7 @@ int dump_disassemble(ref Dumper dump, AdbgMachine machine,
uint stat_total; /// total instruction count
uint stat_illegal; /// Number of illegal instructions
L_STAT:
switch (adbg_dasm(dasm, &op)) with (AdbgError) {
switch (adbg_dis_step(dis, &op)) with (AdbgError) {
case success:
stat_avg += op.size;
++stat_total;
Expand Down Expand Up @@ -467,7 +463,7 @@ L_STAT:

// normal disasm mode
L_DISASM:
switch (adbg_dasm(dasm, &op)) with (AdbgError) {
switch (adbg_dis_step(dis, &op)) with (AdbgError) {
case success:
print_disasm_line(&op);
goto L_DISASM;
Expand Down
63 changes: 25 additions & 38 deletions app/shell.d
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ int shell_loop() {
}

if (globals.file || globals.pid) {
if (adbg_dasm_open(&dasm, adbg_process_get_machine(process))) {
dasm_available = false;
dis = adbg_dis_open(adbg_process_get_machine(process));
if (dis == null) {
printf("warning: Disassembler not available (%s)\n",
adbg_error_msg());
} else dasm_available = true;
}

if (globals.syntax && dasm_available)
adbg_dasm_options(&dasm, AdbgDasmOption.syntax, globals.syntax, 0);
if (globals.syntax && dis)
adbg_dis_options(dis, AdbgDisOpt.syntax, globals.syntax, 0);
}

coninit();
Expand Down Expand Up @@ -163,12 +163,8 @@ int shell_execv(int argc, const(char) **argv) {
private:
__gshared:

adbg_process_t* process;
//TODO: Make disassembler return instance pointer
adbg_disassembler_t dasm;

//TODO: Rely on disassembler pointer instead
bool dasm_available;
adbg_process_t *process;
adbg_disassembler_t *dis;

void function(const(char)* sev, const(char)* msg) userlog;

Expand Down Expand Up @@ -497,7 +493,7 @@ immutable(command2_t)* shell_findcommand(const(char) *ucommand) {
//TODO: shell_attach

void shell_event_disassemble(size_t address, int count = 1, bool showAddress = true) {
if (dasm_available == false)
if (dis == null)
return;

for (int i; i < count; ++i) {
Expand All @@ -508,7 +504,7 @@ void shell_event_disassemble(size_t address, int count = 1, bool showAddress = t
return;
}
adbg_opcode_t op = void;
if (adbg_dasm_once(&dasm, &op, data.ptr, RDSZ)) {
if (adbg_dis_once(dis, &op, data.ptr, RDSZ)) {
printf("%8llx (error:%s)\n", cast(ulong)address, adbg_error_msg);
return;
}
Expand Down Expand Up @@ -655,11 +651,11 @@ int command_spawn(int argc, const(char) **argv) {
serror("Could not spawn process.");
return ShellError.alicedbg;
}
if (adbg_dasm_open(&dasm, adbg_process_get_machine(process))) {
dasm_available = false;
dis = adbg_dis_open(adbg_process_get_machine(process));
if (dis == null) {
printf("warning: Disassembler not available (%s)\n",
adbg_error_msg());
} else dasm_available = true;
}

return 0;
}
Expand All @@ -679,11 +675,11 @@ int command_attach(int argc, const(char) **argv) {
serror("Could not attach to process.");
return ShellError.alicedbg;
}
if (adbg_dasm_open(&dasm, adbg_process_get_machine(process))) {
dasm_available = false;
dis = adbg_dis_open(adbg_process_get_machine(process));
if (dis == null) {
printf("warning: Disassembler not available (%s)\n",
adbg_error_msg());
} else dasm_available = true;
}

return 0;
}
Expand All @@ -693,8 +689,7 @@ int command_detach(int argc, const(char) **argv) {
serror("Could not detach process.");
return ShellError.alicedbg;
}
if (dasm_available)
adbg_dasm_close(&dasm);
adbg_dis_close(dis);

return 0;
}
Expand All @@ -712,13 +707,6 @@ int command_restart(int argc, const(char) **argv) {
serror("Could not attach process.");
return ShellError.alicedbg;
}
if (dasm_available)
adbg_dasm_close(&dasm);
if (adbg_dasm_open(&dasm, adbg_process_get_machine(process))) {
dasm_available = false;
printf("warning: Disassembler not available (%s)\n",
adbg_error_msg());
} else dasm_available = true;
puts("Debugger re-attached");
break;
case spawned:
Expand All @@ -733,20 +721,20 @@ int command_restart(int argc, const(char) **argv) {
serror("Could not spawn process.");
return ShellError.alicedbg;
}
if (dasm_available)
adbg_dasm_close(&dasm);
if (adbg_dasm_open(&dasm, adbg_process_get_machine(process))) {
dasm_available = false;
printf("warning: Disassembler not available (%s)\n",
adbg_error_msg());
} else dasm_available = true;
puts("Process respawned");
break;
default:
serror("No process attached or spawned.");
return 0;
}

adbg_dis_close(dis);
dis = adbg_dis_open(adbg_process_get_machine(process));
if (dis == null) {
printf("warning: Disassembler not available (%s)\n",
adbg_error_msg());
}

return 0;
}

Expand Down Expand Up @@ -885,11 +873,10 @@ int command_maps(int argc, const(char) **argv) {

//TODO: start,+length start,end syntax
int command_disassemble(int argc, const(char) **argv) {
if (dasm_available == false)
if (dis == null)
return ShellError.unavailable;
if (argc < 2) {
if (argc < 2)
return ShellError.missingOption;
}

long uaddress = void;
if (unformat64(&uaddress, argv[1]))
Expand Down
36 changes: 16 additions & 20 deletions examples/simple.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module examples.simple;

import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.ctype : isprint;
import adbg;

extern (C):
Expand All @@ -19,12 +20,11 @@ int main(int argc, const(char) **argv) {
if (process == null)
die;

feature_disasm = adbg_dasm_open(&dasm, adbg_process_get_machine(process)) == 0;
if (feature_disasm == false)
printf("warning: Disassembler unavailable (%s)", adbg_error_msg);
dis = adbg_dis_open(adbg_process_get_machine(process));
if (dis == null)
printf("warning: Disassembler unavailable (%s)\n", adbg_error_msg());

// Process input
LOOP:
LOOP: // Process input
switch (choice("Action [?=Help]")) {
case '?':
puts(
Expand Down Expand Up @@ -61,40 +61,36 @@ void die(int code = 0, const(char) *reason = null) {
}

int choice(const(char) *msg) {
import core.stdc.ctype : isprint;
printf("\n%s: ", msg);
INPUT:
int c = getchar;
LINPUT: int c = getchar;
if (isprint(c)) return c;
goto INPUT;
goto LINPUT;
}

__gshared adbg_process_t *process;
__gshared adbg_disassembler_t dasm;
__gshared bool feature_disasm;
__gshared adbg_disassembler_t *dis;

void loop_handler(adbg_exception_t *ex) {
__gshared uint ex_num; /// Exception counter
printf(
"\n----------------------------------------\n"~
"* EXCEPTION #%u: %s ("~ADBG_OS_ERROR_FORMAT~")\n"~
"* EXCEPTION ("~ADBG_OS_ERROR_FORMAT~"): %s\n"~
"* PID=%u TID=%u\n"~
"* FAULT=%8llx ",
ex_num++, adbg_exception_name(ex), ex.oscode,
"* FAULT=%8llx",
ex.oscode, adbg_exception_name(ex),
ex.pid, ex.tid,
ex.fault_address
);

// Print disassembly if available
if (feature_disasm && ex.faultz) {
if (dis && ex.faultz) {
adbg_opcode_t op = void;
if (adbg_dasm_process_once(&dasm, &op, process, ex.fault_address)) {
printf(" (error:%s)\n", adbg_error_msg);
if (adbg_dis_process_once(dis, &op, process, ex.fault_address)) {
printf(" (error:%s)\n", adbg_error_msg);
return;
}
if (op.operands)
printf(" (%s %s)\n", op.mnemonic, op.operands);
printf(" (%s %s)\n", op.mnemonic, op.operands);
else
printf(" (%s)\n", op.mnemonic);
printf(" (%s)\n", op.mnemonic);
}
}
Loading

0 comments on commit d8d4bb3

Please sign in to comment.