touched up vm debugging code

This commit is contained in:
hyunghwan.chung 2016-10-06 14:17:24 +00:00
parent 1960efb7e1
commit 1879417d9c
3 changed files with 68 additions and 56 deletions

View File

@ -29,10 +29,10 @@
#define DECODE_LOG_MASK (STIX_LOG_MNEMONIC) #define DECODE_LOG_MASK (STIX_LOG_MNEMONIC)
#define LOG_INST_0(stix,fmt) STIX_LOG0(stix, DECODE_LOG_MASK, "\t" fmt "\n") #define LOG_INST_0(stix,fmt) STIX_LOG1(stix, DECODE_LOG_MASK, " %06zd " fmt "\n", fetched_instruction_pointer)
#define LOG_INST_1(stix,fmt,a1) STIX_LOG1(stix, DECODE_LOG_MASK, "\t" fmt "\n",a1) #define LOG_INST_1(stix,fmt,a1) STIX_LOG2(stix, DECODE_LOG_MASK, " %06zd " fmt "\n", fetched_instruction_pointer, a1)
#define LOG_INST_2(stix,fmt,a1,a2) STIX_LOG2(stix, DECODE_LOG_MASK, "\t" fmt "\n", a1, a2) #define LOG_INST_2(stix,fmt,a1,a2) STIX_LOG3(stix, DECODE_LOG_MASK, " %06zd " fmt "\n", fetched_instruction_pointer, a1, a2)
#define LOG_INST_3(stix,fmt,a1,a2,a3) STIX_LOG3(stix, DECODE_LOG_MASK, "\t" fmt "\n", a1, a2, a3) #define LOG_INST_3(stix,fmt,a1,a2,a3) STIX_LOG4(stix, DECODE_LOG_MASK, " %06zd " fmt "\n", fetched_instruction_pointer, a1, a2, a3)
#define FETCH_BYTE_CODE(stix) (cdptr[ip++]) #define FETCH_BYTE_CODE(stix) (cdptr[ip++])
#define FETCH_BYTE_CODE_TO(stix,v_oow) (v_oow = FETCH_BYTE_CODE(stix)) #define FETCH_BYTE_CODE_TO(stix,v_oow) (v_oow = FETCH_BYTE_CODE(stix))
@ -51,6 +51,7 @@ int stix_decode (stix_t* stix, stix_oop_method_t mth, const stix_oocs_t* classfq
{ {
stix_oob_t bcode, * cdptr; stix_oob_t bcode, * cdptr;
stix_ooi_t ip = 0, cdlen; /* byte code length is limited by the compiler. so stix_ooi_t is good enough */ stix_ooi_t ip = 0, cdlen; /* byte code length is limited by the compiler. so stix_ooi_t is good enough */
stix_ooi_t fetched_instruction_pointer;
stix_oow_t b1, b2; stix_oow_t b1, b2;
cdptr = STIX_METHOD_GET_CODE_BYTE(mth); cdptr = STIX_METHOD_GET_CODE_BYTE(mth);
@ -64,6 +65,7 @@ int stix_decode (stix_t* stix, stix_oop_method_t mth, const stix_oocs_t* classfq
/* TODO: check if ip increases beyond bcode when fetching parameters too */ /* TODO: check if ip increases beyond bcode when fetching parameters too */
while (ip < cdlen) while (ip < cdlen)
{ {
fetched_instruction_pointer = ip;
FETCH_BYTE_CODE_TO(stix, bcode); FETCH_BYTE_CODE_TO(stix, bcode);
switch (bcode) switch (bcode)
@ -503,7 +505,7 @@ return -1;
/* print literal frame contents */ /* print literal frame contents */
for (ip = 0; ip < STIX_OBJ_GET_SIZE(mth) - STIX_METHOD_NAMED_INSTVARS; ip++) for (ip = 0; ip < STIX_OBJ_GET_SIZE(mth) - STIX_METHOD_NAMED_INSTVARS; ip++)
{ {
LOG_INST_2 (stix, " @%-3zd %O", ip, mth->slot[ip]); STIX_LOG2(stix, DECODE_LOG_MASK, " @%-5zd %O\n", ip, mth->slot[ip]);
} }
return 0; return 0;

View File

@ -116,11 +116,11 @@
#if defined(STIX_DEBUG_VM_EXEC) #if defined(STIX_DEBUG_VM_EXEC)
# define LOG_MASK_INST (STIX_LOG_IC | STIX_LOG_MNEMONIC) # define LOG_MASK_INST (STIX_LOG_IC | STIX_LOG_MNEMONIC)
# define LOG_INST_0(stix,fmt) STIX_LOG0(stix, LOG_MASK_INST, "\t" fmt "\n") /* TODO: for send_message, display the method name. or include the method name before 'ip' */
# define LOG_INST_1(stix,fmt,a1) STIX_LOG1(stix, LOG_MASK_INST, "\t" fmt "\n",a1) # define LOG_INST_0(stix,fmt) STIX_LOG1(stix, LOG_MASK_INST, " %06zd " fmt "\n", fetched_instruction_pointer)
# define LOG_INST_2(stix,fmt,a1,a2) STIX_LOG2(stix, LOG_MASK_INST, "\t" fmt "\n", a1, a2) # define LOG_INST_1(stix,fmt,a1) STIX_LOG2(stix, LOG_MASK_INST, " %06zd " fmt "\n",fetched_instruction_pointer, a1)
# define LOG_INST_3(stix,fmt,a1,a2,a3) STIX_LOG3(stix, LOG_MASK_INST, "\t" fmt "\n", a1, a2, a3) # define LOG_INST_2(stix,fmt,a1,a2) STIX_LOG3(stix, LOG_MASK_INST, " %06zd " fmt "\n", fetched_instruction_pointer, a1, a2)
# define LOG_INST_3(stix,fmt,a1,a2,a3) STIX_LOG4(stix, LOG_MASK_INST, " %06zd " fmt "\n", fetched_instruction_pointer, a1, a2, a3)
#else #else
# define LOG_INST_0(stix,fmt) # define LOG_INST_0(stix,fmt)
# define LOG_INST_1(stix,fmt,a1) # define LOG_INST_1(stix,fmt,a1)
@ -128,7 +128,6 @@
# define LOG_INST_3(stix,fmt,a1,a2,a3) # define LOG_INST_3(stix,fmt,a1,a2,a3)
#endif #endif
#define __PRIMITIVE_NAME__ (&__FUNCTION__[4]) #define __PRIMITIVE_NAME__ (&__FUNCTION__[4])
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
@ -2909,6 +2908,10 @@ static int start_method (stix_t* stix, stix_oop_method_t method, stix_oow_t narg
{ {
stix_ooi_t preamble, preamble_code; stix_ooi_t preamble, preamble_code;
#if defined(STIX_DEBUG_VM_EXEC)
stix_ooi_t fetched_instruction_pointer = 0; /* set it to a fake value */
#endif
STIX_ASSERT (STIX_OOP_TO_SMOOI(method->tmpr_nargs) == nargs); STIX_ASSERT (STIX_OOP_TO_SMOOI(method->tmpr_nargs) == nargs);
preamble = STIX_OOP_TO_SMOOI(method->preamble); preamble = STIX_OOP_TO_SMOOI(method->preamble);
@ -3151,6 +3154,10 @@ int stix_execute (stix_t* stix)
stix_uintmax_t inst_counter = 0; stix_uintmax_t inst_counter = 0;
#endif #endif
#if defined(STIX_DEBUG_VM_EXEC)
stix_ooi_t fetched_instruction_pointer;
#endif
STIX_ASSERT (stix->active_context != STIX_NULL); STIX_ASSERT (stix->active_context != STIX_NULL);
vm_startup (stix); vm_startup (stix);
@ -3254,6 +3261,9 @@ if (there is semaphore awaited.... )
stix->proc_switched = 0; stix->proc_switched = 0;
#if defined(STIX_DEBUG_VM_EXEC)
fetched_instruction_pointer = stix->ip;
#endif
FETCH_BYTE_CODE_TO (stix, bcode); FETCH_BYTE_CODE_TO (stix, bcode);
/*while (bcode == BCODE_NOOP) FETCH_BYTE_CODE_TO (stix, bcode);*/ /*while (bcode == BCODE_NOOP) FETCH_BYTE_CODE_TO (stix, bcode);*/

View File

@ -372,6 +372,30 @@ static void* mod_getsym (stix_t* stix, void* handle, const stix_ooch_t* name)
/* ========================================================================= */ /* ========================================================================= */
static int write_all (int fd, const char* ptr, stix_oow_t len)
{
while (len > 0)
{
stix_ooi_t wr;
wr = write (1, ptr, len);
if (wr <= -1)
{
/*if (errno == EAGAIN || errno == EWOULDBLOCK)
{
push it to internal buffers? before writing data just converted, need to write buffered data first.
}*/
return -1;
}
ptr += wr;
len -= wr;
}
return 0;
}
static void log_write (stix_t* stix, stix_oow_t mask, const stix_ooch_t* msg, stix_oow_t len) static void log_write (stix_t* stix, stix_oow_t mask, const stix_ooch_t* msg, stix_oow_t len)
{ {
#if defined(_WIN32) #if defined(_WIN32)
@ -381,26 +405,24 @@ static void log_write (stix_t* stix, stix_oow_t mask, const stix_ooch_t* msg, st
stix_bch_t buf[256]; stix_bch_t buf[256];
stix_oow_t ucslen, bcslen, msgidx; stix_oow_t ucslen, bcslen, msgidx;
int n; int n;
char ts[32];
struct tm tm, *tmp;
time_t now;
/*if (mask & STIX_LOG_GC) return;*/ /* don't show gc logs */ /*if (mask & STIX_LOG_GC) return;*/ /* don't show gc logs */
/* TODO: beautify the log message. /* TODO: beautify the log message.
* do classification based on mask. */ * do classification based on mask. */
/*
{
char ts[32];
struct tm tm;
time_t now;
now = time(NULL); now = time(NULL);
localtime_r (&now, &tm); #if defined(__MSDOS__)
strftime (ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %z ", &tm); tmp = localtime (&now);
write (1, ts, strlen(ts)); #else
} tmp = localtime_r (&now, &tm);
*/ #endif
strftime (ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %z ", tmp);
write_all (1, ts, strlen(ts));
msgidx = 0; msgidx = 0;
while (len > 0) while (len > 0)
{ {
@ -410,8 +432,6 @@ static void log_write (stix_t* stix, stix_oow_t mask, const stix_ooch_t* msg, st
n = stix_ucstoutf8 (&msg[msgidx], &ucslen, buf, &bcslen); n = stix_ucstoutf8 (&msg[msgidx], &ucslen, buf, &bcslen);
if (n == 0 || n == -2) if (n == 0 || n == -2)
{ {
stix_oow_t rem;
const stix_bch_t* ptr;
/* n = 0: /* n = 0:
* converted all successfully * converted all successfully
* n == -2: * n == -2:
@ -421,25 +441,7 @@ static void log_write (stix_t* stix, stix_oow_t mask, const stix_ooch_t* msg, st
STIX_ASSERT (ucslen > 0); /* if this fails, the buffer size must be increased */ STIX_ASSERT (ucslen > 0); /* if this fails, the buffer size must be increased */
/* attempt to write all converted characters */ /* attempt to write all converted characters */
rem = bcslen; if (write_all (1, buf, bcslen) <= -1) break;
ptr = buf;
while (rem > 0)
{
stix_ooi_t wr;
wr = write (1, ptr, rem); /* TODO: write all */
if (wr <= -1)
{
/*if (errno == EAGAIN || errno == EWOULDBLOCK)
{
push it to internal buffers? before writing data just converted, need to write buffered data first.
}*/
break;
}
ptr += wr;
rem -= wr;
}
if (n == 0) break; if (n == 0) break;
else else
@ -517,11 +519,9 @@ static char* syntax_error_msg[] =
/* ========================================================================= */ /* ========================================================================= */
stix_ooch_t str_my_object[] = { 'M', 'y', 'O', 'b','j','e','c','t' }; static stix_ooch_t str_my_object[] = { 'M', 'y', 'O', 'b','j','e','c','t' };
stix_ooch_t str_main[] = { 'm', 'a', 'i', 'n' }; static stix_ooch_t str_main[] = { 'm', 'a', 'i', 'n' };
static stix_t* g_stix = STIX_NULL;
stix_t* g_stix = STIX_NULL;
/* ========================================================================= */ /* ========================================================================= */