[Pkg-clamav-commits] [SCM] Debian repository for ClamAV branch, debian/unstable, updated. debian/0.95+dfsg-1-6156-g094ec9b

Török Edvin edwin at clamav.net
Sun Apr 4 01:11:17 UTC 2010


The following commit has been merged in the debian/unstable branch:
commit 0a11015bf52f5367d240f93cf42944fae6301ddc
Author: Török Edvin <edwin at clamav.net>
Date:   Wed Dec 9 16:50:55 2009 +0200

    Refactor trace API, so that trace printing happens in clambc.
    
    This will allow clambc to implement breakpoints, and watchpoints.

diff --git a/clambc/bcrun.c b/clambc/bcrun.c
index 55647e1..5c9c6e6 100644
--- a/clambc/bcrun.c
+++ b/clambc/bcrun.c
@@ -52,6 +52,63 @@ static void help(void)
     return;
 }
 
+static struct dbg_state {
+    const char *directory;
+    const char *file;
+    const char *scope;
+    uint32_t scopeid;
+    unsigned line;
+    unsigned col;
+    unsigned showline;
+} dbg_state;
+
+static void tracehook(struct cli_bc_ctx *ctx, unsigned event)
+{
+    dbg_state.directory = ctx->directory;
+    if (*ctx->file == '?')
+	return;
+    switch (event) {
+	case trace_func:
+	    printf("[trace] %s:%u:%u -> %s:%u:%u Entered function %s\n",
+		   dbg_state.file, dbg_state.line, dbg_state.col,
+		   ctx->file, ctx->line, ctx->col, ctx->scope);
+	    dbg_state.scope = ctx->scope;
+	    break;
+	case trace_param:
+	    printf("[trace] function parameter:\n");
+	    return;
+	case trace_scope:
+	    printf("[trace] %s:%u:%u -> %s:%u:%u\n",
+		   dbg_state.file, dbg_state.line, dbg_state.col,
+		   ctx->file, ctx->line, ctx->col);
+	    dbg_state.scope = ctx->scope;
+	    break;
+	case trace_line:
+	case trace_col:
+	    if (dbg_state.showline)
+		cli_bytecode_debug_printsrc(ctx);
+	    else
+		printf("[trace] %s:%u:%u\n",
+		       dbg_state.file, dbg_state.line, dbg_state.col);
+	    break;
+	default:
+	    break;
+    }
+    dbg_state.file = ctx->file;
+    dbg_state.line = ctx->line;
+    dbg_state.col = ctx->col;
+}
+
+static void tracehook_op(struct cli_bc_ctx *ctx, const char *op)
+{
+    printf("[trace] %s\n", op);
+}
+
+static void tracehook_val(struct cli_bc_ctx *ctx, const char *name, uint32_t value)
+{
+    printf("[trace] %s = %u\n", name, value);
+}
+
 int main(int argc, char *argv[])
 {
     FILE *f;
@@ -142,7 +199,15 @@ int main(int argc, char *argv[])
 	fprintf(stderr,"Out of memory\n");
 	exit(3);
     }
-    ctx->trace_mask = BC_TRACE_ALL;
+    memset(&dbg_state, 0, sizeof(dbg_state));
+    dbg_state.file = "<libclamav>";
+    dbg_state.line = 0;
+    dbg_state.col = 0;
+    dbg_state.showline = 1;
+    cli_bytecode_context_set_trace(ctx, trace_val,
+				   tracehook,
+				   tracehook_op,
+				   tracehook_val);
 
     if (opts->filename[1]) {
 	funcid = atoi(opts->filename[1]);
diff --git a/libclamav/bytecode.c b/libclamav/bytecode.c
index 27f4e2b..6674ebf 100644
--- a/libclamav/bytecode.c
+++ b/libclamav/bytecode.c
@@ -52,13 +52,16 @@ struct cli_bc_ctx *cli_bytecode_context_alloc(void)
     ctx->outfd = -1;
     ctx->tempfile = NULL;
     ctx->written = 0;
-    ctx->trace_mask = 0;
+    ctx->trace_level = trace_none;
+    ctx->trace = NULL;
+    ctx->trace_op = NULL;
+    ctx->trace_val = NULL;
     ctx->scope = NULL;
     ctx->scopeid = 0;
-    ctx->file = NULL;
-    ctx->directory = NULL;
-    ctx->lastline = 0;
-    ctx->lastcol = 0;
+    ctx->file = "??";
+    ctx->directory = "";
+    ctx->line = 0;
+    ctx->col = 0;
     return ctx;
 }
 
