Imported from ../lua-3.2.tar.gz.
[lua.git] / etc / trace.c
blob2c92850f41533cd90b0773ff4108fedac060e32a
1 /*
2 * trace.c
3 * a simple execution tracer for Lua
4 */
6 #include <stdio.h>
7 #include <string.h>
8 #include "lua.h"
9 #include "luadebug.h"
11 static FILE* LOG; /* output file */
12 static int L=0; /* indentation level */
14 static void linehook(int line)
16 fprintf(LOG,"%*sLINE(%d)\t-- %d\n",L,"",line,L);
19 static void callhook(lua_Function func, char* file, int line)
21 fprintf(LOG,"%*sCALL('%s',%d)\t-- %d\n",L,"",file,line,L);
22 if (line==0 && strcmp(file,"(return)")==0) --L; else ++L;
25 void start_trace(FILE* logfile)
27 lua_setlinehook(linehook);
28 lua_setcallhook(callhook);
29 lua_setdebug(1);
30 LOG=logfile;
33 void stop_trace(void)
35 lua_setlinehook(NULL);
36 lua_setcallhook(NULL);
37 lua_setdebug(0);
38 fclose(LOG);
41 int main(void)
43 int rc;
44 lua_open();
45 start_trace(stderr);
46 rc=lua_dofile(0);
47 stop_trace();
48 return rc;