Coverage Report

Created: 2025-07-11 06:33

/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
16.3M
#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
13.2M
static int currentpc (CallInfo *ci) {
44
13.2M
  lua_assert(isLua(ci));
45
13.2M
  return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
46
13.2M
}
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
291M
static int getbaseline (const Proto *f, int pc, int *basepc) {
63
291M
  if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) {
64
252M
    *basepc = -1;  /* start from the beginning */
65
252M
    return f->linedefined;
66
252M
  }
67
39.3M
  else {
68
39.3M
    int i = pc / MAXIWTHABS - 1;  /* get an estimate */
69
    /* estimate must be a lower bound of the correct base */
70
39.3M
    lua_assert(i < 0 ||
71
39.3M
              (i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc));
72
39.5M
    while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc)
73
164k
      i++;  /* low estimate; adjust it */
74
39.3M
    *basepc = f->abslineinfo[i].pc;
75
39.3M
    return f->abslineinfo[i].line;
76
39.3M
  }
77
291M
}
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
291M
int luaG_getfuncline (const Proto *f, int pc) {
86
291M
  if (f->lineinfo == NULL)  /* no debug information? */
87
3
    return -1;
88
291M
  else {
89
291M
    int basepc;
90
291M
    int baseline = getbaseline(f, pc, &basepc);
91
3.87G
    while (basepc++ < pc) {  /* walk until given instruction */
92
3.58G
      lua_assert(f->lineinfo[basepc] != ABSLINEINFO);
93
3.58G
      baseline += f->lineinfo[basepc];  /* correct line */
94
3.58G
    }
95
291M
    return baseline;
96
291M
  }
97
291M
}
98
99
100
9.60M
static int getcurrentline (CallInfo *ci) {
101
9.60M
  return luaG_getfuncline(ci_func(ci)->p, currentpc(ci));
102
9.60M
}
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
378k
static void settraps (CallInfo *ci) {
117
14.2M
  for (; ci != NULL; ci = ci->previous)
118
13.9M
    if (isLua(ci))
119
11.2M
      ci->u.l.trap = 1;
120
378k
}
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
544k
LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
134
544k
  if (func == NULL || mask == 0) {  /* turn off hooks? */
135
166k
    mask = 0;
136
166k
    func = NULL;
137
166k
  }
138
544k
  L->hook = func;
139
544k
  L->basehookcount = count;
140
544k
  resethookcount(L);
141
544k
  L->hookmask = cast_byte(mask);
142
544k
  if (mask)
143
378k
    settraps(L->ci);  /* to trace inside 'luaV_execute' */
144
544k
}
145
146
147
47.6k
LUA_API lua_Hook lua_gethook (lua_State *L) {
148
47.6k
  return L->hook;
149
47.6k
}
150
151
152
47.6k
LUA_API int lua_gethookmask (lua_State *L) {
153
47.6k
  return L->hookmask;
154
47.6k
}
155
156
157
47.5k
LUA_API int lua_gethookcount (lua_State *L) {
158
47.5k
  return L->basehookcount;
159
47.5k
}
160
161
162
10.6M
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
163
10.6M
  int status;
164
10.6M
  CallInfo *ci;
165
10.6M
  if (level < 0) return 0;  /* invalid (negative) level */
166
10.6M
  lua_lock(L);
167
519M
  for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
168
509M
    level--;
169
10.6M
  if (level == 0 && ci != &L->base_ci) {  /* level found? */
170
9.99M
    status = 1;
171
9.99M
    ar->i_ci = ci;
172
9.99M
  }
173
644k
  else status = 0;  /* no such level */
174
10.6M
  lua_unlock(L);
175
10.6M
  return status;
176
10.6M
}
177
178
179
2.38M
static const char *upvalname (const Proto *p, int uv) {
180
2.38M
  TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
181
2.38M
  if (s == NULL) return "?";
182
2.38M
  else return getstr(s);
183
2.38M
}
184
185
186
3
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
187
9
  if (clLvalue(s2v(ci->func.p))->p->flag & PF_ISVARARG) {
188
3
    int nextra = ci->u.l.nextraargs;
189
3
    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
3
  }
194
3
  return NULL;  /* no such vararg */
