1
0
mirror of https://github.com/pevans/erc-c.git synced 2024-12-21 08:30:55 +00:00

Add new file, vm_debug.c, and help command for it

This commit is contained in:
Peter Evans 2018-02-23 21:58:30 -06:00
parent ac5d532a7f
commit a614c1e5df
4 changed files with 135 additions and 0 deletions

60
include/vm_debug.h Normal file
View File

@ -0,0 +1,60 @@
#ifndef _VM_DEBUG_H_
#define _VM_DEBUG_H_
typedef struct {
/*
* Most commands that need an argument will simply use addr1, but a
* few have more than one address--hence addr2.
*/
int addr1;
int addr2;
/*
* If we have a thing we want to work with, but want to leave what
* that is up to the helper func, then you can write it into the
* target.
*
* If a command uses target, followed by an address, that address
* will be in addr1.
*/
char target[256];
} vm_debug_args;
typedef void (*vm_debug_func)(vm_debug_args *);
typedef struct {
/*
* The name field is the full name of the command; each command also
* has an abbreviated form (either is acceptable as input), which is
* defined in the abbrev field.
*/
char *name;
char *abbrev;
/*
* The number of arguments we expect to see
*/
int nargs;
/*
* The function that will do something with the command's input
*/
vm_debug_func handler;
/*
* What do our arguments look like?
*/
char *argdesc;
/*
* What do we do?
*/
char *desc;
} vm_debug_cmd;
#define DEBUG_CMD(x) \
void vm_debug_cmd_##x (vm_debug_args *args)
extern DEBUG_CMD(help);
#endif

View File

@ -25,6 +25,7 @@ set(erc_sources
option.c
vm_area.c
vm_bitfont.c
vm_debug.c
vm_di.c
vm_event.c
vm_reflect.c

27
src/vm_debug.c Normal file
View File

@ -0,0 +1,27 @@
/*
* vm_debug.c
*/
#include <stdio.h>
#include "vm_debug.h"
#include "vm_di.h"
vm_debug_cmd cmdtable[] = {
{ "help", "h", 0, vm_debug_cmd_help, "",
"Print out this list of commands", },
};
#define CMDTABLE_SIZE (sizeof(cmdtable) / sizeof(vm_debug_cmd))
DEBUG_CMD(help)
{
FILE *stream = (FILE *)vm_di_get(VM_OUTPUT);
vm_debug_cmd *cmd;
for (int i = 0; i < CMDTABLE_SIZE; i++) {
cmd = &cmdtable[i];
fprintf(stream, "%-15s%-5s%-15s%s\n", cmd->name, cmd->abbrev,
cmd->argdesc, cmd->desc);
}
}

47
tests/vm_debug.c Normal file
View File

@ -0,0 +1,47 @@
#include <criterion/criterion.h>
#include <stdio.h>
#include <stdlib.h>
#include "vm_debug.h"
#include "vm_di.h"
static char buf[BUFSIZ];
static FILE *stream = NULL;
static vm_debug_args args;
static void
setup()
{
// Don't worry...we don't care about /dev/null, because we'll hijack
// the output with setvbuf below
stream = fopen("/dev/null", "w");
if (stream == NULL) {
perror("Could not open /dev/null for stream testing");
exit(1);
}
vm_di_set(VM_OUTPUT, stream);
// Writing to stream will now write to buf
setvbuf(stream, buf, _IOFBF, BUFSIZ);
}
static void
teardown()
{
memset(buf, 0, BUFSIZ);
fclose(stream);
}
TestSuite(vm_debug, .init = setup, .fini = teardown);
/*
* I dunno what we'll end up printing! It'll change as we add commands,
* so we just test if we print _something_.
*/
Test(vm_debug, cmd_help)
{
vm_debug_cmd_help(&args);
cr_assert_neq(strlen(buf), 0);
printf("%s", buf);
}