|
0
|
1 |
#include "CS.h"
|
|
|
2 |
|
|
|
3 |
char *prg_name;
|
|
|
4 |
struct timeval t0;
|
|
|
5 |
|
|
|
6 |
void debug_init(char *prgname) {
|
|
|
7 |
prg_name = prgname;
|
|
|
8 |
gettimeofday(&t0, NULL);
|
|
|
9 |
}
|
|
|
10 |
void pre(char *o) {
|
|
|
11 |
fprintf(stderr, "%s\n", o);
|
|
|
12 |
fflush(stderr);
|
|
|
13 |
}
|
|
|
14 |
void deb(int level, DebugP deP) {
|
|
|
15 |
if ((csP->debMaxLev >= level && csP->debMaxLev != 7) || (csP->debMaxLev == 7 && level == 7)) {
|
|
|
16 |
if(sem_wait(&(csP->shP->debugSem))< 0) ERR("LOG sem_wait");
|
|
|
17 |
struct timeval t;
|
|
|
18 |
gettimeofday(&t, NULL);
|
|
|
19 |
fprintf(stderr, "%03ld%06d %s: %s\n", (long)t.tv_sec-t0.tv_sec, (int)t.tv_usec, deP->id, deP->msg);
|
|
|
20 |
fflush(stderr);
|
|
|
21 |
if(sem_post(&(csP->shP->debugSem)) < 0) ERR("LOG sem_post");
|
|
|
22 |
}
|
|
|
23 |
}
|
|
|
24 |
void err(int level, char *o, DebugP deP) {
|
|
|
25 |
fprintf(stderr, "%s: %s\n", deP->id, o);
|
|
|
26 |
fflush(stderr);
|
|
|
27 |
}
|
|
|
28 |
void back_trace() {
|
|
|
29 |
if(sem_wait(&(csP->shP->debugSem))< 0) ERR("LOG sem_wait");
|
|
|
30 |
|
|
|
31 |
int j, nptrs;
|
|
|
32 |
void *buffer[24];
|
|
|
33 |
char **strings;
|
|
|
34 |
|
|
|
35 |
nptrs = backtrace(buffer, 24);
|
|
|
36 |
|
|
|
37 |
strings = backtrace_symbols(buffer, nptrs);
|
|
|
38 |
if (strings == NULL) {
|
|
|
39 |
ERR("ABORT backtrace_symbols");
|
|
|
40 |
exit(EXIT_FAILURE);
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
char shcmd[] = "sed -e 's/.*\\[\\(.*\\)\\]$/\\1/' | addr2line -fspe ";
|
|
|
44 |
char *cmdbuf = malloc(sizeof(shcmd) + 64);
|
|
|
45 |
strcpy(cmdbuf, shcmd);
|
|
|
46 |
strcat(cmdbuf, prg_name);
|
|
|
47 |
#define SIZE 640
|
|
|
48 |
char *resbuf = malloc(SIZE);;
|
|
|
49 |
for(j = 0; j < nptrs; j++) {
|
|
|
50 |
int pc[2], po[2];
|
|
|
51 |
if(pipe(pc) < 0) ERR("ABORT pipe");
|
|
|
52 |
if(pipe(po) < 0) ERR("ABORT pipe");
|
|
|
53 |
if(!fork()) {
|
|
|
54 |
close(po[0]); dup2(po[1],1);
|
|
|
55 |
close(pc[1]); dup2(pc[0],0);
|
|
|
56 |
execl("/bin/bash", "/bin/bash", "-c", cmdbuf, (char *) NULL);
|
|
|
57 |
}
|
|
|
58 |
if(write(pc[1], strings[j], strlen(strings[j])) < 0) ERR("ABORT write pipe");
|
|
|
59 |
if(write(pc[1], "\n", 1) < 0) ERR("ABORT write pipe");
|
|
|
60 |
close(pc[1]);
|
|
|
61 |
memset(resbuf, 0, SIZE);
|
|
|
62 |
if(read(po[0], resbuf, SIZE)) fprintf(stderr, "%s", resbuf);
|
|
|
63 |
for(int i = 0; i < 2; i++) close(po[i]), close(pc[i]);
|
|
|
64 |
}
|
|
|
65 |
if(sem_post(&(csP->shP->debugSem))< 0) ERR("LOG sem_post");
|
|
|
66 |
free(strings); free(cmdbuf); free(resbuf);
|
|
|
67 |
|
|
|
68 |
}
|