xref: /vim-8.2.3635/src/nbdebug.c (revision 071d4279)
1 /* vi:set ts=8 sts=8 sw=8:
2  *
3  * VIM - Vi IMproved	by Bram Moolenaar
4  *			Visual Workshop integration by Gordon Prieur
5  *
6  * Do ":help uganda"  in Vim to read copying and usage conditions.
7  * Do ":help credits" in Vim to see a list of people who contributed.
8  * See README.txt for an overview of the Vim source code.
9  */
10 
11 /*
12  * NetBeans Debugging Tools. What are these tools and why are they important?
13  * There are two main tools here. The first tool is a tool for delaying or
14  * stopping gvim during startup.  The second tool is a protocol log tool.
15  *
16  * The startup delay tool is called nbdebug_wait(). This is very important for
17  * debugging startup problems because gvim will be started automatically from
18  * netbeans and cannot be run directly from a debugger. The only way to debug
19  * a gvim started by netbeans is by attaching a debugger to it. Without this
20  * tool all starup code will have completed before you can get the pid and
21  * attach.
22  *
23  * The second tool is a log tool.
24  *
25  * This code must have NBDEBUG defined for it to be compiled into vim/gvim.
26  */
27 
28 #ifdef NBDEBUG
29 
30 #include <stdarg.h>
31 
32 #include "vim.h"
33 
34 FILE		*nb_debug = NULL;
35 u_int		 nb_dlevel = 0;		/* nb_debug verbosity level */
36 
37 void		 nbdb(char *, ...);
38 void		 nbtrace(char *, ...);
39 
40 static int	 lookup(char *);
41 #ifndef FEAT_GUI_W32
42 static int	 errorHandler(Display *, XErrorEvent *);
43 #endif
44 
45 /*
46  * nbdebug_wait	-   This function can be used to delay or stop execution of vim.
47  *		    Its normally used to delay startup while attaching a
48  *		    debugger to a running process. Since workshop starts gvim
49  *		    from a background process this is the only way to debug
50  *		    startup problems.
51  */
52 
53 void nbdebug_wait(
54 	u_int		 wait_flags,	/* tells what to do */
55 	char		*wait_var,	/* wait environment variable */
56 	u_int		 wait_secs)	/* how many seconds to wait */
57 {
58 
59 	init_homedir();			/* not inited yet */
60 #ifdef USE_WDDUMP
61 	WDDump(0, 0, 0);
62 #endif
63 
64 	/* for debugging purposes only */
65 	if (wait_flags & WT_ENV && wait_var && getenv(wait_var) != NULL) {
66 		sleep(atoi(getenv(wait_var)));
67 	} else if (wait_flags & WT_WAIT && lookup("~/.gvimwait")) {
68 		sleep(wait_secs > 0 && wait_secs < 120 ? wait_secs : 20);
69 	} else if (wait_flags & WT_STOP && lookup("~/.gvimstop")) {
70 		int w = 1;
71 		while (w) {
72 			;
73 		}
74 	}
75 }    /* end nbdebug_wait */
76 
77 
78 void
79 nbdebug_log_init(
80 	char		*log_var,	/* env var with log file */
81 	char		*level_var)	/* env var with nb_debug level */
82 {
83 	char		*file;		/* possible nb_debug output file */
84 	char		*cp;		/* nb_dlevel pointer */
85 
86 	if (log_var && (file = getenv(log_var)) != NULL) {
87 		time_t now;
88 
89 		nb_debug = fopen(file, "a");
90 		time(&now);
91 		fprintf(nb_debug, "%s", asctime(localtime(&now)));
92 		if (level_var && (cp = getenv(level_var)) != NULL) {
93 			nb_dlevel = strtoul(cp, NULL, 0);
94 		} else {
95 			nb_dlevel = NB_TRACE;	/* default level */
96 		}
97 		/* XSetErrorHandler(errorHandler); */
98 	}
99 
100 }    /* end nbdebug_log_init */
101 
102 
103 
104 
105 void
106 nbtrace(
107 	char		*fmt,
108 	...)
109 {
110 	va_list		 ap;
111 
112 	if (nb_debug!= NULL && (nb_dlevel & (NB_TRACE | NB_TRACE_VERBOSE))) {
113 		va_start(ap, fmt);
114 		vfprintf(nb_debug, fmt, ap);
115 		va_end(ap);
116 		fflush(nb_debug);
117 	}
118 
119 }    /* end nbtrace */
120 
121 
122 void
123 nbdbg(
124 	char		*fmt,
125 	...)
126 {
127 	va_list		 ap;
128 
129 	if (nb_debug != NULL) {
130 		va_start(ap, fmt);
131 		vfprintf(nb_debug, fmt, ap);
132 		va_end(ap);
133 		fflush(nb_debug);
134 	}
135 
136 }    /* end nbdbg */
137 
138 
139 static int
140 lookup(
141 	char		*file)
142 {
143 	char		 buf[BUFSIZ];
144 
145 	expand_env((char_u *) file, (char_u *) buf, BUFSIZ);
146 	return
147 #ifndef FEAT_GUI_W32
148 		(access(buf, F_OK) == 0);
149 #else
150 		(access(buf, 0) == 0);
151 #endif
152 
153 }    /* end lookup */
154 
155 #ifndef FEAT_GUI_W32
156 static int
157 errorHandler(
158 	Display		*dpy,
159 	XErrorEvent	*err)
160 {
161 	char		 msg[256];
162 	char		 buf[256];
163 
164 	XGetErrorText(dpy, err->error_code, msg, sizeof(msg));
165 	nbdbg("\n\nNBDEBUG Vim: X Error of failed request: %s\n", msg);
166 
167 	sprintf(buf, "%d", err->request_code);
168 	XGetErrorDatabaseText(dpy,
169 	    "XRequest", buf, "Unknown", msg, sizeof(msg));
170 	nbdbg("\tMajor opcode of failed request: %d (%s)\n",
171 	    err->request_code, msg);
172 	if (err->request_code > 128) {
173 		nbdbg("\tMinor opcode of failed request: %d\n",
174 		    err->minor_code);
175 	}
176 
177 	return 0;
178 }
179 #endif
180 
181 
182 #endif /* NBDEBUG */
183