195
3
}
196
197
198
5.02k
const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) {
199
5.02k
  StkId base = ci->func.p + 1;
200
5.02k
  const char *name = NULL;
201
5.02k
  if (isLua(ci)) {
202
3.06k
    if (n < 0)  /* access to vararg values? */
203
3
      return findvararg(ci, n, pos);
204
3.05k
    else
205
6.11k
      name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
206
3.06k
  }
207
5.02k
  if (name == NULL) {  /* no 'standard' name? */
208
3.85k
    StkId limit = (ci == L->ci) ? L->top.p : ci->next->func.p;
209
3.85k
    if (limit - base >= n && n > 0) {  /* is 'n' inside 'ci' stack? */
210
      /* generic name for any valid slot */
211
3.01k
      name = isLua(ci) ? "(temporary)" : "(C temporary)";
212
3.01k
    }
213
847
    else
214
847
      return NULL;  /* no name */
215
3.85k
  }
216
4.17k
  if (pos)
217
3.95k
    *pos = base + (n - 1);
218
4.17k
  return name;
219
5.02k
}
220
221
222
3.86k
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
223
3.86k
  const char *name;
224
3.86k
  lua_lock(L);
225
3.86k
  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
3.86k
  else {  /* active function; get information through 'ar' */
232
3.86k
    StkId pos = NULL;  /* to avoid warnings */
233
3.86k
    name = luaG_findlocal(L, ar->i_ci, n, &pos);
234
3.86k
    if (name) {
235
3.21k
      setobjs2s(L, L->top.p, pos);
236
3.21k
      api_incr_top(L);
237
3.21k
    }
238
3.86k
  }
239
3.86k
  lua_unlock(L);
240
3.86k
  return name;
241
3.86k
}
242
243
244
940
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
245
940
  StkId pos = NULL;  /* to avoid warnings */
246
940
  const char *name;
247
940
  lua_lock(L);
248
940
  name = luaG_findlocal(L, ar->i_ci, n, &pos);
249
940
  if (name) {
250
742
    api_checkpop(L, 1);
251
1.48k
    setobjs2s(L, pos, L->top.p - 1);
252
742
    L->top.p--;  /* pop value */
253
742
  }
254
940
  lua_unlock(L);
255
940
  return name;
256
940
}
257
258
259
12.4M
static void funcinfo (lua_Debug *ar, Closure *cl) {
260
12.4M
  if (!LuaClosure(cl)) {
261
4.06M
    ar->source = "=[C]";
262
4.06M
    ar->srclen = LL("=[C]");
263
4.06M
    ar->linedefined = -1;
264
4.06M
    ar->lastlinedefined = -1;
265
4.06M
    ar->what = "C";
266
4.06M
  }
267
8.36M
  else {
268
8.36M
    const Proto *p = cl->l.p;
269
8.36M
    if (p->source) {
270
8.36M
      ar->source = getlstr(p->source, ar->srclen);
271
8.36M
    }
272
0
    else {
273
0
      ar->source = "=?";
274
0
      ar->srclen = LL("=?");
275
0
    }
276
8.36M
    ar->linedefined = p->linedefined;
277
8.36M
    ar->lastlinedefined = p->lastlinedefined;
278
8.36M
    ar->what = (ar->linedefined == 0) ? "main" : "Lua";
279
8.36M
  }
280
12.4M
  luaO_chunkid(ar->short_src, ar->source, ar->srclen);
281
12.4M
}
282
283
284
503k
static int nextline (const Proto *p, int currentline, int pc) {
285
503k
  if (p->lineinfo[pc] != ABSLINEINFO)
286
501k
    return currentline + p->lineinfo[pc];
287
1.56k
  else
288
1.56k
    return luaG_getfuncline(p, pc);
289
503k
}
290
291
292
4.87k
static void collectvalidlines (lua_State *L, Closure *f) {
293
4.87k
  if (!LuaClosure(f)) {
294
2
    setnilvalue(s2v(L->top.p));
295
2
    api_incr_top(L);
296
2
  }
297
4.87k
  else {
298
4.87k
    const Proto *p = f->l.p;
299
4.87k
    int currentline = p->linedefined;
300
4.87k
    Table *t = luaH_new(L);  /* new table to store active lines */
301
4.87k
    sethvalue2s(L, L->top.p, t);  /* push it on stack */
302
4.87k
    api_incr_top(L);
303
4.87k
    if (p->lineinfo != NULL) {  /* proto with debug information? */
304
4.87k
      int i;
305
4.87k
      TValue v;
306
4.87k
      setbtvalue(&v);  /* boolean 'true' to be the value of all indices */
307
4.87k
      if (!(p->flag & PF_ISVARARG))  /* regular function? */
308
744
        i = 0;  /* consider all instructions */
309
4.13k
      else {  /* vararg function */
310
4.13k
        lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP);
311
4.13k
        currentline = nextline(p, currentline, 0);
312
4.13k
        i = 1;  /* skip first instruction (OP_VARARGPREP) */
313
4.13k
      }
314
503k
      for (; i < p->sizelineinfo; i++) {  /* for each instruction */
315
499k
        currentline = nextline(p, currentline, i);  /* get its line */
316
499k
        luaH_setint(L, t, currentline, &v);  /* table[line] = true */
317
499k
      }
318
4.87k
    }
319
4.87k
  }
