• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tarantool / luajit / 5785127203

07 Aug 2023 12:31PM UTC coverage: 84.248% (-0.03%) from 84.282%
5785127203

push

github

ligurio
ci: support coveralls

The patch adds an action that installs gcovr and a workflow that runs
regression test suites, produces a summary of current code coverage and
sends code coverage data to Coveralls. Coveralls is a web service that
lets you inspect every detail of your coverage. See Tarantool's LuaJIT
page on Coveralls [1].

1. https://coveralls.io/github/tarantool/luajit

5123 of 6002 branches covered (85.35%)

Branch coverage included in aggregate %.

19656 of 23410 relevant lines covered (83.96%)

244602.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.49
/src/lib_debug.c
1
/*
2
** Debug library.
3
** Copyright (C) 2005-2017 Mike Pall. See Copyright Notice in luajit.h
4
**
5
** Major portions taken verbatim or adapted from the Lua interpreter.
6
** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
7
*/
8

9
#define lib_debug_c
10
#define LUA_LIB
11

12
#include "lua.h"
13
#include "lauxlib.h"
14
#include "lualib.h"
15

16
#include "lj_obj.h"
17
#include "lj_gc.h"
18
#include "lj_err.h"
19
#include "lj_debug.h"
20
#include "lj_lib.h"
21

22
/* ------------------------------------------------------------------------ */
23

24
#define LJLIB_MODULE_debug
25

26
LJLIB_CF(debug_getregistry)
1✔
27
{
28
  copyTV(L, L->top++, registry(L));
1✔
29
  return 1;
1✔
30
}
31

32
LJLIB_CF(debug_getmetatable)        LJLIB_REC(.)
10✔
33
{
34
  lj_lib_checkany(L, 1);
10✔
35
  if (!lua_getmetatable(L, 1)) {
10✔
36
    setnilV(L->top-1);
3✔
37
  }
38
  return 1;
10✔
39
}
40

41
LJLIB_CF(debug_setmetatable)
23✔
42
{
43
  lj_lib_checktabornil(L, 2);
23✔
44
  L->top = L->base+2;
22✔
45
  lua_setmetatable(L, 1);
22✔
46
#if !LJ_52
47
  setboolV(L->top-1, 1);
22✔
48
#endif
49
  return 1;
22✔
50
}
51

52
LJLIB_CF(debug_getfenv)
13✔
53
{
54
  lj_lib_checkany(L, 1);
13✔
55
  lua_getfenv(L, 1);
13✔
56
  return 1;
13✔
57
}
58

59
LJLIB_CF(debug_setfenv)
6✔
60
{
61
  lj_lib_checktab(L, 2);
6✔
62
  L->top = L->base+2;
6✔
63
  if (!lua_setfenv(L, 1))
6✔
64
    lj_err_caller(L, LJ_ERR_SETFENV);
1✔
65
  return 1;
5✔
66
}
67

68
/* ------------------------------------------------------------------------ */
69

70
static void settabss(lua_State *L, const char *i, const char *v)
193✔
71
{
72
  lua_pushstring(L, v);
193✔
73
  lua_setfield(L, -2, i);
193✔
74
}
77✔
75

76
static void settabsi(lua_State *L, const char *i, int v)
194✔
77
{
78
  lua_pushinteger(L, v);
194✔
79
  lua_setfield(L, -2, i);
194✔
80
}
40✔
81

82
static void settabsb(lua_State *L, const char *i, int v)
38✔
83
{
84
  lua_pushboolean(L, v);
38✔
85
  lua_setfield(L, -2, i);
38✔
86
}
38✔
87

88
static lua_State *getthread(lua_State *L, int *arg)
61✔
89
{
90
  if (L->base < L->top && tvisthread(L->base)) {
59✔
91
    *arg = 1;
1✔
92
    return threadV(L->base);
1✔
93
  } else {
94
    *arg = 0;
95
    return L;
96
  }
97
}
98

99
static void treatstackoption(lua_State *L, lua_State *L1, const char *fname)
39✔
100
{
101
  if (L == L1) {
39✔
102
    lua_pushvalue(L, -2);
39✔
103
    lua_remove(L, -3);
39✔
104
  }
105
  else
106
    lua_xmove(L1, L, 1);
×
107
  lua_setfield(L, -2, fname);
39✔
108
}
39✔
109

