Coverage Report

Created: 2026-03-12 07:14

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