Coverage Report

Created: 2025-08-25 07:03

/src/testdir/build/lua-master/source/ldblib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
** $Id: ldblib.c $
3
** Interface from Lua to its debug API
4
** See Copyright Notice in lua.h
5
*/
6
7
#define ldblib_c
8
#define LUA_LIB
9
10
#include "lprefix.h"
11
12
13
#include <stdio.h>
14
#include <stdlib.h>
15
#include <string.h>
16
17
#include "lua.h"
18
19
#include "lauxlib.h"
20
#include "lualib.h"
21
#include "llimits.h"
22
23
24
/*
25
** The hook table at registry[HOOKKEY] maps threads to their current
26
** hook function.
27
*/
28
static const char *const HOOKKEY = "_HOOKKEY";
29
30
31
/*
32
** If L1 != L, L1 can be in any state, and therefore there are no
33
** guarantees about its stack space; any push in L1 must be
34
** checked.
35
*/
36
4.61M
static void checkstack (lua_State *L, lua_State *L1, int n) {
37
4.61M
  if (l_unlikely(L != L1 && !lua_checkstack(L1, n)))
38
0
    luaL_error(L, "stack overflow");
39
4.61M
}
40
41
42
78
static int db_getregistry (lua_State *L) {
43
78
  lua_pushvalue(L, LUA_REGISTRYINDEX);
44
78
  return 1;
45
78
}
46
47
48
3.23k
static int db_getmetatable (lua_State *L) {
49
3.23k
  luaL_checkany(L, 1);
50
3.23k
  if (!lua_getmetatable(L, 1)) {
51
568
    lua_pushnil(L);  /* no metatable */
52
568
  }
53
3.23k
  return 1;
54
3.23k
}
55
56
57
168k
static int db_setmetatable (lua_State *L) {
58
168k
  int t = lua_type(L, 2);
59
168k
  luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
60
168k
  lua_settop(L, 2);
61
168k
  lua_setmetatable(L, 1);
62
168k
  return 1;  /* return 1st argument */
63
168k
}
64
65
66
1.08k
static int db_getuservalue (lua_State *L) {
67
1.08k
  int n = (int)luaL_optinteger(L, 2, 1);
68
1.08k
  if (lua_type(L, 1) != LUA_TUSERDATA)
69
1.08k
    luaL_pushfail(L);
70
0
  else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) {
71
0
    lua_pushboolean(L, 1);
72
0
    return 2;
73
0
  }
74
1.08k
  return 1;
75
1.08k
}
76
77
78
8
static int db_setuservalue (lua_State *L) {
79
8
  int n = (int)luaL_optinteger(L, 3, 1);
80
8
  luaL_checktype(L, 1, LUA_TUSERDATA);
81
8
  luaL_checkany(L, 2);
82
8
  lua_settop(L, 2);
83
8
  if (!lua_setiuservalue(L, 1, n))
84
0
    luaL_pushfail(L);
85
8
  return 1;
86
8
}
87
88
89
/*
90
** Auxiliary function used by several library functions: check for
91
** an optional thread as function's first argument and set 'arg' with
92
** 1 if this argument is present (so that functions can skip it to
93
** access their other arguments)
94
*/
95
4.76M
static lua_State *getthread (lua_State *L, int *arg) {
96
4.76M
  if (lua_isthread(L, 1)) {
97
0
    *arg = 1;
98
0
    return lua_tothread(L, 1);
99
0
  }
100
4.76M
  else {
101
4.76M
    *arg = 0;
102
4.76M
    return L;  /* function will operate over current thread */
103
4.76M
  }
104
4.76M
}
105
106
107
/*
108
** Variations of 'lua_settable', used by 'db_getinfo' to put results
109
** from 'lua_getinfo' into result table. Key is always a string;
110
** value can be a string, an int, or a boolean.
111
*/
112
13.1M
static void settabss (lua_State *L, const char *k, const char *v) {
113
13.1M
  lua_pushstring(L, v);
114
13.1M
  lua_setfield(L, -2, k);
115
13.1M
}
116
117
26.2M
static void settabsi (lua_State *L, const char *k, int v) {
118
26.2M
  lua_pushinteger(L, v);
119
26.2M
  lua_setfield(L, -2, k);
120
26.2M
}
121
122
6.56M
static void settabsb (lua_State *L, const char *k, int v) {
123
6.56M
  lua_pushboolean(L, v);
124
6.56M
  lua_setfield(L, -2, k);
125
6.56M
}
126
127
128
/*
129
** In function 'db_getinfo', the call to 'lua_getinfo' may push
130
** results on the stack; later it creates the result table to put
131
** these objects. Function 'treatstackoption' puts the result from
132
** 'lua_getinfo' on top of the result table so that it can call
133
** 'lua_setfield'.
134
*/
135
3.42M
static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
136
3.42M
  if (L == L1)
