debug.c 13 KB

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