Small fixes.
[wine] / misc / error.c
1 /*
2  * Log internal errors 
3  *
4  * Copyright 1997 Andrew Taylor
5  */
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10
11 #include "winbase.h"
12 #include "windef.h"
13 #include "stackframe.h"
14 #include "debugtools.h"
15
16 #define ErrorString(manifest) { manifest, # manifest }
17
18 static const struct {
19         int constant;
20         const char *name;
21 } ErrorStrings[] = {
22
23         ErrorString(ERR_GALLOC),
24         ErrorString(ERR_GREALLOC),
25         ErrorString(ERR_GLOCK),
26         ErrorString(ERR_LALLOC),
27         ErrorString(ERR_LREALLOC),
28         ErrorString(ERR_LLOCK),
29         ErrorString(ERR_ALLOCRES),
30         ErrorString(ERR_LOCKRES),
31         ErrorString(ERR_LOADMODULE),
32         ErrorString(ERR_CREATEDLG),
33         ErrorString(ERR_CREATEDLG2),
34         ErrorString(ERR_REGISTERCLASS),
35         ErrorString(ERR_DCBUSY),
36         ErrorString(ERR_CREATEWND),
37         ErrorString(ERR_STRUCEXTRA),
38         ErrorString(ERR_LOADSTR),
39         ErrorString(ERR_LOADMENU),
40         ErrorString(ERR_NESTEDBEGINPAINT),
41         ErrorString(ERR_BADINDEX),
42         ErrorString(ERR_CREATEMENU),
43         ErrorString(ERR_CREATEDC),
44         ErrorString(ERR_CREATEMETA),
45         ErrorString(ERR_DELOBJSELECTED),
46         ErrorString(ERR_SELBITMAP)
47 };
48
49 static const struct {
50         int constant;
51         const char *name;
52 } ParamErrorStrings[] = {
53
54         ErrorString(ERR_BAD_VALUE),
55         ErrorString(ERR_BAD_FLAGS),
56         ErrorString(ERR_BAD_INDEX),
57         ErrorString(ERR_BAD_DVALUE),
58         ErrorString(ERR_BAD_DFLAGS),
59         ErrorString(ERR_BAD_DINDEX),
60         ErrorString(ERR_BAD_PTR),
61         ErrorString(ERR_BAD_FUNC_PTR),
62         ErrorString(ERR_BAD_SELECTOR),
63         ErrorString(ERR_BAD_STRING_PTR),
64         ErrorString(ERR_BAD_HANDLE),
65         ErrorString(ERR_BAD_HINSTANCE),
66         ErrorString(ERR_BAD_HMODULE),
67         ErrorString(ERR_BAD_GLOBAL_HANDLE),
68         ErrorString(ERR_BAD_LOCAL_HANDLE),
69         ErrorString(ERR_BAD_ATOM),
70         ErrorString(ERR_BAD_HFILE),
71         ErrorString(ERR_BAD_HWND),
72         ErrorString(ERR_BAD_HMENU),
73         ErrorString(ERR_BAD_HCURSOR),
74         ErrorString(ERR_BAD_HICON),
75         ErrorString(ERR_BAD_HDWP),
76         ErrorString(ERR_BAD_CID),
77         ErrorString(ERR_BAD_HDRVR),
78         ErrorString(ERR_BAD_COORDS),
79         ErrorString(ERR_BAD_GDI_OBJECT),
80         ErrorString(ERR_BAD_HDC),
81         ErrorString(ERR_BAD_HPEN),
82         ErrorString(ERR_BAD_HFONT),
83         ErrorString(ERR_BAD_HBRUSH),
84         ErrorString(ERR_BAD_HBITMAP),
85         ErrorString(ERR_BAD_HRGN),
86         ErrorString(ERR_BAD_HPALETTE),
87         ErrorString(ERR_BAD_HMETAFILE)
88 };
89
90 #undef  ErrorString
91 #define ErrorStringCount (sizeof(ErrorStrings) / sizeof(ErrorStrings[0]))
92 #define ParamErrorStringCount (sizeof(ParamErrorStrings) / sizeof(ParamErrorStrings[0]))
93
94 /***********************************************************************
95 *       GetErrorString (internal)
96 */
97 static const char *GetErrorString(UINT16 uErr) 
98 {
99   static char buffer[80];
100   int i;
101
102   for (i = 0; i < ErrorStringCount; i++) {
103     if (uErr == ErrorStrings[i].constant)
104       return ErrorStrings[i].name;
105   }
106
107   sprintf(buffer, "%x", uErr);
108   return buffer;
109 }
110
111
112 /***********************************************************************
113 *       GetParamErrorString (internal)
114 */
115 static const char *GetParamErrorString(UINT16 uErr) {
116         static char buffer[80];
117
118         if (uErr & ERR_WARNING) {
119                 strcpy(buffer, "ERR_WARNING | ");
120                 uErr &= ~ERR_WARNING;
121         } else
122                 buffer[0] = '\0';
123
124         {
125                 int i;
126
127                 for (i = 0; i < ParamErrorStringCount; i++) {
128                         if (uErr == ParamErrorStrings[i].constant) {
129                                 strcat(buffer, ParamErrorStrings[i].name);
130                                 return buffer;
131                         }
132                 }
133         }
134
135         sprintf(buffer + strlen(buffer), "%x", uErr);
136         return buffer;
137 }
138
139
140 /***********************************************************************
141 *       LogError (KERNEL.324)
142 */
143 VOID WINAPI LogError16(UINT16 uErr, LPVOID lpvInfo)
144 {
145         MESSAGE("(%s, %p)\n", GetErrorString(uErr), lpvInfo);
146 }
147
148
149 /***********************************************************************
150 *       LogParamError (KERNEL.325)
151 */
152 void WINAPI LogParamError16(UINT16 uErr, FARPROC16 lpfn, LPVOID lpvParam)
153 {
154         /* FIXME: is it possible to get the module name/function
155          * from the lpfn param?
156          */
157         MESSAGE("(%s, %p, %p)\n", GetParamErrorString(uErr), lpfn, lpvParam);
158 }
159
160 /***********************************************************************
161 *       HandleParamError (KERNEL.327)
162 */
163 void WINAPI HandleParamError( CONTEXT86 *context )
164 {
165         UINT16 uErr = BX_reg( context );
166         FARPROC16 lpfn = (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( CS_reg(context),
167                                                            EIP_reg(context) );
168         LPVOID lpvParam = (LPVOID)MAKELONG( AX_reg( context ), 
169                                             CX_reg( context ) );
170         
171         LogParamError16( uErr, lpfn, lpvParam );
172
173         if (!(uErr & ERR_WARNING))
174         {
175                 /* Abort current procedure: Unwind stack frame and jump
176                    to error handler (location at [bp-2]) */
177
178                 WORD *stack = PTR_SEG_OFF_TO_LIN( SS_reg( context ), 
179                                                   LOWORD(EBP_reg( context )) );
180                 ESP_reg( context ) = LOWORD(EBP_reg( context )) - 2;
181                 EBP_reg( context ) = stack[0] & 0xfffe;
182
183                 EIP_reg( context ) = stack[-1];
184
185                 EAX_reg( context ) = ECX_reg( context ) = EDX_reg( context ) = 0;
186                 ES_reg( context) = 0;
187         }
188 }
189