Export the temp buffer functionality in the debug functions interface
[wine] / dlls / ntdll / debugtools.c
1 /*
2  * Debugging functions
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 #include <ctype.h>
32
33 #include "wine/debug.h"
34 #include "wine/exception.h"
35 #include "wine/library.h"
36 #include "wine/unicode.h"
37 #include "ntstatus.h"
38 #include "winnt.h"
39 #include "winternl.h"
40 #include "excpt.h"
41 #include "ntdll_misc.h"
42
43 WINE_DECLARE_DEBUG_CHANNEL(tid);
44
45 static struct __wine_debug_functions default_funcs;
46
47 /* ---------------------------------------------------------------------- */
48
49 /* filter for page-fault exceptions */
50 static WINE_EXCEPTION_FILTER(page_fault)
51 {
52     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
53         return EXCEPTION_EXECUTE_HANDLER;
54     return EXCEPTION_CONTINUE_SEARCH;
55 }
56
57 /* get the debug info pointer for the current thread */
58 static inline struct debug_info *get_info(void)
59 {
60     return ntdll_get_thread_data()->debug_info;
61 }
62
63 /* allocate some tmp space for a string */
64 static char *get_temp_buffer( size_t n )
65 {
66     struct debug_info *info = get_info();
67     char *res = info->str_pos;
68
69     if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
70     info->str_pos = res + n;
71     return res;
72 }
73
74 /* release extra space that we requested in gimme1() */
75 static void release_temp_buffer( char *ptr, size_t size )
76 {
77     struct debug_info *info = get_info();
78     info->str_pos = ptr + size;
79 }
80
81 /***********************************************************************
82  *              NTDLL_dbgstr_an
83  */
84 static const char *NTDLL_dbgstr_an( const char *src, int n )
85 {
86     const char *res;
87     struct debug_info *info = get_info();
88     /* save current position to restore it on exception */
89     char *old_pos = info->str_pos;
90
91     __TRY
92     {
93         res = default_funcs.dbgstr_an( src, n );
94     }
95     __EXCEPT(page_fault)
96     {
97         release_temp_buffer( old_pos, 0 );
98         return "(invalid)";
99     }
100     __ENDTRY
101     return res;
102 }
103
104 /***********************************************************************
105  *              NTDLL_dbgstr_wn
106  */
107 static const char *NTDLL_dbgstr_wn( const WCHAR *src, int n )
108 {
109     const char *res;
110     struct debug_info *info = get_info();
111     /* save current position to restore it on exception */
112     char *old_pos = info->str_pos;
113
114     __TRY
115     {
116         res = default_funcs.dbgstr_wn( src, n );
117     }
118     __EXCEPT(page_fault)
119     {
120         release_temp_buffer( old_pos, 0 );
121         return "(invalid)";
122     }
123     __ENDTRY
124      return res;
125 }
126
127 /***********************************************************************
128  *              NTDLL_dbg_vprintf
129  */
130 static int NTDLL_dbg_vprintf( const char *format, va_list args )
131 {
132     struct debug_info *info = get_info();
133     char *p;
134
135     int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
136                          format, args );
137
138     /* make sure we didn't exceed the buffer length
139      * the two checks are due to glibc changes in vsnprintfs return value
140      * the buffer size can be exceeded in case of a missing \n in
141      * debug output */
142     if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
143     {
144        fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
145                 info->output);
146        info->out_pos = info->output;
147        abort();
148     }
149
150     p = strrchr( info->out_pos, '\n' );
151     if (!p) info->out_pos += ret;
152     else
153     {
154         char *pos = info->output;
155         p++;
156         write( 2, pos, p - pos );
157         /* move beginning of next line to start of buffer */
158         while ((*pos = *p++)) pos++;
159         info->out_pos = pos;
160     }
161     return ret;
162 }
163
164 /***********************************************************************
165  *              NTDLL_dbg_vlog
166  */
167 static int NTDLL_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
168                            const char *function, const char *format, va_list args )
169 {
170     static const char * const classes[] = { "fixme", "err", "warn", "trace" };
171     struct debug_info *info = get_info();
172     int ret = 0;
173
174     /* only print header if we are at the beginning of the line */
175     if (info->out_pos == info->output || info->out_pos[-1] == '\n')
176     {
177         if (TRACE_ON(tid))
178             ret = wine_dbg_printf( "%04lx:", GetCurrentThreadId() );
179         if (cls < sizeof(classes)/sizeof(classes[0]))
180             ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel->name, function );
181     }
182     if (format)
183         ret += NTDLL_dbg_vprintf( format, args );
184     return ret;
185 }
186
187
188 static const struct __wine_debug_functions funcs =
189 {
190     get_temp_buffer,
191     release_temp_buffer,
192     NTDLL_dbgstr_an,
193     NTDLL_dbgstr_wn,
194     NTDLL_dbg_vprintf,
195     NTDLL_dbg_vlog
196 };
197
198 /***********************************************************************
199  *              debug_init
200  */
201 void debug_init(void)
202 {
203     extern void __wine_dbg_ntdll_init(void);
204
205     __wine_dbg_set_functions( &funcs, &default_funcs, sizeof(funcs) );
206     __wine_dbg_ntdll_init();  /* hack: register debug channels early */
207 }