Coverage Report

Created: 2025-07-18 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
6.42M
static void checkstack (lua_State *L, lua_State *L1, int n) {
37
6.42M
  if (l_unlikely(L != L1 && !lua_checkstack(L1, n)))
38
0
    luaL_error(L, "stack overflow");
39
6.42M
}
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
895
static int db_getmetatable (lua_State *L) {
49
895
  luaL_checkany(L, 1);
50
895
  if (!lua_getmetatable(L, 1)) {
51
526
    lua_pushnil(L);  /* no metatable */
52
526
  }
53
895
  return 1;
54
895
}
55
56
57
330k
static int db_setmetatable (lua_State *L) {
58
330k
  int t = lua_type(L, 2);
59
330k
  luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
60
330k
  lua_settop(L, 2);
61
330k
  lua_setmetatable(L, 1);
62
330k
  return 1;  /* return 1st argument */
63
330k
}
64
65
66
85
static int db_getuservalue (lua_State *L) {
67
85
  int n = (int)luaL_optinteger(L, 2, 1);
68
85
  if (lua_type(L, 1) != LUA_TUSERDATA)
69
85
    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
85
  return 1;
75
85
}
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
6.68M
static lua_State *getthread (lua_State *L, int *arg) {
96
6.68M
  if (lua_isthread(L, 1)) {
97
6
    *arg = 1;
98
6
    return lua_tothread(L, 1);
99
6
  }
100
6.68M
  else {
101
6.68M
    *arg = 0;
102
6.68M
    return L;  /* function will operate over current thread */
103
6.68M
  }
104
6.68M
}
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
21.8M
static void settabss (lua_State *L, const char *k, const char *v) {
113
21.8M
  lua_pushstring(L, v);
114
21.8M
  lua_setfield(L, -2, k);
115
21.8M
}
116
117
43.6M
static void settabsi (lua_State *L, const char *k, int v) {
118
43.6M
  lua_pushinteger(L, v);
119
43.6M
  lua_setfield(L, -2, k);
120
43.6M
}
121
122
10.9M
static void settabsb (lua_State *L, const char *k, int v) {
123
10.9M
  lua_pushboolean(L, v);
124
10.9M
  lua_setfield(L, -2, k);
125
10.9M
}
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
5.49M
static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
136
5.49M
  if (L == L1)
137
5.49M
    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
5.49M
  lua_setfield(L, -2, fname);  /* put object into table */
141
5.49M
}
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
5.81M
static int db_getinfo (lua_State *L) {
151
5.81M
  lua_Debug ar;
152
5.81M
  int arg;
153
5.81M
  lua_State *L1 = getthread(L, &arg);
154
5.81M
  const char *options = luaL_optstring(L, arg+2, "flnSrtu");
155
5.81M
  checkstack(L, L1, 3);
156
5.81M
  luaL_argcheck(L, options[0] != '>', arg + 2, "invalid option '>'");
157
5.81M
  if (lua_isfunction(L, arg + 1)) {  /* info about a function? */
158
8.77k
    options = lua_pushfstring(L, ">%s", options);  /* add '>' to 'options' */
159
8.77k
    lua_pushvalue(L, arg + 1);  /* move function to 'L1' stack */
160
8.77k
    lua_xmove(L, L1, 1);
161
8.77k
  }
162
5.81M
  else {  /* stack level */
163
5.81M
    if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
164
323k
      luaL_pushfail(L);  /* level out of range */
165
323k
      return 1;
166
323k
    }
167
5.81M
  }
168
5.49M
  if (!lua_getinfo(L1, options, &ar))
169
702
    return luaL_argerror(L, arg+2, "invalid option");
170
5.49M
  lua_newtable(L);  /* table to collect results */
171
5.49M
  if (strchr(options, 'S')) {
172
5.45M
    lua_pushlstring(L, ar.source, ar.srclen);
173
5.45M
    lua_setfield(L, -2, "source");
174
5.45M
    settabss(L, "short_src", ar.short_src);
175
5.45M
    settabsi(L, "linedefined", ar.linedefined);
176
5.45M
    settabsi(L, "lastlinedefined", ar.lastlinedefined);
177
5.45M
    settabss(L, "what", ar.what);
178
5.45M
  }
179
5.49M
  if (strchr(options, 'l'))
180
5.45M
    settabsi(L, "currentline", ar.currentline);
181
5.49M
  if (strchr(options, 'u')) {
182
5.45M
    settabsi(L, "nups", ar.nups);
183
5.45M
    settabsi(L, "nparams", ar.nparams);
184
5.45M
    settabsb(L, "isvararg", ar.isvararg);
185
5.45M
  }
186
5.49M
  if (strchr(options, 'n')) {
187
5.45M
    settabss(L, "name", ar.name);
188
5.45M
    settabss(L, "namewhat", ar.namewhat);
189
5.45M
  }
190
5.49M
  if (strchr(options, 'r')) {
191
5.45M
    settabsi(L, "ftransfer", ar.ftransfer);
192
5.45M
    settabsi(L, "ntransfer", ar.ntransfer);
193
5.45M
  }
