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
201
#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
537
static int currentpc (CallInfo *ci) {
42
537
  lua_assert(isLua(ci));
43
537
  return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
44
537
}
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
354
static int getbaseline (const Proto *f, int pc, int *basepc) {
61
354
  if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) {
62
150
    *basepc = -1;  /* start from the beginning */
63
150
    return f->linedefined;
64
150
  }
65
204
  else {
66
204
    int i = cast_uint(pc) / MAXIWTHABS - 1;  /* get an estimate */
67
    /* estimate must be a lower bound of the correct base */
68
204
    lua_assert(i < 0 ||
69
204
              (i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc));
70
260
    while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc)
71
56
      i++;  /* low estimate; adjust it */
72
204
    *basepc = f->abslineinfo[i].pc;
73
204
    return f->abslineinfo[i].line;
74
204
  }
75
354
}
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
354
int luaG_getfuncline (const Proto *f, int pc) {
84
354
  if (f->lineinfo == NULL)  /* no debug information? */
85
0
    return -1;
86
354
  else {
87
354
    int basepc;
88
354
    int baseline = getbaseline(f, pc, &basepc);
89
15.3k
    while (basepc++ < pc) {  /* walk until given instruction */
90
14.9k
      lua_assert(f->lineinfo[basepc] != ABSLINEINFO);
91
14.9k
      baseline += f->lineinfo[basepc];  /* correct line */
92
14.9k
    }
93
354
    return baseline;
94
354
  }
95
354
}
96
97
98
354
static int getcurrentline (CallInfo *ci) {
99
354
  return luaG_getfuncline(ci_func(ci)->p, currentpc(ci));
100
354
}
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
0
static void settraps (CallInfo *ci) {
115
0
  for (; ci != NULL; ci = ci->previous)
116
0
    if (isLua(ci))
117
0
      ci->u.l.trap = 1;
118
0
}
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
0
LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
132
0
  if (func == NULL || mask == 0) {  /* turn off hooks? */
133
0
    mask = 0;
134
0
    func = NULL;
135
0
  }
136
0
  L->hook = func;
137
0
  L->basehookcount = count;
138
0
  resethookcount(L);
139
0
  L->hookmask = cast_byte(mask);
140
0
  if (mask)
141
0
    settraps(L->ci);  /* to trace inside 'luaV_execute' */
142
0
}
143
144
145
0
LUA_API lua_Hook lua_gethook (lua_State *L) {
146
0
  return L->hook;
147
0
}
148
149
150
0
LUA_API int lua_gethookmask (lua_State *L) {
151
0
  return L->hookmask;
152
0
}
153
154
155
0
LUA_API int lua_gethookcount (lua_State *L) {
156
0
  return L->basehookcount;
157
0
}
158
159
160
845
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
161
845
  int status;
162
845
  CallInfo *ci;
163
845
  if (level < 0) return 0;  /* invalid (negative) level */
164
845
  lua_lock(L);
165
25.3k
  for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
166
24.4k
    level--;
167
845
  if (level == 0 && ci != &L->base_ci) {  /* level found? */
168
533
    status = 1;
169
533
    ar->i_ci = ci;
170
533
  }
171
312
  else status = 0;  /* no such level */
172
845
  lua_unlock(L);
173
845
  return status;
174
845
}
175
176
177
74
static const char *upvalname (const Proto *p, int uv) {
178
74
  TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
179
74
  if (s == NULL) return "?";
180
74
  else return getstr(s);
181
74
}
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
0
const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) {
197
0
  StkId base = ci->func.p + 1;
198
0
  const char *name = NULL;
199
0
  if (isLua(ci)) {
200
0
    if (n < 0)  /* access to vararg values? */
201
0
      return findvararg(ci, n, pos);
202
0
    else
203
0
      name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
204
0
  }
205
0
  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
0
  if (pos)
215
0
    *pos = base + (n - 1);
216
0
  return name;
217
0
}
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
201
static void funcinfo (lua_Debug *ar, Closure *cl) {
258
201
  if (noLuaClosure(cl)) {
259
0
    ar->source = "=[C]";
260
0
    ar->srclen = LL("=[C]");
261
0
    ar->linedefined = -1;
262
0
    ar->lastlinedefined = -1;
263
0
    ar->what = "C";
264
0
  }
265
201
  else {
266
201
    const Proto *p = cl->l.p;
267
201
    if (p->source) {
268
201
      ar->source = getlstr(p->source, ar->srclen);
269
201
    }
270
0
    else {
271
0
      ar->source = "=?";
272
0
      ar->srclen = LL("=?");
273
0
    }
274
201
    ar->linedefined = p->linedefined;
275
201
    ar->lastlinedefined = p->lastlinedefined;
276
201
    ar->what = (ar->linedefined == 0) ? "main" : "Lua";
277
201
  }
278
201
  luaO_chunkid(ar->short_src, ar->source, ar->srclen);
279
201
}
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->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
0
}
317
318
319
201
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
320
  /* calling function is a known function? */
