Coverage Report

Created: 2025-08-25 07:03

/src/testdir/build/lua-master/source/ldebug.c
Line
Count
Source (jump to first uncovered line)
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
14.0M
#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
11.0M
static int currentpc (CallInfo *ci) {
44
11.0M
  lua_assert(isLua(ci));
45
11.0M
  return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
46
11.0M
}
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
25.0M
static int getbaseline (const Proto *f, int pc, int *basepc) {
63
25.0M
  if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) {
64
21.7M
    *basepc = -1;  /* start from the beginning */
65
21.7M
    return f->linedefined;
66
21.7M
  }
67
3.30M
  else {
68
3.30M
    int i = pc / MAXIWTHABS - 1;  /* get an estimate */
69
    /* estimate must be a lower bound of the correct base */
70
3.30M
    lua_assert(i < 0 ||
71
3.30M
              (i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc));
72
3.42M
    while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc)
73
118k
      i++;  /* low estimate; adjust it */
74
3.30M
    *basepc = f->abslineinfo[i].pc;
75
3.30M
    return f->abslineinfo[i].line;
76
3.30M
  }
77
25.0M
}
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
25.0M
int luaG_getfuncline (const Proto *f, int pc) {
86
25.0M
  if (f->lineinfo == NULL)  /* no debug information? */
87
7
    return -1;
88
25.0M
  else {
89
25.0M
    int basepc;
90
25.0M
    int baseline = getbaseline(f, pc, &basepc);
91
616M
    while (basepc++ < pc) {  /* walk until given instruction */
92
591M
      lua_assert(f->lineinfo[basepc] != ABSLINEINFO);
93
591M
      baseline += f->lineinfo[basepc];  /* correct line */
94
591M
    }
95
25.0M
    return baseline;
96
25.0M
  }
97
25.0M
}
98
99
100
8.10M
static int getcurrentline (CallInfo *ci) {
101
8.10M
  return luaG_getfuncline(ci_func(ci)->p, currentpc(ci));
102
8.10M
}
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
550k
static void settraps (CallInfo *ci) {
117
40.7M
  for (; ci != NULL; ci = ci->previous)
118
40.2M
    if (isLua(ci))
119
27.2M
      ci->u.l.trap = 1;
120
550k
}
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
580k
LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
134
580k
  if (func == NULL || mask == 0) {  /* turn off hooks? */
135
30.4k
    mask = 0;
136
30.4k
    func = NULL;
137
30.4k
  }
138
580k
  L->hook = func;
139
580k
  L->basehookcount = count;
140
580k
  resethookcount(L);
141
580k
  L->hookmask = cast_byte(mask);
142
580k
  if (mask)
143
550k
    settraps(L->ci);  /* to trace inside 'luaV_execute' */
144
580k
}
145
146
147
38.8k
LUA_API lua_Hook lua_gethook (lua_State *L) {
148
38.8k
  return L->hook;
149
38.8k
}
150
151
152
38.8k
LUA_API int lua_gethookmask (lua_State *L) {
153
38.8k
  return L->hookmask;
154
38.8k
}
155
156
157
38.5k
LUA_API int lua_gethookcount (lua_State *L) {
158
38.5k
  return L->basehookcount;
159
38.5k
}
160
161
162
9.19M
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
163
9.19M
  int status;
164
9.19M
  CallInfo *ci;
165
9.19M
  if (level < 0) return 0;  /* invalid (negative) level */
166
9.19M
  lua_lock(L);
167
229M
  for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
168
220M
    level--;
169
9.19M
  if (level == 0 && ci != &L->base_ci) {  /* level found? */
170
8.23M
    status = 1;
171
8.23M
    ar->i_ci = ci;
172
8.23M
  }
173
961k
  else status = 0;  /* no such level */
174
9.19M
  lua_unlock(L);
175
9.19M
  return status;
176
9.19M
}
177
178
179
1.66M
static const char *upvalname (const Proto *p, int uv) {
180
1.66M
  TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
181
1.66M
  if (s == NULL) return "?";
182
1.66M
  else return getstr(s);
183
1.66M
}
184
185
186
1.43k
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
187
4.29k
  if (clLvalue(s2v(ci->func.p))->p->flag & PF_ISVARARG) {
188
31
    int nextra = ci->u.l.nextraargs;
189
31
    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
31
  }
194
1.43k
  return NULL;  /* no such vararg */
