Implement the modular backend for handling files

This commit is contained in:
Pierre Pronchery 2015-07-07 21:48:34 +02:00
parent cb4a273e5f
commit df75938bd8
5 changed files with 239 additions and 2 deletions

148
tools/backend/asm.c Normal file
View File

@ -0,0 +1,148 @@
/* $Id$ */
/* Copyright (c) 2015 Pierre Pronchery <khorben@defora.org> */
/* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the authors nor the names of the contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY ITS AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. */
#include <stdarg.h>
#include <string.h>
#include <glib.h>
#include <System.h>
#include <Devel/Asm.h>
#include "../debugger.h"
/* asm */
/* private */
typedef struct _DebuggerBackend AsmBackend;
struct _DebuggerBackend
{
DebuggerBackendHelper const * helper;
Asm * a;
AsmCode * code;
guint source;
};
/* prototypes */
/* plug-in */
static AsmBackend * _asm_init(DebuggerBackendHelper const * helper);
static void _asm_destroy(AsmBackend * backend);
static int _asm_open(AsmBackend * backend, char const * arch,
char const * format, char const * filename);
static int _asm_close(AsmBackend * backend);
/* useful */
/* constants */
static DebuggerBackendDefinition _asm_definition =
{
"asm",
NULL,
LICENSE_GNU_LGPL3_FLAGS,
_asm_init,
_asm_destroy,
_asm_open,
_asm_close
};
/* protected */
/* functions */
/* plug-in */
/* asm_init */
static AsmBackend * _asm_init(DebuggerBackendHelper const * helper)
{
AsmBackend * backend;
if((backend = object_new(sizeof(*backend))) == NULL)
return NULL;
backend->helper = helper;
backend->a = NULL;
backend->code = NULL;
backend->source = 0;
return backend;
}
/* asm_destroy */
static void _asm_destroy(AsmBackend * backend)
{
_asm_close(backend);
object_delete(backend);
}
/* asm_open */
/* callbacks */
static gboolean _open_on_idle(gpointer data);
static int _asm_open(AsmBackend * backend, char const * arch,
char const * format, char const * filename)
{
if(_asm_close(backend) != 0)
return -1;
if((backend->a = asm_new(arch, format)) == NULL)
return -1;
if((backend->code = asm_open_deassemble(backend->a, filename, TRUE))
== NULL)
{
asm_delete(backend->a);
backend->a = NULL;
return -1;
}
else
backend->source = g_idle_add(_open_on_idle, backend);
return 0;
}
static gboolean _open_on_idle(gpointer data)
{
AsmBackend * backend = data;
AsmArchRegister const * registers;
size_t cnt = 0;
backend->source = 0;
if((registers = asmcode_get_arch_registers(backend->code)) != NULL)
for(cnt = 0; registers[cnt].name != NULL; cnt++);
backend->helper->set_registers(backend->helper->debugger, registers,
cnt);
return FALSE;
}
/* asm_close */
static int _asm_close(AsmBackend * backend)
{
if(backend->source != 0)
g_source_remove(backend->source);
backend->source = 0;
if(backend->a != NULL)
asm_delete(backend->a);
backend->a = NULL;
return 0;
}

View File

@ -0,0 +1,11 @@
targets=asm
cflags_force=-W
cflags=-Wall -g -O2 -pedantic -fstack-protector
ldflags=
dist=Makefile
[asm]
type=plugin
cflags=`pkg-config --cflags glib-2.0 Asm` -fPIC
ldflags=`pkg-config --libs glib-2.0 Asm`
sources=asm.c

View File