321
201
  if (ci != NULL && !(ci->callstatus & CIST_TAIL))
322
201
    return funcnamefromcall(L, ci->previous, name);
323
0
  else return NULL;  /* no way to find a name */
324
201
}
325
326
327
static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
328
402
                       Closure *f, CallInfo *ci) {
329
402
  int status = 1;
330
1.40k
  for (; *what; what++) {
331
1.00k
    switch (*what) {
332
201
      case 'S': {
333
201
        funcinfo(ar, f);
334
201
        break;
335
0
      }
336
201
      case 'l': {
337
201
        ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1;
338
201
        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->flag & PF_ISVARARG;
348
0
          ar->nparams = f->l.p->numparams;
349
0
        }
350
0
        break;
351
0
      }
352
201
      case 't': {
353
201
        ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
354
201
        break;
355
0
      }
356
201
      case 'n': {
357
201
        ar->namewhat = getfuncname(L, ci, &ar->name);
358
201
        if (ar->namewhat == NULL) {
359
153
          ar->namewhat = "";  /* not found */
360
153
          ar->name = NULL;
361
153
        }
362
201
        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
201
      case 'f':  /* handled by lua_getinfo */
375
201
        break;
376
0
      default: status = 0;  /* invalid option */
377
1.00k
    }
378
1.00k
  }
379
402
  return status;
380
402
}
381
382
383
402
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
384
402
  int status;
385
402
  Closure *cl;
386
402
  CallInfo *ci;
387
402
  TValue *func;
388
402
  lua_lock(L);
389
402
  if (*what == '>') {
390
0
    ci = NULL;
391
0
    func = s2v(L->top.p - 1);
392
0
    api_check(L, ttisfunction(func), "function expected");
393
0
    what++;  /* skip the '>' */
394
0
    L->top.p--;  /* pop function */
395
0
  }
396
402
  else {
397
402
    ci = ar->i_ci;
398
402
    func = s2v(ci->func.p);
399
402
    lua_assert(ttisfunction(func));
400
402
  }
401
402
  cl = ttisclosure(func) ? clvalue(func) : NULL;
402
0
  status = auxgetinfo(L, what, ar, cl, ci);
403
402
  if (strchr(what, 'f')) {
404
201
    setobj2s(L, L->top.p, func);
405
201
    api_incr_top(L);
406
201
  }
407
402
  if (strchr(what, 'L'))
408
0
    collectvalidlines(L, cl);
409
402
  lua_unlock(L);
410
402
  return status;
