Coverage Report

Created: 2023-09-15 06:20

/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
9.62k
#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
2.73k
static int currentpc (CallInfo *ci) {
42
2.73k
  lua_assert(isLua(ci));
43
2.73k
  return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
44
2.73k
}
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
1.51k
static int getbaseline (const Proto *f, int pc, int *basepc) {
61
1.51k
  if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) {
62
726
    *basepc = -1;  /* start from the beginning */
63
726
    return f->linedefined;
64
726
  }
65
790
  else {
66
790
    int i = cast_uint(pc) / MAXIWTHABS - 1;  /* get an estimate */
67
    /* estimate must be a lower bound of the correct base */
68
790
    lua_assert(i < 0 ||
69
790
              (i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc));
70
1.17k
    while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc)
71
384
      i++;  /* low estimate; adjust it */
72
790
    *basepc = f->abslineinfo[i].pc;
73
790
    return f->abslineinfo[i].line;
74
790
  }
75
1.51k
}
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
1.51k
int luaG_getfuncline (const Proto *f, int pc) {
84
1.51k
  if (f->lineinfo == NULL)  /* no debug information? */
85
0
    return -1;
86
1.51k
  else {
87
1.51k
    int basepc;
88
1.51k
    int baseline = getbaseline(f, pc, &basepc);
89
68.8k
    while (basepc++ < pc) {  /* walk until given instruction */
90
67.3k
      lua_assert(f->lineinfo[basepc] != ABSLINEINFO);
91
67.3k
      baseline += f->lineinfo[basepc];  /* correct line */
92
67.3k
    }
93
1.51k
    return baseline;
94
1.51k
  }
95
1.51k
}
96
97
98
1.51k
static int getcurrentline (CallInfo *ci) {
99
1.51k
  return luaG_getfuncline(ci_func(ci)->p, currentpc(ci));
100
1.51k
}
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
1.00k
static void settraps (CallInfo *ci) {
115
2.01k
  for (; ci != NULL; ci = ci->previous)
116
1.00k
    if (isLua(ci))
117
0
      ci->u.l.trap = 1;
118
1.00k
}
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
1.00k
LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
132
1.00k
  if (func == NULL || mask == 0) {  /* turn off hooks? */
133
0
    mask = 0;
134
0
    func = NULL;
135
0
  }
136
1.00k
  L->hook = func;
137
1.00k
  L->basehookcount = count;
138
1.00k
  resethookcount(L);
139
1.00k
  L->hookmask = cast_byte(mask);
140
1.00k
  if (mask)
141
1.00k
    settraps(L->ci);  /* to trace inside 'luaV_execute' */
142
1.00k
}
143
144
145
1
LUA_API lua_Hook lua_gethook (lua_State *L) {
146
1
  return L->hook;
147
1
}
148
149
150
3
LUA_API int lua_gethookmask (lua_State *L) {
151
3
  return L->hookmask;
152
3
}
153
154
155
2
LUA_API int lua_gethookcount (lua_State *L) {
156
2
  return L->basehookcount;
157
2
}
158
159
160
19.9k
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
161
19.9k
  int status;
162
19.9k
  CallInfo *ci;
163
19.9k
  if (level < 0) return 0;  /* invalid (negative) level */
164
19.9k
  lua_lock(L);
165
29.8k
  for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
166
9.96k
    level--;
167
19.9k
  if (level == 0 && ci != &L->base_ci) {  /* level found? */
168
19.0k
    status = 1;
169
19.0k
    ar->i_ci = ci;
170
19.0k
  }
171
823
  else status = 0;  /* no such level */
172
19.9k
  lua_unlock(L);
173
19.9k
  return status;
174
19.9k
}
175
176
177
484
static const char *upvalname (const Proto *p, int uv) {
178
484
  TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
179
484
  if (s == NULL) return "?";
180
484
  else return getstr(s);
181
484
}
182
183
184
0
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
185
0
  if (clLvalue(s2v(ci->func.p))->p->is_vararg) {
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
6
const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) {
197
6
  StkId base = ci->func.p + 1;
198
6
  const char *name = NULL;
199
6
  if (isLua(ci)) {
200
6
    if (n < 0)  /* access to vararg values? */
201
0
      return findvararg(ci, n, pos);
202
6
    else
203
6
      name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
204
6
  }
205
6
  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
6
  if (pos)
215
0
    *pos = base + (n - 1);
216
6
  return name;
217
6
}
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
    setobjs2s(L, pos, L->top.p - 1);
