2015-06-11 12:09:10 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2019-11-19 09:40:26 +00:00
|
|
|
Copyright (c) 2014-2019 Chung, Hyung-Hwan. All rights reserved.
|
2015-06-11 12:09:10 +00:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
2023-11-21 14:50:00 +00:00
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
2015-06-11 12:09:10 +00:00
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
#include "moo-prv.h"
|
2015-06-11 12:09:10 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
void moo_dumpsymtab (moo_t* moo)
|
2015-06-11 12:09:10 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t i;
|
|
|
|
moo_oop_char_t symbol;
|
2015-06-11 12:09:10 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_DEBUG0 (moo, "--------------------------------------------\n");
|
2018-12-19 16:57:16 +00:00
|
|
|
MOO_DEBUG1 (moo, "MOO Symbol Table %zu\n", MOO_OBJ_GET_SIZE(moo->symtab->bucket));
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_DEBUG0 (moo, "--------------------------------------------\n");
|
2015-06-11 12:09:10 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
for (i = 0; i < MOO_OBJ_GET_SIZE(moo->symtab->bucket); i++)
|
2015-06-11 12:09:10 +00:00
|
|
|
{
|
2018-12-19 15:47:52 +00:00
|
|
|
symbol = (moo_oop_char_t)MOO_OBJ_GET_OOP_VAL(moo->symtab->bucket, i);
|
2018-03-27 09:32:49 +00:00
|
|
|
if ((moo_oop_t)symbol != moo->_nil)
|
2015-06-11 12:09:10 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_DEBUG2 (moo, " %07zu %O\n", i, symbol);
|
2015-06-11 12:09:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-05 18:37:28 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_DEBUG0 (moo, "--------------------------------------------\n");
|
2015-06-11 12:09:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:27:48 +00:00
|
|
|
void moo_dumpdic (moo_t* moo, moo_oop_dic_t dic, const moo_bch_t* title)
|
2015-06-11 12:09:10 +00:00
|
|
|
{
|
2017-05-16 15:01:31 +00:00
|
|
|
moo_oow_t i;
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oop_association_t ass;
|
2015-06-11 12:09:10 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_DEBUG0 (moo, "--------------------------------------------\n");
|
|
|
|
MOO_DEBUG2 (moo, "%s %zu\n", title, MOO_OBJ_GET_SIZE(dic->bucket));
|
|
|
|
MOO_DEBUG0 (moo, "--------------------------------------------\n");
|
2015-06-11 12:09:10 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
for (i = 0; i < MOO_OBJ_GET_SIZE(dic->bucket); i++)
|
2015-06-11 12:09:10 +00:00
|
|
|
{
|
2018-12-19 15:47:52 +00:00
|
|
|
ass = (moo_oop_association_t)MOO_OBJ_GET_OOP_VAL(dic->bucket, i);
|
2017-01-09 09:54:49 +00:00
|
|
|
if ((moo_oop_t)ass != moo->_nil)
|
2015-06-11 12:09:10 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_DEBUG2 (moo, " %07zu %O\n", i, ass->key);
|
2015-06-11 12:09:10 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_DEBUG0 (moo, "--------------------------------------------\n");
|
2015-06-11 12:09:10 +00:00
|
|
|
}
|
2019-07-07 15:24:27 +00:00
|
|
|
|
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
/* TODO: moo_loaddbgifromimage() -> load debug information from compiled image?
|
|
|
|
moo_storedbgitoimage()? -> store debug information to compiled image?
|
|
|
|
moo_compactdbgi()? -> compact debug information by scaning dbgi data. find class and method. if not found, drop the portion.
|
2019-07-07 15:24:27 +00:00
|
|
|
*/
|
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
int moo_initdbgi (moo_t* moo, moo_oow_t capa)
|
2019-07-07 15:24:27 +00:00
|
|
|
{
|
2019-07-10 09:19:38 +00:00
|
|
|
moo_dbgi_t* tmp;
|
2019-07-07 15:24:27 +00:00
|
|
|
|
2019-07-08 07:51:53 +00:00
|
|
|
if (capa < MOO_SIZEOF(*tmp)) capa = MOO_SIZEOF(*tmp);
|
2019-07-07 15:24:27 +00:00
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
tmp = (moo_dbgi_t*)moo_callocmem(moo, capa);
|
2019-07-08 07:51:53 +00:00
|
|
|
if (!tmp) return -1;
|
2019-07-07 15:24:27 +00:00
|
|
|
|
2019-07-08 07:51:53 +00:00
|
|
|
tmp->_capa = capa;
|
|
|
|
tmp->_len = MOO_SIZEOF(*tmp);
|
2019-07-11 15:58:16 +00:00
|
|
|
/* tmp->_last_file = 0;
|
2019-07-08 07:51:53 +00:00
|
|
|
tmp->_last_class = 0;
|
2019-07-11 15:58:16 +00:00
|
|
|
tmp->_last_text = 0;
|
|
|
|
tmp->_last_method = 0; */
|
2019-07-08 07:51:53 +00:00
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
moo->dbgi = tmp;
|
2019-07-07 15:24:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
void moo_finidbgi (moo_t* moo)
|
2019-07-07 15:24:27 +00:00
|
|
|
{
|
2019-07-10 09:19:38 +00:00
|
|
|
if (moo->dbgi)
|
2019-07-07 15:24:27 +00:00
|
|
|
{
|
2019-07-10 09:19:38 +00:00
|
|
|
moo_freemem (moo, moo->dbgi);
|
|
|
|
moo->dbgi = MOO_NULL;
|
2019-07-07 15:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
static MOO_INLINE moo_uint8_t* secure_dbgi_space (moo_t* moo, moo_oow_t req_bytes)
|
2019-07-08 07:51:53 +00:00
|
|
|
{
|
2019-07-10 09:19:38 +00:00
|
|
|
if (moo->dbgi->_capa - moo->dbgi->_len < req_bytes)
|
2019-07-08 07:51:53 +00:00
|
|
|
{
|
2019-07-10 09:19:38 +00:00
|
|
|
moo_dbgi_t* tmp;
|
2019-07-08 07:51:53 +00:00
|
|
|
moo_oow_t newcapa;
|
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
newcapa = moo->dbgi->_len + req_bytes;
|
2019-07-08 07:51:53 +00:00
|
|
|
newcapa = MOO_ALIGN_POW2(newcapa, 65536); /* TODO: make the align value configurable */
|
2019-07-10 09:19:38 +00:00
|
|
|
tmp = moo_reallocmem(moo, moo->dbgi, newcapa);
|
2019-07-08 15:41:57 +00:00
|
|
|
if (!tmp) return MOO_NULL;
|
2019-07-08 07:51:53 +00:00
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
moo->dbgi = tmp;
|
|
|
|
moo->dbgi->_capa = newcapa;
|
2019-07-08 07:51:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
return &((moo_uint8_t*)moo->dbgi)[moo->dbgi->_len];
|
2019-07-08 07:51:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
int moo_addfiletodbgi (moo_t* moo, const moo_ooch_t* file_name, moo_oow_t* start_offset)
|
2019-07-08 07:51:53 +00:00
|
|
|
{
|
|
|
|
moo_oow_t name_len, name_bytes, name_bytes_aligned, req_bytes;
|
2019-07-10 09:19:38 +00:00
|
|
|
moo_dbgi_file_t* di;
|
2019-07-08 07:51:53 +00:00
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
if (!moo->dbgi)
|
2019-07-09 09:59:38 +00:00
|
|
|
{
|
2019-07-26 04:40:33 +00:00
|
|
|
if (start_offset) *start_offset = 0;
|
2019-07-09 09:59:38 +00:00
|
|
|
return 0; /* debug information is disabled*/
|
|
|
|
}
|
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
if (moo->dbgi->_last_file > 0)
|
2019-07-09 09:59:38 +00:00
|
|
|
{
|
2019-07-09 15:56:34 +00:00
|
|
|
/* TODO: avoid linear search. need indexing for speed up */
|
2019-07-10 09:19:38 +00:00
|
|
|
moo_oow_t offset = moo->dbgi->_last_file;
|
2019-07-09 09:59:38 +00:00
|
|
|
do
|
|
|
|
{
|
2019-07-10 09:19:38 +00:00
|
|
|
di = (moo_dbgi_file_t*)&((moo_uint8_t*)moo->dbgi)[offset];
|
|
|
|
if (moo_comp_oocstr((moo_ooch_t*)(di + 1), file_name) == 0)
|
2019-07-09 09:59:38 +00:00
|
|
|
{
|
|
|
|
if (start_offset) *start_offset = offset;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
offset = di->_next;
|
|
|
|
}
|
|
|
|
while (offset > 0);
|
|
|
|
}
|
2019-07-08 07:51:53 +00:00
|
|
|
|
2019-07-08 15:41:57 +00:00
|
|
|
name_len = moo_count_oocstr(file_name);
|
2019-07-09 09:59:38 +00:00
|
|
|
name_bytes = (name_len + 1) * MOO_SIZEOF(*file_name);
|
2019-07-08 07:51:53 +00:00
|
|
|
name_bytes_aligned = MOO_ALIGN_POW2(name_bytes, MOO_SIZEOF_OOW_T);
|
2019-07-10 09:19:38 +00:00
|
|
|
req_bytes = MOO_SIZEOF(moo_dbgi_file_t) + name_bytes_aligned;
|
2019-07-08 07:51:53 +00:00
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
di = (moo_dbgi_file_t*)secure_dbgi_space(moo, req_bytes);
|
2019-07-08 15:41:57 +00:00
|
|
|
if (!di) return -1;
|
2019-07-08 07:51:53 +00:00
|
|
|
|
2019-07-11 15:58:16 +00:00
|
|
|
di->_type = MOO_DBGI_MAKE_TYPE(MOO_DBGI_TYPE_CODE_FILE, 0);
|
2019-07-09 09:59:38 +00:00
|
|
|
di->_len = req_bytes;
|
2019-07-10 09:19:38 +00:00
|
|
|
di->_next = moo->dbgi->_last_file;
|
|
|
|
moo_copy_oocstr ((moo_ooch_t*)(di + 1), name_len + 1, file_name);
|
2019-07-08 07:51:53 +00:00
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
moo->dbgi->_last_file = moo->dbgi->_len;
|
|
|
|
moo->dbgi->_len += req_bytes;
|
2019-07-08 07:51:53 +00:00
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
if (start_offset) *start_offset = moo->dbgi->_last_file;
|
2019-07-08 07:51:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
int moo_addclasstodbgi (moo_t* moo, const moo_ooch_t* class_name, moo_oow_t file_offset, moo_oow_t file_line, moo_oow_t* start_offset)
|
2019-07-08 07:51:53 +00:00
|
|
|
{
|
|
|
|
moo_oow_t name_len, name_bytes, name_bytes_aligned, req_bytes;
|
2019-07-10 09:19:38 +00:00
|
|
|
moo_dbgi_class_t* di;
|
2019-07-08 07:51:53 +00:00
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
if (!moo->dbgi) return 0; /* debug information is disabled*/
|
2019-07-08 07:51:53 +00:00
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
if (moo->dbgi->_last_class > 0)
|
2019-07-09 15:56:34 +00:00
|
|
|
{
|
|
|
|
/* TODO: avoid linear search. need indexing for speed up */
|
2019-07-10 09:19:38 +00:00
|
|
|
moo_oow_t offset = moo->dbgi->_last_class;
|
2019-07-09 15:56:34 +00:00
|
|
|
do
|
|
|
|
{
|
2019-07-10 09:19:38 +00:00
|
|
|
di = (moo_dbgi_class_t*)&((moo_uint8_t*)moo->dbgi)[offset];
|
|
|
|
if (moo_comp_oocstr((moo_ooch_t*)(di + 1), class_name) == 0 && di->_file == file_offset && di->_line == file_line)
|
2019-07-09 15:56:34 +00:00
|
|
|
{
|
|
|
|
if (start_offset) *start_offset = offset;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
offset = di->_next;
|
|
|
|
}
|
|
|
|
while (offset > 0);
|
|
|
|
}
|
|
|
|
|
2019-07-08 15:41:57 +00:00
|
|
|
name_len = moo_count_oocstr(class_name);
|
2019-07-09 09:59:38 +00:00
|
|
|
name_bytes = (name_len + 1) * MOO_SIZEOF(*class_name);
|
2019-07-08 07:51:53 +00:00
|
|
|
name_bytes_aligned = MOO_ALIGN_POW2(name_bytes, MOO_SIZEOF_OOW_T);
|
2019-07-10 09:19:38 +00:00
|
|
|
req_bytes = MOO_SIZEOF(moo_dbgi_class_t) + name_bytes_aligned;
|
2019-07-08 07:51:53 +00:00
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
di = (moo_dbgi_class_t*)secure_dbgi_space(moo, req_bytes);
|
2019-07-08 15:41:57 +00:00
|
|
|
if (!di) return -1;
|
2019-07-08 07:51:53 +00:00
|
|
|
|
2019-07-11 15:58:16 +00:00
|
|
|
di->_type = MOO_DBGI_MAKE_TYPE(MOO_DBGI_TYPE_CODE_CLASS, 0);
|
2019-07-09 09:59:38 +00:00
|
|
|
di->_len = req_bytes;
|
2019-07-10 09:19:38 +00:00
|
|
|
di->_next = moo->dbgi->_last_class;
|
2019-07-08 15:41:57 +00:00
|
|
|
di->_file = file_offset;
|
|
|
|
di->_line = file_line;
|
2019-07-10 09:19:38 +00:00
|
|
|
moo_copy_oocstr ((moo_ooch_t*)(di + 1), name_len + 1, class_name);
|
2019-07-08 07:51:53 +00:00
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
moo->dbgi->_last_class = moo->dbgi->_len;
|
|
|
|
moo->dbgi->_len += req_bytes;
|
2019-07-08 07:51:53 +00:00
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
if (start_offset) *start_offset = moo->dbgi->_last_class;
|
2019-07-08 07:51:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-12 03:38:45 +00:00
|
|
|
int moo_addmethodtodbgi (moo_t* moo, moo_oow_t file_offset, moo_oow_t class_offset, const moo_ooch_t* method_name, moo_oow_t start_line, const moo_oow_t* code_loc_ptr, moo_oow_t code_loc_len, const moo_ooch_t* text_ptr, moo_oow_t text_len, moo_oow_t* start_offset)
|
2019-07-07 15:24:27 +00:00
|
|
|
{
|
2019-07-12 03:38:45 +00:00
|
|
|
moo_oow_t name_len, name_bytes, name_bytes_aligned, code_loc_bytes, code_loc_bytes_aligned, text_bytes, text_bytes_aligned, req_bytes;
|
2019-07-10 09:19:38 +00:00
|
|
|
moo_dbgi_method_t* di;
|
2019-07-12 03:38:45 +00:00
|
|
|
moo_uint8_t* curptr;
|
2019-07-08 07:51:53 +00:00
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
if (!moo->dbgi) return 0; /* debug information is disabled*/
|
2019-07-08 07:51:53 +00:00
|
|
|
|
2019-07-08 15:41:57 +00:00
|
|
|
name_len = moo_count_oocstr(method_name);
|
2019-07-09 09:59:38 +00:00
|
|
|
name_bytes = (name_len + 1) * MOO_SIZEOF(*method_name);
|
2019-07-08 07:51:53 +00:00
|
|
|
name_bytes_aligned = MOO_ALIGN_POW2(name_bytes, MOO_SIZEOF_OOW_T);
|
2019-07-08 15:41:57 +00:00
|
|
|
code_loc_bytes = code_loc_len * MOO_SIZEOF(*code_loc_ptr);
|
|
|
|
code_loc_bytes_aligned = MOO_ALIGN_POW2(code_loc_bytes, MOO_SIZEOF_OOW_T);
|
2019-07-12 03:38:45 +00:00
|
|
|
text_bytes = text_len * MOO_SIZEOF(*text_ptr);
|
|
|
|
text_bytes_aligned = MOO_ALIGN_POW2(text_bytes, MOO_SIZEOF_OOW_T);
|
|
|
|
req_bytes = MOO_SIZEOF(moo_dbgi_method_t) + name_bytes_aligned + code_loc_bytes_aligned + text_bytes_aligned;
|
2019-07-08 15:41:57 +00:00
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
di = (moo_dbgi_method_t*)secure_dbgi_space(moo, req_bytes);
|
2019-07-08 15:41:57 +00:00
|
|
|
if (!di) return -1;
|
|
|
|
|
2019-07-11 15:58:16 +00:00
|
|
|
di->_type = MOO_DBGI_MAKE_TYPE(MOO_DBGI_TYPE_CODE_METHOD, 0);
|
2019-07-09 09:59:38 +00:00
|
|
|
di->_len = req_bytes;
|
2019-07-10 09:19:38 +00:00
|
|
|
di->_next = moo->dbgi->_last_method;
|
2019-07-08 15:41:57 +00:00
|
|
|
di->_file = file_offset;
|
|
|
|
di->_class = class_offset;
|
2019-07-12 03:38:45 +00:00
|
|
|
di->start_line = start_line;
|
2019-07-12 07:24:37 +00:00
|
|
|
di->code_loc_start = name_bytes_aligned; /* distance from the beginning of the variable payload */
|
2019-07-08 15:41:57 +00:00
|
|
|
di->code_loc_len = code_loc_len;
|
2019-07-12 07:24:37 +00:00
|
|
|
di->text_start = name_bytes_aligned + code_loc_bytes_aligned; /* distance from the beginning of the variable payload */
|
2019-07-12 03:38:45 +00:00
|
|
|
di->text_len = text_len;
|
|
|
|
|
|
|
|
curptr = (moo_uint8_t*)(di + 1);
|
|
|
|
moo_copy_oocstr ((moo_ooch_t*)curptr, name_len + 1, method_name);
|
2019-07-08 15:41:57 +00:00
|
|
|
|
2019-07-12 03:38:45 +00:00
|
|
|
curptr += name_bytes_aligned;
|
|
|
|
MOO_MEMCPY (curptr, code_loc_ptr, code_loc_bytes);
|
|
|
|
|
|
|
|
if (text_len > 0)
|
|
|
|
{
|
|
|
|
curptr += code_loc_bytes_aligned;
|
|
|
|
moo_copy_oochars ((moo_ooch_t*)curptr, text_ptr, text_len);
|
|
|
|
}
|
2019-07-09 09:59:38 +00:00
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
moo->dbgi->_last_method = moo->dbgi->_len;
|
|
|
|
moo->dbgi->_len += req_bytes;
|
2019-07-08 07:51:53 +00:00
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
if (start_offset) *start_offset = moo->dbgi->_last_method;
|
2019-07-07 15:24:27 +00:00
|
|
|
return 0;
|
2019-07-08 07:51:53 +00:00
|
|
|
}
|