110
LJLIB_CF(debug_getinfo)
45✔
111
{
112
  lj_Debug ar;
45✔
113
  int arg, opt_f = 0, opt_L = 0;
45✔
114
  lua_State *L1 = getthread(L, &arg);
45✔
115
  const char *options = luaL_optstring(L, arg+2, "flnSu");
45✔
116
  if (lua_isnumber(L, arg+1)) {
45✔
117
    if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), (lua_Debug *)&ar)) {
40✔
118
      setnilV(L->top-1);
1✔
119
      return 1;
1✔
120
    }
121
  } else if (L->base+arg < L->top && tvisfunc(L->base+arg)) {
5✔
122
    options = lua_pushfstring(L, ">%s", options);
4✔
123
    setfuncV(L1, L1->top++, funcV(L->base+arg));
4✔
124
  } else {
125
    lj_err_arg(L, arg+1, LJ_ERR_NOFUNCL);
1✔
126
  }
127
  if (!lj_debug_getinfo(L1, options, &ar, 1))
43✔
128
    lj_err_arg(L, arg+2, LJ_ERR_INVOPT);
1✔
129
  lua_createtable(L, 0, 16);  /* Create result table. */
42✔
130
  for (; *options; options++) {
281✔
131
    switch (*options) {
197✔
132
    case 'S':
39✔
133
      settabss(L, "source", ar.source);
39✔
134
      settabss(L, "short_src", ar.short_src);
39✔
135
      settabsi(L, "linedefined", ar.linedefined);
39✔
136
      settabsi(L, "lastlinedefined", ar.lastlinedefined);
39✔
137
      settabss(L, "what", ar.what);
39✔
138
      break;
139
    case 'l':
40✔
140
      settabsi(L, "currentline", ar.currentline);
40✔
141
      break;
142
    case 'u':
38✔
143
      settabsi(L, "nups", ar.nups);
38✔
144
      settabsi(L, "nparams", ar.nparams);
38✔
145
      settabsb(L, "isvararg", ar.isvararg);
38✔
146
      break;
147
    case 'n':
38✔
148
      settabss(L, "name", ar.name);
38✔
149
      settabss(L, "namewhat", ar.namewhat);
235✔
150
      break;
151
    case 'f': opt_f = 1; break;
38✔
152
    case 'L': opt_L = 1; break;
1✔
153
    default: break;
154
    }
155
  }
156
  if (opt_L) treatstackoption(L, L1, "activelines");
42✔
157
  if (opt_f) treatstackoption(L, L1, "func");
42✔
158
  return 1;  /* Return result table. */
159
}
160

161
LJLIB_CF(debug_getlocal)
4✔
162
{
163
  int arg;
4✔
164
  lua_State *L1 = getthread(L, &arg);
4✔
165
  lua_Debug ar;
4✔
166
  const char *name;
4✔
167
  int slot = lj_lib_checkint(L, arg+2);
4✔
168
  if (tvisfunc(L->base+arg)) {
4✔
169
    L->top = L->base+arg+1;
×
170
    lua_pushstring(L, lua_getlocal(L, NULL, slot));
×
171
    return 1;
×
172
  }
173
  if (!lua_getstack(L1, lj_lib_checkint(L, arg+1), &ar))
4✔
174
    lj_err_arg(L, arg+1, LJ_ERR_LVLRNG);
1✔
175
  name = lua_getlocal(L1, &ar, slot);
3✔
176
  if (name) {
3✔
177
    lua_xmove(L1, L, 1);
3✔
178
    lua_pushstring(L, name);
3✔
179
    lua_pushvalue(L, -2);
3✔
180
    return 2;
3✔
181
  } else {
182
    setnilV(L->top-1);
×
183
    return 1;
×
184
  }
185
}
186

187
LJLIB_CF(debug_setlocal)
3✔
188
{
189
  int arg;
3✔
190
  lua_State *L1 = getthread(L, &arg);
3✔
191
  lua_Debug ar;
3✔
192
  TValue *tv;
3✔
193
  if (!lua_getstack(L1, lj_lib_checkint(L, arg+1), &ar))
3✔
194
    lj_err_arg(L, arg+1, LJ_ERR_LVLRNG);
1✔
195
  tv = lj_lib_checkany(L, arg+3);
2✔
196
  copyTV(L1, L1->top++, tv);
2✔
197
  lua_pushstring(L, lua_setlocal(L1, &ar, lj_lib_checkint(L, arg+2)));
2✔
198
  return 1;
2✔
199
}
200