249
0
    L->top.p--;  /* pop value */
250
0
  }
251
0
  lua_unlock(L);
252
0
  return name;
253
0
}
254
255
256
9.62k
static void funcinfo (lua_Debug *ar, Closure *cl) {
257
9.62k
  if (noLuaClosure(cl)) {
258
9.23k
    ar->source = "=[C]";
259
9.23k
    ar->srclen = LL("=[C]");
260
9.23k
    ar->linedefined = -1;
261
9.23k
    ar->lastlinedefined = -1;
262
9.23k
    ar->what = "C";
263
9.23k
  }
264
386
  else {
265
386
    const Proto *p = cl->l.p;
266
386
    if (p->source) {
267
386
      ar->source = getstr(p->source);
268
386
      ar->srclen = tsslen(p->source);
269
386
    }
270
0
    else {
271
0
      ar->source = "=?";
272
0
      ar->srclen = LL("=?");
273
0
    }
274
386
    ar->linedefined = p->linedefined;
275
386
    ar->lastlinedefined = p->lastlinedefined;
276
386
    ar->what = (ar->linedefined == 0) ? "main" : "Lua";
277
386
  }
278
9.62k
  luaO_chunkid(ar->short_src, ar->source, ar->srclen);
279
9.62k
}
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
0
static void collectvalidlines (lua_State *L, Closure *f) {
291
0
  if (noLuaClosure(f)) {
292
0
    setnilvalue(s2v(L->top.p));
293
0
    api_incr_top(L);
294
0
  }
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->is_vararg)  /* 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
0
}
317
318
319
9.39k
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
320
  /* calling function is a known function? */
321
9.39k
  if (ci != NULL && !(ci->callstatus & CIST_TAIL))
322
9.39k
    return funcnamefromcall(L, ci->previous, name);
323
0
  else return NULL;  /* no way to find a name */
324
9.39k
}
325
326
327
static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
328
28.2k
                       Closure *f, CallInfo *ci) {
329
28.2k
  int status = 1;
330
66.2k
  for (; *what; what++) {
331
38.0k
    switch (*what) {
332
9.62k
      case 'S': {
333
9.62k
        funcinfo(ar, f);
334
9.62k
        break;
335
0
      }
336
9.62k
      case 'l': {
337
9.62k
        ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1;
338
9.62k
        break;
339
0
      }
340
0
      case 'u': {
341
0
        ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
342
0
        if (noLuaClosure(f)) {
343
0
          ar->isvararg = 1;
344
0
          ar->nparams = 0;
345
0
        }
346
0
        else {
347
0
          ar->isvararg = f->l.p->is_vararg;
348
0
          ar->nparams = f->l.p->numparams;
349
0
        }
350
0
        break;
351
0
      }
352
56
      case 't': {
353
56
        ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
354
56
        break;
355
0
      }
356
9.39k
      case 'n': {
357
9.39k
        ar->namewhat = getfuncname(L, ci, &ar->name);
358
9.39k
        if (ar->namewhat == NULL) {
359
9.30k
          ar->namewhat = "";  /* not found */
360
9.30k
          ar->name = NULL;
361
9.30k
        }
362
9.39k
        break;
363
0
      }
364
0
      case 'r': {
365
0
        if (ci == NULL || !(ci->callstatus & CIST_TRAN))
366
0
          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
0
        break;
372
0
      }
373
0
      case 'L':
374
9.30k
      case 'f':  /* handled by lua_getinfo */
375
9.30k
        break;
376
0
      default: status = 0;  /* invalid option */
377
38.0k
    }
378
38.0k
  }
379
28.2k
  return status;
380
28.2k
}
381
382
383
28.2k
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
384
28.2k
  int status;
385
28.2k
  Closure *cl;
386
28.2k
  CallInfo *ci;