195
1.43k
}
196
197
198
5.27k
const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) {
199
5.27k
  StkId base = ci->func.p + 1;
200
5.27k
  const char *name = NULL;
201
5.27k
  if (isLua(ci)) {
202
3.14k
    if (n < 0)  /* access to vararg values? */
203
1.43k
      return findvararg(ci, n, pos);
204
1.71k
    else
205
3.43k
      name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
206
3.14k
  }
207
3.84k
  if (name == NULL) {  /* no 'standard' name? */
208
3.11k
    StkId limit = (ci == L->ci) ? L->top.p : ci->next->func.p;
209
3.11k
    if (limit - base >= n && n > 0) {  /* is 'n' inside 'ci' stack? */
210
      /* generic name for any valid slot */
211
1.21k
      name = isLua(ci) ? "(temporary)" : "(C temporary)";
212
1.21k
    }
213
1.90k
    else
214
1.90k
      return NULL;  /* no name */
215
3.11k
  }
216
1.94k
  if (pos)
217
1.32k
    *pos = base + (n - 1);
218
1.94k
  return name;
219
3.84k
}
220
221
222
508
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
223
508
  const char *name;
224
508
  lua_lock(L);
225
508
  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
508
  else {  /* active function; get information through 'ar' */
232
508
    StkId pos = NULL;  /* to avoid warnings */
233
508
    name = luaG_findlocal(L, ar->i_ci, n, &pos);
234
508
    if (name) {
235
397
      setobjs2s(L, L->top.p, pos);
236
397
      api_incr_top(L);
237
397
    }
238
508
  }
239
508
  lua_unlock(L);
240
508
  return name;
241
508
}
242
243
244
4.14k
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
245
4.14k
  StkId pos = NULL;  /* to avoid warnings */
246
4.14k
  const char *name;
247
4.14k
  lua_lock(L);
248
4.14k
  name = luaG_findlocal(L, ar->i_ci, n, &pos);
249
4.14k
  if (name) {
250
928
    api_checkpop(L, 1);
251
1.85k
    setobjs2s(L, pos, L->top.p - 1);
252
928
    L->top.p--;  /* pop value */
253
928
  }
254
4.14k
  lua_unlock(L);
255
4.14k
  return name;
256
4.14k
}
257
258
259
10.5M
static void funcinfo (lua_Debug *ar, Closure *cl) {
260
10.5M
  if (!LuaClosure(cl)) {
261
3.28M
    ar->source = "=[C]";
262
3.28M
    ar->srclen = LL("=[C]");
263
3.28M
    ar->linedefined = -1;
264
3.28M
    ar->lastlinedefined = -1;
265
3.28M
    ar->what = "C";
266
3.28M
  }
267
7.30M
  else {
268
7.30M
    const Proto *p = cl->l.p;
269
7.30M
    if (p->source) {
270
7.30M
      ar->source = getlstr(p->source, ar->srclen);
271
7.30M
    }
272
0
    else {
273
0
      ar->source = "=?";
274
0
      ar->srclen = LL("=?");
275
0
    }
276
7.30M
    ar->linedefined = p->linedefined;
277
7.30M
    ar->lastlinedefined = p->lastlinedefined;
278
7.30M
    ar->what = (ar->linedefined == 0) ? "main" : "Lua";
279
7.30M
  }
280
10.5M
  luaO_chunkid(ar->short_src, ar->source, ar->srclen);
281
10.5M
}
282
283
284
7.00M
static int nextline (const Proto *p, int currentline, int pc) {
285
7.00M
  if (p->lineinfo[pc] != ABSLINEINFO)
286
7.00M
    return currentline + p->lineinfo[pc];
287
6.12k
  else
288
6.12k
    return luaG_getfuncline(p, pc);
289
7.00M
}
290
291
292
141k
static void collectvalidlines (lua_State *L, Closure *f) {
293
141k
  if (!LuaClosure(f)) {
294
4.85k
    setnilvalue(s2v(L->top.p));
295
4.85k
    api_incr_top(L);
296
4.85k
  }
297
136k
  else {
298
136k
    const Proto *p = f->l.p;
299
136k
    int currentline = p->linedefined;
300
136k
    Table *t = luaH_new(L);  /* new table to store active lines */
301
136k
    sethvalue2s(L, L->top.p, t);  /* push it on stack */
302
136k
    api_incr_top(L);
303
136k
    if (p->lineinfo != NULL) {  /* proto with debug information? */
304
136k
      int i;
305
136k
      TValue v;
306
136k
      setbtvalue(&v);  /* boolean 'true' to be the value of all indices */
307
136k
      if (!(p->flag & PF_ISVARARG))  /* regular function? */
308
86.0k
        i = 0;  /* consider all instructions */
309
50.3k
      else {  /* vararg function */
310
50.3k
        lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP);
311
50.3k
        currentline = nextline(p, currentline, 0);
312
50.3k
        i = 1;  /* skip first instruction (OP_VARARGPREP) */
313
50.3k
      }
314
7.09M
      for (; i < p->sizelineinfo; i++) {  /* for each instruction */
315
6.95M
        currentline = nextline(p, currentline, i);  /* get its line */
316
6.95M
        luaH_setint(L, t, currentline, &v);  /* table[line] = true */
317
6.95M
      }
318
136k
    }
319
136k
  }
