Coverage Report

Created: 2025-07-11 06:33

/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.80M
static void checkstack (lua_State *L, lua_State *L1, int n) {
37
4.80M
  if (l_unlikely(L != L1 && !lua_checkstack(L1, n)))
38
0
    luaL_error(L, "stack overflow");
39
4.80M
}
40
41
42
84
static int db_getregistry (lua_State *L) {
43
84
  lua_pushvalue(L, LUA_REGISTRYINDEX);
44
84
  return 1;
45
84
}
46
47
48
465
static int db_getmetatable (lua_State *L) {
49
465
  luaL_checkany(L, 1);
50
465
  if (!lua_getmetatable(L, 1)) {
51
91
    lua_pushnil(L);  /* no metatable */
52
91
  }
53
465
  return 1;
54
465
}
55
56
57
206k
static int db_setmetatable (lua_State *L) {
58
206k
  int t = lua_type(L, 2);
59
206k
  luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
60
206k
  lua_settop(L, 2);
61
206k
  lua_setmetatable(L, 1);
62
206k
  return 1;  /* return 1st argument */
63
206k
}
64
65
66
82
static int db_getuservalue (lua_State *L) {
67
82
  int n = (int)luaL_optinteger(L, 2, 1);
68
82
  if (lua_type(L, 1) != LUA_TUSERDATA)
69
82
    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
82
  return 1;
75
82
}
76
77
78
21
static int db_setuservalue (lua_State *L) {
79
21
  int n = (int)luaL_optinteger(L, 3, 1);
80
21
  luaL_checktype(L, 1, LUA_TUSERDATA);
81
21
  luaL_checkany(L, 2);
82
21
  lua_settop(L, 2);
83
21
  if (!lua_setiuservalue(L, 1, n))
84
0
    luaL_pushfail(L);
85
21
  return 1;
86
21
}
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.92M
static lua_State *getthread (lua_State *L, int *arg) {
96
4.92M
  if (lua_isthread(L, 1)) {
97
8
    *arg = 1;
98
8
    return lua_tothread(L, 1);
99
8
  }
100
4.92M
  else {
101
4.92M
    *arg = 0;
102
4.92M
    return L;  /* function will operate over current thread */
103
4.92M
  }
104
4.92M
}
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
15.7M
static void settabss (lua_State *L, const char *k, const char *v) {
113
15.7M
  lua_pushstring(L, v);
114
15.7M
  lua_setfield(L, -2, k);
115
15.7M
}
116
117
31.4M
static void settabsi (lua_State *L, const char *k, int v) {
118
31.4M
  lua_pushinteger(L, v);
119
31.4M
  lua_setfield(L, -2, k);
120
31.4M
}
121
122
7.86M
static void settabsb (lua_State *L, const char *k, int v) {
123
7.86M
  lua_pushboolean(L, v);
124
7.86M
  lua_setfield(L, -2, k);
125
7.86M
}
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.93M
static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
136
3.93M
  if (L == L1)
137
3.93M
    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.93M
  lua_setfield(L, -2, fname);  /* put object into table */
141
3.93M
}
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
4.24M
static int db_getinfo (lua_State *L) {
151
4.24M
  lua_Debug ar;
152
4.24M
  int arg;
153
4.24M
  lua_State *L1 = getthread(L, &arg);
154
4.24M
  const char *options = luaL_optstring(L, arg+2, "flnSrtu");
155
4.24M
  checkstack(L, L1, 3);
156
4.24M
  luaL_argcheck(L, options[0] != '>', arg + 2, "invalid option '>'");
157
4.24M
  if (lua_isfunction(L, arg + 1)) {  /* info about a function? */
158
5.11k
    options = lua_pushfstring(L, ">%s", options);  /* add '>' to 'options' */
159
5.11k
    lua_pushvalue(L, arg + 1);  /* move function to 'L1' stack */
160
5.11k
    lua_xmove(L, L1, 1);
161
5.11k
  }
162
4.23M
  else {  /* stack level */
163
4.23M
    if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
164
305k
      luaL_pushfail(L);  /* level out of range */
165
305k
      return 1;
166
305k
    }
167
4.23M
  }
168
3.93M
  if (!lua_getinfo(L1, options, &ar))
169
29
    return luaL_argerror(L, arg+2, "invalid option");
170
3.93M
  lua_newtable(L);  /* table to collect results */
