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