summaryrefslogtreecommitdiff
path: root/etc/trace.c
blob: 2c92850f41533cd90b0773ff4108fedac060e32a (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
/*
* trace.c
* a simple execution tracer for Lua
*/

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

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

static void linehook(int line)
{
 fprintf(LOG,"%*sLINE(%d)\t-- %d\n",L,"",line,L);
}

static void callhook(lua_Function func, char* file, int line)
{
 fprintf(LOG,"%*sCALL('%s',%d)\t-- %d\n",L,"",file,line,L);
 if (line==0 && strcmp(file,"(return)")==0) --L; else ++L;
}

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

void stop_trace(void)
{
 lua_setlinehook(NULL);
 lua_setcallhook(NULL);
 lua_setdebug(0);
 fclose(LOG);
}

int main(void)
{
 int rc;
 lua_open();
 start_trace(stderr);
 rc=lua_dofile(0);
 stop_trace();
 return rc;
}