debug.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * Management of the debugging channels
  3. *
  4. * Copyright 2000 Alexandre Julliard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include "wine/port.h"
  22. #include "wine/asm.h"
  23. #ifdef __ASM_OBSOLETE
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <stdarg.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #ifdef HAVE_SYS_STAT_H
  30. # include <sys/stat.h>
  31. #endif
  32. #include "wine/debug.h"
  33. struct __wine_debug_functions
  34. {
  35. char * (*get_temp_buffer)( size_t n );
  36. void (*release_temp_buffer)( char *buffer, size_t n );
  37. const char * (*dbgstr_an)( const char * s, int n );
  38. const char * (*dbgstr_wn)( const WCHAR *s, int n );
  39. int (*dbg_vprintf)( const char *format, va_list args );
  40. int (*dbg_vlog)( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
  41. const char *function, const char *format, va_list args );
  42. };
  43. static const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
  44. #define MAX_DEBUG_OPTIONS 256
  45. static unsigned char default_flags = (1 << __WINE_DBCL_ERR) | (1 << __WINE_DBCL_FIXME);
  46. static int nb_debug_options = -1;
  47. static struct __wine_debug_channel debug_options[MAX_DEBUG_OPTIONS];
  48. static struct __wine_debug_functions funcs;
  49. static void debug_init(void);
  50. static int cmp_name( const void *p1, const void *p2 )
  51. {
  52. const char *name = p1;
  53. const struct __wine_debug_channel *chan = p2;
  54. return strcmp( name, chan->name );
  55. }
  56. /* get the flags to use for a given channel, possibly setting them too in case of lazy init */
  57. unsigned char __wine_dbg_get_channel_flags_obsolete( struct __wine_debug_channel *channel )
  58. {
  59. if (nb_debug_options == -1) debug_init();
  60. if (nb_debug_options)
  61. {
  62. struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
  63. sizeof(debug_options[0]), cmp_name );
  64. if (opt) return opt->flags;
  65. }
  66. /* no option for this channel */
  67. if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags;
  68. return default_flags;
  69. }
  70. /* set the flags to use for a given channel; return 0 if the channel is not available to set */
  71. int __wine_dbg_set_channel_flags_obsolete( struct __wine_debug_channel *channel,
  72. unsigned char set, unsigned char clear )
  73. {
  74. if (nb_debug_options == -1) debug_init();
  75. if (nb_debug_options)
  76. {
  77. struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
  78. sizeof(debug_options[0]), cmp_name );
  79. if (opt)
  80. {
  81. opt->flags = (opt->flags & ~clear) | set;
  82. return 1;
  83. }
  84. }
  85. return 0;
  86. }
  87. /* add a new debug option at the end of the option list */
  88. static void add_option( const char *name, unsigned char set, unsigned char clear )
  89. {
  90. int min = 0, max = nb_debug_options - 1, pos, res;
  91. if (!name[0]) /* "all" option */
  92. {
  93. default_flags = (default_flags & ~clear) | set;
  94. return;
  95. }
  96. if (strlen(name) >= sizeof(debug_options[0].name)) return;
  97. while (min <= max)
  98. {
  99. pos = (min + max) / 2;
  100. res = strcmp( name, debug_options[pos].name );
  101. if (!res)
  102. {
  103. debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
  104. return;
  105. }
  106. if (res < 0) max = pos - 1;
  107. else min = pos + 1;
  108. }
  109. if (nb_debug_options >= MAX_DEBUG_OPTIONS) return;
  110. pos = min;
  111. if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos],
  112. (nb_debug_options - pos) * sizeof(debug_options[0]) );
  113. strcpy( debug_options[pos].name, name );
  114. debug_options[pos].flags = (default_flags & ~clear) | set;
  115. nb_debug_options++;
  116. }
  117. /* parse a set of debugging option specifications and add them to the option list */
  118. static void parse_options( const char *str )
  119. {
  120. char *opt, *next, *options;
  121. unsigned int i;
  122. if (!(options = strdup(str))) return;
  123. for (opt = options; opt; opt = next)
  124. {
  125. const char *p;
  126. unsigned char set = 0, clear = 0;
  127. if ((next = strchr( opt, ',' ))) *next++ = 0;
  128. p = opt + strcspn( opt, "+-" );
  129. if (!p[0]) p = opt; /* assume it's a debug channel name */
  130. if (p > opt)
  131. {
  132. for (i = 0; i < ARRAY_SIZE(debug_classes); i++)
  133. {
  134. int len = strlen(debug_classes[i]);
  135. if (len != (p - opt)) continue;
  136. if (!memcmp( opt, debug_classes[i], len )) /* found it */
  137. {
  138. if (*p == '+') set |= 1 << i;
  139. else clear |= 1 << i;
  140. break;
  141. }
  142. }
  143. if (i == ARRAY_SIZE(debug_classes)) /* bad class name, skip it */
  144. continue;
  145. }
  146. else
  147. {
  148. if (*p == '-') clear = ~0;
  149. else set = ~0;
  150. }
  151. if (*p == '+' || *p == '-') p++;
  152. if (!p[0]) continue;
  153. if (!strcmp( p, "all" ))
  154. default_flags = (default_flags & ~clear) | set;
  155. else
  156. add_option( p, set, clear );
  157. }
  158. free( options );
  159. }
  160. /* print the usage message */
  161. static void debug_usage(void)
  162. {
  163. static const char usage[] =
  164. "Syntax of the WINEDEBUG variable:\n"
  165. " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
  166. "Example: WINEDEBUG=+all,warn-heap\n"
  167. " turns on all messages except warning heap messages\n"
  168. "Available message classes: err, warn, fixme, trace\n";
  169. write( 2, usage, sizeof(usage) - 1 );
  170. exit(1);
  171. }
  172. /* initialize all options at startup */
  173. static void debug_init(void)
  174. {
  175. char *wine_debug;
  176. struct stat st1, st2;
  177. if (nb_debug_options != -1) return; /* already initialized */
  178. nb_debug_options = 0;
  179. /* check for stderr pointing to /dev/null */
  180. if (!fstat( 2, &st1 ) && S_ISCHR(st1.st_mode) &&
  181. !stat( "/dev/null", &st2 ) && S_ISCHR(st2.st_mode) &&
  182. st1.st_rdev == st2.st_rdev)
  183. {
  184. default_flags = 0;
  185. return;
  186. }
  187. if ((wine_debug = getenv("WINEDEBUG")))
  188. {
  189. if (!strcmp( wine_debug, "help" )) debug_usage();
  190. parse_options( wine_debug );
  191. }
  192. }
  193. /* varargs wrapper for funcs.dbg_vprintf */
  194. int wine_dbg_printf_obsolete( const char *format, ... )
  195. {
  196. int ret;
  197. va_list valist;
  198. va_start(valist, format);
  199. ret = funcs.dbg_vprintf( format, valist );
  200. va_end(valist);
  201. return ret;
  202. }
  203. /* printf with temp buffer allocation */
  204. const char *wine_dbg_sprintf_obsolete( const char *format, ... )
  205. {
  206. static const int max_size = 200;
  207. char *ret;
  208. int len;
  209. va_list valist;
  210. va_start(valist, format);
  211. ret = funcs.get_temp_buffer( max_size );
  212. len = vsnprintf( ret, max_size, format, valist );
  213. if (len == -1 || len >= max_size) ret[max_size-1] = 0;
  214. else funcs.release_temp_buffer( ret, len + 1 );
  215. va_end(valist);
  216. return ret;
  217. }
  218. /* varargs wrapper for funcs.dbg_vlog */
  219. int wine_dbg_log_obsolete( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
  220. const char *func, const char *format, ... )
  221. {
  222. int ret;
  223. va_list valist;
  224. if (!(__wine_dbg_get_channel_flags_obsolete( channel ) & (1 << cls))) return -1;
  225. va_start(valist, format);
  226. ret = funcs.dbg_vlog( cls, channel, func, format, valist );
  227. va_end(valist);
  228. return ret;
  229. }
  230. /* allocate some tmp string space */
  231. /* FIXME: this is not 100% thread-safe */
  232. static char *get_temp_buffer( size_t size )
  233. {
  234. static char *list[32];
  235. static int pos;
  236. char *ret;
  237. int idx;
  238. idx = pos++ % ARRAY_SIZE(list);
  239. if ((ret = realloc( list[idx], size ))) list[idx] = ret;
  240. return ret;
  241. }
  242. /* release unused part of the buffer */
  243. static void release_temp_buffer( char *buffer, size_t size )
  244. {
  245. /* don't bother doing anything */
  246. }
  247. /* default implementation of wine_dbgstr_an */
  248. static const char *default_dbgstr_an( const char *str, int n )
  249. {
  250. static const char hex[16] = "0123456789abcdef";
  251. char *dst, *res;
  252. size_t size;
  253. if (!((ULONG_PTR)str >> 16))
  254. {
  255. if (!str) return "(null)";
  256. res = funcs.get_temp_buffer( 6 );
  257. sprintf( res, "#%04x", LOWORD(str) );
  258. return res;
  259. }
  260. if (n == -1) n = strlen(str);
  261. if (n < 0) n = 0;
  262. size = 10 + min( 300, n * 4 );
  263. dst = res = funcs.get_temp_buffer( size );
  264. *dst++ = '"';
  265. while (n-- > 0 && dst <= res + size - 9)
  266. {
  267. unsigned char c = *str++;
  268. switch (c)
  269. {
  270. case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
  271. case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
  272. case '\t': *dst++ = '\\'; *dst++ = 't'; break;
  273. case '"': *dst++ = '\\'; *dst++ = '"'; break;
  274. case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
  275. default:
  276. if (c >= ' ' && c <= 126)
  277. *dst++ = c;
  278. else
  279. {
  280. *dst++ = '\\';
  281. *dst++ = 'x';
  282. *dst++ = hex[(c >> 4) & 0x0f];
  283. *dst++ = hex[c & 0x0f];
  284. }
  285. }
  286. }
  287. *dst++ = '"';
  288. if (n > 0)
  289. {
  290. *dst++ = '.';
  291. *dst++ = '.';
  292. *dst++ = '.';
  293. }
  294. *dst++ = 0;
  295. funcs.release_temp_buffer( res, dst - res );
  296. return res;
  297. }
  298. /* default implementation of wine_dbgstr_wn */
  299. static const char *default_dbgstr_wn( const WCHAR *str, int n )
  300. {
  301. char *dst, *res;
  302. size_t size;
  303. if (!((ULONG_PTR)str >> 16))
  304. {
  305. if (!str) return "(null)";
  306. res = funcs.get_temp_buffer( 6 );
  307. sprintf( res, "#%04x", LOWORD(str) );
  308. return res;
  309. }
  310. if (n == -1)
  311. {
  312. const WCHAR *end = str;
  313. while (*end) end++;
  314. n = end - str;
  315. }
  316. if (n < 0) n = 0;
  317. size = 12 + min( 300, n * 5 );
  318. dst = res = funcs.get_temp_buffer( size );
  319. *dst++ = 'L';
  320. *dst++ = '"';
  321. while (n-- > 0 && dst <= res + size - 10)
  322. {
  323. WCHAR c = *str++;
  324. switch (c)
  325. {
  326. case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
  327. case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
  328. case '\t': *dst++ = '\\'; *dst++ = 't'; break;
  329. case '"': *dst++ = '\\'; *dst++ = '"'; break;
  330. case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
  331. default:
  332. if (c >= ' ' && c <= 126)
  333. *dst++ = c;
  334. else
  335. {
  336. *dst++ = '\\';
  337. sprintf(dst,"%04x",c);
  338. dst+=4;
  339. }
  340. }
  341. }
  342. *dst++ = '"';
  343. if (n > 0)
  344. {
  345. *dst++ = '.';
  346. *dst++ = '.';
  347. *dst++ = '.';
  348. }
  349. *dst++ = 0;
  350. funcs.release_temp_buffer( res, dst - res );
  351. return res;
  352. }
  353. /* default implementation of wine_dbg_vprintf */
  354. static int default_dbg_vprintf( const char *format, va_list args )
  355. {
  356. return vfprintf( stderr, format, args );
  357. }
  358. /* default implementation of wine_dbg_vlog */
  359. static int default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
  360. const char *func, const char *format, va_list args )
  361. {
  362. int ret = 0;
  363. if (cls < ARRAY_SIZE(debug_classes))
  364. ret += wine_dbg_printf_obsolete( "%s:%s:%s ", debug_classes[cls], channel->name, func );
  365. if (format)
  366. ret += funcs.dbg_vprintf( format, args );
  367. return ret;
  368. }
  369. /* wrappers to use the function pointers */
  370. const char *wine_dbgstr_an_obsolete( const char * s, int n )
  371. {
  372. return funcs.dbgstr_an(s, n);
  373. }
  374. const char *wine_dbgstr_wn_obsolete( const WCHAR *s, int n )
  375. {
  376. return funcs.dbgstr_wn(s, n);
  377. }
  378. void __wine_dbg_set_functions_obsolete( const struct __wine_debug_functions *new_funcs,
  379. struct __wine_debug_functions *old_funcs, size_t size )
  380. {
  381. if (old_funcs) memcpy( old_funcs, &funcs, min(sizeof(funcs),size) );
  382. if (new_funcs) memcpy( &funcs, new_funcs, min(sizeof(funcs),size) );
  383. }
  384. static struct __wine_debug_functions funcs =
  385. {
  386. get_temp_buffer,
  387. release_temp_buffer,
  388. default_dbgstr_an,
  389. default_dbgstr_wn,
  390. default_dbg_vprintf,
  391. default_dbg_vlog
  392. };
  393. __ASM_OBSOLETE(__wine_dbg_get_channel_flags);
  394. __ASM_OBSOLETE(__wine_dbg_set_channel_flags);
  395. __ASM_OBSOLETE(__wine_dbg_set_functions);
  396. __ASM_OBSOLETE(wine_dbg_log);
  397. __ASM_OBSOLETE(wine_dbg_printf);
  398. __ASM_OBSOLETE(wine_dbg_sprintf);
  399. __ASM_OBSOLETE(wine_dbgstr_an);
  400. __ASM_OBSOLETE(wine_dbgstr_wn);
  401. #endif /* __ASM_OBSOLETE */