Skip to content

Commit

Permalink
Deep log optimize (#4)
Browse files Browse the repository at this point in the history
* deep log optimize

* log: log system optimize
  • Loading branch information
chinesebear authored Aug 7, 2021
1 parent bdb5371 commit a7eb229
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 86 deletions.
23 changes: 14 additions & 9 deletions include/deep_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ extern "C" {
#endif


void log_printf(const char *pFileName, unsigned int uiLine, const char *pFuncName, char *LogFmtBuf, ...);

void
log_data(const char *pFileName, unsigned int uiLine, const char *pFuncName, const char *pcStr, unsigned char *pucBuf,
unsigned int usLen);

#define error(...) log_printf(__FILE__, __LINE__,__FUNCTION__,__VA_ARGS__)
#define debug(...) log_printf(__FILE__, __LINE__,__FUNCTION__,__VA_ARGS__)
#define dump(pcStr, pucBuf, usLen) log_data(__FILE__, __LINE__,__FUNCTION__,pcStr,pucBuf,usLen)
void log_printf (const char* pFileName, unsigned int uiLine, const char* pFuncName, const char *pFlag, char *LogFmtBuf, ...);
void log_data(const char *pFileName, unsigned int uiLine, const char* pFuncName, const char *pcStr,unsigned char *pucBuf,unsigned int usLen);
#define deep_error(...) log_printf(__FILE__, __LINE__,__FUNCTION__,"<error>",__VA_ARGS__)
#define deep_warn(...) log_printf(__FILE__, __LINE__,__FUNCTION__,"<warn>",__VA_ARGS__)
#define deep_debug(...) log_printf(__FILE__, __LINE__,__FUNCTION__,"<debug>",__VA_ARGS__)
#define deep_info(...) log_printf(__FILE__, __LINE__,__FUNCTION__,"<info>",__VA_ARGS__)
#define deep_dump(pcStr,pucBuf,usLen) log_data(__FILE__, __LINE__,__FUNCTION__,pcStr,pucBuf,usLen)

// #define DBG
#ifdef DBG
#define PRINT_ARG(FSTRING, ARG) do {printf(FSTRING, ARG); fflush(stdout);} while (0)
#else
#define PRINT_ARG(FSTRING, ARG) ((void *)0)
#endif


#ifdef __cplusplus
Expand Down
11 changes: 6 additions & 5 deletions src/deep_interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "deep_loader.h"
#include "deep_mem.h"
#include "deep_opcode.h"
#include "deep_log.h"

#define popS32() (int32_t)*(--sp)
#define popF32() (float)*(--sp)
Expand All @@ -29,13 +30,13 @@
DEEPStack *stack_cons(void) {
DEEPStack *stack = (DEEPStack *) deep_malloc(sizeof(DEEPStack));
if (stack == NULL) {
printf("Operand stack creation failed!\r\n");
deep_error("Operand stack creation failed!");
return NULL;
}
stack->capacity = STACK_CAPACITY;
stack->sp = (uint32_t *) deep_malloc(sizeof(uint32_t) * STACK_CAPACITY);
if (stack->sp == NULL) {
printf("Malloc area for stack error!\r\n");
deep_error("Malloc area for stack error!");
}
stack->sp_end = stack->sp + stack->capacity;
return stack;
Expand Down Expand Up @@ -303,7 +304,7 @@ void call_function(DEEPExecEnv *current_env, DEEPModule *module, int func_index)
//为func函数创建帧
DEEPInterpFrame *frame = (DEEPInterpFrame *) deep_malloc(sizeof(DEEPInterpFrame));
if (frame == NULL) {
printf("Malloc area for normal_frame error!\r\n");
deep_error("Malloc area for normal_frame error!");
}
//初始化
frame->sp = current_env->sp;
Expand Down Expand Up @@ -338,7 +339,7 @@ int32_t call_main(DEEPExecEnv *current_env, DEEPModule *module) {
}
}
if (main_index < 0) {
printf("the main function index failed!\r\n");
deep_error("the main function index failed!");
return -1;
}

Expand All @@ -348,7 +349,7 @@ int32_t call_main(DEEPExecEnv *current_env, DEEPModule *module) {
//为main函数创建帧
DEEPInterpFrame *main_frame = (DEEPInterpFrame *) deep_malloc(sizeof(struct DEEPInterpFrame));
if (main_frame == NULL) {
printf("Malloc area for main_frame error!\r\n");
deep_error("Malloc area for main_frame error!");
}
//初始化
main_frame->sp = current_env->sp;
Expand Down
22 changes: 3 additions & 19 deletions src/deep_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,18 @@ static void decode_each_sections(DEEPModule* module, section_listnode* section_l

DEEPModule* deep_load(uint8_t** p, int size) {
if (!check_magic_number_and_version(p)) {
error("magic number error");
deep_error("magic number error");
return NULL;
}
section_listnode* section_list = create_section_list((const uint8_t**)p, size);
if(section_list == NULL) {
error("create section list fail");
deep_error("create section list fail");
return NULL;
}
size -= 8;
DEEPModule* module = (DEEPModule*)deep_malloc(sizeof(DEEPModule));
if(module == NULL) {
error("module malloc fail");
deep_error("module malloc fail");
return NULL;
}
decode_each_sections(module, section_list);
Expand Down Expand Up @@ -364,22 +364,6 @@ void write_mem64(uint8_t* mem, uint64_t val, uint32_t offset) {
write_memory(mem, buf, offset, 8);
}

// DEEPModule* module = deep_load(&p, size);
//DOING:开发dump指令,方便后续调试程序

// void print_type(uint8_t t) {
// switch (t)
// {
// case :

// break;

// default:
// error("not correct parameter type");
// break;
// }
// }

void type_section_dump(DEEPModule* module) {
uint32_t i, j = 0;
printf("%s\n", "========================================================");
Expand Down
92 changes: 53 additions & 39 deletions src/deep_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,73 +29,87 @@ Description: dump/debug funtions for logs.
#include <stdarg.h>
#include "deep_log.h"

void log_printf(const char *pFileName, unsigned int uiLine, const char *pFnucName, char *LogFmtBuf, ...) {
va_list args;
if (pFileName == NULL || uiLine == 0 || LogFmtBuf == NULL) {
return;
}
void log_printf (const char* pFileName, unsigned int uiLine, const char* pFnucName, const char *pFlag, char *LogFmtBuf, ...)
{
va_list args;
if(pFileName == NULL || uiLine == 0 || LogFmtBuf == NULL)
{
return ;
}
char logbuf[256];
memset(logbuf, '\0', 256);
sprintf(logbuf, "%s:%d, %s(), ", pFileName, uiLine, pFnucName);
printf("%s", logbuf);
memset(logbuf, '\0', 256);
va_start (args, LogFmtBuf);
vsnprintf(logbuf, 256, LogFmtBuf, args);
va_end (args);
printf("%s\r\n", logbuf);
memset (logbuf,'\0',256);
sprintf (logbuf,"%s:%d, %s(), %s, ", pFileName, uiLine, pFnucName, pFlag);
printf ("%s",logbuf);
memset (logbuf,'\0',256);
va_start (args, LogFmtBuf);
vsnprintf (logbuf, 256, LogFmtBuf, args);
va_end (args);
printf ("%s\r\n",logbuf);
}

void
log_data(const char *pFileName, unsigned int uiLine, const char *pFnucName, const char *pcStr, unsigned char *pucBuf,
unsigned int usLen) {
void log_data(const char *pFileName, unsigned int uiLine, const char* pFnucName, const char *pcStr,unsigned char *pucBuf,unsigned int usLen)
{
unsigned int i;
unsigned char acTmp[17];
unsigned char *p;
unsigned char *pucAddr = pucBuf;

if (pcStr) {
log_printf(pFileName, uiLine, pFnucName, "[%s]: length = %d (0x%X)\r\n", pcStr, usLen, usLen);
if(pcStr)
{
log_printf (pFileName, uiLine, pFnucName, "<dump>", "[%s]: length = %d (0x%X)\r\n",pcStr, usLen, usLen);
}
if (usLen == 0) {
if(usLen == 0)
{
return;
}
p = acTmp;
printf(" %p ", pucAddr);
for (i = 0; i < usLen; i++) {
printf (" %p ", pucAddr);
for(i=0;i<usLen;i++)
{

printf("%02X ", pucBuf[i]);
if ((pucBuf[i] >= 0x20) && (pucBuf[i] < 0x7F)) {
printf ("%02X ",pucBuf[i]);
if((pucBuf[i] >= 0x20) && (pucBuf[i] < 0x7F))
{
*p++ = pucBuf[i];
} else {
}
else
{
*p++ = '.';
}
if ((i + 1) % 16 == 0) {
if((i+1)%16==0)
{
*p++ = 0;
printf(" | %s", acTmp);
printf (" | %s", acTmp);
p = acTmp;

printf("\r\n");
printf ("\r\n");

if ((i + 1) < usLen) {
if((i+1) < usLen)
{
pucAddr += 16;
printf(" %p ", pucAddr);
printf (" %p ", pucAddr);
}
} else if ((i + 1) % 8 == 0) {
printf("- ");
}
else if((i+1)%8==0)
{
printf ("- ");
}
}
if (usLen % 16 != 0) {
for (i = usLen % 16; i < 16; i++) {
printf(" ");
if (((i + 1) % 8 == 0) && ((i + 1) % 16 != 0)) {
printf("- ");
if(usLen%16!=0)
{
for(i=usLen%16;i<16;i++)
{
printf (" ");
if(((i+1)%8==0) && ((i+1)%16!=0))
{
printf ("- ");
}
}
*p++ = 0;
printf(" | %s", acTmp);
printf("\r\n");
printf (" | %s", acTmp);
printf ("\r\n");
}
printf("\r\n");
printf ("\r\n");
}


10 changes: 5 additions & 5 deletions src/deep_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,30 @@ int32_t main(int argv, char **args) {

char *path;
if(argv==1){
error("no file input!");
deep_error("no file input!");
}else{
path = args[1];
}
uint8_t *q = (uint8_t *) deep_malloc(WASM_FILE_SIZE);
uint8_t *p = q;
if (p == NULL) {
error("malloc fail.");
deep_error("malloc fail.");
return -1;
}

FILE *fp = fopen(path, "rb"); /* read wasm file with binary mode */
if (fp == NULL) {
error("file open fail.");
deep_error("file open fail.");
return -1;
}
int32_t size = fread(p, 1, WASM_FILE_SIZE, fp);
if (size == 0) {
error ("fread faill.");
deep_error ("fread faill.");
return -1;
}
DEEPModule *module = deep_load(&p, size);
if (module == NULL) {
error("load fail.");
deep_error("load fail.");
return -1;
}
//创建操作数栈
Expand Down
9 changes: 0 additions & 9 deletions src/deep_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@
#include "deep_mem.h"
#include "deep_log.h"

// #define DBG

#ifdef DBG
#define PRINT_ARG(FSTRING, ARG) do {printf(FSTRING, ARG); fflush(stdout);} while (0)
#else
#define PRINT_ARG(FSTRING, ARG) ((void *)0)
#endif

mem_pool_t *pool;

/*
Expand Down Expand Up @@ -163,7 +155,6 @@ bool deep_mem_init (void *mem, uint32_t size)
// initialise remainder block's head
block_set_A_flag (pool->remainder_block_head, false);
block_set_P_flag (pool->remainder_block_head, true);

return true;
}

Expand Down

0 comments on commit a7eb229

Please sign in to comment.