source: titan/titan/debug.h @ 23191

Last change on this file since 23191 was 23191, checked in by nit, 11 years ago

[titan] optimize stacktrace

File size: 4.0 KB
Line 
1/*****************************************************/
2/* this file is part of the tiTan / tiTanNIT Project */
3/* and allowed only for use with this.               */
4/*                                                   */
5/* copyright by NIT                                  */
6/*****************************************************/
7
8#ifndef DEBUG_H
9#define DEBUG_H
10
11short debug_level = 10;
12
13// mc debug_level = 50;
14// panel debug_level = 60;
15
16void __cyg_profile_func_enter(void *this_fn, void* call_size) __attribute__((no_instrument_function));
17void __cyg_profile_func_enter(void *this_fn, void* call_size)
18{
19        int i = 0, treffer = 0;
20        pthread_t threadid = pthread_self();
21
22        for(i = 0; i < MAXSTACKTRACE; i++)
23        {
24                if(stacktrace[i].thread == threadid)
25                {
26                        stacktrace[i].func[stacktrace[i].pos] = this_fn;
27                        stacktrace[i].pos++;
28                        if(stacktrace[i].pos == MAXSTACKTRACE) stacktrace[i].pos = MAXSTACKTRACE - 1;
29                        treffer = 1;
30                        break;
31                }
32        }
33
34        if(treffer == 0)
35        {
36                for(i = 0; i < MAXSTACKTRACE; i++)
37                {
38                        if(stacktrace[i].pos == 0)
39                        {
40                                stacktrace[i].thread = threadid;
41                                stacktrace[i].func[stacktrace[i].pos] = this_fn;
42                                stacktrace[i].pos++;
43                                if(stacktrace[i].pos == MAXSTACKTRACE) stacktrace[i].pos = MAXSTACKTRACE - 1;
44                                break;
45                        }
46                }
47        }
48}
49
50void __cyg_profile_func_exit(void *this_fn, void* call_size) __attribute__((no_instrument_function));
51void __cyg_profile_func_exit(void *this_fn, void* call_size)
52{
53        int i = 0;
54        pthread_t threadid = pthread_self();
55
56        for(i = 0; i < MAXSTACKTRACE; i++)
57        {
58                if(stacktrace[i].thread == threadid)
59                {
60                        if(stacktrace[i].pos > 0) stacktrace[i].pos--;
61                        break;
62                }
63        }
64}
65
66//debug
67//first line shows greater/same debuglevel
68//second line shows only same debuglevel
69//#define debug(level, fmt, args...) if(debug_level >= level) { do { printf("[%s] " fmt, PROGNAME, ##args); } while (0); printf(", file=%s, func=%s, line=%d\n", __FILE__, __FUNCTION__, __LINE__); }
70//#define debug(level, fmt, args...) if(debug_level == level) { do { printf("[%s] " fmt, PROGNAME, ##args); } while (0); printf(", file=%s, func=%s, line=%d\n", __FILE__, __FUNCTION__, __LINE__); }
71void debugfunc(int level, char* file, const char* function, int line, char* msg, ...)
72{
73        if(debug_level == level)
74        {
75                va_list ap;
76                va_start(ap, msg);
77                printf("[%s] ", PROGNAME);
78                vfprintf(stdout, msg, ap);
79                va_end(ap);
80                fprintf(stdout, ", file=%s, func=%s, line=%d\n", file, function, line);
81        }
82}
83#define debug(level, msg...) debugfunc(level, __FILE__, __FUNCTION__, __LINE__, msg);
84
85//err
86//#define err(fmt, args...) { do { fprintf(stderr, "[%s] error: " fmt, PROGNAME, ##args); } while (0); fprintf(stderr, ", file=%s, func=%s, line=%d\n", __FILE__, __FUNCTION__, __LINE__); }
87void errfunc(char* file, const char* function, int line, char* msg, ...)
88{
89        va_list ap;
90        va_start(ap, msg);
91        fprintf(stderr, "[%s] error: ", PROGNAME);
92        vfprintf(stderr, msg, ap);
93        va_end(ap);
94        fprintf(stderr, ", file=%s, func=%s, line=%d\n", file, function, line);
95}
96#define err(msg...) errfunc(__FILE__, __FUNCTION__, __LINE__, msg);
97
98//perr
99//#define perr(fmt, args...) { do { fprintf(stderr, "[%s] error: " fmt, PROGNAME, ##args); } while (0); fprintf(stderr, ", err=%m, file=%s, func=%s, line=%d\n", __FILE__, __FUNCTION__, __LINE__); }
100void perrfunc(char* file, const char* function, int line, char* msg, ...)
101{
102        va_list ap;
103        va_start(ap, msg);
104        fprintf(stderr, "[%s] error: ", PROGNAME);
105        vfprintf(stderr, msg, ap);
106        va_end(ap);
107        fprintf(stderr, ", err=%m, file=%s, func=%s, line=%d\n", file, function, line);
108}
109#define perr(msg...) perrfunc(__FILE__, __FUNCTION__, __LINE__, msg);
110
111//filedebug
112//#define filedebug(file, fmt, args...) { FILE* fd = fopen(file, "a"); if(fd != NULL) { do { fprintf(fd, "" fmt, ##args); } while (0); fprintf(fd, "\n"); }}
113void filedebugfunc(char* file, char* msg, ...)
114{
115        FILE* fd = fopen(file, "a");
116        if(fd != NULL)
117        {
118                va_list ap;
119                va_start(ap, msg);
120                vfprintf(fd, msg, ap);
121                va_end(ap);
122                fprintf(fd, "\n");
123                fclose(fd);
124        }
125}
126#define filedebug(file, msg...) filedebugfunc(file, msg);
127
128#endif
Note: See TracBrowser for help on using the repository browser.