Coverage Report

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