summaryrefslogtreecommitdiff
path: root/etc/trace.c
blob: 7c0b86ed9a0951e276119e39265ed3a79535f3e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* trace.c
* a simple execution tracer for Lua
*/

#include <stdio.h>
#include <string.h>
#include "lua.h"
#include "lualib.h"
#include "luadebug.h"

lua_State *lua_state = NULL;
#define	L	lua_state	/* lazy! */

static FILE* LOG;		/* output file */
static int I=0;			/* indentation level */

static void linehook(lua_State *L, lua_Debug *ar)
{
 fprintf(LOG,"%*sdo_line(%d)\t-- %d\n",I,"",ar->currentline,I);
}

static void callhook(lua_State *L, lua_Debug *ar)
{
 fprintf(LOG,"%*sdo_%s\t-- %p %d\n",I,"",ar->event,ar->_func,I);
 if (*ar->event=='r') --I; else ++I;
}

void start_trace(FILE* logfile)
{
 lua_setlinehook(L,linehook);
 lua_setcallhook(L,callhook);
 LOG=logfile;
}

void stop_trace(void)
{
 lua_setlinehook(L,NULL);
 lua_setcallhook(L,NULL);
 fclose(LOG);
}

int main(void)
{
 int rc;
 L=lua_open(0);
 lua_baselibopen(L);
 lua_iolibopen(L);
 lua_strlibopen(L);
 lua_mathlibopen(L);
 lua_dblibopen(L);
 start_trace(stderr);
 rc=lua_dofile(L,0);
 stop_trace();
 return rc;
}