387
28.2k
  TValue *func;
388
28.2k
  lua_lock(L);
389
28.2k
  if (*what == '>') {
390
1
    ci = NULL;
391
1
    func = s2v(L->top.p - 1);
392
1
    api_check(L, ttisfunction(func), "function expected");
393
1
    what++;  /* skip the '>' */
394
1
    L->top.p--;  /* pop function */
395
1
  }
396
28.2k
  else {
397
28.2k
    ci = ar->i_ci;
398
28.2k
    func = s2v(ci->func.p);
399
28.2k
    lua_assert(ttisfunction(func));
400
28.2k
  }
401
28.2k
  cl = ttisclosure(func) ? clvalue(func) : NULL;
402
28.2k
  status = auxgetinfo(L, what, ar, cl, ci);
403
28.2k
  if (strchr(what, 'f')) {
404
9.30k
    setobj2s(L, L->top.p, func);
405
9.30k
    api_incr_top(L);
406
9.30k
  }
407
28.2k
  if (strchr(what, 'L'))
408
0
    collectvalidlines(L, cl);
409
28.2k
  lua_unlock(L);
410
28.2k
  return status;
411
28.2k
}
412
413
414
/*
415
** {======================================================
416
** Symbolic Execution
417
** =======================================================
418
*/
419
420
static const char *getobjname (const Proto *p, int lastpc, int reg,
421
                               const char **name);