411
402
}
412
413
414
/*
415
** {======================================================
416
** Symbolic Execution
417
** =======================================================
418
*/
419
420
421
16.5M
static int filterpc (int pc, int jmptarget) {
422
16.5M
  if (pc < jmptarget)  /* is code conditional (inside a jump)? */
423
16.4M
    return -1;  /* cannot know who sets that register */
424
33.4k
  else return pc;  /* current position sets that register */
425
16.5M
}
426
427
428
/*
429
** Try to find last instruction before 'lastpc' that modified register 'reg'.
430
*/
431
191
static int findsetreg (const Proto *p, int lastpc, int reg) {
432
191
  int pc;
433
191
  int setreg = -1;  /* keep last instruction that changed 'reg' */
434
191
  int jmptarget = 0;  /* any code before this address is conditional */
435
191
  if (testMMMode(GET_OPCODE(p->code[lastpc])))
436
59
    lastpc--;  /* previous instruction was not actually executed */
437
44.1M
  for (pc = 0; pc < lastpc; pc++) {
438
44.1M
    Instruction i = p->code[pc];
439
44.1M
    OpCode op = GET_OPCODE(i);
440
44.1M
    int a = GETARG_A(i);
441
44.1M
    int change;  /* true if current instruction changed 'reg' */
442
44.1M
    switch (op) {
443
3.31k
      case OP_LOADNIL: {  /* set registers from 'a' to 'a+b' */
444
3.31k
        int b = GETARG_B(i);
445
3.31k
        change = (a <= reg && reg <= a + b);
446
3.31k
        break;
447
3.31k
      }
448
0
      case OP_TFORCALL: {  /* affect all regs above its base */
449
0
        change = (reg >= a + 2);
450
0
        break;
451
3.31k
      }
452
180k
      case OP_CALL:
453
180k
      case OP_TAILCALL: {  /* affect all registers above base */
454
180k
        change = (reg >= a);
455
180k
        break;
456
180k
      }
457
8.40M
      case OP_JMP: {  /* doesn't change registers, but changes 'jmptarget' */
458
8.40M
        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
8.40M
        if (dest <= lastpc && dest > jmptarget)
462
34.7k
          jmptarget = dest;  /* update 'jmptarget' */
463
8.40M
        change = 0;
464
8.40M
        break;
465
8.40M
      }
466
35.5M
      default:  /* any instruction that sets A */
467
35.5M
        change = (testAMode(op) && reg == a);
468
35.5M
        break;
469
44.1M
    }
470
44.1M
    if (change)
471
16.5M
      setreg = filterpc(pc, jmptarget);
472
44.1M
  }
473
191
  return setreg;
474
191
}
475
476
477
/*
478
** Find a "name" for the constant 'c'.
479
*/
480
143
static const char *kname (const Proto *p, int index, const char **name) {
481
143
  TValue *kvalue = &p->k[index];
482
143
  if (ttisstring(kvalue)) {
483
143
    *name = getstr(tsvalue(kvalue));
484
0
    return "constant";
485
143
  }
486
0
  else {
487
0
    *name = "?";
488
0
    return NULL;
489
0
  }
490
143
}
491
492
493
static const char *basicgetobjname (const Proto *p, int *ppc, int reg,
494
195
                                    const char **name) {
495
195
  int pc = *ppc;
496
195
  *name = luaF_getlocalname(p, reg + 1, pc);
497
195
  if (*name)  /* is a local? */
498
4
    return "local";
499
  /* else try symbolic execution */
500
191
  *ppc = pc = findsetreg(p, pc, reg);
501
191
  if (pc != -1) {  /* could find instruction? */
502
190
    Instruction i = p->code[pc];
503
190
    OpCode op = GET_OPCODE(i);
504
190
    switch (op) {
505
4
      case OP_MOVE: {
506
4
        int b = GETARG_B(i);  /* move from 'b' to 'a' */
507
4
        if (b < GETARG_A(i))
508
4
          return basicgetobjname(p, ppc, b, name);  /* get name for 'b' */
509
0
        break;
510
4
      }
511
7
      case OP_GETUPVAL: {
512
7
        *name = upvalname(p, GETARG_B(i));
513
0
        return "upvalue";
514
7
      }
515
76
      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
103
      default: break;
518
190
    }
519
190
  }
520
104
  return NULL;  /* could not find reasonable name */
521
191
}
522
523
524
/*
525
** Find a "name" for the register 'c'.
526
*/
527
4
static void rname (const Proto *p, int pc, int c, const char **name) {
528
4
  const char *what = basicgetobjname(p, &pc, c, name); /* search for 'c' */
529
4
  if (!(what && *what == 'c'))  /* did not find a constant name? */
530
0
    *name = "?";
531
4
}
532
533
534
/*
535
** Find a "name" for a 'C' value in an RK instruction.
536
*/
537
0
static void rkname (const Proto *p, int pc, Instruction i, const char **name) {
538
0
  int c = GETARG_C(i);  /* key index */
539
0
  if (GETARG_k(i))  /* is 'c' a constant? */
540
0
    kname(p, c, name);
541
0
  else  /* 'c' is a register */
542
0
    rname(p, pc, c, name);
543
0
}
544
545
546
/*
547
** Check whether table being indexed by instruction 'i' is the
548
** environment '_ENV'
549
*/
550
71
static const char *isEnv (const Proto *p, int pc, Instruction i, int isup) {
551
71
  int t = GETARG_B(i);  /* table index */
552
0
  const char *name;  /* name of indexed variable */
553
71
  if (isup)  /* is 't' an upvalue? */
554
67
    name = upvalname(p, t);
555
4
  else  /* 't' is a register */
556
4
    basicgetobjname(p, &pc, t, &name);
557
71
  return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field";
558
71
}
559
560
561
/*
562
** Extend 'basicgetobjname' to handle table accesses
563
*/
564
static const char *getobjname (const Proto *p, int lastpc, int reg,
565
183
                               const char **name) {
566
183
  const char *kind = basicgetobjname(p, &lastpc, reg, name);
567
183
  if (kind != NULL)
568
79
    return kind;
569
104
  else if (lastpc != -1) {  /* could find instruction? */
570
103
    Instruction i = p->code[lastpc];
571
103
    OpCode op = GET_OPCODE(i);
572
103
    switch (op) {
573
67
      case OP_GETTABUP: {
574
67
        int k = GETARG_C(i);  /* key index */
575
0
        kname(p, k, name);
576
67
        return isEnv(p, lastpc, i, 1);
577
67
      }
578
4
      case OP_GETTABLE: {
579
4
        int k = GETARG_C(i);  /* key index */
580
0
        rname(p, lastpc, k, name);
581
4
        return isEnv(p, lastpc, i, 0);
582
4
      }
583
0
      case OP_GETI: {
584
0
        *name = "integer index";
585
0
        return "field";
586
4
      }
587
0
      case OP_GETFIELD: {
588
0
        int k = GETARG_C(i);  /* key index */
589
0
        kname(p, k, name);
590
0
        return isEnv(p, lastpc, i, 0);
591
0
      }
592
0
      case OP_SELF: {
593
0
        rkname(p, lastpc, i, name);
594
0
        return "method";
595
0
      }
596
32
      default: break;  /* go through to return NULL */
597
103
    }
598
103
  }
599
33
  return NULL;  /* could not find reasonable name */
600
183
}
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
51
                                     int pc, const char **name) {
611
51
  TMS tm = (TMS)0;  /* (initial value avoids warnings) */
612
51
  Instruction i = p->code[pc];  /* calling instruction */
613
51
  switch (GET_OPCODE(i)) {
614
51
    case OP_CALL:
615
51
    case OP_TAILCALL:
616
51
      return getobjname(p, pc, GETARG_A(i), name);  /* get function name */
617
0
    case OP_TFORCALL: {  /* for iterator */
618
0
      *name = "for iterator";
619
0
       return "for iterator";
620
51
    }
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
51
  }
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
204
                                                   const char **name) {
655
204
  if (ci->callstatus & CIST_HOOKED) {  /* was it called inside a hook? */
656
0
    *name = "?";
657
0
    return "hook";
658
0
  }
659
204
  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
204
  else if (isLua(ci))
664
102
    return funcnamefromcode(L, ci_func(ci)->p, currentpc(ci), name);
665
153
  else
666
153
    return NULL;
667
204
}
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
132
static int instack (CallInfo *ci, const TValue *o) {
680
132
  int pos;
681
132
  StkId base = ci->func.p + 1;
682
703
  for (pos = 0; base + pos < ci->top.p; pos++) {
683
703
    if (o == s2v(base + pos))
684
132
      return pos;
685
703
  }
686
0
  return -1;  /* not found */
687
132
}
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
132
                                 const char **name) {
697
264
  LClosure *c = ci_func(ci);
698
0
  int i;
699
268
  for (i = 0; i < c->nupvalues; i++) {
700
136
    if (c->upvals[i]->v.p == o) {
701
0
      *name = upvalname(c->p, i);
702
0
      return "upvalue";
703
0
    }
704
136
  }
705
132
  return NULL;
706
264
}
707
708
709
static const char *formatvarinfo (lua_State *L, const char *kind,
710
135
                                                const char *name) {
711
135
  if (kind == NULL)
712
33
    return "";  /* no information */
713
102
  else
714
102
    return luaO_pushfstring(L, " (%s '%s')", kind, name);
715
135
}
716
717
/*
718
** Build a string with a "description" for the value 'o', such as
719
** "variable 'x'" or "upvalue 'y'".
720
*/
721
132
static const char *varinfo (lua_State *L, const TValue *o) {
722
132
  CallInfo *ci = L->ci;
723
132
  const char *name = NULL;  /* to avoid warnings */
724
132
  const char *kind = NULL;
725
132
  if (isLua(ci)) {
726
132
    kind = getupvalname(ci, o, &name);  /* check whether 'o' is an upvalue */
727
132
    if (!kind) {  /* not an upvalue? */
728
132
      int reg = instack(ci, o);  /* try a register */
729
132
      if (reg >= 0)  /* is 'o' a register? */
730
264
        kind = getobjname(ci_func(ci)->p, currentpc(ci), reg, &name);
731
132
    }
732
132
  }
733
132
  return formatvarinfo(L, kind, name);
734
132
}
735
736
737
/*
738
** Raise a type error
739
*/
740
static l_noret typeerror (lua_State *L, const TValue *o, const char *op,
741
134
                          const char *extra) {
742
134
  const char *t = luaT_objtypename(L, o);
743
134
  luaG_runerror(L, "attempt to %s a %s value%s", op, t, extra);
744
134
}
745
746
747
/*
748
** Raise a type error with "standard" information about the faulty
749
** object 'o' (using 'varinfo').
750
*/
751
131
l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
752
131
  typeerror(L, o, op, varinfo(L, o));