320
4.87k
}
321
322
323
6.86M
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
324
  /* calling function is a known function? */
325
6.86M
  if (ci != NULL && !(ci->callstatus & CIST_TAIL))
326
6.66M
    return funcnamefromcall(L, ci->previous, name);
327
194k
  else return NULL;  /* no way to find a name */
328
6.86M
}
329
330
331
static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
332
16.5M
                       Closure *f, CallInfo *ci) {
333
16.5M
  int status = 1;
334
69.0M
  for (; *what; what++) {
335
52.4M
    switch (*what) {
336
12.4M
      case 'S': {
337
12.4M
        funcinfo(ar, f);
338
12.4M
        break;
339
0
      }
340
12.4M
      case 'l': {
341
12.4M
        ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1;
342
12.4M
        break;
343
0
      }
344
3.93M
      case 'u': {
345
3.93M
        ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
346
3.93M
        if (!LuaClosure(f)) {
347
844k
          ar->isvararg = 1;
348
844k
          ar->nparams = 0;
349
844k
        }
350
3.08M
        else {
351
3.08M
          ar->isvararg = (f->l.p->flag & PF_ISVARARG) ? 1 : 0;
352
3.08M
          ar->nparams = f->l.p->numparams;
353
3.08M
        }
354
3.93M
        break;
355
0
      }
356
6.86M
      case 't': {
357
6.86M
        if (ci != NULL) {
358
6.85M
          ar->istailcall = !!(ci->callstatus & CIST_TAIL);
359
6.85M
          ar->extraargs =
360
6.85M
                   cast_uchar((ci->callstatus & MAX_CCMT) >> CIST_CCMT);
361
6.85M
        }
362
4.50k
        else {
363
4.50k
          ar->istailcall = 0;
364
4.50k
          ar->extraargs = 0;
365
4.50k
        }
366
6.86M
        break;
367
0
      }
368
6.86M
      case 'n': {
369
6.86M
        ar->namewhat = getfuncname(L, ci, &ar->name);
370
6.86M
        if (ar->namewhat == NULL) {
371
4.16M
          ar->namewhat = "";  /* not found */
372
4.16M
          ar->name = NULL;
373
4.16M
        }
374
6.86M
        break;
375
0
      }
376
3.93M
      case 'r': {
377
3.93M
        if (ci == NULL || !(ci->callstatus & CIST_HOOKED))
378
898k
          ar->ftransfer = ar->ntransfer = 0;
379
3.03M
        else {
380
3.03M
          ar->ftransfer = L->transferinfo.ftransfer;
381
3.03M
          ar->ntransfer = L->transferinfo.ntransfer;
382
3.03M
        }
383
3.93M
        break;
384
0
      }
385
5.32k
      case 'L':
386
6.05M
      case 'f':  /* handled by lua_getinfo */
387
6.05M
        break;
388
1.34k
      default: status = 0;  /* invalid option */
389
52.4M
    }
390
52.4M
  }
391
16.5M
  return status;
392
16.5M
}
393
394
395
16.5M
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
396
16.5M
  int status;
397
16.5M
  Closure *cl;
398
16.5M
  CallInfo *ci;
399
16.5M
  TValue *func;
400
16.5M
  lua_lock(L);
401
16.5M
  if (*what == '>') {
402
5.11k
    ci = NULL;
403
5.11k
    func = s2v(L->top.p - 1);
404
5.11k
    api_check(L, ttisfunction(func), "function expected");
405
5.11k
    what++;  /* skip the '>' */
406
5.11k
    L->top.p--;  /* pop function */
407
5.11k
  }
408
16.5M
  else {
409
16.5M
    ci = ar->i_ci;
410
16.5M
    func = s2v(ci->func.p);
411
16.5M
    lua_assert(ttisfunction(func));
412
16.5M
  }