201
static int debug_getupvalue(lua_State *L, int get)
4✔
202
{
203
  int32_t n = lj_lib_checkint(L, 2);
4✔
204
  const char *name;
4✔
205
  lj_lib_checkfunc(L, 1);
4✔
206
  name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
4✔
207
  if (name) {
4✔
208
    lua_pushstring(L, name);
3✔
209
    if (!get) return 1;
3✔
210
    copyTV(L, L->top, L->top-2);
1✔
211
    L->top++;
1✔
212
    return 2;
1✔
213
  }
214
  return 0;
215
}
216

217
LJLIB_CF(debug_getupvalue)
1✔
218
{
219
  return debug_getupvalue(L, 1);
1✔
220
}
221

222
LJLIB_CF(debug_setupvalue)
3✔
223
{
224
  lj_lib_checkany(L, 3);
3✔
225
  return debug_getupvalue(L, 0);
3✔
226
}
227

228
LJLIB_CF(debug_upvalueid)
1✔
229
{
230
  GCfunc *fn = lj_lib_checkfunc(L, 1);
1✔
231
  int32_t n = lj_lib_checkint(L, 2) - 1;
1✔
232
  if ((uint32_t)n >= fn->l.nupvalues)
1✔
233
    lj_err_arg(L, 2, LJ_ERR_IDXRNG);
×
234
  lua_pushlightuserdata(L, isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) :
1✔
235
                                           (void *)&fn->c.upvalue[n]);
236
  return 1;
1✔
237
}
238

239
LJLIB_CF(debug_upvaluejoin)
×
240
{
241
  GCfunc *fn[2];
×
242
  GCRef *p[2];
×
243
  int i;
×
244
  for (i = 0; i < 2; i++) {
×
245
    int32_t n;
×
246
    fn[i] = lj_lib_checkfunc(L, 2*i+1);
×
247
    if (!isluafunc(fn[i]))
×
248
      lj_err_arg(L, 2*i+1, LJ_ERR_NOLFUNC);
×
249
    n = lj_lib_checkint(L, 2*i+2) - 1;
×
250
    if ((uint32_t)n >= fn[i]->l.nupvalues)
×
251
      lj_err_arg(L, 2*i+2, LJ_ERR_IDXRNG);
×
252
    p[i] = &fn[i]->l.uvptr[n];
×
253
  }
254
  setgcrefr(*p[0], *p[1]);
×
255
  lj_gc_objbarrier(L, fn[0], gcref(*p[1]));
×
256
  return 0;
×
257
}
258

259
#if LJ_52
260
LJLIB_CF(debug_getuservalue)
261
{
262
  TValue *o = L->base;
263
  if (o < L->top && tvisudata(o))
264
    settabV(L, o, tabref(udataV(o)->env));
265
  else
266
    setnilV(o);
267
  L->top = o+1;
268
  return 1;
269
}
270

271
LJLIB_CF(debug_setuservalue)
272
{
273
  TValue *o = L->base;
274
  if (!(o < L->top && tvisudata(o)))
275
    lj_err_argt(L, 1, LUA_TUSERDATA);
276
  if (!(o+1 < L->top && tvistab(o+1)))
277
    lj_err_argt(L, 2, LUA_TTABLE);
278
  L->top = o+2;
279
  lua_setfenv(L, 1);
280
  return 1;
281
}
282
#endif
283

284
/* ------------------------------------------------------------------------ */
285

286
#define KEY_HOOK        (U64x(80000000,00000000)|'h')
287

288
static void hookf(lua_State *L, lua_Debug *ar)
240✔
289
{
290
  static const char *const hooknames[] =
240✔
291
    {"call", "return", "line", "count", "tail return"};
292
  (L->top++)->u64 = KEY_HOOK;
240✔
293
  lua_rawget(L, LUA_REGISTRYINDEX);
240✔
294
  if (lua_isfunction(L, -1)) {
240✔
295
    lua_pushstring(L, hooknames[(int)ar->event]);
240✔
296
    if (ar->currentline >= 0)
240✔
297
      lua_pushinteger(L, ar->currentline);
×
298
    else lua_pushnil(L);
240✔
299
    lua_call(L, 2, 0);
240✔
300
  }
301
}
240✔
302

