diff --git a/moo/lib/debug.c b/moo/lib/debug.c index 2268b50..d64f3eb 100644 --- a/moo/lib/debug.c +++ b/moo/lib/debug.c @@ -75,15 +75,19 @@ moo_compactdbginfo()? -> compact debug information by scaning dbginfo data. find int moo_initdbginfo (moo_t* moo, moo_oow_t capa) { - moo_dbginfo_t* info; + moo_dbginfo_t* tmp; - info = (moo_dbginfo_t*)moo_allocmem(moo, MOO_SIZEOF(*info) + capa); - if (!info) return -1; + if (capa < MOO_SIZEOF(*tmp)) capa = MOO_SIZEOF(*tmp); - info->_capa = capa; - info->_len = 0; + tmp = (moo_dbginfo_t*)moo_callocmem(moo, capa); + if (!tmp) return -1; - moo->dbginfo = info; + tmp->_capa = capa; + tmp->_len = MOO_SIZEOF(*tmp); + tmp->_last_class = 0; + tmp->_last_file = 0; + + moo->dbginfo = tmp; return 0; } @@ -96,7 +100,110 @@ void moo_finidbginfo (moo_t* moo) } } -int moo_addmethodtodbginfo (moo_t* moo, const moo_uch_t* class_name, const moo_uch_t* method_name) +static MOO_INLINE secure_dbginfo_space (moo_t* moo, moo_oow_t req_bytes) { + if (moo->dbginfo->_capa - moo->dbginfo->_len < req_bytes) + { + moo_dbginfo_t* tmp; + moo_oow_t newcapa; + + newcapa = moo->dbginfo->_len + req_bytes; + newcapa = MOO_ALIGN_POW2(newcapa, 65536); /* TODO: make the align value configurable */ + tmp = moo_reallocmem(moo, moo->dbginfo, newcapa); + if (!tmp) return -1; + + moo->dbginfo = tmp; + moo->dbginfo->_capa = newcapa; + } + return 0; -} \ No newline at end of file +} + +int moo_addclasstodbginfo (moo_t* moo, const moo_ooch_t* class_name, moo_oow_t* start_offset) +{ + moo_oow_t name_len, name_bytes, name_bytes_aligned, req_bytes; + moo_dbginfo_class_t* di; + + if (!moo->dbginfo) return 0; /* debug information is disabled*/ + + name_len = moo_count_oocstr(class_name); + name_bytes = name_len * MOO_SIZEOF(*class_name); + name_bytes_aligned = MOO_ALIGN_POW2(name_bytes, MOO_SIZEOF_OOW_T); + req_bytes = MOO_SIZEOF(moo_dbginfo_class_t) + name_bytes_aligned; + + if (secure_dbginfo_space(moo, req_bytes) <= -1) return -1; + + di = (moo_dbginfo_class_t*)&((moo_uint8_t*)moo->dbginfo)[moo->dbginfo->_len]; + di->_type = MOO_DBGINFO_MAKE_TYPE(MOO_DBGINFO_TYPE_CODE_CLASS, 0); + di->_len = name_len; + di->_next = moo->dbginfo->_last_class; + + MOO_MEMCPY (di + 1, class_name, name_len * MOO_SIZEOF(*class_name)); +/* TODO: set '\0' after file names? */ + + moo->dbginfo->_last_class = moo->dbginfo->_len; + moo->dbginfo->_len += req_bytes; + + if (start_offset) *start_offset = moo->dbginfo->_last_class; + return 0; +} + +int moo_addfiletodbginfo (moo_t* moo, const moo_ooch_t* file_name, moo_oow_t* start_offset) +{ + moo_oow_t name_len, name_bytes, name_bytes_aligned, req_bytes; + moo_dbginfo_file_t* di; + + if (!moo->dbginfo) return 0; /* debug information is disabled*/ + + name_len = moo_count_oocstr(file_name); + name_bytes = name_len * MOO_SIZEOF(*file_name); + name_bytes_aligned = MOO_ALIGN_POW2(name_bytes, MOO_SIZEOF_OOW_T); + req_bytes = MOO_SIZEOF(moo_dbginfo_file_t) + name_bytes_aligned; + + if (secure_dbginfo_space(moo, req_bytes) <= -1) return -1; + + di = (moo_dbginfo_file_t*)&((moo_uint8_t*)moo->dbginfo)[moo->dbginfo->_len]; + di->_type = MOO_DBGINFO_MAKE_TYPE(MOO_DBGINFO_TYPE_CODE_FILE, 0); + di->_len = name_len; + di->_next = moo->dbginfo->_last_file; + + MOO_MEMCPY (di + 1, file_name, name_len * MOO_SIZEOF(*file_name)); +/* TODO: set '\0' after file names? */ + + moo->dbginfo->_last_file = moo->dbginfo->_len; + moo->dbginfo->_len += req_bytes; + + if (start_offset) *start_offset = moo->dbginfo->_last_file; + return 0; +} + +#if 0 +int moo_addmethodtodbginfo (moo_t* moo, const moo_ooch_t* class_name, const moo_ooch_t* method_name, const moo_ooch_t* file_name) +{ + moo_oow_t name_len, name_bytes, name_bytes_aligned, req_bytes; + moo_dbginfo_file_t* di; + + if (!moo->dbginfo) return 0; /* debug information is disabled*/ + + name_len = moo_count_oocstr(file_name); + name_bytes = name_len * MOO_SIZEOF(*file_name); + name_bytes_aligned = MOO_ALIGN_POW2(name_bytes, MOO_SIZEOF_OOW_T); + req_bytes = MOO_SIZEOF(moo_dbginfo_file_t) + name_bytes_aligned; + + if (secure_dbginfo_space(moo, req_bytes) <= -1) return -1; + + di = (moo_dbginfo_file_t*)&((moo_uint8_t*)moo->dbginfo)[moo->dbginfo->_len]; + di->_type = MOO_DBGINFO_MAKE_TYPE(MOO_DBGINFO_TYPE_CODE_FILE, 0); + di->_len = name_len; + di->_next = moo->dbginfo->_last_file; + + MOO_MEMCPY (di + 1, file_name, name_len * MOO_SIZEOF(*file_name)); +/* TODO: set '\0' after file names? */ + + moo->dbginfo->_last_file = moo->dbginfo->_len; + moo->dbginfo->_len += req_bytes; + + if (start_offset) *start_offset = moo->dbginfo->_last_file; + return 0; +} +#endif \ No newline at end of file diff --git a/moo/lib/moo.h b/moo/lib/moo.h index 27628d0..a301c8d 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -949,9 +949,56 @@ struct moo_dbginfo_t { moo_oow_t _capa; moo_oow_t _len; + moo_oow_t _last_class; /* offset to the last class element added */ + moo_oow_t _last_file; /* offset to the last file element added */ /* actual information is recorded here */ }; +enum moo_dbginfo_type_t +{ + /* bit 8 to bit 15 */ + MOO_DBGINFO_TYPE_CODE_CLASS = 0, + MOO_DBGINFO_TYPE_CODE_FILE = 1, + MOO_DBGINFO_TYPE_CODE_METHOD = 2, + + /* low 8 bits */ + MOO_DBGINFO_TYPE_FLAG_INVALID = (1 << 0) +}; +typedef enum moo_dbginfo_type_t moo_dbginfo_type_t; + +#define MOO_DBGINFO_MAKE_TYPE(code,flags) (((code) << 8) | (flags)) + +typedef struct moo_dbginfo_class_t moo_dbginfo_class_t; +struct moo_dbginfo_class_t +{ + moo_oow_t _type; + moo_oow_t _len; + moo_oow_t _next; /* offset to a previous class */ + /* ... class name here ... */ +}; + +typedef struct moo_dbginfo_file_t moo_dbginfo_file_t; +struct moo_dbginfo_file_t +{ + moo_oow_t _type; + moo_oow_t _len; + moo_oow_t _next; + /* ... file path here ... */ +}; + +typedef struct moo_dbginfo_method_t moo_dbginfo_method_t; +struct moo_dbginfo_method_t +{ + moo_oow_t _type; + moo_oow_t _name_len; + moo_oow_t _code_len; + moo_oow_t _next; + moo_oow_t _class; + moo_oow_t _file; + /* ... method name here ... */ + /* ... code info here ... */ +}; + /* ========================================================================= * VM LOGGING * ========================================================================= */ diff --git a/moo/lib/std.c b/moo/lib/std.c index 68bb0ce..079ed51 100644 --- a/moo/lib/std.c +++ b/moo/lib/std.c @@ -1745,8 +1745,8 @@ static int _add_poll_fd (moo_t* moo, int fd, int event_mask) moo_oow_t newcapa; newcapa = MOO_ALIGN_POW2 (xtn->ev.reg.len + 1, 256); - tmp = (struct pollfd*)moo_reallocmem (moo, xtn->ev.reg.ptr, newcapa * MOO_SIZEOF(*tmp)); - tmp2 = (struct pollfd*)moo_reallocmem (moo, xtn->ev.buf, newcapa * MOO_SIZEOF(*tmp2)); + tmp = (struct pollfd*)moo_reallocmem(moo, xtn->ev.reg.ptr, newcapa * MOO_SIZEOF(*tmp)); + tmp2 = (struct pollfd*)moo_reallocmem(moo, xtn->ev.buf, newcapa * MOO_SIZEOF(*tmp2)); if (!tmp || !tmp2) { MOO_DEBUG2 (moo, "Cannot add file descriptor %d to poll - %hs\n", fd, strerror(errno)); @@ -3904,4 +3904,4 @@ void moo_uncatch_termreq (void) unset_signal_handler(SIGINT); } -#endif +#endif \ No newline at end of file