320
141k
}
321
322
323
5.67M
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
324
  /* calling function is a known function? */
325
5.67M
  if (ci != NULL && !(ci->callstatus & CIST_TAIL))
326
5.56M
    return funcnamefromcall(L, ci->previous, name);
327
106k
  else return NULL;  /* no way to find a name */
328
5.67M
}
329
330
331
static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
332
14.0M
                       Closure *f, CallInfo *ci) {
333
14.0M
  int status = 1;
334
58.2M
  for (; *what; what++) {
335
44.2M
    switch (*what) {
336
10.5M
      case 'S': {
337
10.5M
        funcinfo(ar, f);
338
10.5M
        break;
339
0
      }
340
10.5M
      case 'l': {
341
10.5M
        ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1;
342
10.5M
        break;
343
0
      }
344
3.28M
      case 'u': {
345
3.28M
        ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
346
3.28M
        if (!LuaClosure(f)) {
347
667k
          ar->isvararg = 1;
348
667k
          ar->nparams = 0;
349
667k
        }
350
2.61M
        else {
351
2.61M
          ar->isvararg = (f->l.p->flag & PF_ISVARARG) ? 1 : 0;
352
2.61M
          ar->nparams = f->l.p->numparams;
353
2.61M
        }
354
3.28M
        break;
355
0
      }
356
5.67M
      case 't': {
357
5.67M
        if (ci != NULL) {
358
5.65M
          ar->istailcall = !!(ci->callstatus & CIST_TAIL);
359
5.65M
          ar->extraargs =
360
5.65M
                   cast_uchar((ci->callstatus & MAX_CCMT) >> CIST_CCMT);
361
5.65M
        }
362
20.4k
        else {
363
20.4k
          ar->istailcall = 0;
364
20.4k
          ar->extraargs = 0;
365
20.4k
        }
366
5.67M
        break;
367
0
      }
368
5.67M
      case 'n': {
369
5.67M
        ar->namewhat = getfuncname(L, ci, &ar->name);
370
5.67M
        if (ar->namewhat == NULL) {
371
3.29M
          ar->namewhat = "";  /* not found */
372
3.29M
          ar->name = NULL;
373
3.29M
        }
374
5.67M
        break;
375
0
      }
376
3.28M
      case 'r': {
377
3.28M
        if (ci == NULL || !(ci->callstatus & CIST_HOOKED))
378
927k
          ar->ftransfer = ar->ntransfer = 0;
379
2.35M
        else {
380
2.35M
          ar->ftransfer = L->transferinfo.ftransfer;
381
2.35M
          ar->ntransfer = L->transferinfo.ntransfer;
382
2.35M
        }
383
3.28M
        break;
384
0
      }
385
141k
      case 'L':
386
5.10M
      case 'f':  /* handled by lua_getinfo */
387
5.10M
        break;
388
9.92k
      default: status = 0;  /* invalid option */
389
44.2M
    }
390
44.2M
  }
391
14.0M
  return status;
392
14.0M
}
393
394
395
14.0M
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
396
14.0M
  int status;
397
14.0M
  Closure *cl;
398
14.0M
  CallInfo *ci;
399
14.0M
  TValue *func;
400
14.0M
  lua_lock(L);
401
14.0M
  if (*what == '>') {
402
86.2k
    ci = NULL;
403
86.2k
    func = s2v(L->top.p - 1);
404
86.2k
    api_check(L, ttisfunction(func), "function expected");
405
86.2k
    what++;  /* skip the '>' */
406
86.2k
    L->top.p--;  /* pop function */
407
86.2k
  }
408
13.9M
  else {
409
13.9M
    ci = ar->i_ci;
410
13.9M
    func = s2v(ci->func.p);
411
13.9M
    lua_assert(ttisfunction(func));
412
13.9M
  }
413
14.0M
  cl = ttisclosure(func) ? clvalue(func) : NULL;
414
14.0M
  status = auxgetinfo(L, what, ar, cl, ci);
415
14.0M
  if (strchr(what, 'f')) {
416
4.96M
    setobj2s(L, L->top.p, func);
417
4.96M
    api_incr_top(L);
418
4.96M
  }
419
14.0M
  if (strchr(what, 'L'))
420
141k
    collectvalidlines(L, cl);
421
14.0M
  lua_unlock(L);
422
14.0M
  return status;