413
16.5M
  cl = ttisclosure(func) ? clvalue(func) : NULL;
414
16.5M
  status = auxgetinfo(L, what, ar, cl, ci);
415
16.5M
  if (strchr(what, 'f')) {
416
6.04M
    setobj2s(L, L->top.p, func);
417
6.04M
    api_incr_top(L);
418
6.04M
  }
419
16.5M
  if (strchr(what, 'L'))
420
4.87k
    collectvalidlines(L, cl);
421
16.5M
  lua_unlock(L);
422
16.5M
  return status;
423
16.5M
}
424
425
426
/*
427
** {======================================================
428
** Symbolic Execution
429
** =======================================================
430
*/
431
432
433
100M
static int filterpc (int pc, int jmptarget) {
434
100M
  if (pc < jmptarget)  /* is code conditional (inside a jump)? */
435
15.5M
    return -1;  /* cannot know who sets that register */
436
84.8M
  else return pc;  /* current position sets that register */
437
100M
}
438
439
440
/*
441
** Try to find last instruction before 'lastpc' that modified register 'reg'.
442
*/
443
3.75M
static int findsetreg (const Proto *p, int lastpc, int reg) {
444
3.75M
  int pc;
445
3.75M
  int setreg = -1;  /* keep last instruction that changed 'reg' */
446
3.75M
  int jmptarget = 0;  /* any code before this address is conditional */
447
3.75M
  if (testMMMode(GET_OPCODE(p->code[lastpc])))
448
333k
    lastpc--;  /* previous instruction was not actually executed */
449
433M
  for (pc = 0; pc < lastpc; pc++) {
450
429M
    Instruction i = p->code[pc];
451
429M
    OpCode op = GET_OPCODE(i);
452
429M
    int a = GETARG_A(i);
453
429M
    int change;  /* true if current instruction changed 'reg' */
454
429M
    switch (op) {
455
1.04M
      case OP_LOADNIL: {  /* set registers from 'a' to 'a+b' */
456
1.04M
        int b = GETARG_B(i);
457
1.04M
        change = (a <= reg && reg <= a + b);
458
1.04M
        break;
459
1.04M
      }
460
2.29M
      case OP_TFORCALL: {  /* affect all regs above its base */
461
2.29M
        change = (reg >= a + 2);
462
2.29M
        break;
463
1.04M
      }
464
77.0M
      case OP_CALL:
465
77.0M
      case OP_TAILCALL: {  /* affect all registers above base */
466
77.0M
        change = (reg >= a);
467
77.0M
        break;
468
77.0M
      }
469
22.1M
      case OP_JMP: {  /* doesn't change registers, but changes 'jmptarget' */
470
22.1M
        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
22.1M
        if (dest <= lastpc && dest > jmptarget)
474
15.1M
          jmptarget = dest;  /* update 'jmptarget' */
475
22.1M
        change = 0;
476
22.1M
        break;
477
22.1M
      }
478
327M
      default:  /* any instruction that sets A */
479
327M
        change = (testAMode(op) && reg == a);
480
327M
        break;
481
429M
    }
482
429M
    if (change)
483
100M
      setreg = filterpc(pc, jmptarget);
484
429M
  }
485
3.75M
  return setreg;
486
3.75M
}
487
488
489
/*
490
** Find a "name" for the constant 'c'.
491
*/
492
2.75M
static const char *kname (const Proto *p, int index, const char **name) {
493
2.75M
  TValue *kvalue = &p->k[index];
494
2.75M
  if (ttisstring(kvalue)) {
495
2.69M
    *name = getstr(tsvalue(kvalue));
496
2.69M
    return "constant";
497
2.69M
  }
498
51.3k
  else {
499
51.3k
    *name = "?";
500
51.3k
    return NULL;
501
51.3k
  }
502
2.75M
}
503
504
505
static const char *basicgetobjname (const Proto *p, int *ppc, int reg,
506
3.84M
                                    const char **name) {
507
3.84M
  int pc = *ppc;
508
3.84M
  *name = luaF_getlocalname(p, reg + 1, pc);
509
3.84M
  if (*name)  /* is a local? */
510
90.2k
    return strlocal;
511
  /* else try symbolic execution */
512
3.75M
  *ppc = pc = findsetreg(p, pc, reg);
513
3.75M
  if (pc != -1) {  /* could find instruction? */
514
3.75M
    Instruction i = p->code[pc];
515
3.75M
    OpCode op = GET_OPCODE(i);
516
3.75M
    switch (op) {
517
67.7k
      case OP_MOVE: {
518
67.7k
        int b = GETARG_B(i);  /* move from 'b' to 'a' */
519
67.7k
        if (b < GETARG_A(i))
520
67.7k
          return basicgetobjname(p, ppc, b, name);  /* get name for 'b' */
521
0
        break;
522
67.7k
      }
523
287k
      case OP_GETUPVAL: {
524
287k
        *name = upvalname(p, GETARG_B(i));
525
0
        return strupval;
526
287k
      }
527
196k
      case OP_LOADK: return kname(p, GETARG_Bx(i), name);
528
1
      case OP_LOADKX: return kname(p, GETARG_Ax(p->code[pc + 1]), name);
529
3.20M
      default: break;
530
3.75M
    }
531
3.75M
  }
532
3.20M
  return NULL;  /* could not find reasonable name */
533
3.75M
}
534
535
536
/*
537
** Find a "name" for the register 'c'.
538
*/
539
131k
static void rname (const Proto *p, int pc, int c, const char **name) {
540
131k
  const char *what = basicgetobjname(p, &pc, c, name); /* search for 'c' */
541
131k
  if (!(what && *what == 'c'))  /* did not find a constant name? */
542
3.36k
    *name = "?";
543
131k
}
544
545
546
/*
547
** Check whether table being indexed by instruction 'i' is the
548
** environment '_ENV'
549
*/
550
2.59M
static const char *isEnv (const Proto *p, int pc, Instruction i, int isup) {
551
2.59M
  int t = GETARG_B(i);  /* table index */
552
0
  const char *name;  /* name of indexed variable */
553
2.59M
  if (isup)  /* is 't' an upvalue? */
554
2.09M
    name = upvalname(p, t);
555
497k
  else {  /* 't' is a register */
556
497k
    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
497k
    if (what != strlocal && what != strupval)
560
363k
      name = NULL;  /* cannot be the variable _ENV */
561
497k
  }
562
2.59M
  return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field";
563
2.59M
}
564
565
566
/*
567
** Extend 'basicgetobjname' to handle table accesses
568
*/
569
static const char *getobjname (const Proto *p, int lastpc, int reg,
570
3.15M
                               const char **name) {
571
3.15M
  const char *kind = basicgetobjname(p, &lastpc, reg, name);
572
3.15M
  if (kind != NULL)
573
260k
    return kind;
574
2.89M
  else if (lastpc != -1) {  /* could find instruction? */
575
2.89M
    Instruction i = p->code[lastpc];
576
2.89M
    OpCode op = GET_OPCODE(i);
577
2.89M
    switch (op) {
578
2.09M
      case OP_GETTABUP: {
579
2.09M
        int k = GETARG_C(i);  /* key index */
580
0
        kname(p, k, name);
581
2.09M
        return isEnv(p, lastpc, i, 1);
582
2.09M
      }
583
131k
      case OP_GETTABLE: {
584
131k
        int k = GETARG_C(i);  /* key index */
585
0
        rname(p, lastpc, k, name);
586
131k
        return isEnv(p, lastpc, i, 0);
587
131k
      }
588
238
      case OP_GETI: {
589
238
        *name = "integer index";
590
238
        return "field";
591
131k
      }
592
365k
      case OP_GETFIELD: {
593
365k
        int k = GETARG_C(i);  /* key index */
594
0
        kname(p, k, name);
595
365k
        return isEnv(p, lastpc, i, 0);
596
365k
      }
597
90.9k
      case OP_SELF: {
598
90.9k
        int k = GETARG_C(i);  /* key index */
599
0
        kname(p, k, name);
600
90.9k
        return "method";
601
90.9k
      }
602
204k
      default: break;  /* go through to return NULL */
603
2.89M
    }
604
2.89M
  }
605
205k
  return NULL;  /* could not find reasonable name */
606
3.15M
}
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
3.15M
                                     int pc, const char **name) {
617
3.15M
  TMS tm = (TMS)0;  /* (initial value avoids warnings) */
618
3.15M
  Instruction i = p->code[pc];  /* calling instruction */
619
3.15M
  switch (GET_OPCODE(i)) {
620
2.68M
    case OP_CALL:
621
2.68M
    case OP_TAILCALL:
622
2.68M
      return getobjname(p, pc, GETARG_A(i), name);  /* get function name */
623
9.57k
    case OP_TFORCALL: {  /* for iterator */
624
9.57k
      *name = "for iterator";
625
9.57k
       return "for iterator";
626
2.68M
    }
627
    /* other instructions can do calls through metamethods */
628
8.99k
    case OP_SELF: case OP_GETTABUP: case OP_GETTABLE:
629
13.6k
    case OP_GETI: case OP_GETFIELD:
630
13.6k
      tm = TM_INDEX;
631
13.6k
      break;
632
13.0k
    case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD:
633
13.0k
      tm = TM_NEWINDEX;
634
13.0k
      break;
635
349k
    case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
636
349k
      tm = cast(TMS, GETARG_C(i));
637
0
      break;
638
349k
    }
639
7.30k
    case OP_UNM: tm = TM_UNM; break;
640
27.2k
    case OP_BNOT: tm = TM_BNOT; break;
641
510
    case OP_LEN: tm = TM_LEN; break;
642
4.70k
    case OP_CONCAT: tm = TM_CONCAT; break;
643
18
    case OP_EQ: tm = TM_EQ; break;
644
    /* no cases for OP_EQI and OP_EQK, as they don't call metamethods */
645
23.0k
    case OP_LT: case OP_LTI: case OP_GTI: tm = TM_LT; break;
646
19.9k
    case OP_LE: case OP_LEI: case OP_GEI: tm = TM_LE; break;
647
817
    case OP_CLOSE: case OP_RETURN: tm = TM_CLOSE; break;
648
1.81k
    default:
649
1.81k
      return NULL;  /* cannot find a reasonable name */
650
3.15M
  }
