static void lua_printstack (FILE *f) { int level = 1; /* skip level 0 (it's this function) */ lua_Object func; while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) { char *name; int currentline; char *filename; int linedefined; lua_funcinfo(func, &filename, &linedefined); fprintf(f, (level==2) ? "Active Stack:\n\t" : "\t"); switch (*lua_getobjname(func, &name)) { case 'g': fprintf(f, "function %s", name); break; case 't': fprintf(f, "`%s' tag method", name); break; default: { if (linedefined == 0) fprintf(f, "main of %s", filename); else if (linedefined < 0) fprintf(f, "%s", filename); else fprintf(f, "function (%s:%d)", filename, linedefined); filename = NULL; } } if ((currentline = lua_currentline(func)) > 0) fprintf(f, " at line %d", currentline); if (filename) fprintf(f, " [in file %s]", filename); fprintf(f, "\n"); } }
static lua_Object getfuncinfo (lua_Object func) { lua_Object result = lua_createtable(); char *str; int line; lua_funcinfo(func, &str, &line); if (line == -1) /* C function? */ settabss(result, "kind", "C"); else if (line == 0) { /* "main"? */ settabss(result, "kind", "chunk"); settabss(result, "source", str); } else { /* Lua function */ settabss(result, "kind", "Lua"); settabsi(result, "def_line", line); settabss(result, "source", str); } if (line != 0) { /* is it not a "main"? */ char *kind = lua_getobjname(func, &str); if (*kind) { settabss(result, "name", str); settabss(result, "where", kind); } } return result; }
void callHook(lua_Function func, const char *filename, int32 line) { const char *name, *type; FILE *output = stdout; int i; type = lua_getobjname(func, &name); if (func == LUA_NOOBJECT) { fprintf(output, "%s\n", filename); return; } switch (*type) { case 'g': fprintf(output, "function: %s(", name); for (i = 1; ; i++) { if (lua_getparam(i) == LUA_NOOBJECT) break; if (lua_isnil(lua_getparam(i))) fprintf(output, "nil"); else if (lua_istable(lua_getparam(i))) fprintf(output, "{...}"); else if (lua_isuserdata(lua_getparam(i))) { if (lua_tag(lua_getparam(i)) == MKTAG('A','C','T','R')) { Actor *a = Actor::getPool().getObject(lua_getuserdata(lua_getparam(i))); fprintf(output, "<actor \"%s\">", a->getName().c_str()); } else if (lua_tag(lua_getparam(i)) == MKTAG('C','O','L','R')) { Color c(lua_getuserdata(lua_getparam(i))); fprintf(output, "<color #%02x%02x%02x>", c.getRed(), c.getGreen(), c.getBlue()); } else fprintf(output, "<userdata %d>", lua_getuserdata(lua_getparam(i))); } else if (lua_isfunction(lua_getparam(i))) { fprintf(output, "<function>"); } else if (lua_isnumber(lua_getparam(i))) fprintf(output, "%g", lua_getnumber(lua_getparam(i))); else if (lua_isstring(lua_getparam(i))) fprintf(output, "\"%s\"", lua_getstring(lua_getparam(i))); else fprintf(output, "<unknown>"); if (lua_getparam(i + 1) != LUA_NOOBJECT) fprintf(output, ", "); } fprintf(output, ")"); break; case 't': fprintf(output, "`%s' tag method", name); break; default: { if (line == 0) fprintf(output, "{START SCRIPT: %s}", filename); else if (line < 0) { fprintf(output, "%s", filename); } else fprintf(output, "function (%s:%d)", filename, line); } } fprintf(output, "\n"); }
void luaL_argerror(int32 numarg, const char *extramsg) { const char *funcname; lua_getobjname(lua_stackedfunction(0), &funcname); if (!funcname) funcname = "???"; if (!extramsg) luaL_verror("bad argument #%d to function `%.50s'", numarg, funcname); else luaL_verror("bad argument #%d to function `%.50s' (%.100s)", numarg, funcname, extramsg); }
void luaL_argerror (int numarg, char *extramsg) { lua_Function f = lua_stackedfunction(0); char *funcname; lua_getobjname(f, &funcname); numarg -= lua_nups(f); if (funcname == NULL) funcname = "?"; if (extramsg == NULL) luaL_verror("bad argument #%d to function `%.50s'", numarg, funcname); else luaL_verror("bad argument #%d to function `%.50s' (%.100s)", numarg, funcname, extramsg); }
void luaL_arg_check(int cond, int numarg, char *extramsg) { if (!cond) { char *funcname; lua_getobjname(lua_stackedfunction(0), &funcname); if (funcname == NULL) funcname = "???"; if (extramsg == NULL) luaL_verror("bad argument #%d to function `%s'", numarg, funcname); else luaL_verror("bad argument #%d to function `%s' (%s)", numarg, funcname, extramsg); } }
static void lua_printstack() { int32 level = 1; // skip level 0 (it's this function) lua_Object func; char buf[256]; while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) { const char *name; int32 currentline; const char *filename; int32 linedefined; lua_funcinfo(func, &filename, &linedefined); sprintf(buf, (level == 2) ? "Active Stack:\n\t" : "\t"); g_stderr->write(buf, strlen(buf)); switch (*lua_getobjname(func, &name)) { case 'g': sprintf(buf, "function %s", name); break; case 't': sprintf(buf, "`%s' tag method", name); break; default: { if (linedefined == 0) sprintf(buf, "main of %s", filename); else if (linedefined < 0) sprintf(buf, "%s", filename); else sprintf(buf, "function (%s:%d)", filename, (int)linedefined); filename = NULL; } } g_stderr->write(buf, strlen(buf)); if ((currentline = lua_currentline(func)) > 0) { sprintf(buf, " at line %d", (int)currentline); g_stderr->write(buf, strlen(buf)); } if (filename) { sprintf(buf, " [in file %s]", filename); g_stderr->write(buf, strlen(buf)); } sprintf(buf, "\n"); g_stderr->write(buf, strlen(buf)); } }
void L1_FunctionName() { const char *name; char buf[256]; const char *filename = 0; int32 line; lua_Object param1 = lua_getparam(1); if (!lua_isfunction(param1)) { sprintf(buf, "function InvalidArgsToFunctionName"); lua_pushstring(buf); return; } lua_funcinfo(param1, &filename, &line); switch (*lua_getobjname(param1, &name)) { case 'g': sprintf(buf, "function %.100s", name); break; case 't': sprintf(buf, "`%.100s' tag method", name); break; default: { // cout<<(void*)filename<<endl; if (line == 0) sprintf(buf, "main of %.100s", filename); else if (line < 0) sprintf(buf, "%.100s", filename); else { sprintf(buf, "function (%.100s:%d)", filename, (int)line); filename = NULL; } } } int curr_line = lua_currentline(param1); if (curr_line > 0) sprintf(buf + strlen(buf), " at line %d", curr_line); if (filename) sprintf(buf + strlen(buf), " [in file %.100s]", filename); lua_pushstring(buf); }
/* CALL FUNCTION HOOK */ static void callhook(lua_State *L, lua_Function func, char *file, int line) { check_start(L); Meta **array = get_metadata_array(L); if (!array) return; // check if exists (call profile_stop ?) if (STACK_INDEX > MEM_BLOCKSIZE - 1) { // Reached memory limit, relocated to double. int blocksize = MEM_BLOCKSIZE * 2; array = realloc(array, blocksize * sizeof(Meta **)); if (array) { lua_unref(L, META_REF); // Remove the old reference (new block of memory). lua_pushuserdata(L, array); // Saves the new reference. META_REF = lua_ref(L, 1); MEM_BLOCKSIZE = blocksize; // Updates the size of the memory block. } else { lua_error(L, "profiler: out of memory!"); return; // suppress inspect } } char *func_name; char *func_scope; if (lua_isfunction(L, func)) { func_scope = lua_getobjname(L, func, &func_name); Meta *meta = (Meta *) malloc(sizeof(Meta)); meta->fun_name = func_name ? func_name : "unnamed"; if (func_scope && strlen(func_scope) > 0) { meta->fun_scope = func_scope; } else { meta->fun_scope = "unknown"; } meta->func_file = file ? file : "unnamed"; meta->stack_level = STACK_SIZE; meta->line = line; Children *children = (Children *) malloc(sizeof(Children)); meta->children = children; children->index = 0; children->list = NULL; children->size = 20; Measure *measure = (Measure *) malloc(sizeof(Measure)); measure->begin = clock(); meta->measure = measure; stack_record.meta = meta; push(&stack, stack_record); if (STACK_SIZE == 0) { array[STACK_INDEX] = meta; STACK_INDEX++; } STACK_SIZE++; } else if (STACK_SIZE > 0) { STACK_RECORD top_record = pop(&stack); STACK_RECORD *new_record = next(&stack); Meta *meta = top_record.meta; meta->measure->end = clock(); meta->measure->time_spent = calc_time_spent(meta->measure); if (new_record != NULL && meta->measure->time_spent >= PROFILE_RECORD_TIME) { Meta *_meta = new_record->meta; if (!_meta->children->list) { // already allocated ? _meta->children->list = (Meta **) malloc(_meta->children->size * sizeof(Meta **)); if (!_meta->children->list) lua_error(L, "out of memory"); } if (_meta->children->index > _meta->children->size - 1) { _meta->children->size *= 2; // more _meta->children->list = (Meta **) realloc(_meta->children->list, _meta->children->size * sizeof(Meta **)); if (!_meta->children->list) lua_error(L, "out of memory"); } _meta->children->list[_meta->children->index] = meta; _meta->children->index++; } STACK_SIZE--; } }