422
423
424
/*
425
** Find a "name" for the constant 'c'.
426
*/
427
75.0k
static void kname (const Proto *p, int c, const char **name) {
428
75.0k
  TValue *kvalue = &p->k[c];
429
75.0k
  *name = (ttisstring(kvalue)) ? getstr(tsvalue(kvalue)) : "?";
430
75.0k
}
431
432
433
/*
434
** Find a "name" for the register 'c'.
435
*/
436
58
static void rname (const Proto *p, int pc, int c, const char **name) {
437
58
  const char *what = getobjname(p, pc, c, name); /* search for 'c' */
438
58
  if (!(what && *what == 'c'))  /* did not find a constant name? */
439
40
    *name = "?";
440
58
}
441
442
443
/*
444
** Find a "name" for a 'C' value in an RK instruction.
445
*/
446
71
static void rkname (const Proto *p, int pc, Instruction i, const char **name) {
447
71
  int c = GETARG_C(i);  /* key index */
448
71
  if (GETARG_k(i))  /* is 'c' a constant? */
449
70
    kname(p, c, name);
450
1
  else  /* 'c' is a register */
451
1
    rname(p, pc, c, name);
452
71
}
453
454
455
157M
static int filterpc (int pc, int jmptarget) {
456
157M
  if (pc < jmptarget)  /* is code conditional (inside a jump)? */
457
248k
    return -1;  /* cannot know who sets that register */
458
157M
  else return pc;  /* current position sets that register */
459
157M
}
460
461
462
/*
463
** Try to find last instruction before 'lastpc' that modified register 'reg'.
464
*/
465
75.6k
static int findsetreg (const Proto *p, int lastpc, int reg) {
466
75.6k
  int pc;
467
75.6k
  int setreg = -1;  /* keep last instruction that changed 'reg' */
468
75.6k
  int jmptarget = 0;  /* any code before this address is conditional */
469
75.6k
  if (testMMMode(GET_OPCODE(p->code[lastpc])))
470
187
    lastpc--;  /* previous instruction was not actually executed */
471
159M
  for (pc = 0; pc < lastpc; pc++) {
472
159M
    Instruction i = p->code[pc];
473
159M
    OpCode op = GET_OPCODE(i);
474
159M
    int a = GETARG_A(i);
475
159M
    int change;  /* true if current instruction changed 'reg' */
476
159M
    switch (op) {
477
10.4k
      case OP_LOADNIL: {  /* set registers from 'a' to 'a+b' */
478
10.4k
        int b = GETARG_B(i);
479
10.4k
        change = (a <= reg && reg <= a + b);
480
10.4k
        break;
481
0
      }
482
386
      case OP_TFORCALL: {  /* affect all regs above its base */
483
386
        change = (reg >= a + 2);
484
386
        break;
485
0
      }
486
226k
      case OP_CALL:
487
226k
      case OP_TAILCALL: {  /* affect all registers above base */
488
226k
        change = (reg >= a);
489
226k
        break;
490
226k
      }
491
211k
      case OP_JMP: {  /* doesn't change registers, but changes 'jmptarget' */
492
211k
        int b = GETARG_sJ(i);
493
211k
        int dest = pc + 1 + b;
494
        /* jump does not skip 'lastpc' and is larger than current one? */
495
211k
        if (dest <= lastpc && dest > jmptarget)
496
31.3k
          jmptarget = dest;  /* update 'jmptarget' */
497
211k
        change = 0;
498
211k
        break;
499
226k
      }
500
159M
      default:  /* any instruction that sets A */
501
159M
        change = (testAMode(op) && reg == a);
502
159M
        break;
503
159M
    }
504
159M
    if (change)
505
157M
      setreg = filterpc(pc, jmptarget);
506
159M
  }
507
75.6k
  return setreg;
508
75.6k
}
509
510
511
/*
512
** Check whether table being indexed by instruction 'i' is the
513
** environment '_ENV'
514
*/
515
75.0k
static const char *gxf (const Proto *p, int pc, Instruction i, int isup) {
516
75.0k
  int t = GETARG_B(i);  /* table index */
517
75.0k
  const char *name;  /* name of indexed variable */
518
75.0k
  if (isup)  /* is an upvalue? */
519
456
    name = upvalname(p, t);
520
74.5k
  else
521
74.5k
    getobjname(p, pc, t, &name);
522
75.0k
  return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field";
523
75.0k
}
524
525
526
static const char *getobjname (const Proto *p, int lastpc, int reg,
527
75.7k
                               const char **name) {
528
75.7k
  int pc;
529
75.7k
  *name = luaF_getlocalname(p, reg + 1, lastpc);
530
75.7k
  if (*name)  /* is a local? */
531
73
    return "local";
532
  /* else try symbolic execution */
533
75.6k
  pc = findsetreg(p, lastpc, reg);
534
75.6k
  if (pc != -1) {  /* could find instruction? */
535
75.6k
    Instruction i = p->code[pc];
536
75.6k
    OpCode op = GET_OPCODE(i);
537
75.6k
    switch (op) {
538
16
      case OP_MOVE: {
539
16
        int b = GETARG_B(i);  /* move from 'b' to 'a' */
540
16
        if (b < GETARG_A(i))
541
16
          return getobjname(p, pc, b, name);  /* get name for 'b' */
542
0
        break;
543
16
      }
544
456
      case OP_GETTABUP: {
545
456
        int k = GETARG_C(i);  /* key index */
546
456
        kname(p, k, name);
547
456
        return gxf(p, pc, i, 1);
548
16
      }
549
57
      case OP_GETTABLE: {
550
57
        int k = GETARG_C(i);  /* key index */
551
57
        rname(p, pc, k, name);
552
57
        return gxf(p, pc, i, 0);
553
16
      }
554
1
      case OP_GETI: {
555
1
        *name = "integer index";
556
1
        return "field";
557
16
      }
558
74.5k
      case OP_GETFIELD: {
559
74.5k
        int k = GETARG_C(i);  /* key index */
560
74.5k
        kname(p, k, name);
561
74.5k
        return gxf(p, pc, i, 0);
562
16
      }
563
22
      case OP_GETUPVAL: {
564
22
        *name = upvalname(p, GETARG_B(i));
565
22
        return "upvalue";
566
16
      }
567
82
      case OP_LOADK:
568
82
      case OP_LOADKX: {
569
82
        int b = (op == OP_LOADK) ? GETARG_Bx(i)
570
82
                                 : GETARG_Ax(p->code[pc + 1]);
571
82
        if (ttisstring(&p->k[b])) {
572
72
          *name = getstr(tsvalue(&p->k[b]));
573
72
          return "constant";
574
72
        }
575
10
        break;
576
82
      }
577
71
      case OP_SELF: {
578
71
        rkname(p, pc, i, name);
579
71
        return "method";
580
82
      }
581
378
      default: break;  /* go through to return NULL */
582
75.6k
    }
583
75.6k
  }
584
410
  return NULL;  /* could not find reasonable name */
585
75.6k
}
586
587
588
/*
589
** Try to find a name for a function based on the code that called it.
590
** (Only works when function was called by a Lua function.)
591
** Returns what the name is (e.g., "for iterator", "method",
592
** "metamethod") and sets '*name' to point to the name.
593
*/
594
static const char *funcnamefromcode (lua_State *L, const Proto *p,
595
672
                                     int pc, const char **name) {
596
672
  TMS tm = (TMS)0;  /* (initial value avoids warnings) */
597
672
  Instruction i = p->code[pc];  /* calling instruction */
598
672
  switch (GET_OPCODE(i)) {
599
506
    case OP_CALL:
600
508
    case OP_TAILCALL:
601
508
      return getobjname(p, pc, GETARG_A(i), name);  /* get function name */
602
164
    case OP_TFORCALL: {  /* for iterator */
603
164
      *name = "for iterator";
604
164
       return "for iterator";
605
506
    }
606
    /* other instructions can do calls through metamethods */
607
0
    case OP_SELF: case OP_GETTABUP: case OP_GETTABLE:
608
0
    case OP_GETI: case OP_GETFIELD:
609
0
      tm = TM_INDEX;
610
0
      break;
611
0
    case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD:
612
0
      tm = TM_NEWINDEX;
613
0
      break;
614
0
    case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
615
0
      tm = cast(TMS, GETARG_C(i));
616
0
      break;
617
0
    }
618
0
    case OP_UNM: tm = TM_UNM; break;
619
0
    case OP_BNOT: tm = TM_BNOT; break;
620
0
    case OP_LEN: tm = TM_LEN; break;
621
0
    case OP_CONCAT: tm = TM_CONCAT; break;
622
0
    case OP_EQ: tm = TM_EQ; break;
623
    /* no cases for OP_EQI and OP_EQK, as they don't call metamethods */
624
0
    case OP_LT: case OP_LTI: case OP_GTI: tm = TM_LT; break;
625
0
    case OP_LE: case OP_LEI: case OP_GEI: tm = TM_LE; break;
626
0
    case OP_CLOSE: case OP_RETURN: tm = TM_CLOSE; break;
627
0
    default:
628
0
      return NULL;  /* cannot find a reasonable name */
629
672
  }