651
459k
  *name = getshrstr(G(L)->tmname[tm]) + 2;
652
0
  return "metamethod";
653
459k
}
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
7.51M
                                                   const char **name) {
661
7.51M
  if (ci->callstatus & CIST_HOOKED) {  /* was it called inside a hook? */
662
274k
    *name = "?";
663
274k
    return "hook";
664
274k
  }
665
7.23M
  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
7.23M
  else if (isLua(ci))
670
6.31M
    return funcnamefromcode(L, ci_func(ci)->p, currentpc(ci), name);
671
4.07M
  else
672
4.07M
    return NULL;
673
7.51M
}
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
465k
static int instack (CallInfo *ci, const TValue *o) {
686
465k
  int pos;
687
465k
  StkId base = ci->func.p + 1;
688
2.74M
  for (pos = 0; base + pos < ci->top.p; pos++) {
689
2.74M
    if (o == s2v(base + pos))
690
465k
      return pos;
691
2.74M
  }
692
0
  return -1;  /* not found */
693
465k
}
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
468k
                                 const char **name) {
703
936k
  LClosure *c = ci_func(ci);
704
0
  int i;
705
1.05M
  for (i = 0; i < c->nupvalues; i++) {
706
584k
    if (c->upvals[i]->v.p == o) {
707
2.37k
      *name = upvalname(c->p, i);
708
2.37k
      return strupval;
709
2.37k
    }
710
584k
  }
711
465k
  return NULL;
712
936k
}
713
714
715
static const char *formatvarinfo (lua_State *L, const char *kind,
716
1.29M
                                                const char *name) {
717
1.29M
  if (kind == NULL)
718
298k
    return "";  /* no information */
719
998k
  else
720
998k
    return luaO_pushfstring(L, " (%s '%s')", kind, name);
721
1.29M
}
722
723
/*
724
** Build a string with a "description" for the value 'o', such as
725
** "variable 'x'" or "upvalue 'y'".
726
*/
727
603k
static const char *varinfo (lua_State *L, const TValue *o) {
728
603k
  CallInfo *ci = L->ci;
729
603k
  const char *name = NULL;  /* to avoid warnings */
730
603k
  const char *kind = NULL;
731
603k
  if (isLua(ci)) {
732
468k
    kind = getupvalname(ci, o, &name);  /* check whether 'o' is an upvalue */
733
468k
    if (!kind) {  /* not an upvalue? */
734
465k
      int reg = instack(ci, o);  /* try a register */
735
465k
      if (reg >= 0)  /* is 'o' a register? */
736
931k
        kind = getobjname(ci_func(ci)->p, currentpc(ci), reg, &name);
737
465k
    }
738
468k
  }
739
603k
  return formatvarinfo(L, kind, name);
740
603k
}
741
742
743
/*
744
** Raise a type error
745
*/
746
static l_noret typeerror (lua_State *L, const TValue *o, const char *op,
747
1.20M
                          const char *extra) {
748
1.20M
  const char *t = luaT_objtypename(L, o);
749
1.20M
  luaG_runerror(L, "attempt to %s a %s value%s", op, t, extra);
750
1.20M
}
751
752
753
/*
754
** Raise a type error with "standard" information about the faulty
755
** object 'o' (using 'varinfo').
756
*/
757
359k
l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
758
359k
  typeerror(L, o, op, varinfo(L, o));