diff --git a/libclamav/bytecode.h b/libclamav/bytecode.h
index f0407ff..1f56651 100644
--- a/libclamav/bytecode.h
+++ b/libclamav/bytecode.h
@@ -107,9 +107,18 @@ int cli_bytecode_runhook(const struct cl_engine *engine, struct cli_bc_ctx *ctx,
 extern "C" {
 #endif
 
+int bytecode_init(void);
+/* Bytecode internal debug API */
 void cli_bytecode_debug(int argc, char **argv);
 void cli_bytecode_debug_printsrc(const struct cli_bc_ctx *ctx);
-int bytecode_init(void);
+
+typedef void (*bc_dbg_callback_trace)(struct cli_bc_ctx*, unsigned event);
+typedef void (*bc_dbg_callback_trace_op)(struct cli_bc_ctx*, const char *op);
+typedef void (*bc_dbg_callback_trace_val)(struct cli_bc_ctx*, const char *name, uint32_t value);
+void cli_bytecode_context_set_trace(struct cli_bc_ctx*, unsigned mask,
+				    bc_dbg_callback_trace,
+				    bc_dbg_callback_trace_op,
+				    bc_dbg_callback_trace_val);
 
 #ifdef __cplusplus
 }
diff --git a/libclamav/bytecode_api.c b/libclamav/bytecode_api.c
index e781838..3c4cd62 100644
--- a/libclamav/bytecode_api.c
+++ b/libclamav/bytecode_api.c
@@ -27,6 +27,7 @@
 #include <errno.h>
 #include "cltypes.h"
 #include "clambc.h"
+#include "bytecode.h"
 #include "bytecode_priv.h"
 #include "type_desc.h"
 #include "bytecode_api.h"
@@ -132,108 +133,87 @@ int32_t cli_bcapi_write(struct cli_bc_ctx *ctx, uint8_t*data, int32_t len)
     return res;
 }
 