630
0
  *name = getshrstr(G(L)->tmname[tm]) + 2;
631
0
  return "metamethod";
632
672
}
633
634
635
/*
636
** Try to find a name for a function based on how it was called.
637
*/
638
static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
639
15.4k
                                                   const char **name) {
640
15.4k
  if (ci->callstatus & CIST_HOOKED) {  /* was it called inside a hook? */
641
0
    *name = "?";
642
0
    return "hook";
643
0
  }
644
15.4k
  else if (ci->callstatus & CIST_FIN) {  /* was it called as a finalizer? */
645
0
    *name = "__gc";
646
0
    return "metamethod";  /* report it as such */
647
0
  }
648
15.4k
  else if (isLua(ci))
649
672
    return funcnamefromcode(L, ci_func(ci)->p, currentpc(ci), name);
650
14.7k
  else
651
14.7k
    return NULL;
652
15.4k
}
653
654
/* }====================================================== */
655
656
657
658
/*
659
** Check whether pointer 'o' points to some value in the stack frame of
660
** the current function and, if so, returns its index.  Because 'o' may
661
** not point to a value in this stack, we cannot compare it with the
662
** region boundaries (undefined behavior in ISO C).
663
*/
664
539
static int instack (CallInfo *ci, const TValue *o) {
665
539
  int pos;
666
539
  StkId base = ci->func.p + 1;
667
2.23k
  for (pos = 0; base + pos < ci->top.p; pos++) {
668
2.23k
    if (o == s2v(base + pos))
669
539
      return pos;
670
2.23k
  }
671
0
  return -1;  /* not found */
672
539
}
673
674
675
/*
676
** Checks whether value 'o' came from an upvalue. (That can only happen
677
** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on
678
** upvalues.)
679
*/
680
static const char *getupvalname (CallInfo *ci, const TValue *o,
681
545
                                 const char **name) {
682
545
  LClosure *c = ci_func(ci);
683
545
  int i;
684
1.08k
  for (i = 0; i < c->nupvalues; i++) {
685
547
    if (c->upvals[i]->v.p == o) {
686
6
      *name = upvalname(c->p, i);
687
6
      return "upvalue";
688
6
    }
689
547
  }
690
539
  return NULL;
691
545
}
692
693
694
static const char *formatvarinfo (lua_State *L, const char *kind,
695
6.50k
                                                const char *name) {
696
6.50k
  if (kind == NULL)
697
5.76k
    return "";  /* no information */
698
739
  else
699
739
    return luaO_pushfstring(L, " (%s '%s')", kind, name);
700
6.50k
}
701
702
/*
703
** Build a string with a "description" for the value 'o', such as
704
** "variable 'x'" or "upvalue 'y'".
705
*/
706
6.04k
static const char *varinfo (lua_State *L, const TValue *o) {
707
6.04k
  CallInfo *ci = L->ci;
708
6.04k
  const char *name = NULL;  /* to avoid warnings */
709
6.04k
  const char *kind = NULL;
710
6.04k
  if (isLua(ci)) {
711
545
    kind = getupvalname(ci, o, &name);  /* check whether 'o' is an upvalue */
712
545
    if (!kind) {  /* not an upvalue? */
713
539
      int reg = instack(ci, o);  /* try a register */
714
539
      if (reg >= 0)  /* is 'o' a register? */
715
539
        kind = getobjname(ci_func(ci)->p, currentpc(ci), reg, &name);
716
539
    }
717
545
  }
718
6.04k
  return formatvarinfo(L, kind, name);
719
6.04k
}
720
721
722
/*
723
** Raise a type error
724
*/
725
static l_noret typeerror (lua_State *L, const TValue *o, const char *op,
726
6.48k
                          const char *extra) {
727
6.48k
  const char *t = luaT_objtypename(L, o);
728
6.48k
  luaG_runerror(L, "attempt to %s a %s value%s", op, t, extra);
729
6.48k
}
730
731
732
/*
733
** Raise a type error with "standard" information about the faulty
734
** object 'o' (using 'varinfo').
735
*/
736
414
l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
737
414
  typeerror(L, o, op, varinfo(L, o));
