Skip to content

Commit

Permalink
add branchlut2
Browse files Browse the repository at this point in the history
  • Loading branch information
firodj committed Feb 10, 2018
1 parent 1c8d095 commit 83346cd
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if (NOT DynamoRIO_FOUND)
message(FATAL_ERROR "DynamoRIO package required to build")
endif(NOT DynamoRIO_FOUND)

add_library(bbtrace_core STATIC clients/bbtrace_core.c)
add_library(bbtrace_core STATIC clients/bbtrace_core.c clients/branchlut2.cpp)
target_compile_definitions(bbtrace_core PUBLIC WINDOWS X86_32)
target_include_directories(bbtrace_core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/clients $ENV{DYNAMORIO_HOME}/include)

Expand Down
33 changes: 28 additions & 5 deletions clients/bbtrace_core.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "bbtrace_core.h"
#include <intrin.h>

#include <intrin.h>
#include "branchlut2.h"

#pragma intrinsic(__rdtsc)

static uint64 g_log_size;
Expand All @@ -26,10 +27,32 @@ bbtrace_escape_string(const char *str, char *out, size_t n)
}
}
}
out[j++] = 0;
out[j++] = '\0';
return j;
}

char *
bbtrace_append_string(char *dst, const char *val, size_t len, bool comma)
{
char *result = dst;
*result++ = '"';
result = strncpy(result, val, len) + len;
*result++ = '"';
if (comma) *result++ = ',';
*result = '\0';
return result;
}

char *
bbtrace_append_integer(char *dst, uint val, bool comma)
{
char *result = dst;
result = u32toa_branchlut2(val, result);
if (comma) *result++ = ',';
*result = '\0';
return result;
}

