2 ** Debugging and introspection.
3 ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
17 #include "lj_strfmt.h"
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(). */
34 *size
= (int)(nextframe
- frame
);
35 return frame
; /* Level found. */
38 if (frame_islua(frame
)) {
39 frame
= frame_prevl(frame
);
41 if (frame_isvarg(frame
))
42 level
++; /* Skip vararg pseudo-frame. */
43 frame
= frame_prevd(frame
);
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
)
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. */
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
))
66 ins
= cframe_pc(cf
); /* Only happens during error/hook handling. */
68 if (frame_islua(nextframe
)) {
69 ins
= frame_pc(nextframe
);
70 } else if (frame_iscont(nextframe
)) {
71 ins
= frame_contpc(nextframe
);
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;
79 while (cframe_nres(cf
) < 0) {
80 if (f
>= restorestack(L
, -cframe_nres(cf
)))
82 cf
= cframe_raw(cframe_prev(cf
));
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
));
101 pos
= proto_bcpos(pt
, ins
) - 1;
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
));
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
];
127 return first
+ (BCLine
)((const uint32_t *)lineinfo
)[pc
];
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
);
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
);
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. */
159 do { p
++; } while (*(const uint8_t *)p
); /* Skip over variable name. */
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
);
170 if (--vn
) while (*name
++ || --vn
) ;
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) */
197 frame
= frame_prevd(frame
);
199 if (frame
+ slot1
< nextframe
) {
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)";
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
);
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
)
229 GCfunc
*fn
= funcV(o
);
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
);
237 if (idx
< fn
->c
.nupvalues
) {
238 *tvp
= &fn
->c
.upvalue
[idx
];
246 /* Deduce name of an object from slot number and PC. */
247 const char *lj_debug_slotname(GCproto
*pt
, const BCIns
*ip
, BCReg slot
,
252 lname
= debug_varname(pt
, proto_bcpos(pt
, ip
), slot
);
253 if (lname
!= NULL
) { *name
= lname
; return "local"; }
254 while (--ip
> proto_bc(pt
)) {
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
)))
261 } else if (bcmode_a(op
) == BCMdst
&& ra
== slot
) {
262 switch (bc_op(ins
)) {
264 if (ra
== slot
) { slot
= bc_d(ins
); goto restart
; }
267 *name
= strdata(gco2str(proto_kgc(pt
, ~(ptrdiff_t)bc_d(ins
))));
270 *name
= strdata(gco2str(proto_kgc(pt
, ~(ptrdiff_t)bc_c(ins
))));
271 if (ip
> proto_bc(pt
)) {
273 if (bc_op(insp
) == BC_MOV
&& bc_a(insp
) == ra
+1 &&
274 bc_d(insp
) == bc_b(ins
))
279 *name
= lj_debug_uvname(pt
, bc_d(ins
));
289 /* Deduce function name from caller of a frame. */
290 const char *lj_debug_funcname(lua_State
*L
, cTValue
*frame
, const char **name
)
295 if (frame
<= tvref(L
->stack
))
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
));
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
));
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
);
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
++ = '.';
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;
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
)
356 GCfunc
*fn
= frame_func(frame
);
358 BCLine line
= debug_frameline(L
, fn
, nextframe
);
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
);
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
== '@') {
382 for (i
= len
; i
> 0; i
--)
383 if (s
[i
] == '/' || s
[i
] == '\\') {
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
);
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
;
405 TValue
*o
= debug_localname(L
, ar
, &name
, (BCReg
)n
);
407 copyTV(L
, L
->top
, o
);
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);
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
);
421 copyTV(L
, o
, L
->top
-1);
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
;
433 TValue
*func
= L
->top
- 1;
434 api_check(L
, tvisfunc(func
));
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
++) {
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";
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;
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
;
476 GCproto
*pt
= funcproto(fn
);
477 ar
->nparams
= pt
->numparams
;
478 ar
->isvararg
= !!(pt
->flags
& PROTO_VARARG
);
484 } else if (*what
== 'n') {
485 ar
->namewhat
= frame
? lj_debug_funcname(L
, frame
, &ar
->name
) : NULL
;
486 if (ar
->namewhat
== NULL
) {
490 } else if (*what
== 'f') {
492 } else if (*what
== 'L') {
495 return 0; /* Bad option. */
499 setfuncV(L
, L
->top
, fn
);
504 GCtab
*t
= lj_tab_new(L
, 0, 0);
505 GCproto
*pt
= funcproto(fn
);
506 const void *lineinfo
= proto_lineinfo(pt
);
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
);
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
)
536 cTValue
*frame
= lj_debug_frame(L
, level
, &size
);
538 ar
->i_ci
= (size
<< 16) + (int)(frame
- tvref(L
->stack
));
541 ar
->i_ci
= level
- size
;
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
, ']');
558 if (*p
== '=' || *p
== '@') {
559 MSize len
= name
->len
-1;
563 for (i
= len
-1; i
>= 0; i
--)
564 if (p
[i
] == '/' || p
[i
] == '\\') {
570 lj_buf_putmem(sb
, p
, len
);
572 lj_buf_putmem(sb
, "[string]", 8);
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;
582 if (depth
< 0) { level
= ~depth
; depth
= dir
= -1; } /* Reverse frames. */
583 while (level
!= depth
) { /* Loop through all frame. */
585 cTValue
*frame
= lj_debug_frame(L
, level
, &size
);
587 cTValue
*nextframe
= size
? frame
+size
: NULL
;
588 GCfunc
*fn
= frame_func(frame
);
589 const uint8_t *p
= (const uint8_t *)fmt
;
593 case 'p': /* Preserve full path. */
596 case 'F': case 'f': { /* Dump function name. */
598 const char *what
= lj_debug_funcname(L
, frame
, &name
);
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
));
609 } /* else: can't derive a name, dump module:line. */
612 case 'l': /* Dump module:line. */
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
) :
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
);
631 case 'Z': /* Zap trailing separator. */
632 lastlen
= sbuflen(sb
);
639 } else if (dir
== 1) {
642 level
-= size
; /* Reverse frame order: quickly skip missing level. */
647 setsbufP(sb
, sbufB(sb
) + lastlen
); /* Zap trailing separator. */
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
,
658 int top
= (int)(L
->top
- L
->base
);
659 int lim
= TRACEBACK_LEVELS1
;
661 if (msg
) lua_pushfstring(L
, "%s\n", msg
);
662 lua_pushliteral(L
, "stack traceback:");
663 while (lua_getstack(L1
, level
++, &ar
)) {
666 if (!lua_getstack(L1
, level
+ TRACEBACK_LEVELS2
, &ar
)) {
669 lua_pushliteral(L
, "\n\t...");
670 lua_getstack(L1
, -10, &ar
);
671 level
= ar
.i_ci
- TRACEBACK_LEVELS2
;
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
);
681 lua_pushfstring(L
, "\n\t%s:", ar
.short_src
);
682 if (ar
.currentline
> 0)
683 lua_pushfstring(L
, "%d:", ar
.currentline
);
685 lua_pushfstring(L
, " in function " LUA_QS
, ar
.name
);
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
);
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
);