753
131
}
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
3
l_noret luaG_callerror (lua_State *L, const TValue *o) {
762
3
  CallInfo *ci = L->ci;
763
3
  const char *name = NULL;  /* to avoid warnings */
764
3
  const char *kind = funcnamefromcall(L, ci, &name);
765
3
  const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o);
766
3
  typeerror(L, o, "call", extra);
767
3
}
768
769
770
1
l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what) {
771
1
  luaG_runerror(L, "bad 'for' %s (number expected, got %s)",
772
1
                   what, luaT_objtypename(L, o));
773
1
}
774
775
776
0
l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
777
0
  if (ttisstring(p1) || cvt2str(p1)) p1 = p2;
778
0
  luaG_typeerror(L, p1, "concatenate");
779
0
}
780
781
782
l_noret luaG_opinterror (lua_State *L, const TValue *p1,
783
121
                         const TValue *p2, const char *msg) {
784
121
  if (!ttisnumber(p1))  /* first operand is wrong? */
785
109
    p2 = p1;  /* now second is wrong */
786
121
  luaG_typeerror(L, p2, msg);
787
121
}
788
789
790
/*
791
** Error when both values are convertible to numbers, but not to integers
792
*/
793
1
l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
794
1
  lua_Integer temp;
795
1
  if (!luaV_tointegerns(p1, &temp, LUA_FLOORN2I))
