home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / unix / vim-6.2.tar.bz2 / vim-6.2.tar / vim62 / src / nbdebug.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-02-13  |  4.0 KB  |  176 lines

  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. static int     errorHandler(Display *, XErrorEvent *);
  42.  
  43.  
  44. /*
  45.  * nbdebug_wait    -   This function can be used to delay or stop execution of vim.
  46.  *            Its normally used to delay startup while attaching a
  47.  *            debugger to a running process. Since workshop starts gvim
  48.  *            from a background process this is the only way to debug
  49.  *            startup problems.
  50.  */
  51.  
  52. void nbdebug_wait(
  53.     u_int         wait_flags,    /* tells what to do */
  54.     char        *wait_var,    /* wait environment variable */
  55.     u_int         wait_secs)    /* how many seconds to wait */
  56. {
  57.  
  58.     init_homedir();            /* not inited yet */
  59. #ifdef USE_WDDUMP
  60.     WDDump(0, 0, 0);
  61. #endif
  62.  
  63.     /* for debugging purposes only */
  64.     if (wait_flags & WT_ENV && wait_var && getenv(wait_var) != NULL) {
  65.         sleep(atoi(getenv(wait_var)));
  66.     } else if (wait_flags & WT_WAIT && lookup("~/.gvimwait")) {
  67.         sleep(wait_secs > 0 && wait_secs < 120 ? wait_secs : 20);
  68.     } else if (wait_flags & WT_STOP && lookup("~/.gvimstop")) {
  69.         int w = 1;
  70.         while (w) {
  71.             ;
  72.         }
  73.     }
  74. }    /* end nbdebug_wait */
  75.  
  76.  
  77. void
  78. nbdebug_log_init(
  79.     char        *log_var,    /* env var with log file */
  80.     char        *level_var)    /* env var with nb_debug level */
  81. {
  82.     char        *file;        /* possible nb_debug output file */
  83.     char        *cp;        /* nb_dlevel pointer */
  84.  
  85.     if (log_var && (file = getenv(log_var)) != NULL) {
  86.         char buf[BUFSIZ];
  87.  
  88.         sprintf(buf, "date > %s", file);
  89.         system(buf);
  90.         nb_debug = fopen(file, "a");
  91.         if (level_var && (cp = getenv(level_var)) != NULL) {
  92.             nb_dlevel = strtoul(cp, NULL, 0);
  93.         } else {
  94.             nb_dlevel = NB_TRACE;    /* default level */
  95.         }
  96.         /* XSetErrorHandler(errorHandler); */
  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) {
  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. static int
  139. lookup(
  140.     char        *file)
  141. {
  142.     char         buf[BUFSIZ];
  143.  
  144.     expand_env((char_u *) file, (char_u *) buf, BUFSIZ);
  145.     return (access(buf, F_OK) == 0);
  146.  
  147. }    /* end lookup */
  148.  
  149. static int
  150. errorHandler(
  151.     Display        *dpy,
  152.     XErrorEvent    *err)
  153. {
  154.     char         msg[256];
  155.     char         buf[256];
  156.  
  157.     XGetErrorText(dpy, err->error_code, msg, sizeof(msg));
  158.     nbdbg("\n\nNBDEBUG Vim: X Error of failed request: %s\n", msg);
  159.  
  160.     sprintf(buf, "%d", err->request_code);
  161.     XGetErrorDatabaseText(dpy,
  162.         "XRequest", buf, "Unknown", msg, sizeof(msg));
  163.     nbdbg("\tMajor opcode of failed request: %d (%s)\n",
  164.         err->request_code, msg);
  165.     if (err->request_code > 128) {
  166.         nbdbg("\tMinor opcode of failed request: %d\n",
  167.             err->minor_code);
  168.     }
  169.  
  170.     return 0;
  171. }
  172.  
  173.  
  174.  
  175. #endif /* NBDEBUG */
  176.