303
static int makemask(const char *smask, int count)
1✔
304
{
305
  int mask = 0;
1✔
306
  if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
1✔
307
  if (strchr(smask, 'r')) mask |= LUA_MASKRET;
1✔
308
  if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
1✔
309
  if (count > 0) mask |= LUA_MASKCOUNT;
1✔
310
  return mask;
1✔
311
}
312

313
static char *unmakemask(int mask, char *smask)
3✔
314
{
315
  int i = 0;
3✔
316
  if (mask & LUA_MASKCALL) smask[i++] = 'c';
2✔
317
  if (mask & LUA_MASKRET) smask[i++] = 'r';
3✔
318
  if (mask & LUA_MASKLINE) smask[i++] = 'l';
3✔
319
  smask[i] = '\0';
3✔
320
  return smask;
3✔
321
}
322

323
LJLIB_CF(debug_sethook)
2✔
324
{
325
  int arg, mask, count;
2✔
326
  lua_Hook func;
2✔
327
  (void)getthread(L, &arg);
2✔
328
  if (lua_isnoneornil(L, arg+1)) {
2✔
329
    lua_settop(L, arg+1);
1✔
330
    func = NULL; mask = 0; count = 0;  /* turn off hooks */
1✔
331
  } else {
332
    const char *smask = luaL_checkstring(L, arg+2);
1✔
333
    luaL_checktype(L, arg+1, LUA_TFUNCTION);
1✔
334
    count = luaL_optint(L, arg+3, 0);
1✔
335
    func = hookf; mask = makemask(smask, count);
1✔
336
  }
337
  (L->top++)->u64 = KEY_HOOK;
2✔
338
  lua_pushvalue(L, arg+1);
2✔
339
  lua_rawset(L, LUA_REGISTRYINDEX);
2✔
340
  lua_sethook(L, func, mask, count);
2✔
341
  return 0;
2✔
342
}
343

344
LJLIB_CF(debug_gethook)
3✔
345
{
346
  char buff[5];
3✔
347
  int mask = lua_gethookmask(L);
3✔
348
  lua_Hook hook = lua_gethook(L);
3✔
349
  if (hook != NULL && hook != hookf) {  /* external hook? */
3✔
350
    lua_pushliteral(L, "external hook");
×
351
  } else {
352
    (L->top++)->u64 = KEY_HOOK;
3✔
353
    lua_rawget(L, LUA_REGISTRYINDEX);   /* get hook */
3✔
354
  }
355
  lua_pushstring(L, unmakemask(mask, buff));
6✔
356
  lua_pushinteger(L, lua_gethookcount(L));
3✔
357
  return 3;
3✔
358
}
359

360
/* ------------------------------------------------------------------------ */
361

362
LJLIB_CF(debug_debug)
2✔
363
{
364
  for (;;) {
10✔
365
    char buffer[250];
6✔
366
    fputs("lua_debug> ", stderr);
6✔
367
    if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
6✔
368
        strcmp(buffer, "cont\n") == 0)
5✔
369
      return 0;
2✔
370
    if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
8✔
371
        lua_pcall(L, 0, 0, 0)) {
4✔
372
      const char *s = lua_tostring(L, -1);
2✔
373
      fputs(s ? s : "(error object is not a string)", stderr);
3✔
374
      fputs("\n", stderr);
2✔
375
    }
376
    lua_settop(L, 0);  /* remove eventual returns */
4✔
377
  }
378
}
379

380
/* ------------------------------------------------------------------------ */
381

382
#define LEVELS1        12        /* size of the first part of the stack */
383
#define LEVELS2        10        /* size of the second part of the stack */
384

385
LJLIB_CF(debug_traceback)
7✔
386
{
387
  int arg;
7✔
388
  lua_State *L1 = getthread(L, &arg);
7✔
389
  const char *msg = lua_tostring(L, arg+1);
7✔
390
  if (msg == NULL && L->top > L->base+arg)
7✔
391
    L->top = L->base+arg+1;
1✔
392
  else
393
    luaL_traceback(L, L1, msg, lj_lib_optint(L, arg+2, (L == L1)));
6✔
394
  return 1;
7✔
395
}
396

397
/* ------------------------------------------------------------------------ */
398

399
#include "lj_libdef.h"
400

401
LUALIB_API int luaopen_debug(lua_State *L)
213✔
402
{
403
  LJ_LIB_REG(L, LUA_DBLIBNAME, debug);
213✔
404
  return 1;
213✔
405
}
406

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc