Merge branch 'master' into v2.1
[luajit-2.0.git] / src / lj_debug.c
blob889ac01702b4ae2973d8b103a3de171993f2ca1e
1 /*
2 ** Debugging and introspection.
3 ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #define lj_debug_c
7 #define LUA_CORE
9 #include "lj_obj.h"
10 #include "lj_err.h"
11 #include "lj_debug.h"
12 #include "lj_buf.h"
13 #include "lj_tab.h"
14 #include "lj_state.h"
15 #include "lj_frame.h"
16 #include "lj_bc.h"
17 #include "lj_strfmt.h"
18 #include "lj_vm.h"
19 #if LJ_HASJIT
20 #include "lj_jit.h"
21 #endif
23 /* -- Frames -------------------------------------------------------------- */
25 /* Get frame corresponding to a level. */
26 cTValue *lj_debug_frame(lua_State *L, int level, int *size)
28 cTValue *frame, *nextframe, *bot = tvref(L->stack);
29 /* Traverse frames backwards. */
30 for (nextframe = frame = L->base-1; frame > bot; ) {
31 if (frame_gc(frame) == obj2gco(L))
32 level++; /* Skip dummy frames. See lj_err_optype_call(). */
33 if (level-- == 0) {
34 *size = (int)(nextframe - frame);
35 return frame; /* Level found. */
37 nextframe = frame;
38 if (frame_islua(frame)) {
39 frame = frame_prevl(frame);
40 } else {
41 if (frame_isvarg(frame))
42 level++; /* Skip vararg pseudo-frame. */
43 frame = frame_prevd(frame);
46 *size = level;
47 return NULL; /* Level not found. */
50 /* Invalid bytecode position. */
51 #define NO_BCPOS (~(BCPos)0)
53 /* Return bytecode position for function/frame or NO_BCPOS. */
54 static BCPos debug_framepc(lua_State *L, GCfunc *fn, cTValue *nextframe)
56 const BCIns *ins;
57 GCproto *pt;
58 BCPos pos;
59 lua_assert(fn->c.gct == ~LJ_TFUNC || fn->c.gct == ~LJ_TTHREAD);
60 if (!isluafunc(fn)) { /* Cannot derive a PC for non-Lua functions. */
61 return NO_BCPOS;
62 } else if (nextframe == NULL) { /* Lua function on top. */
63 void *cf = cframe_raw(L->cframe);
64 if (cf == NULL || (char *)cframe_pc(cf) == (char *)cframe_L(cf))
65 return NO_BCPOS;
66 ins = cframe_pc(cf); /* Only happens during error/hook handling. */
67 } else {
68 if (frame_islua(nextframe)) {
69 ins = frame_pc(nextframe);
70 } else if (frame_iscont(nextframe)) {
71 ins = frame_contpc(nextframe);
72 } else {
73 /* Lua function below errfunc/gc/hook: find cframe to get the PC. */
74 void *cf = cframe_raw(L->cframe);
75 TValue *f = L->base-1;
76 for (;;) {
77 if (cf == NULL)
78 return NO_BCPOS;
79 while (cframe_nres(cf) < 0) {
80 if (f >= restorestack(L, -cframe_nres(cf)))
81 break;
82 cf = cframe_raw(cframe_prev(cf));
83 if (cf == NULL)
84 return NO_BCPOS;
86 if (f < nextframe)
87 break;
88 if (frame_islua(f)) {
89 f = frame_prevl(f);
90 } else {
91 if (frame_isc(f) || (LJ_HASFFI && frame_iscont(f) &&
92 (f-1)->u32.lo == LJ_CONT_FFI_CALLBACK))
93 cf = cframe_raw(cframe_prev(cf));
94 f = frame_prevd(f);
97 ins = cframe_pc(cf);
100 pt = funcproto(fn);
101 pos = proto_bcpos(pt, ins) - 1;
102 #if LJ_HASJIT
103 if (pos > pt->sizebc) { /* Undo the effects of lj_trace_exit for JLOOP. */
104 GCtrace *T = (GCtrace *)((char *)(ins-1) - offsetof(GCtrace, startins));
105 lua_assert(bc_isret(bc_op(ins[-1])));
106 pos = proto_bcpos(pt, mref(T->startpc, const BCIns));
108 #endif
109 return pos;
112 /* -- Line numbers -------------------------------------------------------- */
114 /* Get line number for a bytecode position. */
115 BCLine LJ_FASTCALL lj_debug_line(GCproto *pt, BCPos pc)
117 const void *lineinfo = proto_lineinfo(pt);
118 if (pc <= pt->sizebc && lineinfo) {
119 BCLine first = pt->firstline;
120 if (pc == pt->sizebc) return first + pt->numline;
121 if (pc-- == 0) return first;
122 if (pt->numline < 256)
123 return first + (BCLine)((const uint8_t *)lineinfo)[pc];
124 else if (pt->numline < 65536)
125 return first + (BCLine)((const uint16_t *)lineinfo)[pc];
126 else
127 return first + (BCLine)((const uint32_t *)lineinfo)[pc];
129 return 0;
132 /* Get line number for function/frame. */
133 static BCLine debug_frameline(lua_State *L, GCfunc *fn, cTValue *nextframe)
135 BCPos pc = debug_framepc(L, fn, nextframe);
136 if (pc != NO_BCPOS) {
137 GCproto *pt = funcproto(fn);
138 lua_assert(pc <= pt->sizebc);
139 return lj_debug_line(pt, pc);
141 return -1;
144 /* -- Variable names ------------------------------------------------------ */
146 /* Get name of a local variable from slot number and PC. */
147 static const char *debug_varname(const GCproto *pt, BCPos pc, BCReg slot)
149 const char *p = (const char *)proto_varinfo(pt);
150 if (p) {
151 BCPos lastpc = 0;
152 for (;;) {
153 const char *name = p;
154 uint32_t vn = *(const uint8_t *)p;
155 BCPos startpc, endpc;
156 if (vn < VARNAME__MAX) {
157 if (vn == VARNAME_END) break; /* End of varinfo. */
158 } else {
159 do { p++; } while (*(const uint8_t *)p); /* Skip over variable name. */
161 p++;
162 lastpc = startpc = lastpc + lj_buf_ruleb128(&p);
163 if (startpc > pc) break;
164 endpc = startpc + lj_buf_ruleb128(&p);
165 if (pc < endpc && slot-- == 0) {
166 if (vn < VARNAME__MAX) {
167 #define VARNAMESTR(name, str) str "\0"
168 name = VARNAMEDEF(VARNAMESTR);
169 #undef VARNAMESTR
170 if (--vn) while (*name++ || --vn) ;
172 return name;
176 return NULL;
179 /* Get name of local variable from 1-based slot number and function/frame. */
180 static TValue *debug_localname(lua_State *L, const lua_Debug *ar,
181 const char **name, BCReg slot1)
183 uint32_t offset = (uint32_t)ar->i_ci & 0xffff;
184 uint32_t size = (uint32_t)ar->i_ci >> 16;
185 TValue *frame = tvref(L->stack) + offset;
186 TValue *nextframe = size ? frame + size : NULL;
187 GCfunc *fn = frame_func(frame);
188 BCPos pc = debug_framepc(L, fn, nextframe);
189 if (!nextframe) nextframe = L->top;
190 if ((int)slot1 < 0) { /* Negative slot number is for varargs. */
191 if (pc != NO_BCPOS) {
192 GCproto *pt = funcproto(fn);
193 if ((pt->flags & PROTO_VARARG)) {
194 slot1 = pt->numparams + (BCReg)(-(int)slot1);
195 if (frame_isvarg(frame)) { /* Vararg frame has been set up? (pc!=0) */
196 nextframe = frame;
197 frame = frame_prevd(frame);
199 if (frame + slot1 < nextframe) {
200 *name = "(*vararg)";
201 return frame+slot1;
205 return NULL;
207 if (pc != NO_BCPOS &&
208 (*name = debug_varname(funcproto(fn), pc, slot1-1)) != NULL)
210 else if (slot1 > 0 && frame + slot1 < nextframe)
211 *name = "(*temporary)";
212 return frame+slot1;
215 /* Get name of upvalue. */
216 const char *lj_debug_uvname(GCproto *pt, uint32_t idx)
218 const uint8_t *p = proto_uvinfo(pt);
219 lua_assert(idx < pt->sizeuv);
220 if (!p) return "";
221 if (idx) while (*p++ || --idx) ;
222 return (const char *)p;
225 /* Get name and value of upvalue. */
226 const char *lj_debug_uvnamev(cTValue *o, uint32_t idx, TValue **tvp)
228 if (tvisfunc(o)) {
229 GCfunc *fn = funcV(o);
230 if (isluafunc(fn)) {
231 GCproto *pt = funcproto(fn);
232 if (idx < pt->sizeuv) {
233 *tvp = uvval(&gcref(fn->l.uvptr[idx])->uv);
234 return lj_debug_uvname(pt, idx);
236 } else {
237 if (idx < fn->c.nupvalues) {
238 *tvp = &fn->c.upvalue[idx];
239 return "";
243 return NULL;
246 /* Deduce name of an object from slot number and PC. */
247 const char *lj_debug_slotname(GCproto *pt, const BCIns *ip, BCReg slot,
248 const char **name)
250 const char *lname;
251 restart:
252 lname = debug_varname(pt, proto_bcpos(pt, ip), slot);
253 if (lname != NULL) { *name = lname; return "local"; }
254 while (--ip > proto_bc(pt)) {
255 BCIns ins = *ip;
256 BCOp op = bc_op(ins);
257 BCReg ra = bc_a(ins);
258 if (bcmode_a(op) == BCMbase) {
259 if (slot >= ra && (op != BC_KNIL || slot <= bc_d(ins)))
260 return NULL;
261 } else if (bcmode_a(op) == BCMdst && ra == slot) {
262 switch (bc_op(ins)) {
263 case BC_MOV:
264 if (ra == slot) { slot = bc_d(ins); goto restart; }
265 break;
266 case BC_GGET:
267 *name = strdata(gco2str(proto_kgc(pt, ~(ptrdiff_t)bc_d(ins))));
268 return "global";
269 case BC_TGETS:
270 *name = strdata(gco2str(proto_kgc(pt, ~(ptrdiff_t)bc_c(ins))));
271 if (ip > proto_bc(pt)) {
272 BCIns insp = ip[-1];
273 if (bc_op(insp) == BC_MOV && bc_a(insp) == ra+1 &&
274 bc_d(insp) == bc_b(ins))
275 return "method";
277 return "field";
278 case BC_UGET:
279 *name = lj_debug_uvname(pt, bc_d(ins));
280 return "upvalue";
281 default:
282 return NULL;
286 return NULL;
289 /* Deduce function name from caller of a frame. */
290 const char *lj_debug_funcname(lua_State *L, cTValue *frame, const char **name)
292 cTValue *pframe;
293 GCfunc *fn;
294 BCPos pc;
295 if (frame <= tvref(L->stack))
296 return NULL;
297 if (frame_isvarg(frame))
298 frame = frame_prevd(frame);
299 pframe = frame_prev(frame);
300 fn = frame_func(pframe);
301 pc = debug_framepc(L, fn, frame);
302 if (pc != NO_BCPOS) {
303 GCproto *pt = funcproto(fn);
304 const BCIns *ip = &proto_bc(pt)[check_exp(pc < pt->sizebc, pc)];
305 MMS mm = bcmode_mm(bc_op(*ip));
306 if (mm == MM_call) {
307 BCReg slot = bc_a(*ip);
308 if (bc_op(*ip) == BC_ITERC) slot -= 3;
309 return lj_debug_slotname(pt, ip, slot, name);
310 } else if (mm != MM__MAX) {
311 *name = strdata(mmname_str(G(L), mm));
312 return "metamethod";
315 return NULL;
318 /* -- Source code locations ----------------------------------------------- */
320 /* Generate shortened source name. */
321 void lj_debug_shortname(char *out, GCstr *str, BCLine line)
323 const char *src = strdata(str);
324 if (*src == '=') {
325 strncpy(out, src+1, LUA_IDSIZE); /* Remove first char. */
326 out[LUA_IDSIZE-1] = '\0'; /* Ensures null termination. */
327 } else if (*src == '@') { /* Output "source", or "...source". */
328 size_t len = str->len-1;
329 src++; /* Skip the `@' */
330 if (len >= LUA_IDSIZE) {
331 src += len-(LUA_IDSIZE-4); /* Get last part of file name. */
332 *out++ = '.'; *out++ = '.'; *out++ = '.';
334 strcpy(out, src);
335 } else { /* Output [string "string"] or [builtin:name]. */
336 size_t len; /* Length, up to first control char. */
337 for (len = 0; len < LUA_IDSIZE-12; len++)
338 if (((const unsigned char *)src)[len] < ' ') break;
339 strcpy(out, line == ~(BCLine)0 ? "[builtin:" : "[string \""); out += 9;
340 if (src[len] != '\0') { /* Must truncate? */
341 if (len > LUA_IDSIZE-15) len = LUA_IDSIZE-15;
342 strncpy(out, src, len); out += len;
343 strcpy(out, "..."); out += 3;
344 } else {
345 strcpy(out, src); out += len;
347 strcpy(out, line == ~(BCLine)0 ? "]" : "\"]");
351 /* Add current location of a frame to error message. */
352 void lj_debug_addloc(lua_State *L, const char *msg,
353 cTValue *frame, cTValue *nextframe)
355 if (frame) {
356 GCfunc *fn = frame_func(frame);
357 if (isluafunc(fn)) {
358 BCLine line = debug_frameline(L, fn, nextframe);
359 if (line >= 0) {
360 GCproto *pt = funcproto(fn);
361 char buf[LUA_IDSIZE];
362 lj_debug_shortname(buf, proto_chunkname(pt), pt->firstline);
363 lj_strfmt_pushf(L, "%s:%d: %s", buf, line, msg);
364 return;
368 lj_strfmt_pushf(L, "%s", msg);
371 /* Push location string for a bytecode position to Lua stack. */
372 void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc)
374 GCstr *name = proto_chunkname(pt);
375 const char *s = strdata(name);
376 MSize i, len = name->len;
377 BCLine line = lj_debug_line(pt, pc);
378 if (pt->firstline == ~(BCLine)0) {
379 lj_strfmt_pushf(L, "builtin:%s", s);
380 } else if (*s == '@') {
381 s++; len--;
382 for (i = len; i > 0; i--)
383 if (s[i] == '/' || s[i] == '\\') {
384 s += i+1;
385 break;
387 lj_strfmt_pushf(L, "%s:%d", s, line);
388 } else if (len > 40) {
389 lj_strfmt_pushf(L, "%p:%d", pt, line);
390 } else if (*s == '=') {
391 lj_strfmt_pushf(L, "%s:%d", s+1, line);
392 } else {
393 lj_strfmt_pushf(L, "\"%s\":%d", s, line);
397 /* -- Public debug API ---------------------------------------------------- */
399 /* lua_getupvalue() and lua_setupvalue() are in lj_api.c. */
401 LUA_API const char *lua_getlocal(lua_State *L, const lua_Debug *ar, int n)
403 const char *name = NULL;
404 if (ar) {
405 TValue *o = debug_localname(L, ar, &name, (BCReg)n);
406 if (name) {
407 copyTV(L, L->top, o);
408 incr_top(L);
410 } else if (tvisfunc(L->top-1) && isluafunc(funcV(L->top-1))) {
411 name = debug_varname(funcproto(funcV(L->top-1)), 0, (BCReg)n-1);
413 return name;
416 LUA_API const char *lua_setlocal(lua_State *L, const lua_Debug *ar, int n)
418 const char *name = NULL;
419 TValue *o = debug_localname(L, ar, &name, (BCReg)n);
420 if (name)
421 copyTV(L, o, L->top-1);
422 L->top--;
423 return name;
426 int lj_debug_getinfo(lua_State *L, const char *what, lj_Debug *ar, int ext)
428 int opt_f = 0, opt_L = 0;
429 TValue *frame = NULL;
430 TValue *nextframe = NULL;
431 GCfunc *fn;
432 if (*what == '>') {
433 TValue *func = L->top - 1;
434 api_check(L, tvisfunc(func));
435 fn = funcV(func);
436 L->top--;
437 what++;
438 } else {
439 uint32_t offset = (uint32_t)ar->i_ci & 0xffff;
440 uint32_t size = (uint32_t)ar->i_ci >> 16;
441 lua_assert(offset != 0);
442 frame = tvref(L->stack) + offset;
443 if (size) nextframe = frame + size;
444 lua_assert(frame <= tvref(L->maxstack) &&
445 (!nextframe || nextframe <= tvref(L->maxstack)));
446 fn = frame_func(frame);
447 lua_assert(fn->c.gct == ~LJ_TFUNC);
449 for (; *what; what++) {
450 if (*what == 'S') {
451 if (isluafunc(fn)) {
452 GCproto *pt = funcproto(fn);
453 BCLine firstline = pt->firstline;
454 GCstr *name = proto_chunkname(pt);
455 ar->source = strdata(name);
456 lj_debug_shortname(ar->short_src, name, pt->firstline);
457 ar->linedefined = (int)firstline;
458 ar->lastlinedefined = (int)(firstline + pt->numline);
459 ar->what = (firstline || !pt->numline) ? "Lua" : "main";
460 } else {
461 ar->source = "=[C]";
462 ar->short_src[0] = '[';
463 ar->short_src[1] = 'C';
464 ar->short_src[2] = ']';
465 ar->short_src[3] = '\0';
466 ar->linedefined = -1;
467 ar->lastlinedefined = -1;
468 ar->what = "C";
470 } else if (*what == 'l') {
471 ar->currentline = frame ? debug_frameline(L, fn, nextframe) : -1;
472 } else if (*what == 'u') {
473 ar->nups = fn->c.nupvalues;
474 if (ext) {
475 if (isluafunc(fn)) {
476 GCproto *pt = funcproto(fn);
477 ar->nparams = pt->numparams;
478 ar->isvararg = !!(pt->flags & PROTO_VARARG);
479 } else {
480 ar->nparams = 0;
481 ar->isvararg = 1;
484 } else if (*what == 'n') {
485 ar->namewhat = frame ? lj_debug_funcname(L, frame, &ar->name) : NULL;
486 if (ar->namewhat == NULL) {
487 ar->namewhat = "";
488 ar->name = NULL;
490 } else if (*what == 'f') {
491 opt_f = 1;
492 } else if (*what == 'L') {
493 opt_L = 1;
494 } else {
495 return 0; /* Bad option. */
498 if (opt_f) {
499 setfuncV(L, L->top, fn);
500 incr_top(L);
502 if (opt_L) {
503 if (isluafunc(fn)) {
504 GCtab *t = lj_tab_new(L, 0, 0);
505 GCproto *pt = funcproto(fn);
506 const void *lineinfo = proto_lineinfo(pt);
507 if (lineinfo) {
508 BCLine first = pt->firstline;
509 int sz = pt->numline < 256 ? 1 : pt->numline < 65536 ? 2 : 4;
510 MSize i, szl = pt->sizebc-1;
511 for (i = 0; i < szl; i++) {
512 BCLine line = first +
513 (sz == 1 ? (BCLine)((const uint8_t *)lineinfo)[i] :
514 sz == 2 ? (BCLine)((const uint16_t *)lineinfo)[i] :
515 (BCLine)((const uint32_t *)lineinfo)[i]);
516 setboolV(lj_tab_setint(L, t, line), 1);
519 settabV(L, L->top, t);
520 } else {
521 setnilV(L->top);
523 incr_top(L);
525 return 1; /* Ok. */
528 LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar)
530 return lj_debug_getinfo(L, what, (lj_Debug *)ar, 0);
533 LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar)
535 int size;
536 cTValue *frame = lj_debug_frame(L, level, &size);
537 if (frame) {
538 ar->i_ci = (size << 16) + (int)(frame - tvref(L->stack));
539 return 1;
540 } else {
541 ar->i_ci = level - size;
542 return 0;
546 #if LJ_HASPROFILE
547 /* Put the chunkname into a buffer. */
548 static int debug_putchunkname(SBuf *sb, GCproto *pt, int pathstrip)
550 GCstr *name = proto_chunkname(pt);
551 const char *p = strdata(name);
552 if (pt->firstline == ~(BCLine)0) {
553 lj_buf_putmem(sb, "[builtin:", 9);
554 lj_buf_putstr(sb, name);
555 lj_buf_putb(sb, ']');
556 return 0;
558 if (*p == '=' || *p == '@') {
559 MSize len = name->len-1;
560 p++;
561 if (pathstrip) {
562 int i;
563 for (i = len-1; i >= 0; i--)
564 if (p[i] == '/' || p[i] == '\\') {
565 len -= i+1;
566 p = p+i+1;
567 break;
570 lj_buf_putmem(sb, p, len);
571 } else {
572 lj_buf_putmem(sb, "[string]", 8);
574 return 1;
577 /* Put a compact stack dump into a buffer. */
578 void lj_debug_dumpstack(lua_State *L, SBuf *sb, const char *fmt, int depth)
580 int level = 0, dir = 1, pathstrip = 1;
581 MSize lastlen = 0;
582 if (depth < 0) { level = ~depth; depth = dir = -1; } /* Reverse frames. */
583 while (level != depth) { /* Loop through all frame. */
584 int size;
585 cTValue *frame = lj_debug_frame(L, level, &size);
586 if (frame) {
587 cTValue *nextframe = size ? frame+size : NULL;
588 GCfunc *fn = frame_func(frame);
589 const uint8_t *p = (const uint8_t *)fmt;
590 int c;
591 while ((c = *p++)) {
592 switch (c) {
593 case 'p': /* Preserve full path. */
594 pathstrip = 0;
595 break;
596 case 'F': case 'f': { /* Dump function name. */
597 const char *name;
598 const char *what = lj_debug_funcname(L, frame, &name);
599 if (what) {
600 if (c == 'F' && isluafunc(fn)) { /* Dump module:name for 'F'. */
601 GCproto *pt = funcproto(fn);
602 if (pt->firstline != ~(BCLine)0) { /* Not a bytecode builtin. */
603 debug_putchunkname(sb, pt, pathstrip);
604 lj_buf_putb(sb, ':');
607 lj_buf_putmem(sb, name, (MSize)strlen(name));
608 break;
609 } /* else: can't derive a name, dump module:line. */
611 /* fallthrough */
612 case 'l': /* Dump module:line. */
613 if (isluafunc(fn)) {
614 GCproto *pt = funcproto(fn);
615 if (debug_putchunkname(sb, pt, pathstrip)) {
616 /* Regular Lua function. */
617 BCLine line = c == 'l' ? debug_frameline(L, fn, nextframe) :
618 pt->firstline;
619 lj_buf_putb(sb, ':');
620 lj_strfmt_putint(sb, line >= 0 ? line : pt->firstline);
622 } else if (isffunc(fn)) { /* Dump numbered builtins. */
623 lj_buf_putmem(sb, "[builtin#", 9);
624 lj_strfmt_putint(sb, fn->c.ffid);
625 lj_buf_putb(sb, ']');
626 } else { /* Dump C function address. */
627 lj_buf_putb(sb, '@');
628 lj_strfmt_putptr(sb, fn->c.f);
630 break;
631 case 'Z': /* Zap trailing separator. */
632 lastlen = sbuflen(sb);
633 break;
634 default:
635 lj_buf_putb(sb, c);
636 break;
639 } else if (dir == 1) {
640 break;
641 } else {
642 level -= size; /* Reverse frame order: quickly skip missing level. */
644 level += dir;
646 if (lastlen)
647 setsbufP(sb, sbufB(sb) + lastlen); /* Zap trailing separator. */
649 #endif
651 /* Number of frames for the leading and trailing part of a traceback. */
652 #define TRACEBACK_LEVELS1 12
653 #define TRACEBACK_LEVELS2 10
655 LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,
656 int level)
658 int top = (int)(L->top - L->base);
659 int lim = TRACEBACK_LEVELS1;
660 lua_Debug ar;
661 if (msg) lua_pushfstring(L, "%s\n", msg);
662 lua_pushliteral(L, "stack traceback:");
663 while (lua_getstack(L1, level++, &ar)) {
664 GCfunc *fn;
665 if (level > lim) {
666 if (!lua_getstack(L1, level + TRACEBACK_LEVELS2, &ar)) {
667 level--;
668 } else {
669 lua_pushliteral(L, "\n\t...");
670 lua_getstack(L1, -10, &ar);
671 level = ar.i_ci - TRACEBACK_LEVELS2;
673 lim = 2147483647;
674 continue;
676 lua_getinfo(L1, "Snlf", &ar);
677 fn = funcV(L1->top-1); L1->top--;
678 if (isffunc(fn) && !*ar.namewhat)
679 lua_pushfstring(L, "\n\t[builtin#%d]:", fn->c.ffid);
680 else
681 lua_pushfstring(L, "\n\t%s:", ar.short_src);
682 if (ar.currentline > 0)
683 lua_pushfstring(L, "%d:", ar.currentline);
684 if (*ar.namewhat) {
685 lua_pushfstring(L, " in function " LUA_QS, ar.name);
686 } else {
687 if (*ar.what == 'm') {
688 lua_pushliteral(L, " in main chunk");
689 } else if (*ar.what == 'C') {
690 lua_pushfstring(L, " at %p", fn->c.f);
691 } else {
692 lua_pushfstring(L, " in function <%s:%d>",
693 ar.short_src, ar.linedefined);
696 if ((int)(L->top - L->base) - top >= 15)
697 lua_concat(L, (int)(L->top - L->base) - top);
699 lua_concat(L, (int)(L->top - L->base) - top);