796
0
    p2 = p1;
797
1
  luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
798
1
}
799
800
801
16
l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
802
16
  const char *t1 = luaT_objtypename(L, p1);
803
16
  const char *t2 = luaT_objtypename(L, p2);
804
16
  if (strcmp(t1, t2) == 0)
805
7
    luaG_runerror(L, "attempt to compare two %s values", t1);
806
9
  else
807
9
    luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
808
16
}
809
810
811
/* add src:line information to 'msg' */
812
const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
813
546
                                        int line) {
814
546
  char buff[LUA_IDSIZE];
815
546
  if (src) {
816
546
    size_t idlen;
817
546
    const char *id = getlstr(src, idlen);
818
546
    luaO_chunkid(buff, id, idlen);
819
546
  }
820
0
  else {  /* no source available; use "?" instead */
821
0
    buff[0] = '?'; buff[1] = '\0';
822
0
  }
823
546
  return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
824
546
}
825
826
827
159
l_noret luaG_errormsg (lua_State *L) {
828
159
  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
159
  luaD_throw(L, LUA_ERRRUN);
837
159
}
838
839
840
159
l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
841
159
  CallInfo *ci = L->ci;
842
159
  const char *msg;
843
159
  va_list argp;
844
159
  luaC_checkGC(L);  /* error message uses memory */