759
359k
}
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
842k
l_noret luaG_callerror (lua_State *L, const TValue *o) {
768
842k
  CallInfo *ci = L->ci;
769
842k
  const char *name = NULL;  /* to avoid warnings */
770
842k
  const char *kind = funcnamefromcall(L, ci, &name);
771
842k
  const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o);
772
842k
  typeerror(L, o, "call", extra);
773
842k
}
774
775
776
3.46k
l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what) {
777
3.46k
  luaG_runerror(L, "bad 'for' %s (number expected, got %s)",
778
3.46k
                   what, luaT_objtypename(L, o));
779
3.46k
}
780
781
782
5.69k
l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
783
5.69k
  if (ttisstring(p1) || cvt2str(p1)) p1 = p2;
784
5.69k
  luaG_typeerror(L, p1, "concatenate");
785
5.69k
}
786
787
788
l_noret luaG_opinterror (lua_State *L, const TValue *p1,
789
282k
                         const TValue *p2, const char *msg) {
790
282k
  if (!ttisnumber(p1))  /* first operand is wrong? */
791
248k
    p2 = p1;  /* now second is wrong */
792
282k
  luaG_typeerror(L, p2, msg);
793
282k
}
794
795
796
/*
797
** Error when both values are convertible to numbers, but not to integers
798
*/
799
94.9k
l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
800
94.9k
  lua_Integer temp;
