Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/lua/src/ldebug.c
2065 views
1
/*
2
** $Id: ldebug.c $
3
** Debug Interface
4
** See Copyright Notice in lua.h
5
*/
6
7
#define ldebug_c
8
#define LUA_CORE
9
10
#include "lprefix.h"
11
12
13
#include <stdarg.h>
14
#include <stddef.h>
15
#include <string.h>
16
17
#include "lua.h"
18
19
#include "lapi.h"
20
#include "lcode.h"
21
#include "ldebug.h"
22
#include "ldo.h"
23
#include "lfunc.h"
24
#include "lobject.h"
25
#include "lopcodes.h"
26
#include "lstate.h"
27
#include "lstring.h"
28
#include "ltable.h"
29
#include "ltm.h"
30
#include "lvm.h"
31
32
33
34
#define LuaClosure(f) ((f) != NULL && (f)->c.tt == LUA_VLCL)
35
36
37
static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
38
const char **name);
39
40
41
static int currentpc (CallInfo *ci) {
42
lua_assert(isLua(ci));
43
return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
44
}
45
46
47
/*
48
** Get a "base line" to find the line corresponding to an instruction.
49
** Base lines are regularly placed at MAXIWTHABS intervals, so usually
50
** an integer division gets the right place. When the source file has
51
** large sequences of empty/comment lines, it may need extra entries,
52
** so the original estimate needs a correction.
53
** If the original estimate is -1, the initial 'if' ensures that the
54
** 'while' will run at least once.
55
** The assertion that the estimate is a lower bound for the correct base
56
** is valid as long as the debug info has been generated with the same
57
** value for MAXIWTHABS or smaller. (Previous releases use a little
58
** smaller value.)
59
*/
60
static int getbaseline (const Proto *f, int pc, int *basepc) {
61
if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) {
62
*basepc = -1; /* start from the beginning */
63
return f->linedefined;
64
}
65
else {
66
int i = cast_uint(pc) / MAXIWTHABS - 1; /* get an estimate */
67
/* estimate must be a lower bound of the correct base */
68
lua_assert(i < 0 ||
69
(i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc));
70
while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc)
71
i++; /* low estimate; adjust it */
72
*basepc = f->abslineinfo[i].pc;
73
return f->abslineinfo[i].line;
74
}
75
}
76
77
78
/*
79
** Get the line corresponding to instruction 'pc' in function 'f';
80
** first gets a base line and from there does the increments until
81
** the desired instruction.
82
*/
83
int luaG_getfuncline (const Proto *f, int pc) {
84
if (f->lineinfo == NULL) /* no debug information? */
85
return -1;
86
else {
87
int basepc;
88
int baseline = getbaseline(f, pc, &basepc);
89
while (basepc++ < pc) { /* walk until given instruction */
90
lua_assert(f->lineinfo[basepc] != ABSLINEINFO);
91
baseline += f->lineinfo[basepc]; /* correct line */
92
}
93
return baseline;
94
}
95
}
96
97
98
static int getcurrentline (CallInfo *ci) {
99
return luaG_getfuncline(ci_func(ci)->p, currentpc(ci));
100
}
101
102
103
/*
104
** Set 'trap' for all active Lua frames.
105
** This function can be called during a signal, under "reasonable"
106
** assumptions. A new 'ci' is completely linked in the list before it
107
** becomes part of the "active" list, and we assume that pointers are
108
** atomic; see comment in next function.
109
** (A compiler doing interprocedural optimizations could, theoretically,
110
** reorder memory writes in such a way that the list could be
111
** temporarily broken while inserting a new element. We simply assume it
112
** has no good reasons to do that.)
113
*/
114
static void settraps (CallInfo *ci) {
115
for (; ci != NULL; ci = ci->previous)
116
if (isLua(ci))
117
ci->u.l.trap = 1;
118
}
119
120
121
/*
122
** This function can be called during a signal, under "reasonable"
123
** assumptions.
124
** Fields 'basehookcount' and 'hookcount' (set by 'resethookcount')
125
** are for debug only, and it is no problem if they get arbitrary
126
** values (causes at most one wrong hook call). 'hookmask' is an atomic
127
** value. We assume that pointers are atomic too (e.g., gcc ensures that
128
** for all platforms where it runs). Moreover, 'hook' is always checked
129
** before being called (see 'luaD_hook').
130
*/
131
LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
132
if (func == NULL || mask == 0) { /* turn off hooks? */
133
mask = 0;
134
func = NULL;
135
}
136
L->hook = func;
137
L->basehookcount = count;
138
resethookcount(L);
139
L->hookmask = cast_byte(mask);
140
if (mask)
141
settraps(L->ci); /* to trace inside 'luaV_execute' */
142
}
143
144
145
LUA_API lua_Hook lua_gethook (lua_State *L) {
146
return L->hook;
147
}
148
149
150
LUA_API int lua_gethookmask (lua_State *L) {
151
return L->hookmask;
152
}
153
154
155
LUA_API int lua_gethookcount (lua_State *L) {
156
return L->basehookcount;
157
}
158
159
160
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
161
int status;
162
CallInfo *ci;
163
if (level < 0) return 0; /* invalid (negative) level */
164
lua_lock(L);
165
for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
166
level--;
167
if (level == 0 && ci != &L->base_ci) { /* level found? */
168
status = 1;
169
ar->i_ci = ci;
170
}
171
else status = 0; /* no such level */
172
lua_unlock(L);
173
return status;
174
}
175
176
177
static const char *upvalname (const Proto *p, int uv) {
178
TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
179
if (s == NULL) return "?";
180
else return getstr(s);
181
}
182
183
184
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
185
if (clLvalue(s2v(ci->func.p))->p->is_vararg) {
186
int nextra = ci->u.l.nextraargs;
187
if (n >= -nextra) { /* 'n' is negative */
188
*pos = ci->func.p - nextra - (n + 1);
189
return "(vararg)"; /* generic name for any vararg */
190
}
191
}
192
return NULL; /* no such vararg */
193
}
194
195
196
const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) {
197
StkId base = ci->func.p + 1;
198
const char *name = NULL;
199
if (isLua(ci)) {
200
if (n < 0) /* access to vararg values? */
201
return findvararg(ci, n, pos);
202
else
203
name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
204
}
205
if (name == NULL) { /* no 'standard' name? */
206
StkId limit = (ci == L->ci) ? L->top.p : ci->next->func.p;
207
if (limit - base >= n && n > 0) { /* is 'n' inside 'ci' stack? */
208
/* generic name for any valid slot */
209
name = isLua(ci) ? "(temporary)" : "(C temporary)";
210
}
211
else
212
return NULL; /* no name */
213
}
214
if (pos)
215
*pos = base + (n - 1);
216
return name;
217
}
218
219
220
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
221
const char *name;
222
lua_lock(L);
223
if (ar == NULL) { /* information about non-active function? */
224
if (!isLfunction(s2v(L->top.p - 1))) /* not a Lua function? */
225
name = NULL;
226
else /* consider live variables at function start (parameters) */
227
name = luaF_getlocalname(clLvalue(s2v(L->top.p - 1))->p, n, 0);
228
}
229
else { /* active function; get information through 'ar' */
230
StkId pos = NULL; /* to avoid warnings */
231
name = luaG_findlocal(L, ar->i_ci, n, &pos);
232
if (name) {
233
setobjs2s(L, L->top.p, pos);
234
api_incr_top(L);
235
}
236
}
237
lua_unlock(L);
238
return name;
239
}
240
241
242
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
243
StkId pos = NULL; /* to avoid warnings */
244
const char *name;
245
lua_lock(L);
246
name = luaG_findlocal(L, ar->i_ci, n, &pos);
247
if (name) {
248
setobjs2s(L, pos, L->top.p - 1);
249
L->top.p--; /* pop value */
250
}
251
lua_unlock(L);
252
return name;
253
}
254
255
256
static void funcinfo (lua_Debug *ar, Closure *cl) {
257
if (!LuaClosure(cl)) {
258
ar->source = "=[C]";
259
ar->srclen = LL("=[C]");
260
ar->linedefined = -1;
261
ar->lastlinedefined = -1;
262
ar->what = "C";
263
}
264
else {
265
const Proto *p = cl->l.p;
266
if (p->source) {
267
ar->source = getstr(p->source);
268
ar->srclen = tsslen(p->source);
269
}
270
else {
271
ar->source = "=?";
272
ar->srclen = LL("=?");
273
}
274
ar->linedefined = p->linedefined;
275
ar->lastlinedefined = p->lastlinedefined;
276
ar->what = (ar->linedefined == 0) ? "main" : "Lua";
277
}
278
luaO_chunkid(ar->short_src, ar->source, ar->srclen);
279
}
280
281
282
static int nextline (const Proto *p, int currentline, int pc) {
283
if (p->lineinfo[pc] != ABSLINEINFO)
284
return currentline + p->lineinfo[pc];
285
else
286
return luaG_getfuncline(p, pc);
287
}
288
289
290
static void collectvalidlines (lua_State *L, Closure *f) {
291
if (!LuaClosure(f)) {
292
setnilvalue(s2v(L->top.p));
293
api_incr_top(L);
294
}
295
else {
296
const Proto *p = f->l.p;
297
int currentline = p->linedefined;
298
Table *t = luaH_new(L); /* new table to store active lines */
299
sethvalue2s(L, L->top.p, t); /* push it on stack */
300
api_incr_top(L);
301
if (p->lineinfo != NULL) { /* proto with debug information? */
302
int i;
303
TValue v;
304
setbtvalue(&v); /* boolean 'true' to be the value of all indices */
305
if (!p->is_vararg) /* regular function? */
306
i = 0; /* consider all instructions */
307
else { /* vararg function */
308
lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP);
309
currentline = nextline(p, currentline, 0);
310
i = 1; /* skip first instruction (OP_VARARGPREP) */
311
}
312
for (; i < p->sizelineinfo; i++) { /* for each instruction */
313
currentline = nextline(p, currentline, i); /* get its line */
314
luaH_setint(L, t, currentline, &v); /* table[line] = true */
315
}
316
}
317
}
318
}
319
320
321
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
322
/* calling function is a known function? */
323
if (ci != NULL && !(ci->callstatus & CIST_TAIL))
324
return funcnamefromcall(L, ci->previous, name);
325
else return NULL; /* no way to find a name */
326
}
327
328
329
static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
330
Closure *f, CallInfo *ci) {
331
int status = 1;
332
for (; *what; what++) {
333
switch (*what) {
334
case 'S': {
335
funcinfo(ar, f);
336
break;
337
}
338
case 'l': {
339
ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1;
340
break;
341
}
342
case 'u': {
343
ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
344
if (!LuaClosure(f)) {
345
ar->isvararg = 1;
346
ar->nparams = 0;
347
}
348
else {
349
ar->isvararg = f->l.p->is_vararg;
350
ar->nparams = f->l.p->numparams;
351
}
352
break;
353
}
354
case 't': {
355
ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
356
break;
357
}
358
case 'n': {
359
ar->namewhat = getfuncname(L, ci, &ar->name);
360
if (ar->namewhat == NULL) {
361
ar->namewhat = ""; /* not found */
362
ar->name = NULL;
363
}
364
break;
365
}
366
case 'r': {
367
if (ci == NULL || !(ci->callstatus & CIST_TRAN))
368
ar->ftransfer = ar->ntransfer = 0;
369
else {
370
ar->ftransfer = ci->u2.transferinfo.ftransfer;
371
ar->ntransfer = ci->u2.transferinfo.ntransfer;
372
}
373
break;
374
}
375
case 'L':
376
case 'f': /* handled by lua_getinfo */
377
break;
378
default: status = 0; /* invalid option */
379
}
380
}
381
return status;
382
}
383
384
385
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
386
int status;
387
Closure *cl;
388
CallInfo *ci;
389
TValue *func;
390
lua_lock(L);
391
if (*what == '>') {
392
ci = NULL;
393
func = s2v(L->top.p - 1);
394
api_check(L, ttisfunction(func), "function expected");
395
what++; /* skip the '>' */
396
L->top.p--; /* pop function */
397
}
398
else {
399
ci = ar->i_ci;
400
func = s2v(ci->func.p);
401
lua_assert(ttisfunction(func));
402
}
403
cl = ttisclosure(func) ? clvalue(func) : NULL;
404
status = auxgetinfo(L, what, ar, cl, ci);
405
if (strchr(what, 'f')) {
406
setobj2s(L, L->top.p, func);
407
api_incr_top(L);
408
}
409
if (strchr(what, 'L'))
410
collectvalidlines(L, cl);
411
lua_unlock(L);
412
return status;
413
}
414
415
416
/*
417
** {======================================================
418
** Symbolic Execution
419
** =======================================================
420
*/
421
422
423
static int filterpc (int pc, int jmptarget) {
424
if (pc < jmptarget) /* is code conditional (inside a jump)? */
425
return -1; /* cannot know who sets that register */
426
else return pc; /* current position sets that register */
427
}
428
429
430
/*
431
** Try to find last instruction before 'lastpc' that modified register 'reg'.
432
*/
433
static int findsetreg (const Proto *p, int lastpc, int reg) {
434
int pc;
435
int setreg = -1; /* keep last instruction that changed 'reg' */
436
int jmptarget = 0; /* any code before this address is conditional */
437
if (testMMMode(GET_OPCODE(p->code[lastpc])))
438
lastpc--; /* previous instruction was not actually executed */
439
for (pc = 0; pc < lastpc; pc++) {
440
Instruction i = p->code[pc];
441
OpCode op = GET_OPCODE(i);
442
int a = GETARG_A(i);
443
int change; /* true if current instruction changed 'reg' */
444
switch (op) {
445
case OP_LOADNIL: { /* set registers from 'a' to 'a+b' */
446
int b = GETARG_B(i);
447
change = (a <= reg && reg <= a + b);
448
break;
449
}
450
case OP_TFORCALL: { /* affect all regs above its base */
451
change = (reg >= a + 2);
452
break;
453
}
454
case OP_CALL:
455
case OP_TAILCALL: { /* affect all registers above base */
456
change = (reg >= a);
457
break;
458
}
459
case OP_JMP: { /* doesn't change registers, but changes 'jmptarget' */
460
int b = GETARG_sJ(i);
461
int dest = pc + 1 + b;
462
/* jump does not skip 'lastpc' and is larger than current one? */
463
if (dest <= lastpc && dest > jmptarget)
464
jmptarget = dest; /* update 'jmptarget' */
465
change = 0;
466
break;
467
}
468
default: /* any instruction that sets A */
469
change = (testAMode(op) && reg == a);
470
break;
471
}
472
if (change)
473
setreg = filterpc(pc, jmptarget);
474
}
475
return setreg;
476
}
477
478
479
/*
480
** Find a "name" for the constant 'c'.
481
*/
482
static const char *kname (const Proto *p, int index, const char **name) {
483
TValue *kvalue = &p->k[index];
484
if (ttisstring(kvalue)) {
485
*name = getstr(tsvalue(kvalue));
486
return "constant";
487
}
488
else {
489
*name = "?";
490
return NULL;
491
}
492
}
493
494
495
static const char *basicgetobjname (const Proto *p, int *ppc, int reg,
496
const char **name) {
497
int pc = *ppc;
498
*name = luaF_getlocalname(p, reg + 1, pc);
499
if (*name) /* is a local? */
500
return "local";
501
/* else try symbolic execution */
502
*ppc = pc = findsetreg(p, pc, reg);
503
if (pc != -1) { /* could find instruction? */
504
Instruction i = p->code[pc];
505
OpCode op = GET_OPCODE(i);
506
switch (op) {
507
case OP_MOVE: {
508
int b = GETARG_B(i); /* move from 'b' to 'a' */
509
if (b < GETARG_A(i))
510
return basicgetobjname(p, ppc, b, name); /* get name for 'b' */
511
break;
512
}
513
case OP_GETUPVAL: {
514
*name = upvalname(p, GETARG_B(i));
515
return "upvalue";
516
}
517
case OP_LOADK: return kname(p, GETARG_Bx(i), name);
518
case OP_LOADKX: return kname(p, GETARG_Ax(p->code[pc + 1]), name);
519
default: break;
520
}
521
}
522
return NULL; /* could not find reasonable name */
523
}
524
525
526
/*
527
** Find a "name" for the register 'c'.
528
*/
529
static void rname (const Proto *p, int pc, int c, const char **name) {
530
const char *what = basicgetobjname(p, &pc, c, name); /* search for 'c' */
531
if (!(what && *what == 'c')) /* did not find a constant name? */
532
*name = "?";
533
}
534
535
536
/*
537
** Find a "name" for a 'C' value in an RK instruction.
538
*/
539
static void rkname (const Proto *p, int pc, Instruction i, const char **name) {
540
int c = GETARG_C(i); /* key index */
541
if (GETARG_k(i)) /* is 'c' a constant? */
542
kname(p, c, name);
543
else /* 'c' is a register */
544
rname(p, pc, c, name);
545
}
546
547
548
/*
549
** Check whether table being indexed by instruction 'i' is the
550
** environment '_ENV'
551
*/
552
static const char *isEnv (const Proto *p, int pc, Instruction i, int isup) {
553
int t = GETARG_B(i); /* table index */
554
const char *name; /* name of indexed variable */
555
if (isup) /* is 't' an upvalue? */
556
name = upvalname(p, t);
557
else /* 't' is a register */
558
basicgetobjname(p, &pc, t, &name);
559
return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field";
560
}
561
562
563
/*
564
** Extend 'basicgetobjname' to handle table accesses
565
*/
566
static const char *getobjname (const Proto *p, int lastpc, int reg,
567
const char **name) {
568
const char *kind = basicgetobjname(p, &lastpc, reg, name);
569
if (kind != NULL)
570
return kind;
571
else if (lastpc != -1) { /* could find instruction? */
572
Instruction i = p->code[lastpc];
573
OpCode op = GET_OPCODE(i);
574
switch (op) {
575
case OP_GETTABUP: {
576
int k = GETARG_C(i); /* key index */
577
kname(p, k, name);
578
return isEnv(p, lastpc, i, 1);
579
}
580
case OP_GETTABLE: {
581
int k = GETARG_C(i); /* key index */
582
rname(p, lastpc, k, name);
583
return isEnv(p, lastpc, i, 0);
584
}
585
case OP_GETI: {
586
*name = "integer index";
587
return "field";
588
}
589
case OP_GETFIELD: {
590
int k = GETARG_C(i); /* key index */
591
kname(p, k, name);
592
return isEnv(p, lastpc, i, 0);
593
}
594
case OP_SELF: {
595
rkname(p, lastpc, i, name);
596
return "method";
597
}
598
default: break; /* go through to return NULL */
599
}
600
}
601
return NULL; /* could not find reasonable name */
602
}
603
604
605
/*
606
** Try to find a name for a function based on the code that called it.
607
** (Only works when function was called by a Lua function.)
608
** Returns what the name is (e.g., "for iterator", "method",
609
** "metamethod") and sets '*name' to point to the name.
610
*/
611
static const char *funcnamefromcode (lua_State *L, const Proto *p,
612
int pc, const char **name) {
613
TMS tm = (TMS)0; /* (initial value avoids warnings) */
614
Instruction i = p->code[pc]; /* calling instruction */
615
switch (GET_OPCODE(i)) {
616
case OP_CALL:
617
case OP_TAILCALL:
618
return getobjname(p, pc, GETARG_A(i), name); /* get function name */
619
case OP_TFORCALL: { /* for iterator */
620
*name = "for iterator";
621
return "for iterator";
622
}
623
/* other instructions can do calls through metamethods */
624
case OP_SELF: case OP_GETTABUP: case OP_GETTABLE:
625
case OP_GETI: case OP_GETFIELD:
626
tm = TM_INDEX;
627
break;
628
case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD:
629
tm = TM_NEWINDEX;
630
break;
631
case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
632
tm = cast(TMS, GETARG_C(i));
633
break;
634
}
635
case OP_UNM: tm = TM_UNM; break;
636
case OP_BNOT: tm = TM_BNOT; break;
637
case OP_LEN: tm = TM_LEN; break;
638
case OP_CONCAT: tm = TM_CONCAT; break;
639
case OP_EQ: tm = TM_EQ; break;
640
/* no cases for OP_EQI and OP_EQK, as they don't call metamethods */
641
case OP_LT: case OP_LTI: case OP_GTI: tm = TM_LT; break;
642
case OP_LE: case OP_LEI: case OP_GEI: tm = TM_LE; break;
643
case OP_CLOSE: case OP_RETURN: tm = TM_CLOSE; break;
644
default:
645
return NULL; /* cannot find a reasonable name */
646
}
647
*name = getshrstr(G(L)->tmname[tm]) + 2;
648
return "metamethod";
649
}
650
651
652
/*
653
** Try to find a name for a function based on how it was called.
654
*/
655
static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
656
const char **name) {
657
if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */
658
*name = "?";
659
return "hook";
660
}
661
else if (ci->callstatus & CIST_FIN) { /* was it called as a finalizer? */
662
*name = "__gc";
663
return "metamethod"; /* report it as such */
664
}
665
else if (isLua(ci))
666
return funcnamefromcode(L, ci_func(ci)->p, currentpc(ci), name);
667
else
668
return NULL;
669
}
670
671
/* }====================================================== */
672
673
674
675
/*
676
** Check whether pointer 'o' points to some value in the stack frame of
677
** the current function and, if so, returns its index. Because 'o' may
678
** not point to a value in this stack, we cannot compare it with the
679
** region boundaries (undefined behavior in ISO C).
680
*/
681
static int instack (CallInfo *ci, const TValue *o) {
682
int pos;
683
StkId base = ci->func.p + 1;
684
for (pos = 0; base + pos < ci->top.p; pos++) {
685
if (o == s2v(base + pos))
686
return pos;
687
}
688
return -1; /* not found */
689
}
690
691
692
/*
693
** Checks whether value 'o' came from an upvalue. (That can only happen
694
** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on
695
** upvalues.)
696
*/
697
static const char *getupvalname (CallInfo *ci, const TValue *o,
698
const char **name) {
699
LClosure *c = ci_func(ci);
700
int i;
701
for (i = 0; i < c->nupvalues; i++) {
702
if (c->upvals[i]->v.p == o) {
703
*name = upvalname(c->p, i);
704
return "upvalue";
705
}
706
}
707
return NULL;
708
}
709
710
711
static const char *formatvarinfo (lua_State *L, const char *kind,
712
const char *name) {
713
if (kind == NULL)
714
return ""; /* no information */
715
else
716
return luaO_pushfstring(L, " (%s '%s')", kind, name);
717
}
718
719
/*
720
** Build a string with a "description" for the value 'o', such as
721
** "variable 'x'" or "upvalue 'y'".
722
*/
723
static const char *varinfo (lua_State *L, const TValue *o) {
724
CallInfo *ci = L->ci;
725
const char *name = NULL; /* to avoid warnings */
726
const char *kind = NULL;
727
if (isLua(ci)) {
728
kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
729
if (!kind) { /* not an upvalue? */
730
int reg = instack(ci, o); /* try a register */
731
if (reg >= 0) /* is 'o' a register? */
732
kind = getobjname(ci_func(ci)->p, currentpc(ci), reg, &name);
733
}
734
}
735
return formatvarinfo(L, kind, name);
736
}
737
738
739
/*
740
** Raise a type error
741
*/
742
static l_noret typeerror (lua_State *L, const TValue *o, const char *op,
743
const char *extra) {
744
const char *t = luaT_objtypename(L, o);
745
luaG_runerror(L, "attempt to %s a %s value%s", op, t, extra);
746
}
747
748
749
/*
750
** Raise a type error with "standard" information about the faulty
751
** object 'o' (using 'varinfo').
752
*/
753
l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
754
typeerror(L, o, op, varinfo(L, o));
755
}
756
757
758
/*
759
** Raise an error for calling a non-callable object. Try to find a name
760
** for the object based on how it was called ('funcnamefromcall'); if it
761
** cannot get a name there, try 'varinfo'.
762
*/
763
l_noret luaG_callerror (lua_State *L, const TValue *o) {
764
CallInfo *ci = L->ci;
765
const char *name = NULL; /* to avoid warnings */
766
const char *kind = funcnamefromcall(L, ci, &name);
767
const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o);
768
typeerror(L, o, "call", extra);
769
}
770
771
772
l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what) {
773
luaG_runerror(L, "bad 'for' %s (number expected, got %s)",
774
what, luaT_objtypename(L, o));
775
}
776
777
778
l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
779
if (ttisstring(p1) || cvt2str(p1)) p1 = p2;
780
luaG_typeerror(L, p1, "concatenate");
781
}
782
783
784
l_noret luaG_opinterror (lua_State *L, const TValue *p1,
785
const TValue *p2, const char *msg) {
786
if (!ttisnumber(p1)) /* first operand is wrong? */
787
p2 = p1; /* now second is wrong */
788
luaG_typeerror(L, p2, msg);
789
}
790
791
792
/*
793
** Error when both values are convertible to numbers, but not to integers
794
*/
795
l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
796
lua_Integer temp;
797
if (!luaV_tointegerns(p1, &temp, LUA_FLOORN2I))
798
p2 = p1;
799
luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
800
}
801
802
803
l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
804
const char *t1 = luaT_objtypename(L, p1);
805
const char *t2 = luaT_objtypename(L, p2);
806
if (strcmp(t1, t2) == 0)
807
luaG_runerror(L, "attempt to compare two %s values", t1);
808
else
809
luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
810
}
811
812
813
/* add src:line information to 'msg' */
814
const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
815
int line) {
816
char buff[LUA_IDSIZE];
817
if (src)
818
luaO_chunkid(buff, getstr(src), tsslen(src));
819
else { /* no source available; use "?" instead */
820
buff[0] = '?'; buff[1] = '\0';
821
}
822
return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
823
}
824
825
826
l_noret luaG_errormsg (lua_State *L) {
827
if (L->errfunc != 0) { /* is there an error handling function? */
828
StkId errfunc = restorestack(L, L->errfunc);
829
lua_assert(ttisfunction(s2v(errfunc)));
830
setobjs2s(L, L->top.p, L->top.p - 1); /* move argument */
831
setobjs2s(L, L->top.p - 1, errfunc); /* push function */
832
L->top.p++; /* assume EXTRA_STACK */
833
luaD_callnoyield(L, L->top.p - 2, 1); /* call it */
834
}
835
luaD_throw(L, LUA_ERRRUN);
836
}
837
838
839
l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
840
CallInfo *ci = L->ci;
841
const char *msg;
842
va_list argp;
843
luaC_checkGC(L); /* error message uses memory */
844
va_start(argp, fmt);
845
msg = luaO_pushvfstring(L, fmt, argp); /* format message */
846
va_end(argp);
847
if (isLua(ci)) { /* if Lua function, add source:line information */
848
luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci));
849
setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */
850
L->top.p--;
851
}
852
luaG_errormsg(L);
853
}
854
855
856
/*
857
** Check whether new instruction 'newpc' is in a different line from
858
** previous instruction 'oldpc'. More often than not, 'newpc' is only
859
** one or a few instructions after 'oldpc' (it must be after, see
860
** caller), so try to avoid calling 'luaG_getfuncline'. If they are
861
** too far apart, there is a good chance of a ABSLINEINFO in the way,
862
** so it goes directly to 'luaG_getfuncline'.
863
*/
864
static int changedline (const Proto *p, int oldpc, int newpc) {
865
if (p->lineinfo == NULL) /* no debug information? */
866
return 0;
867
if (newpc - oldpc < MAXIWTHABS / 2) { /* not too far apart? */
868
int delta = 0; /* line difference */
869
int pc = oldpc;
870
for (;;) {
871
int lineinfo = p->lineinfo[++pc];
872
if (lineinfo == ABSLINEINFO)
873
break; /* cannot compute delta; fall through */
874
delta += lineinfo;
875
if (pc == newpc)
876
return (delta != 0); /* delta computed successfully */
877
}
878
}
879
/* either instructions are too far apart or there is an absolute line
880
info in the way; compute line difference explicitly */
881
return (luaG_getfuncline(p, oldpc) != luaG_getfuncline(p, newpc));
882
}
883
884
885
/*
886
** Traces Lua calls. If code is running the first instruction of a function,
887
** and function is not vararg, and it is not coming from an yield,
888
** calls 'luaD_hookcall'. (Vararg functions will call 'luaD_hookcall'
889
** after adjusting its variable arguments; otherwise, they could call
890
** a line/count hook before the call hook. Functions coming from
891
** an yield already called 'luaD_hookcall' before yielding.)
892
*/
893
int luaG_tracecall (lua_State *L) {
894
CallInfo *ci = L->ci;
895
Proto *p = ci_func(ci)->p;
896
ci->u.l.trap = 1; /* ensure hooks will be checked */
897
if (ci->u.l.savedpc == p->code) { /* first instruction (not resuming)? */
898
if (p->is_vararg)
899
return 0; /* hooks will start at VARARGPREP instruction */
900
else if (!(ci->callstatus & CIST_HOOKYIELD)) /* not yieded? */
901
luaD_hookcall(L, ci); /* check 'call' hook */
902
}
903
return 1; /* keep 'trap' on */
904
}
905
906
907
/*
908
** Traces the execution of a Lua function. Called before the execution
909
** of each opcode, when debug is on. 'L->oldpc' stores the last
910
** instruction traced, to detect line changes. When entering a new
911
** function, 'npci' will be zero and will test as a new line whatever
912
** the value of 'oldpc'. Some exceptional conditions may return to
913
** a function without setting 'oldpc'. In that case, 'oldpc' may be
914
** invalid; if so, use zero as a valid value. (A wrong but valid 'oldpc'
915
** at most causes an extra call to a line hook.)
916
** This function is not "Protected" when called, so it should correct
917
** 'L->top.p' before calling anything that can run the GC.
918
*/
919
int luaG_traceexec (lua_State *L, const Instruction *pc) {
920
CallInfo *ci = L->ci;
921
lu_byte mask = L->hookmask;
922
const Proto *p = ci_func(ci)->p;
923
int counthook;
924
if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */
925
ci->u.l.trap = 0; /* don't need to stop again */
926
return 0; /* turn off 'trap' */
927
}
928
pc++; /* reference is always next instruction */
929
ci->u.l.savedpc = pc; /* save 'pc' */
930
counthook = (mask & LUA_MASKCOUNT) && (--L->hookcount == 0);
931
if (counthook)
932
resethookcount(L); /* reset count */
933
else if (!(mask & LUA_MASKLINE))
934
return 1; /* no line hook and count != 0; nothing to be done now */
935
if (ci->callstatus & CIST_HOOKYIELD) { /* hook yielded last time? */
936
ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
937
return 1; /* do not call hook again (VM yielded, so it did not move) */
938
}
939
if (!isIT(*(ci->u.l.savedpc - 1))) /* top not being used? */
940
L->top.p = ci->top.p; /* correct top */
941
if (counthook)
942
luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */
943
if (mask & LUA_MASKLINE) {
944
/* 'L->oldpc' may be invalid; use zero in this case */
945
int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0;
946
int npci = pcRel(pc, p);
947
if (npci <= oldpc || /* call hook when jump back (loop), */
948
changedline(p, oldpc, npci)) { /* or when enter new line */
949
int newline = luaG_getfuncline(p, npci);
950
luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */
951
}
952
L->oldpc = npci; /* 'pc' of last call to line hook */
953
}
954
if (L->status == LUA_YIELD) { /* did hook yield? */
955
if (counthook)
956
L->hookcount = 1; /* undo decrement to zero */
957
ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */
958
luaD_throw(L, LUA_YIELD);
959
}
960
return 1; /* keep 'trap' on */
961
}
962
963
964