137
3.42M
    lua_rotate(L, -2, 1);  /* exchange object and table */
138
0
  else
139
0
    lua_xmove(L1, L, 1);  /* move object to the "main" stack */
140
3.42M
  lua_setfield(L, -2, fname);  /* put object into table */
141
3.42M
}
142
143
144
/*
145
** Calls 'lua_getinfo' and collects all results in a new table.
146
** L1 needs stack space for an optional input (function) plus
147
** two optional outputs (function and line table) from function
148
** 'lua_getinfo'.
149
*/
150
3.99M
static int db_getinfo (lua_State *L) {
151
3.99M
  lua_Debug ar;
152
3.99M
  int arg;
153
3.99M
  lua_State *L1 = getthread(L, &arg);
154
3.99M
  const char *options = luaL_optstring(L, arg+2, "flnSrtu");
155
3.99M
  checkstack(L, L1, 3);
156
3.99M
  luaL_argcheck(L, options[0] != '>', arg + 2, "invalid option '>'");
157
3.99M
  if (lua_isfunction(L, arg + 1)) {  /* info about a function? */
158
86.2k
    options = lua_pushfstring(L, ">%s", options);  /* add '>' to 'options' */
159
86.2k
    lua_pushvalue(L, arg + 1);  /* move function to 'L1' stack */
160
86.2k
    lua_xmove(L, L1, 1);
161
86.2k
  }
162
3.90M
  else {  /* stack level */
163
3.90M
    if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
164
567k
      luaL_pushfail(L);  /* level out of range */
165
567k
      return 1;
166
567k
    }
167
3.90M
  }
168
3.42M
  if (!lua_getinfo(L1, options, &ar))
169
692
    return luaL_argerror(L, arg+2, "invalid option");
170
3.42M
  lua_newtable(L);  /* table to collect results */
171
3.42M
  if (strchr(options, 'S')) {
172
3.28M
    lua_pushlstring(L, ar.source, ar.srclen);
173
3.28M
    lua_setfield(L, -2, "source");
174
3.28M
    settabss(L, "short_src", ar.short_src);
175
3.28M
    settabsi(L, "linedefined", ar.linedefined);
176
3.28M
    settabsi(L, "lastlinedefined", ar.lastlinedefined);
177
3.28M
    settabss(L, "what", ar.what);
178
3.28M
  }
179
3.42M
  if (strchr(options, 'l'))
180
3.28M
    settabsi(L, "currentline", ar.currentline);
181
3.42M
  if (strchr(options, 'u')) {
182
3.28M
    settabsi(L, "nups", ar.nups);
183
3.28M
    settabsi(L, "nparams", ar.nparams);
184
3.28M
    settabsb(L, "isvararg", ar.isvararg);
185
3.28M
  }
186
3.42M
  if (strchr(options, 'n')) {
187
3.28M
    settabss(L, "name", ar.name);
188
3.28M
    settabss(L, "namewhat", ar.namewhat);
189
3.28M
  }
190
3.42M
  if (strchr(options, 'r')) {
191
3.28M
    settabsi(L, "ftransfer", ar.ftransfer);
192
3.28M
    settabsi(L, "ntransfer", ar.ntransfer);
193
3.28M
  }
194
3.42M
  if (strchr(options, 't')) {
195
3.28M
    settabsb(L, "istailcall", ar.istailcall);
196
3.28M
    settabsi(L, "extraargs", ar.extraargs);
197
3.28M
  }
198
3.42M
  if (strchr(options, 'L'))
