-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.cc
128 lines (107 loc) · 2.85 KB
/
debug.cc
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "debug.h"
#ifndef WIN32
#include <unistd.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <stdarg.h>
#define BREAK_SCRIPT_NAME "_____I_HATE_THE_WORLD_____"
#define BREAK_SIGNAL SIGUSR1
#ifndef WIN32
//#define USE_XTERM // This works
//#define USE_DDD // This is currently broken
#define USE_XEMACS_GDB
#else
#define USE_NT
#endif
void BreakHere()
{
fprintf(stderr,"Dummy Debug Function Execed\n");
return;
}
char *GetExecName()
{
// DUX 4
#if defined(__osf1__)
extern char **__Argv;
return __Argv[0];
#else
return 0;
#endif
}
void AttacheDebuggerHereSigHandler(int sig)
{
sig=0; // avoid compiler warning
}
#if defined(USE_XTERM) || defined(USE_DDD)
void AttachDebuggerHere(char *execname=0)
{
char s[1000];
FILE *fd;
execname = execname==0 ? GetExecName() : execname;
fd = fopen(BREAK_SCRIPT_NAME,"w");
#ifdef USE_XTERM
fprintf(fd,"#!/bin/sh\necho \"break BreakHere\ncont\ncont\" > %s.glarp ; (cat %s.glarp - | gdb %s %d) \n",
BREAK_SCRIPT_NAME,BREAK_SCRIPT_NAME,execname,getpid());
#endif
#ifdef USE_DDD
fprintf(fd,"#!/bin/sh\necho \"break BreakHere\ncont\ncont\" > %s.glarp ; (cat %s.glarp | ddd --gdb --tty %s %d) \n",
BREAK_SCRIPT_NAME,BREAK_SCRIPT_NAME,execname,getpid());
#endif
fclose(fd);
sprintf(s,"chmod +x %s",BREAK_SCRIPT_NAME);
system(s);
signal(SIGUSR2,&AttacheDebuggerHereSigHandler);
#ifdef USE_XTERM
sprintf(s, "((xterm -fg cyan -bg black -sl 5000 -T \"Debug %s\" -e %s) &) ; ((sleep 5; kill -s USR2 %d) &)", execname, BREAK_SCRIPT_NAME,getpid());
#endif
#ifdef USE_DDD
sprintf(s, "( %s &) ; ((sleep 10; kill -s USR2 %d) &)", BREAK_SCRIPT_NAME,getpid());
#endif
system(s);
pause();
signal(SIGUSR2,SIG_DFL);
sprintf(s,"%s.glarp",BREAK_SCRIPT_NAME);
unlink(BREAK_SCRIPT_NAME);
unlink(s);
}
#endif
#if defined(USE_XEMACS_GDB)
void AttachDebuggerHere(char *execname)
{
char s[1000];
execname = execname==0 ? GetExecName() : execname;
//signal(SIGUSR2,&AttacheDebuggerHereSigHandler);
sprintf(s, "( EXEC=`which %s`; gnudoit \"(progn (gdb \\\"$EXEC\\\") (gdb-call \\\"attach %d\\\") (gdb-call \\\"break BreakHere\\\") (gdb-call \\\"continue\\\") )\") ; sleep 10", execname, getpid());
fprintf(stderr,"%s\n",s);
system(s);
//pause();
//signal(SIGUSR2,SIG_DFL);
//sprintf(s,"%s.glarp",BREAK_SCRIPT_NAME);
}
#endif
#if defined(USE_NT)
void AttachDebuggerHere(char *execname)
{
fprintf(stderr,"AttachDebuggerHere not implemented on NT\n");
}
#endif
static FILE *db_file=DEFAULT_DEBUG_PRINT_FILE;
static int db_level=(getenv("MINET_DEBUGLEVEL")==0 ? DEFAULT_DEBUG_PRINT_LEVEL : atoi(getenv("MINET_DEBUGLEVEL")));
void DEBUGSETLEVEL(int level)
{
db_level=level;
}
void DEBUGSETFILE(FILE *file)
{
db_file=file;
}
void DEBUGPRINTF(int level, char *fmt, ...)
{
va_list list;
va_start(list,fmt);
if (level<db_level) {
vfprintf(db_file,fmt,list);
}
}