171
3.93M
  if (strchr(options, 'S')) {
172
3.93M
    lua_pushlstring(L, ar.source, ar.srclen);
173
3.93M
    lua_setfield(L, -2, "source");
174
3.93M
    settabss(L, "short_src", ar.short_src);
175
3.93M
    settabsi(L, "linedefined", ar.linedefined);
176
3.93M
    settabsi(L, "lastlinedefined", ar.lastlinedefined);
177
3.93M
    settabss(L, "what", ar.what);
178
3.93M
  }
179
3.93M
  if (strchr(options, 'l'))
180
3.93M
    settabsi(L, "currentline", ar.currentline);
181
3.93M
  if (strchr(options, 'u')) {
182
3.93M
    settabsi(L, "nups", ar.nups);
183
3.93M
    settabsi(L, "nparams", ar.nparams);
184
3.93M
    settabsb(L, "isvararg", ar.isvararg);
185
3.93M
  }
186
3.93M
  if (strchr(options, 'n')) {
187
3.93M
    settabss(L, "name", ar.name);
188
3.93M
    settabss(L, "namewhat", ar.namewhat);
189
3.93M
  }
190
3.93M
  if (strchr(options, 'r')) {
191
3.93M
    settabsi(L, "ftransfer", ar.ftransfer);
192
3.93M
    settabsi(L, "ntransfer", ar.ntransfer);
193
3.93M
  }
194
3.93M
  if (strchr(options, 't')) {
195
3.93M
    settabsb(L, "istailcall", ar.istailcall);
196
3.93M
    settabsi(L, "extraargs", ar.extraargs);
197
3.93M
  }
198
3.93M
  if (strchr(options, 'L'))
199
4.85k
    treatstackoption(L, L1, "activelines");
200
3.93M
  if (strchr(options, 'f'))
201
3.93M
    treatstackoption(L, L1, "func");
202
3.93M
  return 1;  /* return table */