738
414
}
739
740
741
/*
742
** Raise an error for calling a non-callable object. Try to find a name
743
** for the object based on how it was called ('funcnamefromcall'); if it
744
** cannot get a name there, try 'varinfo'.
745
*/
746
6.06k
l_noret luaG_callerror (lua_State *L, const TValue *o) {
747
6.06k
  CallInfo *ci = L->ci;
748
6.06k
  const char *name = NULL;  /* to avoid warnings */
749
6.06k
  const char *kind = funcnamefromcall(L, ci, &name);
750
6.06k
  const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o);
751
6.06k
  typeerror(L, o, "call", extra);
752
6.06k
}
753
754
755
49
l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what) {
756
49
  luaG_runerror(L, "bad 'for' %s (number expected, got %s)",
757
49
                   what, luaT_objtypename(L, o));
758
49
}
759
760
761
10
l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
762
10
  if (ttisstring(p1) || cvt2str(p1)) p1 = p2;
763
10
  luaG_typeerror(L, p1, "concatenate");
764
10
}
765
766
767
l_noret luaG_opinterror (lua_State *L, const TValue *p1,
768
206
                         const TValue *p2, const char *msg) {
769
206
  if (!ttisnumber(p1))  /* first operand is wrong? */
770
117
    p2 = p1;  /* now second is wrong */
771
206
  luaG_typeerror(L, p2, msg);
772
206
}
773
774
775
/*
776
** Error when both values are convertible to numbers, but not to integers
777
*/
778
19
l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
779
19
  lua_Integer temp;
780
19
  if (!luaV_tointegerns(p1, &temp, LUA_FLOORN2I))
781
12
    p2 = p1;
782
19
  luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