423
14.0M
}
424
425
426
/*
427
** {======================================================
428
** Symbolic Execution
429
** =======================================================
430
*/
431
432
433
117M
static int filterpc (int pc, int jmptarget) {
434
117M
  if (pc < jmptarget)  /* is code conditional (inside a jump)? */
435
27.8M
    return -1;  /* cannot know who sets that register */
436
89.4M
  else return pc;  /* current position sets that register */
437
117M
}
438
439
440
/*
441
** Try to find last instruction before 'lastpc' that modified register 'reg'.
442
*/
443
2.72M
static int findsetreg (const Proto *p, int lastpc, int reg) {
444
2.72M
  int pc;
445
2.72M
  int setreg = -1;  /* keep last instruction that changed 'reg' */
446
2.72M
  int jmptarget = 0;  /* any code before this address is conditional */
447
2.72M
  if (testMMMode(GET_OPCODE(p->code[lastpc])))
448
120k
    lastpc--;  /* previous instruction was not actually executed */
449
486M
  for (pc = 0; pc < lastpc; pc++) {
450
483M
    Instruction i = p->code[pc];
451
483M
    OpCode op = GET_OPCODE(i);
452
483M
    int a = GETARG_A(i);
453
483M
    int change;  /* true if current instruction changed 'reg' */
454
483M
    switch (op) {
455
866k
      case OP_LOADNIL: {  /* set registers from 'a' to 'a+b' */
456
866k
        int b = GETARG_B(i);
457
866k
        change = (a <= reg && reg <= a + b);
458
866k
        break;
459
866k
      }
460
5.24M
      case OP_TFORCALL: {  /* affect all regs above its base */
461
5.24M
        change = (reg >= a + 2);
462
5.24M
        break;
463
866k
      }
464
81.6M
      case OP_CALL:
465
81.6M
      case OP_TAILCALL: {  /* affect all registers above base */
466
81.6M
        change = (reg >= a);
467
81.6M
        break;
468
81.6M
      }
469
27.7M
      case OP_JMP: {  /* doesn't change registers, but changes 'jmptarget' */
470
27.7M
        int b = GETARG_sJ(i);
471
0
        int dest = pc + 1 + b;
472
        /* jump does not skip 'lastpc' and is larger than current one? */
473
27.7M
        if (dest <= lastpc && dest > jmptarget)
474
14.6M
          jmptarget = dest;  /* update 'jmptarget' */
475
27.7M
        change = 0;
476
27.7M
        break;
477
27.7M
      }
478
368M
      default:  /* any instruction that sets A */
479
368M
        change = (testAMode(op) && reg == a);
480
368M
        break;
481
483M
    }
482
483M
    if (change)
483
117M
      setreg = filterpc(pc, jmptarget);
484
483M
  }
485
2.72M
  return setreg;
486
2.72M
}
487
488
489
/*
490
** Find a "name" for the constant 'c'.
491
*/
492
1.92M
static const char *kname (const Proto *p, int index, const char **name) {
493
1.92M
  TValue *kvalue = &p->k[index];
494
1.92M
  if (ttisstring(kvalue)) {
495
1.88M
    *name = getstr(tsvalue(kvalue));
496
1.88M
    return "constant";
497
1.88M
  }
498
39.4k
  else {
499
39.4k
    *name = "?";
500
39.4k
    return NULL;
501
39.4k
  }
502
1.92M
}
503
504
505
static const char *basicgetobjname (const Proto *p, int *ppc, int reg,
506
2.88M
                                    const char **name) {
507
2.88M
  int pc = *ppc;
508
2.88M
  *name = luaF_getlocalname(p, reg + 1, pc);
509
2.88M
  if (*name)  /* is a local? */
510
157k
    return strlocal;
511
  /* else try symbolic execution */
512
2.72M
  *ppc = pc = findsetreg(p, pc, reg);
513
2.72M
  if (pc != -1) {  /* could find instruction? */
514
2.72M
    Instruction i = p->code[pc];
515
2.72M
    OpCode op = GET_OPCODE(i);
516
2.72M
    switch (op) {
517
130k
      case OP_MOVE: {
518
130k
        int b = GETARG_B(i);  /* move from 'b' to 'a' */
519
130k
        if (b < GETARG_A(i))
520
130k
          return basicgetobjname(p, ppc, b, name);  /* get name for 'b' */
521
0
        break;
522
130k
      }
523
197k
      case OP_GETUPVAL: {
524
197k
        *name = upvalname(p, GETARG_B(i));
525
0
        return strupval;
526
197k
      }
527
42.9k
      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
2.35M
      default: break;
530
2.72M
    }
531
2.72M
  }
532
2.35M
  return NULL;  /* could not find reasonable name */
533
2.72M
}
534
535
536
/*
537
** Find a "name" for the register 'c'.
538
*/
539
5.95k
static void rname (const Proto *p, int pc, int c, const char **name) {
540
5.95k
  const char *what = basicgetobjname(p, &pc, c, name); /* search for 'c' */
541
5.95k
  if (!(what && *what == 'c'))  /* did not find a constant name? */
542
4.07k
    *name = "?";
543
5.95k
}
544
545
546
/*
547
** Check whether table being indexed by instruction 'i' is the
548
** environment '_ENV'
549
*/
550
1.81M
static const char *isEnv (const Proto *p, int pc, Instruction i, int isup) {
551
1.81M
  int t = GETARG_B(i);  /* table index */
552
0
  const char *name;  /* name of indexed variable */
553
1.81M
  if (isup)  /* is 't' an upvalue? */
554
1.45M
    name = upvalname(p, t);
555
353k
  else {  /* 't' is a register */
556
353k
    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
353k
    if (what != strlocal && what != strupval)
560
343k
      name = NULL;  /* cannot be the variable _ENV */
561
353k
  }
562
1.81M
  return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field";
563
1.81M
}
564
565
566
/*
567
** Extend 'basicgetobjname' to handle table accesses
568
*/
569
static const char *getobjname (const Proto *p, int lastpc, int reg,
570
2.39M
                               const char **name) {
571
2.39M
  const char *kind = basicgetobjname(p, &lastpc, reg, name);
572
2.39M
  if (kind != NULL)
573
345k
    return kind;
574
2.04M
  else if (lastpc != -1) {  /* could find instruction? */
575
2.04M
    Instruction i = p->code[lastpc];
576
2.04M
    OpCode op = GET_OPCODE(i);
577
2.04M
    switch (op) {
578
1.45M
      case OP_GETTABUP: {
579
1.45M
        int k = GETARG_C(i);  /* key index */
580
0
        kname(p, k, name);
581
1.45M
        return isEnv(p, lastpc, i, 1);
582
1.45M
      }
583
5.95k
      case OP_GETTABLE: {
584
5.95k
        int k = GETARG_C(i);  /* key index */
585
0
        rname(p, lastpc, k, name);
586
5.95k
        return isEnv(p, lastpc, i, 0);
587
5.95k
      }
588
1.81k
      case OP_GETI: {
589
1.81k
        *name = "integer index";
590
1.81k
        return "field";
591
5.95k
      }
592
347k
      case OP_GETFIELD: {
593
347k
        int k = GETARG_C(i);  /* key index */
594
0
        kname(p, k, name);
595
347k
        return isEnv(p, lastpc, i, 0);
596
347k
      }
597
75.1k
      case OP_SELF: {
598
75.1k
        int k = GETARG_C(i);  /* key index */
599
0
        kname(p, k, name);
600
75.1k
        return "method";
601
75.1k
      }
602
155k
      default: break;  /* go through to return NULL */
603
2.04M
    }
604
2.04M
  }
605
156k
  return NULL;  /* could not find reasonable name */
606
2.39M
}
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
2.67M
                                     int pc, const char **name) {
617
2.67M
  TMS tm = (TMS)0;  /* (initial value avoids warnings) */
618
2.67M
  Instruction i = p->code[pc];  /* calling instruction */
619
2.67M
  switch (GET_OPCODE(i)) {
620
2.17M
    case OP_CALL:
621
2.17M
    case OP_TAILCALL:
622
2.17M
      return getobjname(p, pc, GETARG_A(i), name);  /* get function name */
623
17.0k
    case OP_TFORCALL: {  /* for iterator */
624
17.0k
      *name = "for iterator";
625
17.0k
       return "for iterator";
626
2.17M
    }
627
    /* other instructions can do calls through metamethods */
628
4.33k
    case OP_SELF: case OP_GETTABUP: case OP_GETTABLE:
629
12.7k
    case OP_GETI: case OP_GETFIELD:
630
12.7k
      tm = TM_INDEX;
631
12.7k
      break;
632
30.6k
    case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD:
633
30.6k
      tm = TM_NEWINDEX;
634
30.6k
      break;
635
221k
    case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
636
221k
      tm = cast(TMS, GETARG_C(i));
637
0
      break;
638
221k
    }
639
1.15k
    case OP_UNM: tm = TM_UNM; break;
640
182k
    case OP_BNOT: tm = TM_BNOT; break;
641
7.91k
    case OP_LEN: tm = TM_LEN; break;
642
3.68k
    case OP_CONCAT: tm = TM_CONCAT; break;
643
5.31k
    case OP_EQ: tm = TM_EQ; break;
644
    /* no cases for OP_EQI and OP_EQK, as they don't call metamethods */
645
11.4k
    case OP_LT: case OP_LTI: case OP_GTI: tm = TM_LT; break;
646
8.52k
    case OP_LE: case OP_LEI: case OP_GEI: tm = TM_LE; break;
647
2.19k
    case OP_CLOSE: case OP_RETURN: tm = TM_CLOSE; break;
648
442
    default:
649
442
      return NULL;  /* cannot find a reasonable name */
650
2.67M
  }