const char *
bbtrace_log_filename(uint count)
{
Expand All @@ -47,9 +70,9 @@ bbtrace_formatinfo_module(const module_data_t *mod)
{
static char info[1024];
const char *mod_name = dr_module_preferred_name(mod);
char path[512];
char path[512];

bbtrace_escape_string(mod->full_path, path, sizeof(path));
bbtrace_escape_string(mod->full_path, path, sizeof(path));

dr_snprintf(info, sizeof(info),
"{\n"
Expand Down
2 changes: 2 additions & 0 deletions clients/bbtrace_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ typedef struct {
extern "C" {
#endif

char * bbtrace_append_string(char *, const char *, size_t, bool);
char * bbtrace_append_integer(char *, uint, bool);
size_t bbtrace_escape_string(const char *, char *, size_t);
const char *bbtrace_log_filename(uint);
const char *bbtrace_formatinfo_module(const module_data_t*);
Expand Down
80 changes: 80 additions & 0 deletions clients/branchlut2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <stdint.h>
#include "branchlut2.h"


#define BEGIN2(n) \
do { \
int t = (n); \
if(t < 10) *p++ = '0' + t; \
else { \
t *= 2; \
*p++ = gDigitsLut[t]; \
*p++ = gDigitsLut[t + 1]; \
} \
} while(0)
#define MIDDLE2(n) \
do { \
int t = (n) * 2; \
*p++ = gDigitsLut[t]; \
*p++ = gDigitsLut[t + 1]; \
} while(0)
#define BEGIN4(n) \
do { \
int t4 = (n); \
if(t4 < 100) BEGIN2(t4); \
else { BEGIN2(t4 / 100); MIDDLE2(t4 % 100); } \
} while(0)
#define MIDDLE4(n) \
do { \
int t4 = (n); \
MIDDLE2(t4 / 100); MIDDLE2(t4 % 100); \
} while(0)
#define BEGIN8(n) \
do { \
uint32_t t8 = (n); \
if(t8 < 10000) BEGIN4(t8); \
else { BEGIN4(t8 / 10000); MIDDLE4(t8 % 10000); } \
} while(0)
#define MIDDLE8(n) \
do { \
uint32_t t8 = (n); \
MIDDLE4(t8 / 10000); MIDDLE4(t8 % 10000); \
} while(0)
#define MIDDLE16(n) \
do { \
uint64_t t16 = (n); \
MIDDLE8(t16 / 100000000); MIDDLE8(t16 % 100000000); \
} while(0)

char * u32toa_branchlut2(uint32_t x, char* p) {

if(x < 100000000) BEGIN8(x);
else { BEGIN2(x / 100000000); MIDDLE8(x % 100000000); }
*p = '\0';
return p;

}
void i32toa_branchlut2(int32_t x, char* p) {

uint64_t t;
if(x >= 0) t = x;
else *p++ = '-', t = -uint32_t(x);
u32toa_branchlut2(t, p);

}
void u64toa_branchlut2(uint64_t x, char* p) {

if(x < 100000000) BEGIN8(x);
else if(x < 10000000000000000) { BEGIN8(x / 100000000); MIDDLE8(x % 100000000); }
else { BEGIN4(x / 10000000000000000); MIDDLE16(x % 10000000000000000); }
*p = 0;

}
void i64toa_branchlut2(int64_t x, char* p) {

uint64_t t;
if(x >= 0) t = x;
else *p++ = '-', t = -uint64_t(x);
u64toa_branchlut2(t, p);

}
26 changes: 26 additions & 0 deletions clients/branchlut2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <stdint.h>

const char gDigitsLut[200] = {
'0','0','0','1','0','2','0','3','0','4','0','5','0','6','0','7','0','8','0','9',
'1','0','1','1','1','2','1','3','1','4','1','5','1','6','1','7','1','8','1','9',
'2','0','2','1','2','2','2','3','2','4','2','5','2','6','2','7','2','8','2','9',
'3','0','3','1','3','2','3','3','3','4','3','5','3','6','3','7','3','8','3','9',
'4','0','4','1','4','2','4','3','4','4','4','5','4','6','4','7','4','8','4','9',
'5','0','5','1','5','2','5','3','5','4','5','5','5','6','5','7','5','8','5','9',
'6','0','6','1','6','2','6','3','6','4','6','5','6','6','6','7','6','8','6','9',
'7','0','7','1','7','2','7','3','7','4','7','5','7','6','7','7','7','8','7','9',
'8','0','8','1','8','2','8','3','8','4','8','5','8','6','8','7','8','8','8','9',
'9','0','9','1','9','2','9','3','9','4','9','5','9','6','9','7','9','8','9','9'
};

#ifdef __cplusplus
extern "C" {
#endif

char * u32toa_branchlut2(uint32_t x, char* p);

#ifdef __cplusplus
}
#endif
72 changes: 68 additions & 4 deletions tests/test_bbtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,40 @@ void test_bbtrace_formatinfo_block() {
dr_fprintf(STDERR, "%s\n", actual);
}

void test_bbtrace_formatinfo_symbol_import() {
dr_symbol_import_t sym;

const char *actual = NULL;
const char *modname = "SPEED2.EXE";

memset(&sym, 0, sizeof(sym));

sym.modname = "advapi32.dll";
sym.name = "RegQueryValueExA";
sym.ordinal = 99;

actual = bbtrace_formatinfo_symbol_import(&sym, modname);
dr_fprintf(STDERR, "%s\n", actual);
}

void test_bbtrace_formatinfo_exception() {
dr_exception_t excpt;
const char *actual = NULL;

memset(&excpt, 0, sizeof(excpt));

EXCEPTION_RECORD record;
memset(&record, 0, sizeof(record));

record.ExceptionCode = 0x40010006;
record.ExceptionAddress = (void*)0x7764c41f;
record.ExceptionInformation[1] = 0x0018fd34;
excpt.record = &record;

actual = bbtrace_formatinfo_exception(&excpt);
dr_fprintf(STDERR, "%s\n", actual);
}

void test_instrlist_app_length(void *drcontext) {
uint actual = 0;

Expand Down Expand Up @@ -105,6 +139,22 @@ void test_bbtrace_escape_string() {
dr_fprintf(STDERR, "escaped = %s\n", out);
}

void test_bbtrace_append_string() {
char out[256];
char *next = out;
next = bbtrace_append_string(next, "satu", 4, true);
next = bbtrace_append_string(next, "dua", 3, false);
dr_fprintf(STDERR, "output = %s\n", out);
}

void test_bbtrace_append_integer() {
char out[256];
char *next = out;
next = bbtrace_append_integer(next, 12345678, true);
next = bbtrace_append_integer(next, 0, false);
dr_fprintf(STDERR, "output = %s\n", out);
}

/*
TEST(oh, ah) {
instrlist_t* ilist = instrlist_create(drcontext);
Expand Down Expand Up @@ -146,16 +196,30 @@ static void run_tests(void *drcontext)
dr_fprintf(STDERR, "[ ] test_bbtrace_formatinfo_block:\n");
test_bbtrace_formatinfo_block();

dr_fprintf(STDERR, "[ ] test_bbtrace_formatinfo_symbol_import:\n");
test_bbtrace_formatinfo_symbol_import();

dr_fprintf(STDERR, "[ ] test_bbtrace_formatinfo_exception:\n");
test_bbtrace_formatinfo_exception();

dr_fprintf(STDERR, "[ ] test_instrlist_app_length:\n");
test_instrlist_app_length(drcontext);

#if 0
dr_fprintf(STDERR, "[ ] test_bbtrace_dump_thread_data:\n");
test_bbtrace_dump_thread_data(drcontext);

test_bbtrace_dump_thread_data(drcontext);
#endif

dr_fprintf(STDERR, "[ ] test_bbtrace_escape_string:\n");
test_bbtrace_escape_string();
test_bbtrace_escape_string();

dr_fprintf(STDERR, "[ ] test_bbtrace_append_string:\n");
test_bbtrace_append_string();

dr_fprintf(STDERR, "[ ] test_bbtrace_append_integer:\n");
test_bbtrace_append_integer();

dr_fprintf(STDERR, "[ ] DONE testing.\n");
dr_fprintf(STDERR, "[ ] DONE testing.\n");
}

static dr_emit_flags_t
Expand Down

0 comments on commit 83346cd

Please sign in to comment.