783
19
}
784
785
786
56
l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
787
56
  const char *t1 = luaT_objtypename(L, p1);
788
56
  const char *t2 = luaT_objtypename(L, p2);
789
56
  if (strcmp(t1, t2) == 0)
790
6
    luaG_runerror(L, "attempt to compare two %s values", t1);
791
50
  else
792
50
    luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
793
56
}
794
795
796
/* add src:line information to 'msg' */
797
const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
798
161k
                                        int line) {
799
161k
  char buff[LUA_IDSIZE];
800
161k
  if (src)
801
161k
    luaO_chunkid(buff, getstr(src), tsslen(src));
802
0
  else {  /* no source available; use "?" instead */
803
0
    buff[0] = '?'; buff[1] = '\0';
804
0
  }
805
161k
  return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
806
161k
}
807
808
809
16.6k
l_noret luaG_errormsg (lua_State *L) {
810
16.6k
  if (L->errfunc != 0) {  /* is there an error handling function? */
811
56
    StkId errfunc = restorestack(L, L->errfunc);
812
56
    lua_assert(ttisfunction(s2v(errfunc)));
813
56
    setobjs2s(L, L->top.p, L->top.p - 1);  /* move argument */
814
56
    setobjs2s(L, L->top.p - 1, errfunc);  /* push function */
815
56
    L->top.p++;  /* assume EXTRA_STACK */
816
56
    luaD_callnoyield(L, L->top.p - 2, 1);  /* call it */
817
56
  }
818
16.6k
  luaD_throw(L, LUA_ERRRUN);
819
16.6k
}
820
821
822
7.12k
l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
823
7.12k
  CallInfo *ci = L->ci;
824
7.12k
  const char *msg;
825
7.12k
  va_list argp;
826
7.12k
  luaC_checkGC(L);  /* error message uses memory */
827
7.12k
  va_start(argp, fmt);
828
7.12k
  msg = luaO_pushvfstring(L, fmt, argp);  /* format message */
829
7.12k
  va_end(argp);
830
7.12k
  if (isLua(ci)) {  /* if Lua function, add source:line information */
831
1.13k
    luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci));
832
1.13k
    setobjs2s(L, L->top.p - 2, L->top.p - 1);  /* remove 'msg' */
833
1.13k
    L->top.p--;
834
1.13k
  }
835
7.12k
  luaG_errormsg(L);