845
159
  va_start(argp, fmt);
846
159
  msg = luaO_pushvfstring(L, fmt, argp);  /* format message */
847
159
  va_end(argp);
848
159
  if (isLua(ci)) {  /* if Lua function, add source:line information */
849
306
    luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci));
850
153
    setobjs2s(L, L->top.p - 2, L->top.p - 1);  /* remove 'msg' */
851
153
    L->top.p--;
852
153
  }
853
159
  luaG_errormsg(L);
854
159
}
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
0
static int changedline (const Proto *p, int oldpc, int newpc) {
866
0
  if (p->lineinfo == NULL)  /* no debug information? */
867
0
    return 0;
868
0
  if (newpc - oldpc < MAXIWTHABS / 2) {  /* not too far apart? */
869
0
    int delta = 0;  /* line difference */
870
0
    int pc = oldpc;
871
0
    for (;;) {
872
0
      int lineinfo = p->lineinfo[++pc];
873
0
      if (lineinfo == ABSLINEINFO)
874
0
        break;  /* cannot compute delta; fall through */
875
0
      delta += lineinfo;
876
0
      if (pc == newpc)
877
0
        return (delta != 0);  /* delta computed successfully */
878
0
    }
879
0
  }
880
  /* either instructions are too far apart or there is an absolute line
881
     info in the way; compute line difference explicitly */
882
0
  return (luaG_getfuncline(p, oldpc) != luaG_getfuncline(p, newpc));
883
0
}
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
0
int luaG_tracecall (lua_State *L) {
895
0
  CallInfo *ci = L->ci;
896
0
  Proto *p = ci_func(ci)->p;
897
0
  ci->u.l.trap = 1;  /* ensure hooks will be checked */
898
0
  if (ci->u.l.savedpc == p->code) {  /* first instruction (not resuming)? */
899
0
    if (p->flag & PF_ISVARARG)
900
0
      return 0;  /* hooks will start at VARARGPREP instruction */
901
0
    else if (!(ci->callstatus & CIST_HOOKYIELD))  /* not yieded? */
902
0
      luaD_hookcall(L, ci);  /* check 'call' hook */
903
0
  }
904
0
  return 1;  /* keep 'trap' on */
905
0
}
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
344
int luaG_traceexec (lua_State *L, const Instruction *pc) {
921
344
  CallInfo *ci = L->ci;
922
344
  lu_byte mask = L->hookmask;
923
688
  const Proto *p = ci_func(ci)->p;
924
0
  int counthook;
925
688
  if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) {  /* no hooks? */
926
344
    ci->u.l.trap = 0;  /* don't need to stop again */
927
344
    return 0;  /* turn off 'trap' */
928
344
  }
929
0
  pc++;  /* reference is always next instruction */
930
0
  ci->u.l.savedpc = pc;  /* save 'pc' */
931
0
  counthook = (mask & LUA_MASKCOUNT) && (--L->hookcount == 0);
932
0
  if (counthook)
933
0
    resethookcount(L);  /* reset count */
934
0
  else if (!(mask & LUA_MASKLINE))
935
0
    return 1;  /* no line hook and count != 0; nothing to be done now */
936
0
  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
0
  if (!isIT(*(ci->u.l.savedpc - 1)))  /* top not being used? */
941
0
    L->top.p = ci->top.p;  /* correct top */
942
0
  if (counthook)
943
0
    luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0);  /* call count hook */
944
0
  if (mask & LUA_MASKLINE) {
945
    /* 'L->oldpc' may be invalid; use zero in this case */
946
0
    int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0;
947
0
    int npci = pcRel(pc, p);
948
0
    if (npci <= oldpc ||  /* call hook when jump back (loop), */
949
0
        changedline(p, oldpc, npci)) {  /* or when enter new line */
950
0
      int newline = luaG_getfuncline(p, npci);
951
0
      luaD_hook(L, LUA_HOOKLINE, newline, 0, 0);  /* call line hook */
952
0
    }
953
0
    L->oldpc = npci;  /* 'pc' of last call to line hook */
954
0
  }
955
0
  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
0
  return 1;  /* keep 'trap' on */
962
0
}
963