194
5.49M
  if (strchr(options, 't')) {
195
5.45M
    settabsb(L, "istailcall", ar.istailcall);
196
5.45M
    settabsi(L, "extraargs", ar.extraargs);
197
5.45M
  }
198
5.49M
  if (strchr(options, 'L'))
199
40.7k
    treatstackoption(L, L1, "activelines");
200
5.49M
  if (strchr(options, 'f'))
201
5.45M
    treatstackoption(L, L1, "func");
202
5.49M
  return 1;  /* return table */
203
5.49M
}
204
205
206
6.12k
static int db_getlocal (lua_State *L) {
207
6.12k
  int arg;
208
6.12k
  lua_State *L1 = getthread(L, &arg);
209
6.12k
  int nvar = (int)luaL_checkinteger(L, arg + 2);  /* local-variable index */
210
6.12k
  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
6.12k
  else {  /* stack-level argument */
216
6.12k
    lua_Debug ar;
217
6.12k
    const char *name;
218
6.12k
    int level = (int)luaL_checkinteger(L, arg + 1);
219
6.12k
    if (l_unlikely(!lua_getstack(L1, level, &ar)))  /* out of range? */
220
712
      return luaL_argerror(L, arg+1, "level out of range");
221
5.40k
    checkstack(L, L1, 1);
222
5.40k
    name = lua_getlocal(L1, &ar, nvar);
223
5.40k
    if (name) {
224
4.12k
      lua_xmove(L1, L, 1);  /* move local value */
225
4.12k
      lua_pushstring(L, name);  /* push name */
226
4.12k
      lua_rotate(L, -2, 1);  /* re-order */
227
4.12k
      return 2;
228
4.12k
    }
229
1.28k
    else {
230
1.28k
      luaL_pushfail(L);  /* no name (nor value) */
231
1.28k
      return 1;
232
1.28k
    }
233
5.40k
  }
234
6.12k
}
235
236
237
5.87k
static int db_setlocal (lua_State *L) {
238
5.87k
  int arg;
239
5.87k
  const char *name;
240
5.87k
  lua_State *L1 = getthread(L, &arg);
241
5.87k
  lua_Debug ar;
242
5.87k
  int level = (int)luaL_checkinteger(L, arg + 1);
243
5.87k
  int nvar = (int)luaL_checkinteger(L, arg + 2);
244
5.87k
  if (l_unlikely(!lua_getstack(L1, level, &ar)))  /* out of range? */
245
1.03k
    return luaL_argerror(L, arg+1, "level out of range");
246
4.84k
  luaL_checkany(L, arg+3);
247
4.84k
  lua_settop(L, arg+3);
248
4.84k
  checkstack(L, L1, 1);
249
4.84k
  lua_xmove(L, L1, 1);
250
4.84k
  name = lua_setlocal(L1, &ar, nvar);
251
4.84k
  if (name == NULL)
252
2.40k
    lua_pop(L1, 1);  /* pop value (if not popped by 'lua_setlocal') */
253
4.84k
  lua_pushstring(L, name);
254
4.84k
  return 1;
255
5.87k
}
256
257
258
/*
259
** get (if 'get' is true) or set an upvalue from a closure
260
*/
261
72
static int auxupvalue (lua_State *L, int get) {
262
72
  const char *name;
263
72
  int n = (int)luaL_checkinteger(L, 2);  /* upvalue index */
264
72
  luaL_checktype(L, 1, LUA_TFUNCTION);  /* closure */
265
72
  name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
266
72
  if (name == NULL) return 0;
267
72
  lua_pushstring(L, name);
268
72
  lua_insert(L, -(get+1));  /* no-op if get is false */
269
72
  return get + 1;
270
72
}
271
272
273
72
static int db_getupvalue (lua_State *L) {
274
72
  return auxupvalue(L, 1);
275
72
}
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
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
6.91M
static void hookf (lua_State *L, lua_Debug *ar) {
327
6.91M
  static const char *const hooknames[] =
328
6.91M
    {"call", "return", "line", "count", "tail call"};
329
6.91M
  lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
330
6.91M
  lua_pushthread(L);
331
6.91M
  if (lua_rawget(L, -2) == LUA_TFUNCTION) {  /* is there a hook function? */
332
6.66M
    lua_pushstring(L, hooknames[(int)ar->event]);  /* push event name */
333
6.66M
    if (ar->currentline >= 0)
334
3.03M
      lua_pushinteger(L, ar->currentline);  /* push current line */
335
3.62M
    else lua_pushnil(L);
336
6.66M
    lua_assert(lua_getinfo(L, "lS", ar));
337
6.66M
    lua_call(L, 2, 0);  /* call hook function */
338
6.66M
  }
339
6.91M
}
340
341
342
/*
343
** Convert a string mask (for 'sethook') into a bit mask
344
*/
345
425k
static int makemask (const char *smask, int count) {
346
425k
  int mask = 0;
347
425k
  if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
348
425k
  if (strchr(smask, 'r')) mask |= LUA_MASKRET;
349
425k
  if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
350
425k
  if (count > 0) mask |= LUA_MASKCOUNT;
351
425k
  return mask;
352
425k
}
353
354
355
/*
356
** Convert a bit mask (for 'gethook') into a string mask
357
*/
358
12.1k
static char *unmakemask (int mask, char *smask) {
359
12.1k
  int i = 0;
360
12.1k
  if (mask & LUA_MASKCALL) smask[i++] = 'c';
361
12.1k
  if (mask & LUA_MASKRET) smask[i++] = 'r';
362
12.1k
  if (mask & LUA_MASKLINE) smask[i++] = 'l';
363
12.1k
  smask[i] = '\0';
364
12.1k
  return smask;
365
12.1k
}
366
367
368
586k
static int db_sethook (lua_State *L) {
369
586k
  int arg, mask, count;
370
586k
  lua_Hook func;
371
586k
  lua_State *L1 = getthread(L, &arg);
372
586k
  if (lua_isnoneornil(L, arg+1)) {  /* no hook? */
373
161k
    lua_settop(L, arg+1);
374
161k
    func = NULL; mask = 0; count = 0;  /* turn off hooks */
375
161k
  }
376
425k
  else {
377
425k
    const char *smask = luaL_checkstring(L, arg+2);
378
425k
    luaL_checktype(L, arg+1, LUA_TFUNCTION);
379
425k
    count = (int)luaL_optinteger(L, arg + 3, 0);
380
425k
    func = hookf; mask = makemask(smask, count);
381
425k
  }
382
586k
  if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) {
383
    /* table just created; initialize it */
384
1.78k
    lua_pushliteral(L, "k");
385
1.78k
    lua_setfield(L, -2, "__mode");  /** hooktable.__mode = "k" */
386
1.78k
    lua_pushvalue(L, -1);
387
1.78k
    lua_setmetatable(L, -2);  /* metatable(hooktable) = hooktable */
388
1.78k
  }