651
487k
  *name = getshrstr(G(L)->tmname[tm]) + 2;
652
0
  return "metamethod";
653
487k
}
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
6.21M
                                                   const char **name) {
661
6.21M
  if (ci->callstatus & CIST_HOOKED) {  /* was it called inside a hook? */
662
316k
    *name = "?";
663
316k
    return "hook";
664
316k
  }
665
5.89M
  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
5.89M
  else if (isLua(ci))
670
5.35M
    return funcnamefromcode(L, ci_func(ci)->p, currentpc(ci), name);
671
3.21M
  else
672
3.21M
    return NULL;
673
6.21M
}
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
218k
static int instack (CallInfo *ci, const TValue *o) {
686
218k
  int pos;
687
218k
  StkId base = ci->func.p + 1;
688
1.16M
  for (pos = 0; base + pos < ci->top.p; pos++) {
689
1.16M
    if (o == s2v(base + pos))
690
218k
      return pos;
691
1.16M
  }
692
0
  return -1;  /* not found */
693
218k
}
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
221k
                                 const char **name) {
703
442k
  LClosure *c = ci_func(ci);
704
0
  int i;
705
555k
  for (i = 0; i < c->nupvalues; i++) {
706
337k
    if (c->upvals[i]->v.p == o) {
707
2.95k
      *name = upvalname(c->p, i);
708
2.95k
      return strupval;
709
2.95k
    }
710
337k
  }
711
218k
  return NULL;
712
442k
}
713
714
715
static const char *formatvarinfo (lua_State *L, const char *kind,
716
858k
                                                const char *name) {
717
858k
  if (kind == NULL)
718
181k
    return "";  /* no information */
719
676k
  else
720
676k
    return luaO_pushfstring(L, " (%s '%s')", kind, name);
721
858k
}
722
723
/*
724
** Build a string with a "description" for the value 'o', such as
725
** "variable 'x'" or "upvalue 'y'".
726
*/
727
289k
static const char *varinfo (lua_State *L, const TValue *o) {
728
289k
  CallInfo *ci = L->ci;
729
289k
  const char *name = NULL;  /* to avoid warnings */
730
289k
  const char *kind = NULL;
731
289k
  if (isLua(ci)) {
732
221k
    kind = getupvalname(ci, o, &name);  /* check whether 'o' is an upvalue */
733
221k
    if (!kind) {  /* not an upvalue? */
734
218k
      int reg = instack(ci, o);  /* try a register */
735
218k
      if (reg >= 0)  /* is 'o' a register? */
736
436k
        kind = getobjname(ci_func(ci)->p, currentpc(ci), reg, &name);
737
218k
    }
738
221k
  }
739
289k
  return formatvarinfo(L, kind, name);
740
289k
}
741
742
743
/*
744
** Raise a type error
745
*/
746
static l_noret typeerror (lua_State *L, const TValue *o, const char *op,
747
804k
                          const char *extra) {
748
804k
  const char *t = luaT_objtypename(L, o);
749
804k
  luaG_runerror(L, "attempt to %s a %s value%s", op, t, extra);
750
804k
}
751
752
753
/*
754
** Raise a type error with "standard" information about the faulty
755
** object 'o' (using 'varinfo').
756
*/
757
158k
l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
758
158k
  typeerror(L, o, op, varinfo(L, o));