+void cli_bytecode_context_set_trace(struct cli_bc_ctx* ctx, enum trace_level level,
+				    bc_dbg_callback_trace trace,
+				    bc_dbg_callback_trace_op trace_op,
+				    bc_dbg_callback_trace_val trace_val)
+{
+    ctx->trace = trace;
+    ctx->trace_op = trace_op;
+    ctx->trace_val = trace_val;
+    ctx->trace_level = level;
+}
+
 uint32_t cli_bcapi_trace_scope(struct cli_bc_ctx *ctx, const const uint8_t *scope, uint32_t scopeid)
 {
-    if (LIKELY(!ctx->trace_mask))
+    if (LIKELY(!ctx->trace_level))
 	return 0;
-    if ((ctx->trace_mask&BC_TRACE_FUNC) && (scope != ctx->scope)) {
-	ctx->scope = scope;
-	ctx->trace_mask |= BC_TRACE_TMP_FUNC;
-    }
-    if ((ctx->trace_mask&BC_TRACE_SCOPE) && (scopeid != ctx->scopeid)) {
+    if (ctx->scope != (const char*)scope) {
+	ctx->scope = (const char*)scope ? (const char*)scope : "?";
 	ctx->scopeid = scopeid;
-	ctx->trace_mask |= BC_TRACE_TMP_SCOPE;
+	ctx->trace_level |= 0x80;/* temporarely increase level to print params */
+    } else if ((ctx->trace_level >= trace_scope) && ctx->scopeid != scopeid) {
+	ctx->scopeid = scopeid;
+	ctx->trace_level |= 0x40;/* temporarely increase level to print location */
     }
+    return 0;
 }
 
-uint32_t cli_bcapi_trace_source(struct cli_bc_ctx *ctx, const const uint8_t *file, uint32_t line)
+uint32_t cli_bcapi_trace_directory(struct cli_bc_ctx *ctx, const const uint8_t* dir, uint32_t dummy)
 {
-    if (LIKELY(!ctx->trace_mask))
+    if (LIKELY(!ctx->trace_level))
 	return 0;
-    if (ctx->trace_mask&BC_TRACE_TMP_FUNC) {
-	cli_dbgmsg("[trace] Entering function %s (%s:%u:%u -> %s:%u)\n",
-		   ctx->scope,
-		   ctx->file ? ctx->file : "??", ctx->lastline,
-		   ctx->lastcol, file ? file : "??", line);
-	ctx->file = file;
-	ctx->lastline = line;
-	cli_bytecode_debug_printsrc(ctx);
-    } else if (ctx->trace_mask&BC_TRACE_TMP_SCOPE) {
-	cli_dbgmsg("[trace] Entering scope (%s:%u:%u -> %s:%u)\n",
-		   ctx->file ? ctx->file : "??", ctx->lastline,
-		   ctx->lastcol, file ? file : "??", line,
-		   ctx->scope);
-	ctx->file = file;
-	ctx->lastline = line;
-	cli_bytecode_debug_printsrc(ctx);
-    } else {
-	if (ctx->file != file || ctx->lastline != line) {
-	    ctx->file = file;
-	    ctx->lastline = line;
-	    if (ctx->trace_mask&BC_TRACE_LINE)
-		ctx->trace_mask |= BC_TRACE_TMP_SRC;
-	}
-    }
-    ctx->trace_mask &= ~(BC_TRACE_TMP_FUNC|BC_TRACE_TMP_SCOPE);
+    ctx->directory = (const char*)dir ? (const char*)dir : "";
     return 0;
 }
 
-uint32_t cli_bcapi_trace_op(struct cli_bc_ctx *ctx, const const uint8_t *op, uint32_t col)
+uint32_t cli_bcapi_trace_source(struct cli_bc_ctx *ctx, const const uint8_t *file, uint32_t line)
 {
-    if (LIKELY(!ctx->trace_mask))
+    if (LIKELY(ctx->trace_level < trace_line))
 	return 0;
-    if (ctx->lastcol != col) {
-	ctx->lastcol = col;
-	if (ctx->trace_mask&BC_TRACE_COL)
-	    ctx->trace_mask |= BC_TRACE_TMP_SRC;
+    if (ctx->file != (const char*)file || ctx->line != line) {
+	ctx->col = 0;
+	ctx->file =(const char*)file ? (const char*)file : "??";
+	ctx->line = line;
     }
-    if ((ctx->trace_mask&BC_TRACE_OP) && op) {
-	if (ctx->trace_mask&BC_TRACE_TMP_SRC) {
-	    cli_dbgmsg("[trace] %s (@%s:%u:%u)\n",
-		       op,
-		       ctx->file ? ctx->file : "??", ctx->lastline, col);
-	    cli_bytecode_debug_printsrc(ctx);
-	    ctx->trace_mask &= ~BC_TRACE_TMP_SRC;
-	} else
-	    cli_dbgmsg("[trace] %s\n", op);
-    }
-    ctx->trace_mask |= BC_TRACE_TMP_OP;
     return 0;
 }
 
-uint32_t cli_bcapi_trace_value(struct cli_bc_ctx *ctx, const const uint8_t* name, uint32_t value)
+uint32_t cli_bcapi_trace_op(struct cli_bc_ctx *ctx, const const uint8_t *op, uint32_t col)
 {
-    if (LIKELY(!ctx->trace_mask))
-	return 0;
-    if ((ctx->trace_mask&BC_TRACE_PARAM) && !(ctx->trace_mask&BC_TRACE_TMP_OP)) {
-	if (name)
-	    cli_dbgmsg("[trace] param %s = %u\n", name, value);
-	ctx->trace_mask &= ~BC_TRACE_TMP_OP;
+    if (LIKELY(ctx->trace_level < trace_col))
 	return 0;
+    if (ctx->trace_level&0xc0) {
+	ctx->col = col;
+	/* func/scope changed and they needed param/location event */
+	ctx->trace(ctx, (ctx->trace_level&0x80) ? trace_func : trace_scope);
+	ctx->trace_level &= ~0xc0;
     }
-    if ((ctx->trace_mask&BC_TRACE_VAL) && name) {
-	if (ctx->trace_mask&BC_TRACE_TMP_SRC) {
-	    cli_dbgmsg("[trace] %s = %u (@%s:%u:%u)\n",
-		       name, value,
-		       ctx->file ? ctx->file : "??",
-		       ctx->lastline, ctx->lastcol);
-	    cli_bytecode_debug_printsrc(ctx);
-	} else {
-	    cli_dbgmsg("[trace] %s = %u\n", name, value);
-	}
-    } else if (ctx->trace_mask&BC_TRACE_TMP_SRC) {
-	cli_dbgmsg("[trace] %s:%u:%u\n",
-		   ctx->file ? ctx->file : "??",
-		   ctx->lastline, ctx->lastcol);
-	cli_bytecode_debug_printsrc(ctx);
+    if (LIKELY(ctx->trace_level < trace_col))
+	return 0;
+    if (ctx->col != col) {
+	ctx->col = col;
+	ctx->trace(ctx, trace_col);
+    } else {
+	ctx->trace(ctx, trace_line);
     }
-    ctx->trace_mask &= ~(BC_TRACE_TMP_SRC|BC_TRACE_TMP_OP);
+    if (LIKELY(ctx->trace_level < trace_op))
+	return 0;
+    if (ctx->trace_op && op)
+	ctx->trace_op(ctx, (const char*)op);
     return 0;
 }
 
-uint32_t cli_bcapi_trace_directory(struct cli_bc_ctx *ctx, const const uint8_t* dir, uint32_t dummy)
+uint32_t cli_bcapi_trace_value(struct cli_bc_ctx *ctx, const const uint8_t* name, uint32_t value)
 {
-    if (LIKELY(!ctx->trace_mask))
+    if (LIKELY(ctx->trace_level < trace_val))
 	return 0;
-    ctx->directory = dir;
+    if (ctx->trace_level&0x80) {
+	if ((ctx->trace_level&0x7f) < trace_param)
+	    return 0;
+	ctx->trace(ctx, trace_param);
+    }
+    if (ctx->trace_val && name)
+	ctx->trace_val(ctx, name, value);
+    return 0;
 }
diff --git a/libclamav/bytecode_priv.h b/libclamav/bytecode_priv.h
index 20ce64b..9d5411e 100644
--- a/libclamav/bytecode_priv.h
+++ b/libclamav/bytecode_priv.h
@@ -23,6 +23,7 @@
 #ifndef BYTECODE_PRIV_H
 #define BYTECODE_PRIV_H
 
+#include "bytecode.h"
 #include "type_desc.h"
 #include "execs.h"
 #include "bytecode_hooks.h"
@@ -103,20 +104,16 @@ struct cli_bc_dbgnode {
 };
 
 #define MAX_OP ~0u
-#define BC_TRACE_FUNC  0x1
-#define BC_TRACE_PARAM 0x2
-#define BC_TRACE_SCOPE 0x4
-#define BC_TRACE_LINE  0x8
-#define BC_TRACE_COL   0x10
-#define BC_TRACE_OP    0x20
-#define BC_TRACE_VAL   0x40
-#define BC_TRACE_SHOW_SOURCE 0x80
-#define BC_TRACE_TMP_FUNC  0x100
-#define BC_TRACE_TMP_SCOPE 0x200
-#define BC_TRACE_TMP_SRC   0x400
-#define BC_TRACE_TMP_OP    0x800
-#define BC_TRACE_ALL (BC_TRACE_FUNC | BC_TRACE_PARAM | BC_TRACE_SCOPE | BC_TRACE_LINE | BC_TRACE_COL | BC_TRACE_OP | BC_TRACE_VAL | BC_TRACE_SHOW_SOURCE)
-
+enum trace_level {
+    trace_none=0,
+    trace_func,
+    trace_param,
+    trace_scope,
+    trace_line,
+    trace_col,
+    trace_op,
+    trace_val
+};
 struct cli_bc_ctx {
     /* id and params of toplevel function called */
     const struct cli_bc *bc;
@@ -136,13 +133,16 @@ struct cli_bc_ctx {
     char *tempfile;
     void *ctx;
     unsigned written;
-    unsigned trace_mask;
+    bc_dbg_callback_trace trace;
+    bc_dbg_callback_trace_op trace_op;
+    bc_dbg_callback_trace_val trace_val;
+    unsigned trace_level;
+    const char *directory;
+    const char *file;
     const char *scope;
     uint32_t scopeid;
-    const char *file;
-    const char *directory;
-    unsigned lastline;
-    unsigned lastcol;
+    unsigned line;
+    unsigned col;
 };
 struct cli_all_bc;
 int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct cli_bc_func *func, const struct cli_bc_inst *inst);
diff --git a/libclamav/c++/bytecode2llvm.cpp b/libclamav/c++/bytecode2llvm.cpp
index e5b0cc2..29de24b 100644
--- a/libclamav/c++/bytecode2llvm.cpp
+++ b/libclamav/c++/bytecode2llvm.cpp
@@ -73,8 +73,8 @@
 #endif
 #include "clamav.h"
 #include "clambc.h"
-#include "bytecode_priv.h"
 #include "bytecode.h"
+#include "bytecode_priv.h"
 #include "type_desc.h"
 
 #define MODULE "libclamav JIT: "
@@ -1233,8 +1233,8 @@ static struct lineprinter {
 
 void cli_bytecode_debug_printsrc(const struct cli_bc_ctx *ctx)
 {
-    if (!ctx->file || !ctx->directory || !ctx->lastline) {
-	errs() << (ctx->directory ? "d":"null") << ":" << (ctx->file ? "f" : "null")<< ":" << ctx->lastline << "\n";
+    if (!ctx->file || !ctx->directory || !ctx->line) {
+	errs() << (ctx->directory ? "d":"null") << ":" << (ctx->file ? "f" : "null")<< ":" << ctx->line << "\n";
 	return;
     }
     // acquire a mutex here
@@ -1257,7 +1257,7 @@ void cli_bytecode_debug_printsrc(const struct cli_bc_ctx *ctx)
 	lines = I->getValue();
     }
     const char *linestart;
-    while (lines->lines.size() <= ctx->lastline+1) {
+    while (lines->lines.size() <= ctx->line+1) {
 	const char *p;
 	if (lines->lines.empty()) {
 	    p = lines->buffer->getBufferStart();
@@ -1274,14 +1274,14 @@ void cli_bytecode_debug_printsrc(const struct cli_bc_ctx *ctx)
 		lines->lines.push_back(p+1);
 	}
     }
-    if (ctx->lastline >= lines->lines.size()) {
-	errs() << "Line number " << ctx->lastline << "out of file\n";
+    if (ctx->line >= lines->lines.size()) {
+	errs() << "Line number " << ctx->line << "out of file\n";
 	return;
     }
-    assert(ctx->lastline < lines->lines.size());
-    SMDiagnostic diag(ctx->file, ctx->lastline ? ctx->lastline : -1,
-		 ctx->lastcol ? ctx->lastcol-1 : -1,
-		 "", std::string(lines->lines[ctx->lastline-1], lines->lines[ctx->lastline]-1));
+    assert(ctx->line < lines->lines.size());
+    SMDiagnostic diag(ctx->file, ctx->line ? ctx->line : -1,
+		 ctx->col ? ctx->col-1 : -1,
+		 "", std::string(lines->lines[ctx->line-1], lines->lines[ctx->line]-1));
     diag.Print("[trace]", errs());
 }
 
diff --git a/libclamav/libclamav.map b/libclamav/libclamav.map
index 3d91f19..fca7f42 100644
--- a/libclamav/libclamav.map
+++ b/libclamav/libclamav.map
@@ -165,6 +165,8 @@ CLAMAV_PRIVATE {
     cli_hex2ui;
     fmap;
     funmap;
+    cli_bytecode_context_set_trace;
+    cli_bytecode_debug_printsrc;
   local:
     *;
 };

-- 
Debian repository for ClamAV



More information about the Pkg-clamav-commits mailing list