From cb132d17302199a502af00be238f689e5e786c93 Mon Sep 17 00:00:00 2001 From: Luke Gorrie Date: Sun, 21 May 2017 18:32:52 +0000 Subject: [PATCH] Add lj_auditlog (very preliminary) --- src/Makefile | 2 +- src/lj_auditlog.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/lj_auditlog.h | 16 ++++++++++++++++ src/lj_trace.c | 3 +++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/lj_auditlog.c create mode 100644 src/lj_auditlog.h diff --git a/src/Makefile b/src/Makefile index 896947fb98..0227e719eb 100644 --- a/src/Makefile +++ b/src/Makefile @@ -469,7 +469,7 @@ LJCORE_O= lj_gc.o lj_err.o lj_char.o lj_bc.o lj_obj.o lj_buf.o \ lj_ir.o lj_opt_mem.o lj_opt_fold.o lj_opt_narrow.o \ lj_opt_dce.o lj_opt_loop.o lj_opt_split.o lj_opt_sink.o \ lj_mcode.o lj_snap.o lj_record.o lj_crecord.o lj_ffrecord.o \ - lj_asm.o lj_trace.o lj_gdbjit.o \ + lj_asm.o lj_trace.o lj_gdbjit.o lj_auditlog.o \ lj_ctype.o lj_cdata.o lj_cconv.o lj_ccall.o lj_ccallback.o \ lj_carith.o lj_clib.o lj_cparse.o \ lj_lib.o lj_alloc.o lib_aux.o \ diff --git a/src/lj_auditlog.c b/src/lj_auditlog.c new file mode 100644 index 0000000000..fa1c7fd553 --- /dev/null +++ b/src/lj_auditlog.c @@ -0,0 +1,40 @@ +/* +** Audit log. Records JIT/runtime events for offline analysis. +*/ + +#define lj_auditlog_c + +#include + +#include "lj_auditlog.h" + +FILE *fp; + +/* Ensure that the log file is open. */ +static void ensure_log_open() +{ + if (!fp) { + fp = fopen("audit.log", "w"); + lua_assert(fp != NULL); + } +} + +/* Log a snapshot of an object in memory. */ +static void log(const char *type, void *ptr, unsigned int size) +{ + ensure_log_open(); + fprintf(fp, "type=%s address=%p size=%d data:\n", type, ptr, size); + fwrite(ptr, size, 1, fp); +} + +/* Log a trace that has just been compiled. */ +void lj_auditlog_trace_stop(jit_State *J, GCtrace *T) +{ + /* Log the memory containing the GCtrace object and other important + memory that it references. */ + log("GCtrace", T, sizeof(*T)); + log("MCode[]", T->mcode, T->szmcode); + log("SnapShot[]", T->snap, T->nsnap * sizeof(*T->snap)); + log("SnapEntry[]", T->snapmap, T->nsnapmap * sizeof(*T->snapmap)); +} + diff --git a/src/lj_auditlog.h b/src/lj_auditlog.h new file mode 100644 index 0000000000..63ac71e7b2 --- /dev/null +++ b/src/lj_auditlog.h @@ -0,0 +1,16 @@ +/* +** Audit log. Records JIT/runtime events for offline analysis. +*/ + +#ifndef _LJ_AUDITLOG_H +#define _LJ_AUDITLOG_H + +#include "lj_jit.h" + +void lj_auditlog_trace_flush(jit_State *J); +void lj_auditlog_trace_start(jit_State *J); +void lj_auditlog_trace_stop(jit_State *J, GCtrace *T); +void lj_auditlog_trace_abort(jit_State *J); +void lj_auditlog_trace_record_bytecode(jit_State *J); + +#endif diff --git a/src/lj_trace.c b/src/lj_trace.c index 0b674ec276..21c1e6dafe 100644 --- a/src/lj_trace.c +++ b/src/lj_trace.c @@ -9,6 +9,7 @@ #include "lj_obj.h" +#include "lj_auditlog.h" #include "lj_gc.h" #include "lj_err.h" #include "lj_debug.h" @@ -28,6 +29,7 @@ #include "lj_dispatch.h" #include "lj_vm.h" #include "lj_target.h" +#include "lj_auditlog.h" /* -- Error handling ------------------------------------------------------ */ @@ -122,6 +124,7 @@ static void trace_save(jit_State *J, GCtrace *T) setgcrefp(J->trace[T->traceno], T); lj_gc_barriertrace(J2G(J), T->traceno); lj_gdbjit_addtrace(J, T); + lj_auditlog_trace_stop(J, T); } void lj_trace_free(global_State *g, GCtrace *T)