203
3.93M
}
204
205
206
4.73k
static int db_getlocal (lua_State *L) {
207
4.73k
  int arg;
208
4.73k
  lua_State *L1 = getthread(L, &arg);
209
4.73k
  int nvar = (int)luaL_checkinteger(L, arg + 2);  /* local-variable index */
210
4.73k
  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
4.73k
  else {  /* stack-level argument */
216
4.73k
    lua_Debug ar;
217
4.73k
    const char *name;
218
4.73k
    int level = (int)luaL_checkinteger(L, arg + 1);
219
4.73k
    if (l_unlikely(!lua_getstack(L1, level, &ar)))  /* out of range? */
220
489
      return luaL_argerror(L, arg+1, "level out of range");
221
4.24k
    checkstack(L, L1, 1);
222
4.24k
    name = lua_getlocal(L1, &ar, nvar);
223
4.24k
    if (name) {
224
3.21k
      lua_xmove(L1, L, 1);  /* move local value */
225
3.21k
      lua_pushstring(L, name);  /* push name */
226
3.21k
      lua_rotate(L, -2, 1);  /* re-order */
227
3.21k
      return 2;
228
3.21k
    }
229
1.03k
    else {
230
1.03k
      luaL_pushfail(L);  /* no name (nor value) */
231
1.03k
      return 1;
232
1.03k
    }
233
4.24k
  }
234
4.73k
}
235
236
237
3.41k
static int db_setlocal (lua_State *L) {
238
3.41k
  int arg;
239
3.41k
  const char *name;
240
3.41k
  lua_State *L1 = getthread(L, &arg);
241
3.41k
  lua_Debug ar;
242
3.41k
  int level = (int)luaL_checkinteger(L, arg + 1);
243
3.41k
  int nvar = (int)luaL_checkinteger(L, arg + 2);
244
3.41k
  if (l_unlikely(!lua_getstack(L1, level, &ar)))  /* out of range? */
245
1.02k
    return luaL_argerror(L, arg+1, "level out of range");
246
2.38k
  luaL_checkany(L, arg+3);
247
2.38k
  lua_settop(L, arg+3);
248
2.38k
  checkstack(L, L1, 1);
249
2.38k
  lua_xmove(L, L1, 1);
250
2.38k
  name = lua_setlocal(L1, &ar, nvar);
251
2.38k
  if (name == NULL)
252
198
    lua_pop(L1, 1);  /* pop value (if not popped by 'lua_setlocal') */
253
2.38k
  lua_pushstring(L, name);
254
2.38k
  return 1;
255
3.41k
}
256
257
258
/*
259
** get (if 'get' is true) or set an upvalue from a closure
260
*/
261
75
static int auxupvalue (lua_State *L, int get) {
262
75
  const char *name;
263
75
  int n = (int)luaL_checkinteger(L, 2);  /* upvalue index */
264
75
  luaL_checktype(L, 1, LUA_TFUNCTION);  /* closure */
265
75
  name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
266
75
  if (name == NULL) return 0;
267
75
  lua_pushstring(L, name);
268
75
  lua_insert(L, -(get+1));  /* no-op if get is false */
269
75
  return get + 1;
270
75
}
271
272
273
75
static int db_getupvalue (lua_State *L) {
274
75
  return auxupvalue(L, 1);
275
75
}
276
277
278
27
static int db_setupvalue (lua_State *L) {
279
27
  luaL_checkany(L, 3);
280
27
  return auxupvalue(L, 0);
281
27
}
282
283
284
/*
285
** Check whether a given upvalue from a given closure exists and
286
** returns its index
287
*/
288
25
static void *checkupval (lua_State *L, int argf, int argnup, int *pnup) {
289
25
  void *id;
290
25
  int nup = (int)luaL_checkinteger(L, argnup);  /* upvalue index */
291
25
  luaL_checktype(L, argf, LUA_TFUNCTION);  /* closure */
292
25
  id = lua_upvalueid(L, argf, nup);
293
25
  if (pnup) {
294
0
    luaL_argcheck(L, id != NULL, argnup, "invalid upvalue index");
295
0
    *pnup = nup;
296
0
  }
297
25
  return id;
298
25
}
299
300
301
15
static int db_upvalueid (lua_State *L) {
302
15
  void *id = checkupval(L, 1, 2, NULL);
303
15
  if (id != NULL)
304
0
    lua_pushlightuserdata(L, id);
305
15
  else
306
15
    luaL_pushfail(L);
307
15
  return 1;
308
15
}
309
310
311
10
static int db_upvaluejoin (lua_State *L) {
312
10
  int n1, n2;
313
10
  checkupval(L, 1, 2, &n1);
314
10
  checkupval(L, 3, 4, &n2);
315
10
  luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
316
10
  luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
317
10
  lua_upvaluejoin(L, 1, n1, 3, n2);
318
10
  return 0;
319
10
}
320
321
322
/*
323
** Call hook function registered at hook table for the current
324
** thread (if there is one)
325
*/
326
5.14M
static void hookf (lua_State *L, lua_Debug *ar) {
327
5.14M
  static const char *const hooknames[] =
328
5.14M
    {"call", "return", "line", "count", "tail call"};
329
5.14M
  lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
330
5.14M
  lua_pushthread(L);
331
5.14M
  if (lua_rawget(L, -2) == LUA_TFUNCTION) {  /* is there a hook function? */
332
5.06M
    lua_pushstring(L, hooknames[(int)ar->event]);  /* push event name */
333
5.06M
    if (ar->currentline >= 0)
334
2.32M
      lua_pushinteger(L, ar->currentline);  /* push current line */
335
2.73M
    else lua_pushnil(L);
336
5.06M
    lua_assert(lua_getinfo(L, "lS", ar));
337
5.06M
    lua_call(L, 2, 0);  /* call hook function */
338
5.06M
  }
339
5.14M
}
340
341
342
/*
343
** Convert a string mask (for 'sethook') into a bit mask
344
*/
345
361k
static int makemask (const char *smask, int count) {
346
361k
  int mask = 0;
347
361k
  if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
348
361k
  if (strchr(smask, 'r')) mask |= LUA_MASKRET;
349
361k
  if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
350
361k
  if (count > 0) mask |= LUA_MASKCOUNT;
351
361k
  return mask;
352
361k
}
353
354
355
/*
356
** Convert a bit mask (for 'gethook') into a string mask
357
*/
358
47.5k
static char *unmakemask (int mask, char *smask) {
359
47.5k
  int i = 0;
360
47.5k
  if (mask & LUA_MASKCALL) smask[i++] = 'c';
361
47.5k
  if (mask & LUA_MASKRET) smask[i++] = 'r';
362
47.5k
  if (mask & LUA_MASKLINE) smask[i++] = 'l';
363
47.5k
  smask[i] = '\0';
364
47.5k
  return smask;
365
47.5k
}
366
367
368
508k
static int db_sethook (lua_State *L) {
369
508k
  int arg, mask, count;
370
508k
  lua_Hook func;
371
508k
  lua_State *L1 = getthread(L, &arg);
372
508k
  if (lua_isnoneornil(L, arg+1)) {  /* no hook? */
373
147k
    lua_settop(L, arg+1);
374
147k
    func = NULL; mask = 0; count = 0;  /* turn off hooks */
375
147k
  }
376
361k
  else {
377
361k
    const char *smask = luaL_checkstring(L, arg+2);
378
361k
    luaL_checktype(L, arg+1, LUA_TFUNCTION);
379
361k
    count = (int)luaL_optinteger(L, arg + 3, 0);
380
361k
    func = hookf; mask = makemask(smask, count);
381
361k
  }
382
508k
  if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) {
383
    /* table just created; initialize it */
384
1.51k
    lua_pushliteral(L, "k");
385
1.51k
    lua_setfield(L, -2, "__mode");  /** hooktable.__mode = "k" */
386
1.51k
    lua_pushvalue(L, -1);
387
1.51k
    lua_setmetatable(L, -2);  /* metatable(hooktable) = hooktable */
388
1.51k
  }
