Coverage Report

Created: 2023-09-15 06:20

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