801
94.9k
  if (!luaV_tointegerns(p1, &temp, LUA_FLOORN2I))
802
49.7k
    p2 = p1;
803
94.9k
  luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
804
94.9k
}
805
806
807
70.2k
l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
808
70.2k
  const char *t1 = luaT_objtypename(L, p1);
809
70.2k
  const char *t2 = luaT_objtypename(L, p2);
810
70.2k
  if (strcmp(t1, t2) == 0)
811
6.48k
    luaG_runerror(L, "attempt to compare two %s values", t1);
812
63.7k
  else
813
63.7k
    luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
814
70.2k
}
815
816
817
/* add src:line information to 'msg' */
818
const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
819
7.11M
                                        int line) {
820
7.11M
  if (src == NULL)  /* no debug information? */
821
3
    return luaO_pushfstring(L, "?:?: %s", msg);
822
7.11M
  else {
823
7.11M
    char buff[LUA_IDSIZE];
824
7.11M
    size_t idlen;
825
7.11M
    const char *id = getlstr(src, idlen);
826
7.11M
    luaO_chunkid(buff, id, idlen);
827
7.11M
    return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
828
7.11M
  }
829
7.11M
}
830
831
832
4.04M
l_noret luaG_errormsg (lua_State *L) {
833
4.04M
  if (L->errfunc != 0) {  /* is there an error handling function? */
834
2.95M
    StkId errfunc = restorestack(L, L->errfunc);
835
2.95M
    lua_assert(ttisfunction(s2v(errfunc)));
836
5.90M
    setobjs2s(L, L->top.p, L->top.p - 1);  /* move argument */
837
2.95M
    setobjs2s(L, L->top.p - 1, errfunc);  /* push function */
838
2.95M
    L->top.p++;  /* assume EXTRA_STACK */
839
2.95M
    luaD_callnoyield(L, L->top.p - 2, 1);  /* call it */
840
2.95M
  }
841
4.04M
  if (ttisnil(s2v(L->top.p - 1))) {  /* error object is nil? */
842
    /* change it to a proper message */
843
71.3k
    setsvalue2s(L, L->top.p - 1, luaS_newliteral(L, "<no error object>"));
844
71.3k
  }
845
4.04M
  luaD_throw(L, LUA_ERRRUN);
846
4.04M
}
847
848
849
1.44M
l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
850
1.44M
  CallInfo *ci = L->ci;
851
1.44M
  const char *msg;
852
1.44M
  va_list argp;
853
1.44M
  luaC_checkGC(L);  /* error message uses memory */
854
1.44M
  pushvfstring(L, argp, fmt, msg);