836
7.12k
}
837
838
839
/*
840
** Check whether new instruction 'newpc' is in a different line from
841
** previous instruction 'oldpc'. More often than not, 'newpc' is only
842
** one or a few instructions after 'oldpc' (it must be after, see
843
** caller), so try to avoid calling 'luaG_getfuncline'. If they are
844
** too far apart, there is a good chance of a ABSLINEINFO in the way,
845
** so it goes directly to 'luaG_getfuncline'.
846
*/
847
0
static int changedline (const Proto *p, int oldpc, int newpc) {
848
0
  if (p->lineinfo == NULL)  /* no debug information? */
849
0
    return 0;
850
0
  if (newpc - oldpc < MAXIWTHABS / 2) {  /* not too far apart? */
851
0
    int delta = 0;  /* line difference */
852
0
    int pc = oldpc;
853
0
    for (;;) {
854
0
      int lineinfo = p->lineinfo[++pc];
855
0
      if (lineinfo == ABSLINEINFO)
856
0
        break;  /* cannot compute delta; fall through */
857
0
      delta += lineinfo;
858
0
      if (pc == newpc)
859
0
        return (delta != 0);  /* delta computed successfully */
860
0
    }
861
0
  }
862
  /* either instructions are too far apart or there is an absolute line
863
     info in the way; compute line difference explicitly */
864
0
  return (luaG_getfuncline(p, oldpc) != luaG_getfuncline(p, newpc));
865
0
}
866
867
868
/*
869
** Traces Lua calls. If code is running the first instruction of a function,
870
** and function is not vararg, and it is not coming from an yield,
871
** calls 'luaD_hookcall'. (Vararg functions will call 'luaD_hookcall'
872
** after adjusting its variable arguments; otherwise, they could call
873
** a line/count hook before the call hook. Functions coming from
874
** an yield already called 'luaD_hookcall' before yielding.)
875
*/
876
29
int luaG_tracecall (lua_State *L) {
877
29
  CallInfo *ci = L->ci;
878
29
  Proto *p = ci_func(ci)->p;
879
29
  ci->u.l.trap = 1;  /* ensure hooks will be checked */
880
29
  if (ci->u.l.savedpc == p->code) {  /* first instruction (not resuming)? */
881
0
    if (p->is_vararg)
882
0
      return 0;  /* hooks will start at VARARGPREP instruction */
883
0
    else if (!(ci->callstatus & CIST_HOOKYIELD))  /* not yieded? */
884
0
      luaD_hookcall(L, ci);  /* check 'call' hook */
885
0
  }
886
29
  return 1;  /* keep 'trap' on */
887
29
}
888
889
890
/*
891
** Traces the execution of a Lua function. Called before the execution
892
** of each opcode, when debug is on. 'L->oldpc' stores the last
893
** instruction traced, to detect line changes. When entering a new
894
** function, 'npci' will be zero and will test as a new line whatever
895
** the value of 'oldpc'.  Some exceptional conditions may return to
896
** a function without setting 'oldpc'. In that case, 'oldpc' may be
897
** invalid; if so, use zero as a valid value. (A wrong but valid 'oldpc'
898
** at most causes an extra call to a line hook.)
899
** This function is not "Protected" when called, so it should correct
900
** 'L->top.p' before calling anything that can run the GC.
901
*/
902
5.46k
int luaG_traceexec (lua_State *L, const Instruction *pc) {
903
5.46k
  CallInfo *ci = L->ci;
904
5.46k
  lu_byte mask = L->hookmask;
905
5.46k
  const Proto *p = ci_func(ci)->p;
906
5.46k
  int counthook;
907
5.46k
  if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) {  /* no hooks? */
908
5.46k
    ci->u.l.trap = 0;  /* don't need to stop again */
909
5.46k
    return 0;  /* turn off 'trap' */
910
5.46k
  }
911
0
  pc++;  /* reference is always next instruction */
912
0
  ci->u.l.savedpc = pc;  /* save 'pc' */
913
0
  counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT));
914
0
  if (counthook)
915
0
    resethookcount(L);  /* reset count */
916
0
  else if (!(mask & LUA_MASKLINE))
917
0
    return 1;  /* no line hook and count != 0; nothing to be done now */
918
0
  if (ci->callstatus & CIST_HOOKYIELD) {  /* called hook last time? */
919
0
    ci->callstatus &= ~CIST_HOOKYIELD;  /* erase mark */
920
0
    return 1;  /* do not call hook again (VM yielded, so it did not move) */
921
0
  }
922
0
  if (!isIT(*(ci->u.l.savedpc - 1)))  /* top not being used? */
923
0
    L->top.p = ci->top.p;  /* correct top */
924
0
  if (counthook)
925
0
    luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0);  /* call count hook */
926
0
  if (mask & LUA_MASKLINE) {
927
    /* 'L->oldpc' may be invalid; use zero in this case */
928
0
    int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0;
929
0
    int npci = pcRel(pc, p);
930
0
    if (npci <= oldpc ||  /* call hook when jump back (loop), */
931
0
        changedline(p, oldpc, npci)) {  /* or when enter new line */
932
0
      int newline = luaG_getfuncline(p, npci);
933
0
      luaD_hook(L, LUA_HOOKLINE, newline, 0, 0);  /* call line hook */
934
0
    }
935
0
    L->oldpc = npci;  /* 'pc' of last call to line hook */
936
0
  }
937
0
  if (L->status == LUA_YIELD) {  /* did hook yield? */
938
0
    if (counthook)
939
0
      L->hookcount = 1;  /* undo decrement to zero */
940
0
    ci->u.l.savedpc--;  /* undo increment (resume will increment it again) */
941
0
    ci->callstatus |= CIST_HOOKYIELD;  /* mark that it yielded */
942
0
    luaD_throw(L, LUA_YIELD);
943
0
  }
944
0
  return 1;  /* keep 'trap' on */
945
0
}
946