389
508k
  checkstack(L, L1, 1);
390
508k
  lua_pushthread(L1); lua_xmove(L1, L, 1);  /* key (thread) */
391
508k
  lua_pushvalue(L, arg + 1);  /* value (hook function) */
392
508k
  lua_rawset(L, -3);  /* hooktable[L1] = new Lua hook */
393
508k
  lua_sethook(L1, func, mask, count);
394
508k
  return 0;
395
508k
}
396
397
398
47.6k
static int db_gethook (lua_State *L) {
399
47.6k
  int arg;
400
47.6k
  lua_State *L1 = getthread(L, &arg);
401
47.6k
  char buff[5];
402
47.6k
  int mask = lua_gethookmask(L1);
403
47.6k
  lua_Hook hook = lua_gethook(L1);
404
47.6k
  if (hook == NULL) {  /* no hook? */
405
121
    luaL_pushfail(L);
406
121
    return 1;
407
121
  }
408
47.5k
  else if (hook != hookf)  /* external hook? */
409
82
    lua_pushliteral(L, "external hook");
410
47.4k
  else {  /* hook table must exist */
411
47.4k
    lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
412
47.4k
    checkstack(L, L1, 1);
413
47.4k
    lua_pushthread(L1); lua_xmove(L1, L, 1);
414
47.4k
    lua_rawget(L, -2);   /* 1st result = hooktable[L1] */
415
47.4k
    lua_remove(L, -2);  /* remove hook table */
416
47.4k
  }
417
47.5k
  lua_pushstring(L, unmakemask(mask, buff));  /* 2nd result = mask */
418
47.5k
  lua_pushinteger(L, lua_gethookcount(L1));  /* 3rd result = count */
419
47.5k
  return 3;
420
47.6k
}
421
422
423
97
static int db_debug (lua_State *L) {
424
97
  for (;;) {
425
97
    char buffer[250];
426
97
    lua_writestringerror("%s", "lua_debug> ");
427
97
    if (fgets(buffer, sizeof(buffer), stdin) == NULL ||
428
97
        strcmp(buffer, "cont\n") == 0)
429
97
      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
97
}
436
437
438
112k
static int db_traceback (lua_State *L) {
439
112k
  int arg;
440
112k
  lua_State *L1 = getthread(L, &arg);
441
112k
  const char *msg = lua_tostring(L, arg + 1);
442
112k
  if (msg == NULL && !lua_isnoneornil(L, arg + 1))  /* non-string 'msg'? */
443
84
    lua_pushvalue(L, arg + 1);  /* return it untouched */
444
111k
  else {
445
111k
    int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0);
446
111k
    luaL_traceback(L, L1, msg, level);
447
111k
  }
448
112k
  return 1;
449
112k
}
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
29.9k
LUAMOD_API int luaopen_debug (lua_State *L) {
474
29.9k
  luaL_newlib(L, dblib);
475
29.9k
  return 1;
476
29.9k
}
477