4 * Copyright 2000 Alexandre Julliard
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.
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.
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
22 #include "wine/port.h"
33 #include "wine/debug.h"
34 #include "wine/exception.h"
35 #include "wine/library.h"
36 #include "wine/unicode.h"
39 #include "ntdll_misc.h"
41 WINE_DECLARE_DEBUG_CHANNEL(tid
);
42 WINE_DECLARE_DEBUG_CHANNEL(debug
);
44 static struct __wine_debug_functions default_funcs
;
46 /* ---------------------------------------------------------------------- */
48 /* get the debug info pointer for the current thread */
49 static inline struct debug_info
*get_info(void)
51 return ntdll_get_thread_data()->debug_info
;
54 /* allocate some tmp space for a string */
55 static char *get_temp_buffer( size_t n
)
57 struct debug_info
*info
= get_info();
58 char *res
= info
->str_pos
;
60 if (res
+ n
>= &info
->strings
[sizeof(info
->strings
)]) res
= info
->strings
;
61 info
->str_pos
= res
+ n
;
65 /* release extra space that we requested in gimme1() */
66 static void release_temp_buffer( char *ptr
, size_t size
)
68 struct debug_info
*info
= get_info();
69 info
->str_pos
= ptr
+ size
;
72 /***********************************************************************
75 static const char *NTDLL_dbgstr_an( const char *src
, int n
)
78 struct debug_info
*info
= get_info();
79 /* save current position to restore it on exception */
80 char *old_pos
= info
->str_pos
;
84 res
= default_funcs
.dbgstr_an( src
, n
);
88 release_temp_buffer( old_pos
, 0 );
95 /***********************************************************************
98 static const char *NTDLL_dbgstr_wn( const WCHAR
*src
, int n
)
101 struct debug_info
*info
= get_info();
102 /* save current position to restore it on exception */
103 char *old_pos
= info
->str_pos
;
107 res
= default_funcs
.dbgstr_wn( src
, n
);
111 release_temp_buffer( old_pos
, 0 );
118 /***********************************************************************
121 static int NTDLL_dbg_vprintf( const char *format
, va_list args
)
123 struct debug_info
*info
= get_info();
126 int ret
= vsnprintf( info
->out_pos
, sizeof(info
->output
) - (info
->out_pos
- info
->output
),
129 /* make sure we didn't exceed the buffer length
130 * the two checks are due to glibc changes in vsnprintfs return value
131 * the buffer size can be exceeded in case of a missing \n in
133 if ((ret
== -1) || (ret
>= sizeof(info
->output
) - (info
->out_pos
- info
->output
)))
135 fprintf( stderr
, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
137 info
->out_pos
= info
->output
;
141 for (end
= ret
; end
> 0; end
--) if (info
->out_pos
[end
- 1] == '\n') break;
143 if (!end
) info
->out_pos
+= ret
;
146 char *pos
= info
->output
;
147 write( 2, pos
, info
->out_pos
+ end
- pos
);
148 /* move beginning of next line to start of buffer */
149 memmove( pos
, info
->out_pos
+ end
, ret
- end
);
150 info
->out_pos
= pos
+ ret
- end
;
155 /***********************************************************************
158 static int NTDLL_dbg_vlog( enum __wine_debug_class cls
, struct __wine_debug_channel
*channel
,
159 const char *function
, const char *format
, va_list args
)
161 static const char * const classes
[] = { "fixme", "err", "warn", "trace" };
162 struct debug_info
*info
= get_info();
165 /* only print header if we are at the beginning of the line */
166 if (info
->out_pos
== info
->output
|| info
->out_pos
[-1] == '\n')
169 ret
= wine_dbg_printf( "%04x:", GetCurrentThreadId() );
170 if (cls
< sizeof(classes
)/sizeof(classes
[0]))
171 ret
+= wine_dbg_printf( "%s:%s:%s ", classes
[cls
], channel
->name
, function
);
174 ret
+= NTDLL_dbg_vprintf( format
, args
);
179 static const struct __wine_debug_functions funcs
=
189 /***********************************************************************
192 void debug_init(void)
194 __wine_dbg_set_functions( &funcs
, &default_funcs
, sizeof(funcs
) );
197 /***********************************************************************
198 * DbgUiRemoteBreakin (NTDLL.@)
200 void WINAPI
DbgUiRemoteBreakin(PVOID arg
)
208 TRACE_(debug
)("nobody handled exception in DbgUiRemoteBreakin\n");
212 RtlExitUserThread(0);
215 /***********************************************************************
216 * DbgUiIssueRemoteBreakin (NTDLL.@)
218 * Raises an exception so that a debugger (if attached)
219 * can take some action. Same as DebugBreak, but applies to any process.
222 * hProcess [I] Process handle to break into.
225 * Success: ERROR_SUCCESS.
226 * Failure: An NTSTATUS error code.
228 NTSTATUS WINAPI
DbgUiIssueRemoteBreakin(HANDLE hProcess
)
232 ret
= RtlCreateUserThread(hProcess
, NULL
, FALSE
, NULL
, 0, 0, DbgUiRemoteBreakin
, NULL
, NULL
, NULL
);
233 /* should we wait for the thread to finish? */
234 /* probably no since this would stall the debugger */
238 /***********************************************************************
239 * DbgUiDebugActiveProcess (NTDLL.@)
241 * Attempts to attach the debugger to a process.
244 * hProcess [I] The process to be debugged.
247 * Success: ERROR_SUCCESS.
248 * Failure: An NTSTATUS error code.
250 NTSTATUS WINAPI
DbgUiDebugActiveProcess(HANDLE hProcess
)
254 TRACE_(debug
)("(%p)\n", hProcess
);
255 SERVER_START_REQ( debug_process
)
257 req
->handle
= hProcess
;
259 ret
= wine_server_call( req
);
266 ret
= DbgUiIssueRemoteBreakin( hProcess
);
271 /***********************************************************************
272 * DbgUiStopDebugging (NTDLL.@)
274 * Attempts to detach the debugger from a process.
277 * hProcess [I] The process to be detached from.
280 * Success: ERROR_SUCCESS.
281 * Failure: An NTSTATUS error code.
283 NTSTATUS WINAPI
DbgUiStopDebugging(HANDLE hProcess
)
287 TRACE_(debug
)("(%p)\n", hProcess
);
288 SERVER_START_REQ( debug_process
)
290 req
->handle
= hProcess
;
292 ret
= wine_server_call( req
);