759
158k
}
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
645k
l_noret luaG_callerror (lua_State *L, const TValue *o) {
768
645k
  CallInfo *ci = L->ci;
769
645k
  const char *name = NULL;  /* to avoid warnings */
770
645k
  const char *kind = funcnamefromcall(L, ci, &name);
771
645k
  const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o);
772
645k
  typeerror(L, o, "call", extra);
773
645k
}
774
775
776
7.18k
l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what) {
777
7.18k
  luaG_runerror(L, "bad 'for' %s (number expected, got %s)",
778
7.18k
                   what, luaT_objtypename(L, o));
779
7.18k
}
780
781
782
6.67k
l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
783
6.67k
  if (ttisstring(p1) || cvt2str(p1)) p1 = p2;
784
6.67k
  luaG_typeerror(L, p1, "concatenate");
785
6.67k
}
786
787
788
l_noret luaG_opinterror (lua_State *L, const TValue *p1,
789
99.1k
                         const TValue *p2, const char *msg) {
790
99.1k
  if (!ttisnumber(p1))  /* first operand is wrong? */
791
82.4k
    p2 = p1;  /* now second is wrong */
792
99.1k
  luaG_typeerror(L, p2, msg);
793
99.1k
}
794
795
796
/*
797
** Error when both values are convertible to numbers, but not to integers
798
*/
799
53.7k
l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
800
53.7k
  lua_Integer temp;
