xref: /vim-8.2.3635/src/nbdebug.c (revision dfccaf0f)
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 	}
98 
99 }    /* end nbdebug_log_init */
100 
101 
102 
103 
104 void
105 nbtrace(
106 	char		*fmt,
107 	...)
108 {
109 	va_list		 ap;
110 
111 	if (nb_debug!= NULL && (nb_dlevel & (NB_TRACE | NB_TRACE_VERBOSE))) {
112 		va_start(ap, fmt);
113 		vfprintf(nb_debug, fmt, ap);
114 		va_end(ap);
115 		fflush(nb_debug);
116 	}
117 
118 }    /* end nbtrace */
119 
120 
121 void
122 nbdbg(
123 	char		*fmt,
124 	...)
125 {
126 	va_list		 ap;
127 
128 	if (nb_debug != NULL && nb_dlevel & NB_TRACE) {
129 		va_start(ap, fmt);
130 		vfprintf(nb_debug, fmt, ap);
131 		va_end(ap);
132 		fflush(nb_debug);
133 	}
134 
135 }    /* end nbdbg */
136 
137 
138 void
139 nbprt(
140 	char		*fmt,
141 	...)
142 {
143 	va_list		 ap;
144 
145 	if (nb_debug != NULL && nb_dlevel & NB_PRINT) {
146 		va_start(ap, fmt);
147 		vfprintf(nb_debug, fmt, ap);
148 		va_end(ap);
149 		fflush(nb_debug);
150 	}
151 
152 }    /* end nbprt */
153 
154 
155 static int
156 lookup(
157 	char		*file)
158 {
159 	char		 buf[BUFSIZ];
160 
161 	expand_env((char_u *) file, (char_u *) buf, BUFSIZ);
162 	return
163 #ifndef FEAT_GUI_W32
164 		(access(buf, F_OK) == 0);
165 #else
166 		(access(buf, 0) == 0);
167 #endif
168 
169 }    /* end lookup */
170 
171 #ifndef FEAT_GUI_W32
172 static int
173 errorHandler(
174 	Display		*dpy,
175 	XErrorEvent	*err)
176 {
177 	char		 msg[256];
178 	char		 buf[256];
179 
180 	XGetErrorText(dpy, err->error_code, msg, sizeof(msg));
181 	nbdbg("\n\nNBDEBUG Vim: X Error of failed request: %s\n", msg);
182 
183 	sprintf(buf, "%d", err->request_code);
184 	XGetErrorDatabaseText(dpy,
185 	    "XRequest", buf, "Unknown", msg, sizeof(msg));
186 	nbdbg("\tMajor opcode of failed request: %d (%s)\n",
187 	    err->request_code, msg);
188 	if (err->request_code > 128) {
189 		nbdbg("\tMinor opcode of failed request: %d\n",
190 		    err->minor_code);
191 	}
192 
193 	return 0;
194 }
195 #endif
196 
197 
198 #endif /* NBDEBUG */
199