199
140k
    treatstackoption(L, L1, "activelines");
200
3.42M
  if (strchr(options, 'f'))
201
3.28M
    treatstackoption(L, L1, "func");
202
3.42M
  return 1;  /* return table */
203
3.42M
}
204
205
206
656
static int db_getlocal (lua_State *L) {
207
656
  int arg;
208
656
  lua_State *L1 = getthread(L, &arg);
209
656
  int nvar = (int)luaL_checkinteger(L, arg + 2);  /* local-variable index */
210
656
  if (lua_isfunction(L, arg + 1)) {  /* function argument? */
211
0
    lua_pushvalue(L, arg + 1);  /* push function */
212
0
    lua_pushstring(L, lua_getlocal(L, NULL, nvar));  /* push local name */
213
0
    return 1;  /* return only name (there is no value) */
214
0
  }
215
656
  else {  /* stack-level argument */
216
656
    lua_Debug ar;
217
656
    const char *name;
218
656
    int level = (int)luaL_checkinteger(L, arg + 1);
219
656
    if (l_unlikely(!lua_getstack(L1, level, &ar)))  /* out of range? */
220
108
      return luaL_argerror(L, arg+1, "level out of range");
221
548
    checkstack(L, L1, 1);
222
548
    name = lua_getlocal(L1, &ar, nvar);
223
548
    if (name) {
224
397
      lua_xmove(L1, L, 1);  /* move local value */
225
397
      lua_pushstring(L, name);  /* push name */
226
397
      lua_rotate(L, -2, 1);  /* re-order */
227
397
      return 2;
228
397
    }
229
151
    else {
230
151
      luaL_pushfail(L);  /* no name (nor value) */
231
151
      return 1;
232
151
    }
233
548
  }
234
656
}
235
236
237
6.39k
static int db_setlocal (lua_State *L) {
238
6.39k
  int arg;
239
6.39k
  const char *name;
240
6.39k
  lua_State *L1 = getthread(L, &arg);
241
6.39k
  lua_Debug ar;
242
6.39k
  int level = (int)luaL_checkinteger(L, arg + 1);
243
6.39k
  int nvar = (int)luaL_checkinteger(L, arg + 2);
244
6.39k
  if (l_unlikely(!lua_getstack(L1, level, &ar)))  /* out of range? */
245
1.07k
    return luaL_argerror(L, arg+1, "level out of range");
246
5.31k
  luaL_checkany(L, arg+3);
247
5.31k
  lua_settop(L, arg+3);
248
5.31k
  checkstack(L, L1, 1);
249
5.31k
  lua_xmove(L, L1, 1);
250
5.31k
  name = lua_setlocal(L1, &ar, nvar);
251
5.31k
  if (name == NULL)
252
3.22k
    lua_pop(L1, 1);  /* pop value (if not popped by 'lua_setlocal') */
253
5.31k
  lua_pushstring(L, name);
254
5.31k
  return 1;
255
6.39k
}
256
257
258
/*
259
** get (if 'get' is true) or set an upvalue from a closure
260
*/
261
42
static int auxupvalue (lua_State *L, int get) {
262
42
  const char *name;
263
42
  int n = (int)luaL_checkinteger(L, 2);  /* upvalue index */
264
42
  luaL_checktype(L, 1, LUA_TFUNCTION);  /* closure */
265
42
  name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
266
42
  if (name == NULL) return 0;
267
42
  lua_pushstring(L, name);
268
42
  lua_insert(L, -(get+1));  /* no-op if get is false */
269
42
  return get + 1;
270
42
}
271
272
273
42
static int db_getupvalue (lua_State *L) {
274
42
  return auxupvalue(L, 1);
275
42
}
276
277
278
1
static int db_setupvalue (lua_State *L) {
279
1
  luaL_checkany(L, 3);
280
1
  return auxupvalue(L, 0);
281
1
}
282
283
284
/*
285
** Check whether a given upvalue from a given closure exists and
286
** returns its index
287
*/
288
19
static void *checkupval (lua_State *L, int argf, int argnup, int *pnup) {
289
19
  void *id;
290
19
  int nup = (int)luaL_checkinteger(L, argnup);  /* upvalue index */
291
19
  luaL_checktype(L, argf, LUA_TFUNCTION);  /* closure */
292
19
  id = lua_upvalueid(L, argf, nup);
293
19
  if (pnup) {
294
0
    luaL_argcheck(L, id != NULL, argnup, "invalid upvalue index");
295
0
    *pnup = nup;
296
0
  }
297
19
  return id;
298
19
}
299
300
301
7
static int db_upvalueid (lua_State *L) {
302
7
  void *id = checkupval(L, 1, 2, NULL);
303
7
  if (id != NULL)
304
0
    lua_pushlightuserdata(L, id);
305
7
  else
306
7
    luaL_pushfail(L);
307
7
  return 1;
308
7
}
309
310
311
12
static int db_upvaluejoin (lua_State *L) {
312
12
  int n1, n2;
313
12
  checkupval(L, 1, 2, &n1);
314
12
  checkupval(L, 3, 4, &n2);
315
12
  luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
316
12
  luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
317
12
  lua_upvaluejoin(L, 1, n1, 3, n2);
318
12
  return 0;
319
12
}
320
321
322
/*
323
** Call hook function registered at hook table for the current
324
** thread (if there is one)
325
*/
326
4.78M
static void hookf (lua_State *L, lua_Debug *ar) {
327
4.78M
  static const char *const hooknames[] =
328
4.78M
    {"call", "return", "line", "count", "tail call"};
329
4.78M
  lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
330
4.78M
  lua_pushthread(L);
331
4.78M
  if (lua_rawget(L, -2) == LUA_TFUNCTION) {  /* is there a hook function? */
332
4.62M
    lua_pushstring(L, hooknames[(int)ar->event]);  /* push event name */
333
4.62M
    if (ar->currentline >= 0)
334
2.35M
      lua_pushinteger(L, ar->currentline);  /* push current line */
335
2.26M
    else lua_pushnil(L);
336
4.62M
    lua_assert(lua_getinfo(L, "lS", ar));
337
4.62M
    lua_call(L, 2, 0);  /* call hook function */
338
4.62M
  }
339
4.78M
}
340
341
342
/*
343
** Convert a string mask (for 'sethook') into a bit mask
344
*/
345
548k
static int makemask (const char *smask, int count) {
346
548k
  int mask = 0;
347
548k
  if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
348
548k
  if (strchr(smask, 'r')) mask |= LUA_MASKRET;
349
548k
  if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
350
548k
  if (count > 0) mask |= LUA_MASKCOUNT;
351
548k
  return mask;
352
548k
}
353
354
355
/*
356
** Convert a bit mask (for 'gethook') into a string mask
357
*/
358
38.5k
static char *unmakemask (int mask, char *smask) {
359
38.5k
  int i = 0;
360
38.5k
  if (mask & LUA_MASKCALL) smask[i++] = 'c';
361
38.5k
  if (mask & LUA_MASKRET) smask[i++] = 'r';
362
38.5k
  if (mask & LUA_MASKLINE) smask[i++] = 'l';
363
38.5k
  smask[i] = '\0';
364
38.5k
  return smask;
365
38.5k
}
366
367
368
578k
static int db_sethook (lua_State *L) {
369
578k
  int arg, mask, count;
370
578k
  lua_Hook func;
371
578k
  lua_State *L1 = getthread(L, &arg);
372
578k
  if (lua_isnoneornil(L, arg+1)) {  /* no hook? */
373
29.4k
    lua_settop(L, arg+1);
374
29.4k
    func = NULL; mask = 0; count = 0;  /* turn off hooks */
375
29.4k
  }
376
548k
  else {
377
548k
    const char *smask = luaL_checkstring(L, arg+2);
378
548k
    luaL_checktype(L, arg+1, LUA_TFUNCTION);
379
548k
    count = (int)luaL_optinteger(L, arg + 3, 0);
380
548k
    func = hookf; mask = makemask(smask, count);
381
548k
  }
382
578k
  if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) {
383
    /* table just created; initialize it */
384
1.20k
    lua_pushliteral(L, "k");
385
1.20k
    lua_setfield(L, -2, "__mode");  /** hooktable.__mode = "k" */
386
1.20k
    lua_pushvalue(L, -1);
387
1.20k
    lua_setmetatable(L, -2);  /* metatable(hooktable) = hooktable */
388
1.20k
  }