855
1.44M
  if (isLua(ci)) {  /* Lua function? */
856
    /* add source:line information */
857
2.50M
    luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci));
858
1.25M
    setobjs2s(L, L->top.p - 2, L->top.p - 1);  /* remove 'msg' */
859
1.25M
    L->top.p--;
860
1.25M
  }
861
1.44M
  luaG_errormsg(L);
862
1.44M
}
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
890M
static int changedline (const Proto *p, int oldpc, int newpc) {
874
890M
  if (p->lineinfo == NULL)  /* no debug information? */
875
0
    return 0;
876
890M
  if (newpc - oldpc < MAXIWTHABS / 2) {  /* not too far apart? */
877
890M
    int delta = 0;  /* line difference */
878
890M
    int pc = oldpc;
879
1.54G
    for (;;) {
880
1.54G
      int lineinfo = p->lineinfo[++pc];
881
1.54G
      if (lineinfo == ABSLINEINFO)
882
829k
        break;  /* cannot compute delta; fall through */
883
1.54G
      delta += lineinfo;
884
1.54G
      if (pc == newpc)
885
889M
        return (delta != 0);  /* delta computed successfully */
886
1.54G
    }
887
890M
  }
888
  /* either instructions are too far apart or there is an absolute line
889
     info in the way; compute line difference explicitly */
890
869k
  return (luaG_getfuncline(p, oldpc) != luaG_getfuncline(p, newpc));
891
890M
}
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
174M
int luaG_tracecall (lua_State *L) {
903
174M
  CallInfo *ci = L->ci;
904
349M
  Proto *p = ci_func(ci)->p;
905
0
  ci->u.l.trap = 1;  /* ensure hooks will be checked */
906
349M
  if (ci->u.l.savedpc == p->code) {  /* first instruction (not resuming)? */
907
147M
    if (p->flag & PF_ISVARARG)
908
7.28M
      return 0;  /* hooks will start at VARARGPREP instruction */
909
140M
    else if (!(ci->callstatus & CIST_HOOKYIELD))  /* not yielded? */
910
140M
      luaD_hookcall(L, ci);  /* check 'call' hook */
911
147M
  }
912
167M
  return 1;  /* keep 'trap' on */
913
349M
}
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
1.05G
int luaG_traceexec (lua_State *L, const Instruction *pc) {
929
1.05G
  CallInfo *ci = L->ci;
930
1.05G
  lu_byte mask = cast_byte(L->hookmask);
931
2.10G
  const Proto *p = ci_func(ci)->p;
932
0
  int counthook;
933
2.10G
  if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) {  /* no hooks? */
934
2.16M
    ci->u.l.trap = 0;  /* don't need to stop again */
935
2.16M
    return 0;  /* turn off 'trap' */
936
2.16M
  }
937
1.05G
  pc++;  /* reference is always next instruction */
938
1.05G
  ci->u.l.savedpc = pc;  /* save 'pc' */
939
1.05G
  counthook = (mask & LUA_MASKCOUNT) && (--L->hookcount == 0);
940
1.05G
  if (counthook)
941
45.0M
    resethookcount(L);  /* reset count */
942
1.00G
  else if (!(mask & LUA_MASKLINE))
943
2.61M
    return 1;  /* no line hook and count != 0; nothing to be done now */
944
1.04G
  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
1.04G
  if (!luaP_isIT(*(ci->u.l.savedpc - 1)))  /* top not being used? */
949
1.04G
    L->top.p = ci->top.p;  /* correct top */
950
1.04G
  if (counthook)
951
45.0M
    luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0);  /* call count hook */
952
1.04G
  if (mask & LUA_MASKLINE) {
953
    /* 'L->oldpc' may be invalid; use zero in this case */
954
1.04G
    int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0;
955
1.04G
    int npci = pcRel(pc, p);
956
1.04G
    if (npci <= oldpc ||  /* call hook when jump back (loop), */
957
1.04G
        changedline(p, oldpc, npci)) {  /* or when enter new line */
958
280M
      int newline = luaG_getfuncline(p, npci);
959
280M
      luaD_hook(L, LUA_HOOKLINE, newline, 0, 0);  /* call line hook */
960
280M
    }
961
1.04G
    L->oldpc = npci;  /* 'pc' of last call to line hook */
962
1.04G
  }
963
1.04G
  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
1.04G
  return 1;  /* keep 'trap' on */
970
1.04G
}
971