Skip to content

Commit

Permalink
app: Make slighter better quit functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dd86k committed Jan 14, 2024
1 parent deb55e6 commit 41b56cc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 40 deletions.
52 changes: 29 additions & 23 deletions app/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module common;

import core.stdc.stdio : puts;
import core.stdc.stdlib : exit;
import core.stdc.string : strerror;
import core.stdc.errno : errno;
import adbg.error;
import adbg.v2.disassembler;
import adbg.v2.debugger.exception;
Expand Down Expand Up @@ -85,9 +87,9 @@ struct settings_t {
/// Global variables. Helps keeping track of app variables.
__gshared settings_t globals;

alias oops = show_adbg_error;
alias oops = show_error;

int show_adbg_error(
int show_error(
const(char)* func = cast(char*)__FUNCTION__,
const(char)* mod = cast(char*)__MODULE__,
int line = __LINE__) {
Expand All @@ -114,30 +116,34 @@ int show_adbg_error(
return error.code;
}

//TODO: Fix terrible hack
// Potentially dangerous since some errors require an additional component
void panic(AdbgError code = AdbgError.success, void *add = null) {
if (code) adbg_oops(code);
exit(oops());
}

//TODO: Finish alternative to panic
// Needs to be able to override error message.
// Cases:
// - Regular errors
// - lib errors on the app side
// - external errors on the lib side
/// Quit program.
///
/// If no codes are given, this picks up the code from alicedbg.
/// Params:
/// message = Quit message.
/// code = Exit code.
/*void quit(int code, const(char) *message = null) {
if (message) {
puts(message);
void quit(int code, const(char) *message) {
puts(message);
exit(code);
}

enum ErrSource {
crt,
adbg,
}

/// Quit due to external factor.
void quitext(ErrSource src,
const(char)* func = cast(char*)__FUNCTION__,
const(char)* mod = cast(char*)__MODULE__,
int line = __LINE__) {
switch (src) {
case ErrSource.crt:
int code = errno;
puts(strerror(code));
exit(code);
} else {
exit(oops());
case ErrSource.adbg:
exit(show_error());
default:
puts("(Unknown source)");
exit(1);
}
}*/
}
29 changes: 13 additions & 16 deletions app/dumper.d
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ int app_dump() {
size_t size = void;
ubyte *buffer = readall(globals.file, &size);
if (buffer == null)
panic(AdbgError.crt);
quitext(ErrSource.crt);

//TODO: Warn file is empty
if (size == 0)
Expand All @@ -129,7 +129,7 @@ int app_dump() {

adbg_object_t *o = adbg_object_open_file(globals.file, 0);
if (o == null)
return show_adbg_error();
return show_error();

print_string("filename", basename(globals.file));
print_u64("filesize", o.file_size);
Expand Down Expand Up @@ -174,27 +174,25 @@ void print_disasm_line(adbg_opcode_t *op, const(char)* msg = null) {
return;
}

// Format machine bytes
enum MBFSZ = (16 * 3) + 2;
// Format and print machine bytes
enum MBFSZ = (16 * 3) + 2; // Enough for 16 bytes and spaces
char[MBFSZ] machine = void;
int left = MBFSZ;
int tl;
int left = MBFSZ; // Buffer left
int tl; // Total length
for (size_t bi; bi < op.size; ++bi) {
int l = snprintf(machine.ptr + tl, left, " %02x", op.machine[bi]);
if (l <= 0) break;
if (l <= 0) break; // Ran out of buffer space
tl += l;
left -= l;
}
machine[tl] = 0;

// Print machine bytes string
printf(" %*s ", -24, machine.ptr);

// Print message or mnemonics
if (msg) {
puts(msg);
return;
}

printf("%*s %s\n", -10, op.mnemonic, op.operands);
}

Expand Down Expand Up @@ -350,13 +348,11 @@ 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)
panic(AdbgError.crt);
quitext(ErrSource.crt);
scope(exit) free(dasm);

if (adbg_dasm_open(dasm, machine)) {
panic();
return 1;
}
if (adbg_dasm_open(dasm, machine))
quitext(ErrSource.adbg);
scope(exit) adbg_dasm_close(dasm);

if (globals.syntax)
Expand Down Expand Up @@ -386,7 +382,8 @@ L_STAT:
++stat_illegal;
goto L_STAT;
case outOfData: break;
default: panic();
default:
quitext(ErrSource.adbg);
}

print_f32("average", cast(float)stat_avg / stat_total, 2);
Expand Down
1 change: 0 additions & 1 deletion app/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ ubyte *readall(const(char) *path, size_t *size) {
FILE *fd = fopen(path, "rb");
if (fd == null)
return null;

scope(exit) fclose(fd);

if (fseek(fd, 0, SEEK_END))
Expand Down

0 comments on commit 41b56cc

Please sign in to comment.