801
53.7k
  if (!luaV_tointegerns(p1, &temp, LUA_FLOORN2I))
802
31.5k
    p2 = p1;
803
53.7k
  luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
804
53.7k
}
805
806
807
17.4k
l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
808
17.4k
  const char *t1 = luaT_objtypename(L, p1);
809
17.4k
  const char *t2 = luaT_objtypename(L, p2);
810
17.4k
  if (strcmp(t1, t2) == 0)
811
920
    luaG_runerror(L, "attempt to compare two %s values", t1);
812
16.4k
  else
813
16.4k
    luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
814
17.4k
}
815
816
817
/* add src:line information to 'msg' */
818
const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
819
6.16M
                                        int line) {
820
6.16M
  if (src == NULL)  /* no debug information? */
821
7
    return luaO_pushfstring(L, "?:?: %s", msg);
822
6.16M
  else {
823
6.16M
    char buff[LUA_IDSIZE];
824
6.16M
    size_t idlen;
825
6.16M
    const char *id = getlstr(src, idlen);
826
6.16M
    luaO_chunkid(buff, id, idlen);
827
6.16M
    return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
828
6.16M
  }
829
6.16M
}
830
831
832
2.92M
l_noret luaG_errormsg (lua_State *L) {
833
2.92M
  if (L->errfunc != 0) {  /* is there an error handling function? */
834
2.01M
    StkId errfunc = restorestack(L, L->errfunc);
835
2.01M
    lua_assert(ttisfunction(s2v(errfunc)));
836
4.02M
    setobjs2s(L, L->top.p, L->top.p - 1);  /* move argument */
837
2.01M
    setobjs2s(L, L->top.p - 1, errfunc);  /* push function */
838
2.01M
    L->top.p++;  /* assume EXTRA_STACK */
839
2.01M
    luaD_callnoyield(L, L->top.p - 2, 1);  /* call it */
840
2.01M
  }
841
2.92M
  if (ttisnil(s2v(L->top.p - 1))) {  /* error object is nil? */
842
    /* change it to a proper message */
843
54.5k
    setsvalue2s(L, L->top.p - 1, luaS_newliteral(L, "<no error object>"));
844
54.5k
  }
845
2.92M
  luaD_throw(L, LUA_ERRRUN);
846
2.92M
}
847
848
849
990k
l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
850
990k
  CallInfo *ci = L->ci;
851
990k
  const char *msg;
852
990k
  va_list argp;
853
990k
  luaC_checkGC(L);  /* error message uses memory */
854
990k
  pushvfstring(L, argp, fmt, msg);
855
990k
  if (isLua(ci)) {  /* Lua function? */
856
    /* add source:line information */
857
1.64M
    luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci));
858
822k
    setobjs2s(L, L->top.p - 2, L->top.p - 1);  /* remove 'msg' */
859
822k
    L->top.p--;
860
822k
  }
861
990k
  luaG_errormsg(L);
