blob: f41299a5494d09797d339202e1c44c3e02d7a178 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* 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 * WorkShop 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 wsdebug_wait(). This is very important for
17 * debugging startup problems because gvim will be started automatically from
18 * workshop and cannot be run directly from a debugger. The only way to debug
19 * a gvim started by workshop 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 protocol log tool. The workshop editor server and gvim
24 * pass information back and forth during a workshop session. Sometimes it is
25 * very important to peruse this conversation in order to understand what is
26 * happening. The wsdebug_log_init() call sets up this protocol log tool and
27 * wsdebug() and wstrace() calls output the information to the log.
28 *
29 * This code must have WSDEBUG defined for it to be compiled into vim/gvim.
30 */
31
32#ifdef WSDEBUG
33
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#include "vim.h"
35
36FILE *ws_debug = NULL;
37u_int ws_dlevel = 0; /* ws_debug verbosity level */
38
39void wsdebug(char *, ...);
40void wstrace(char *, ...);
41
42static int lookup(char *);
43static int errorHandler(Display *, XErrorEvent *);
44
45
46/*
47 * wsdebug_wait - This function can be used to delay or stop execution of vim.
48 * Its normally used to delay startup while attaching a
49 * debugger to a running process. Since workshop starts gvim
50 * from a background process this is the only way to debug
51 * startup problems.
52 */
53
54void wsdebug_wait(
55 u_int wait_flags, /* tells what to do */
56 char *wait_var, /* wait environment variable */
57 u_int wait_secs) /* how many seconds to wait */
58{
59
60 init_homedir(); /* not inited yet */
61#ifdef USE_WDDUMP
62 WDDump(0, 0, 0);
63#endif
64
65 /* for debugging purposes only */
66 if (wait_flags & WT_ENV && wait_var && getenv(wait_var) != NULL) {
67 sleep(atoi(getenv(wait_var)));
68 } else if (wait_flags & WT_WAIT && lookup("~/.gvimwait")) {
69 sleep(wait_secs > 0 && wait_secs < 120 ? wait_secs : 20);
70 } else if (wait_flags & WT_STOP && lookup("~/.gvimstop")) {
71 int w = 1;
72 while (w) {
73 ;
74 }
75 }
76} /* end wsdebug_wait */
77
78
79void
80wsdebug_log_init(
81 char *log_var, /* env var with log file */
82 char *level_var) /* env var with ws_debug level */
83{
84 char *file; /* possible ws_debug output file */
85 char *cp; /* ws_dlevel pointer */
86
Bram Moolenaar051b7822005-05-19 21:00:46 +000087 if (log_var && (file = getenv(log_var)) != NULL)
88 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 char buf[BUFSIZ];
90
Bram Moolenaar051b7822005-05-19 21:00:46 +000091 vim_snprintf(buf, sizeof(buf), "date > %s", file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000092 system(buf);
93 ws_debug = fopen(file, "a");
94 if (level_var && (cp = getenv(level_var)) != NULL) {
95 ws_dlevel = strtoul(cp, NULL, 0);
96 } else {
97 ws_dlevel = WS_TRACE; /* default level */
98 }
99 /* XSetErrorHandler(errorHandler); */
100 }
101
102} /* end wsdebug_log_init */
103
104
105
106
107void
108wstrace(
109 char *fmt,
110 ...)
111{
112 va_list ap;
113
114 if (ws_debug!= NULL && (ws_dlevel & (WS_TRACE | WS_TRACE_VERBOSE))) {
115 va_start(ap, fmt);
116 vfprintf(ws_debug, fmt, ap);
117 va_end(ap);
118 fflush(ws_debug);
119 }
120
121} /* end wstrace */
122
123
124void
125wsdebug(
126 char *fmt,
127 ...)
128{
129 va_list ap;
130
131 if (ws_debug != NULL) {
132 va_start(ap, fmt);
133 vfprintf(ws_debug, fmt, ap);
134 va_end(ap);
135 fflush(ws_debug);
136 }
137
138} /* end wsdebug */
139
140
141static int
142lookup(
143 char *file)
144{
145 char buf[BUFSIZ];
146
147 expand_env((char_u *) file, (char_u *) buf, BUFSIZ);
148 return (access(buf, F_OK) == 0);
149
150} /* end lookup */
151
152static int
153errorHandler(
154 Display *dpy,
155 XErrorEvent *err)
156{
157 char msg[256];
158 char buf[256];
159
160 XGetErrorText(dpy, err->error_code, msg, sizeof(msg));
161 wsdebug("\n\nWSDEBUG Vim: X Error of failed request: %s\n", msg);
162
163 sprintf(buf, "%d", err->request_code);
164 XGetErrorDatabaseText(dpy,
165 "XRequest", buf, "Unknown", msg, sizeof(msg));
166 wsdebug("\tMajor opcode of failed request: %d (%s)\n",
167 err->request_code, msg);
168 if (err->request_code > 128) {
169 wsdebug("\tMinor opcode of failed request: %d\n",
170 err->minor_code);
171 }
172
173 return 0;
174}
175
176
177
178#endif /* WSDEBUG */