@ -62,6 +62,12 @@ enum { RV_NAME = 0, RV_VALUE, RV_VALUE_DISPLAY };
struct _Debugger
{
/* backend */
DebuggerBackendHelper bhelper;
Plugin * bplugin;
DebuggerBackendDefinition * bdefinition;
DebuggerBackend * backend;
/* debug */
DebuggerDebugHelper dhelper;
Plugin * dplugin;
@ -96,6 +102,9 @@ static gboolean _debugger_confirm_reset(Debugger * debugger);
/* helpers */
static int _debugger_helper_error(Debugger * debugger, int code,
char const * format, ...);
/* backend */
static void _debugger_helper_backend_set_registers(Debugger * debugger,
AsmArchRegister const * registers, size_t registers_cnt);
/* callbacks */
static void _debugger_on_about(gpointer data);
@ -175,6 +184,7 @@ static DesktopToolbar _debugger_toolbar[] =
/* XXX load at run-time */
#include "backend/asm.c"
#include "debug/ptrace.c"
@ -193,6 +203,14 @@ Debugger * debugger_new(void)
if((debugger = object_new(sizeof(*debugger))) == NULL)
return NULL;
/* backend */
debugger->bhelper.debugger = debugger;
debugger->bhelper.error = _debugger_helper_error;
debugger->bhelper.set_registers
= _debugger_helper_backend_set_registers;
debugger->bplugin = NULL;
debugger->bdefinition = &_asm_definition; /* XXX */
debugger->backend = NULL;
/* debug */
debugger->dhelper.debugger = debugger;
debugger->dhelper.error = _debugger_helper_error;
@ -201,6 +219,13 @@ Debugger * debugger_new(void)
debugger->debug = NULL;
/* child */
debugger->filename = NULL;
/* check for errors */
if((debugger->backend = debugger->bdefinition->init(&debugger->bhelper))
== NULL)
{
debugger_delete(debugger);
return NULL;
}
/* widgets */
accel = gtk_accel_group_new();
debugger->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
@ -377,9 +402,15 @@ int debugger_open(Debugger * debugger, char const * arch, char const * format,
return debugger_open_dialog(debugger, arch, format);
if(debugger_close(debugger) != 0)
return -debugger_error(debugger, error_get(), 1);
/* FIXME really implement */
if((debugger->filename = strdup(filename)) == NULL)
return -1;
if(debugger->bdefinition->open(debugger->backend, arch, format,
filename) != 0)
{
free(debugger->filename);
debugger->filename = NULL;
return -debugger_error(debugger, error_get(), 1);
}
if((s = string_new_append(_("Debugger"), " - ", filename, NULL))
!= NULL)
gtk_window_set_title(GTK_WINDOW(debugger->window), s);
@ -703,6 +734,26 @@ static int _debugger_helper_error(Debugger * debugger, int code,
}
/* helpers: backend */
/* debugger_helper_backend_set_registers */
static void _debugger_helper_backend_set_registers(Debugger * debugger,
AsmArchRegister const * registers, size_t registers_cnt)
{
GtkTreeModel * model;
size_t i;
GtkTreeIter iter;
model = gtk_tree_view_get_model(GTK_TREE_VIEW(debugger->reg_view));
gtk_list_store_clear(GTK_LIST_STORE(model));
for(i = 0; i < registers_cnt; i++)
{
gtk_list_store_append(GTK_LIST_STORE(model), &iter);
gtk_list_store_set(GTK_LIST_STORE(model), &iter,
RV_NAME, registers[i].name, -1);
}
}
/* callbacks */
/* debugger_on_about */
static void _debugger_on_about(gpointer data)

View File

@ -30,6 +30,7 @@
# include <stdarg.h>
# include <System.h>
# include <Devel/Asm.h>
/* Debugger */
@ -37,6 +38,30 @@
/* types */
typedef struct _Debugger Debugger;
/* backend */
typedef struct _DebuggerBackend DebuggerBackend;
typedef struct _DebuggerBackendHelper
{
Debugger * debugger;
int (*error)(Debugger * debugger, int code, char const * format, ...);
void (*set_registers)(Debugger * debugger,
AsmArchRegister const * registers,
size_t registers_cnt);
} DebuggerBackendHelper;
typedef const struct _DebuggerBackendDefinition
{
char const * name;
char const * description;
LicenseFlags license;
DebuggerBackend * (*init)(DebuggerBackendHelper const * helper);
void (*destroy)(DebuggerBackend * backend);
int (*open)(DebuggerBackend * backend, char const * arch,
char const * format, char const * filename);
int (*close)(DebuggerBackend * backend);
} DebuggerBackendDefinition;
/* debug */
typedef struct _DebuggerDebug DebuggerDebug;

View File

@ -1,4 +1,4 @@
subdirs=debug,models
subdirs=backend,debug,models
targets=debugger,gdeasm,sequel,simulator
cflags_force=-W `pkg-config --cflags libDesktop`
cflags=-Wall -g -O2 -pedantic -fstack-protector
@ -7,6 +7,8 @@ dist=Makefile,debugger.h,gdeasm.h,sequel.h,simulator.h
[debugger]
type=binary
cflags=`pkg-config --cflags Asm`
ldflags=`pkg-config --libs Asm`
sources=debugger.c,debugger-main.c
install=$(BINDIR)