Coverage Report

Created: 2024-04-23 06:32

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