862
990k
}
863
864
865
/*
866
** Check whether new instruction 'newpc' is in a different line from
867
** previous instruction 'oldpc'. More often than not, 'newpc' is only
868
** one or a few instructions after 'oldpc' (it must be after, see
869
** caller), so try to avoid calling 'luaG_getfuncline'. If they are
870
** too far apart, there is a good chance of a ABSLINEINFO in the way,
871
** so it goes directly to 'luaG_getfuncline'.
872
*/
873
75.5M
static int changedline (const Proto *p, int oldpc, int newpc) {
874
75.5M
  if (p->lineinfo == NULL)  /* no debug information? */
875
0
    return 0;
876
75.5M
  if (newpc - oldpc < MAXIWTHABS / 2) {  /* not too far apart? */
877
75.4M
    int delta = 0;  /* line difference */
878
75.4M
    int pc = oldpc;
879
87.2M
    for (;;) {
880
87.2M
      int lineinfo = p->lineinfo[++pc];
881
87.2M
      if (lineinfo == ABSLINEINFO)
882
146k
        break;  /* cannot compute delta; fall through */
883
87.0M
      delta += lineinfo;
884
87.0M
      if (pc == newpc)
885
75.3M
        return (delta != 0);  /* delta computed successfully */
886
87.0M
    }
887
75.4M
  }
888
  /* either instructions are too far apart or there is an absolute line
889
     info in the way; compute line difference explicitly */
890
176k
  return (luaG_getfuncline(p, oldpc) != luaG_getfuncline(p, newpc));
891
75.5M
}
892
893
894
/*
895
** Traces Lua calls. If code is running the first instruction of a function,
896
** and function is not vararg, and it is not coming from an yield,
897
** calls 'luaD_hookcall'. (Vararg functions will call 'luaD_hookcall'
898
** after adjusting its variable arguments; otherwise, they could call
899
** a line/count hook before the call hook. Functions coming from
900
** an yield already called 'luaD_hookcall' before yielding.)
901
*/
902
6.15M
int luaG_tracecall (lua_State *L) {
903
6.15M
  CallInfo *ci = L->ci;
904
12.3M
  Proto *p = ci_func(ci)->p;
905
0
  ci->u.l.trap = 1;  /* ensure hooks will be checked */
906
12.3M
  if (ci->u.l.savedpc == p->code) {  /* first instruction (not resuming)? */
907
5.91M
    if (p->flag & PF_ISVARARG)
908
40.8k
      return 0;  /* hooks will start at VARARGPREP instruction */
909
5.87M
    else if (!(ci->callstatus & CIST_HOOKYIELD))  /* not yielded? */
910
5.87M
      luaD_hookcall(L, ci);  /* check 'call' hook */
911
5.91M
  }
912
6.10M
  return 1;  /* keep 'trap' on */
913
12.3M
}
914
915
916
/*
917
** Traces the execution of a Lua function. Called before the execution
918
** of each opcode, when debug is on. 'L->oldpc' stores the last
919
** instruction traced, to detect line changes. When entering a new
920
** function, 'npci' will be zero and will test as a new line whatever
921
** the value of 'oldpc'.  Some exceptional conditions may return to
922
** a function without setting 'oldpc'. In that case, 'oldpc' may be
923
** invalid; if so, use zero as a valid value. (A wrong but valid 'oldpc'
924
** at most causes an extra call to a line hook.)
925
** This function is not "Protected" when called, so it should correct
926
** 'L->top.p' before calling anything that can run the GC.
927
*/
928
94.8M
int luaG_traceexec (lua_State *L, const Instruction *pc) {
929
94.8M
  CallInfo *ci = L->ci;
930
94.8M
  lu_byte mask = cast_byte(L->hookmask);
931
189M
  const Proto *p = ci_func(ci)->p;
932
0
  int counthook;
933
189M
  if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) {  /* no hooks? */
934
6.45M
    ci->u.l.trap = 0;  /* don't need to stop again */
935
6.45M
    return 0;  /* turn off 'trap' */
936
6.45M
  }
937
88.4M
  pc++;  /* reference is always next instruction */
938
88.4M
  ci->u.l.savedpc = pc;  /* save 'pc' */
939
88.4M
  counthook = (mask & LUA_MASKCOUNT) && (--L->hookcount == 0);
940
88.4M
  if (counthook)
941
40.6M
    resethookcount(L);  /* reset count */
942
47.7M
  else if (!(mask & LUA_MASKLINE))
943
1.84M
    return 1;  /* no line hook and count != 0; nothing to be done now */
944
86.5M
  if (ci->callstatus & CIST_HOOKYIELD) {  /* hook yielded last time? */
945
0
    ci->callstatus &= ~CIST_HOOKYIELD;  /* erase mark */
946
0
    return 1;  /* do not call hook again (VM yielded, so it did not move) */
947
0
  }
948
86.5M
  if (!luaP_isIT(*(ci->u.l.savedpc - 1)))  /* top not being used? */
949
86.1M
    L->top.p = ci->top.p;  /* correct top */
950
86.5M
  if (counthook)
951
40.6M
    luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0);  /* call count hook */
952
86.5M
  if (mask & LUA_MASKLINE) {
953
    /* 'L->oldpc' may be invalid; use zero in this case */
954
82.3M
    int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0;
955
82.3M
    int npci = pcRel(pc, p);
956
82.3M
    if (npci <= oldpc ||  /* call hook when jump back (loop), */
957
82.3M
        changedline(p, oldpc, npci)) {  /* or when enter new line */
958
16.6M
      int newline = luaG_getfuncline(p, npci);
959
16.6M
      luaD_hook(L, LUA_HOOKLINE, newline, 0, 0);  /* call line hook */
960
16.6M
    }
961
82.3M
    L->oldpc = npci;  /* 'pc' of last call to line hook */
962
82.3M
  }
963
86.5M
  if (L->status == LUA_YIELD) {  /* did hook yield? */
964
0
    if (counthook)
965
0
      L->hookcount = 1;  /* undo decrement to zero */
966
0
    ci->callstatus |= CIST_HOOKYIELD;  /* mark that it yielded */
967
0
    luaD_throw(L, LUA_YIELD);
968
0
  }
969
86.5M
  return 1;  /* keep 'trap' on */
970
86.5M
}
971