389
578k
  checkstack(L, L1, 1);
390
578k
  lua_pushthread(L1); lua_xmove(L1, L, 1);  /* key (thread) */
391
578k
  lua_pushvalue(L, arg + 1);  /* value (hook function) */
392
578k
  lua_rawset(L, -3);  /* hooktable[L1] = new Lua hook */
393
578k
  lua_sethook(L1, func, mask, count);
394
578k
  return 0;
395
578k
}
396
397
398
38.8k
static int db_gethook (lua_State *L) {
399
38.8k
  int arg;
400
38.8k
  lua_State *L1 = getthread(L, &arg);
401
38.8k
  char buff[5];
402
38.8k
  int mask = lua_gethookmask(L1);
403
38.8k
  lua_Hook hook = lua_gethook(L1);
404
38.8k
  if (hook == NULL) {  /* no hook? */
405
360
    luaL_pushfail(L);
406
360
    return 1;
407
360
  }
408
38.5k
  else if (hook != hookf)  /* external hook? */
409
0
    lua_pushliteral(L, "external hook");
410
38.5k
  else {  /* hook table must exist */
411
38.5k
    lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
412
38.5k
    checkstack(L, L1, 1);
413
38.5k
    lua_pushthread(L1); lua_xmove(L1, L, 1);
414
38.5k
    lua_rawget(L, -2);   /* 1st result = hooktable[L1] */
415
38.5k
    lua_remove(L, -2);  /* remove hook table */
416
38.5k
  }
417
38.5k
  lua_pushstring(L, unmakemask(mask, buff));  /* 2nd result = mask */
418
38.5k
  lua_pushinteger(L, lua_gethookcount(L1));  /* 3rd result = count */
419
38.5k
  return 3;
420
38.8k
}
421
422
423
90
static int db_debug (lua_State *L) {
424
90
  for (;;) {
425
90
    char buffer[250];
426
90
    lua_writestringerror("%s", "lua_debug> ");
427
90
    if (fgets(buffer, sizeof(buffer), stdin) == NULL ||
428
90
        strcmp(buffer, "cont\n") == 0)
429
90
      return 0;
430
0
    if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
431
0
        lua_pcall(L, 0, 0, 0))
432
0
      lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL));
