debug.c 13 KB

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