389
586k
  checkstack(L, L1, 1);
390
586k
  lua_pushthread(L1); lua_xmove(L1, L, 1);  /* key (thread) */
391
586k
  lua_pushvalue(L, arg + 1);  /* value (hook function) */
392
586k
  lua_rawset(L, -3);  /* hooktable[L1] = new Lua hook */
393
586k
  lua_sethook(L1, func, mask, count);
394
586k
  return 0;
395
586k
}
396
397
398
119k
static int db_gethook (lua_State *L) {
399
119k
  int arg;
400
119k
  lua_State *L1 = getthread(L, &arg);
401
119k
  char buff[5];
402
119k
  int mask = lua_gethookmask(L1);
403
119k
  lua_Hook hook = lua_gethook(L1);
404
119k
  if (hook == NULL) {  /* no hook? */
405
107k
    luaL_pushfail(L);
406
107k
    return 1;
407
107k
  }
408
12.1k
  else if (hook != hookf)  /* external hook? */
409
84
    lua_pushliteral(L, "external hook");
410
12.0k
  else {  /* hook table must exist */
411
12.0k
    lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
412
12.0k
    checkstack(L, L1, 1);
413
12.0k
    lua_pushthread(L1); lua_xmove(L1, L, 1);
414
12.0k
    lua_rawget(L, -2);   /* 1st result = hooktable[L1] */
415
12.0k
    lua_remove(L, -2);  /* remove hook table */
416
12.0k
  }
417
12.1k
  lua_pushstring(L, unmakemask(mask, buff));  /* 2nd result = mask */
418
12.1k
  lua_pushinteger(L, lua_gethookcount(L1));  /* 3rd result = count */
419
12.1k
  return 3;
420
119k
}
421
422
423
108
static int db_debug (lua_State *L) {
424
108
  for (;;) {
425
108
    char buffer[250];
426
108
    lua_writestringerror("%s", "lua_debug> ");
427
108
    if (fgets(buffer, sizeof(buffer), stdin) == NULL ||
428
108
        strcmp(buffer, "cont\n") == 0)
429
108
      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
108
}
436
437
438
144k
static int db_traceback (lua_State *L) {
439
144k
  int arg;
440
144k
  lua_State *L1 = getthread(L, &arg);
441
144k
  const char *msg = lua_tostring(L, arg + 1);
442
144k
  if (msg == NULL && !lua_isnoneornil(L, arg + 1))  /* non-string 'msg'? */
443
88
    lua_pushvalue(L, arg + 1);  /* return it untouched */
444
144k
  else {
445
144k
    int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0);
446
144k
    luaL_traceback(L, L1, msg, level);
447
144k
  }
448
144k
  return 1;
449
144k
}
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
31.9k
LUAMOD_API int luaopen_debug (lua_State *L) {
474
31.9k
  luaL_newlib(L, dblib);
475
31.9k
  return 1;
476
31.9k
}
477