433
0
    lua_settop(L, 0);  /* remove eventual returns */
434
0
  }
435
90
}
436
437
438
143k
static int db_traceback (lua_State *L) {
439
143k
  int arg;
440
143k
  lua_State *L1 = getthread(L, &arg);
441
143k
  const char *msg = lua_tostring(L, arg + 1);
442
143k
  if (msg == NULL && !lua_isnoneornil(L, arg + 1))  /* non-string 'msg'? */
443
102
    lua_pushvalue(L, arg + 1);  /* return it untouched */
444
143k
  else {
445
143k
    int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0);
446
143k
    luaL_traceback(L, L1, msg, level);
447
143k
  }
448
143k
  return 1;
449
143k
}
450
451
452
static const luaL_Reg dblib[] = {
453
  {"debug", db_debug},
454
  {"getuservalue", db_getuservalue},
455
  {"gethook", db_gethook},
456
  {"getinfo", db_getinfo},
457
  {"getlocal", db_getlocal},
458
  {"getregistry", db_getregistry},
459
  {"getmetatable", db_getmetatable},
460
  {"getupvalue", db_getupvalue},
461
  {"upvaluejoin", db_upvaluejoin},
462
  {"upvalueid", db_upvalueid},
463
  {"setuservalue", db_setuservalue},
464
  {"sethook", db_sethook},
465
  {"setlocal", db_setlocal},
466
  {"setmetatable", db_setmetatable},
467
  {"setupvalue", db_setupvalue},
468
  {"traceback", db_traceback},
469
  {NULL, NULL}
470
};
471
472
473
26.8k
LUAMOD_API int luaopen_debug (lua_State *L) {
474
26.8k
  luaL_newlib(L, dblib);
475
26.8k
  return 1;
476
26.8k
}
477