Extended WOWCallback16Ex to support register functions too. This
[wine/hacks.git] / if1632 / relay.c
blobbca70f91065d218b066123f87d1e6918bace4cf7
1 /*
2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "config.h"
21 #include "wine/port.h"
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
28 #include "windef.h"
29 #include "wine/winbase16.h"
30 #include "module.h"
31 #include "stackframe.h"
32 #include "selectors.h"
33 #include "builtin16.h"
34 #include "syslevel.h"
35 #include "wine/library.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(relay);
40 /***********************************************************************
41 * RELAY_Init
43 BOOL RELAY_Init(void)
45 #ifdef __i386__
46 WORD codesel;
48 /* Allocate the code selector for CallTo16 routines */
50 extern void Call16_Ret_Start(), Call16_Ret_End();
51 extern void CallTo16_Ret();
52 extern void CALL32_CBClient_Ret();
53 extern void CALL32_CBClientEx_Ret();
54 extern SEGPTR CallTo16_RetAddr;
55 extern DWORD CallTo16_DataSelector;
56 extern SEGPTR CALL32_CBClient_RetAddr;
57 extern SEGPTR CALL32_CBClientEx_RetAddr;
59 codesel = SELECTOR_AllocBlock( (void *)Call16_Ret_Start,
60 (char *)Call16_Ret_End - (char *)Call16_Ret_Start,
61 WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
62 if (!codesel) return FALSE;
64 /* Patch the return addresses for CallTo16 routines */
66 CallTo16_DataSelector = wine_get_ds();
67 CallTo16_RetAddr =
68 MAKESEGPTR( codesel, (char*)CallTo16_Ret - (char*)Call16_Ret_Start );
69 CALL32_CBClient_RetAddr =
70 MAKESEGPTR( codesel, (char*)CALL32_CBClient_Ret - (char*)Call16_Ret_Start );
71 CALL32_CBClientEx_RetAddr =
72 MAKESEGPTR( codesel, (char*)CALL32_CBClientEx_Ret - (char*)Call16_Ret_Start );
73 #endif
74 return TRUE;
78 * Stubs for the CallTo16/CallFrom16 routines on non-Intel architectures
79 * (these will never be called but need to be present to satisfy the linker ...)
81 #ifndef __i386__
82 /***********************************************************************
83 * __wine_call_from_16_word (KERNEL32.@)
85 WORD __wine_call_from_16_word()
87 assert( FALSE );
90 /***********************************************************************
91 * __wine_call_from_16_long (KERNEL32.@)
93 LONG __wine_call_from_16_long()
95 assert( FALSE );
98 /***********************************************************************
99 * __wine_call_from_16_regs (KERNEL32.@)
101 void __wine_call_from_16_regs()
103 assert( FALSE );
106 DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi )
107 { assert( FALSE ); }
109 DWORD WINAPI CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs )
110 { assert( FALSE ); }
111 #endif
114 /***********************************************************************
115 * RELAY_ShowDebugmsgRelay
117 * Simple function to decide if a particular debugging message is
118 * wanted.
120 static int RELAY_ShowDebugmsgRelay(const char *func)
122 /* from relay32/relay386.c */
123 extern const char **debug_relay_excludelist,**debug_relay_includelist;
125 if(debug_relay_excludelist || debug_relay_includelist) {
126 const char *term = strchr(func, ':');
127 const char **listitem;
128 int len, len2, itemlen, show;
130 if(debug_relay_excludelist) {
131 show = 1;
132 listitem = debug_relay_excludelist;
133 } else {
134 show = 0;
135 listitem = debug_relay_includelist;
137 assert(term);
138 assert(strlen(term) > 2);
139 len = term - func;
140 len2 = strchr(func, '.') - func;
141 assert(len2 && len2 > 0 && len2 < 64);
142 term += 2;
143 for(; *listitem; listitem++) {
144 itemlen = strlen(*listitem);
145 if((itemlen == len && !strncasecmp(*listitem, func, len)) ||
146 (itemlen == len2 && !strncasecmp(*listitem, func, len2)) ||
147 !strcasecmp(*listitem, term)) {
148 show = !show;
149 break;
152 return show;
154 return 1;
158 /***********************************************************************
159 * get_entry_point
161 * Return the ordinal, name, and type info corresponding to a CS:IP address.
163 static const CALLFROM16 *get_entry_point( STACK16FRAME *frame, LPSTR name, WORD *pOrd )
165 WORD i, max_offset;
166 register BYTE *p;
167 NE_MODULE *pModule;
168 ET_BUNDLE *bundle;
169 ET_ENTRY *entry;
171 if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16( frame->module_cs ) ))))
172 return NULL;
174 max_offset = 0;
175 *pOrd = 0;
176 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
179 entry = (ET_ENTRY *)((BYTE *)bundle+6);
180 for (i = bundle->first + 1; i <= bundle->last; i++)
182 if ((entry->offs < frame->entry_ip)
183 && (entry->segnum == 1) /* code segment ? */
184 && (entry->offs >= max_offset))
186 max_offset = entry->offs;
187 *pOrd = i;
189 entry++;
191 } while ( (bundle->next)
192 && (bundle = (ET_BUNDLE *)((BYTE *)pModule+bundle->next)));
194 /* Search for the name in the resident names table */
195 /* (built-in modules have no non-resident table) */
197 p = (BYTE *)pModule + pModule->name_table;
198 while (*p)
200 p += *p + 1 + sizeof(WORD);
201 if (*(WORD *)(p + *p + 1) == *pOrd) break;
204 sprintf( name, "%.*s.%d: %.*s",
205 *((BYTE *)pModule + pModule->name_table),
206 (char *)pModule + pModule->name_table + 1,
207 *pOrd, *p, (char *)(p + 1) );
209 /* Retrieve entry point call structure */
210 p = MapSL( MAKESEGPTR( frame->module_cs, frame->callfrom_ip ) );
211 /* p now points to lret, get the start of CALLFROM16 structure */
212 return (CALLFROM16 *)(p - (BYTE *)&((CALLFROM16 *)0)->lret);
216 /***********************************************************************
217 * RELAY_DebugCallFrom16
219 void RELAY_DebugCallFrom16( CONTEXT86 *context )
221 STACK16FRAME *frame;
222 WORD ordinal;
223 char *args16, funstr[80];
224 const CALLFROM16 *call;
225 int i;
227 if (!TRACE_ON(relay)) return;
229 frame = CURRENT_STACK16;
230 call = get_entry_point( frame, funstr, &ordinal );
231 if (!call) return; /* happens for the two snoop register relays */
232 if (!RELAY_ShowDebugmsgRelay(funstr)) return;
233 DPRINTF( "%04lx:Call %s(",GetCurrentThreadId(),funstr);
234 VA_START16( args16 );
236 if (call->lret == 0xcb66) /* cdecl */
238 for (i = 0; i < 20; i++)
240 int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
242 if (type == ARG_NONE) break;
243 if (i) DPRINTF( "," );
244 switch(type)
246 case ARG_WORD:
247 case ARG_SWORD:
248 DPRINTF( "%04x", *(WORD *)args16 );
249 args16 += sizeof(WORD);
250 break;
251 case ARG_LONG:
252 DPRINTF( "%08x", *(int *)args16 );
253 args16 += sizeof(int);
254 break;
255 case ARG_PTR:
256 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
257 args16 += sizeof(SEGPTR);
258 break;
259 case ARG_STR:
260 DPRINTF( "%08x %s", *(int *)args16,
261 debugstr_a( MapSL(*(SEGPTR *)args16 )));
262 args16 += sizeof(int);
263 break;
264 case ARG_SEGSTR:
265 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
266 debugstr_a( MapSL(*(SEGPTR *)args16 )) );
267 args16 += sizeof(SEGPTR);
268 break;
269 default:
270 break;
274 else /* not cdecl */
276 /* Start with the last arg */
277 args16 += call->nArgs;
278 for (i = 0; i < 20; i++)
280 int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
282 if (type == ARG_NONE) break;
283 if (i) DPRINTF( "," );
284 switch(type)
286 case ARG_WORD:
287 case ARG_SWORD:
288 args16 -= sizeof(WORD);
289 DPRINTF( "%04x", *(WORD *)args16 );
290 break;
291 case ARG_LONG:
292 args16 -= sizeof(int);
293 DPRINTF( "%08x", *(int *)args16 );
294 break;
295 case ARG_PTR:
296 args16 -= sizeof(SEGPTR);
297 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
298 break;
299 case ARG_STR:
300 args16 -= sizeof(int);
301 DPRINTF( "%08x %s", *(int *)args16,
302 debugstr_a( MapSL(*(SEGPTR *)args16 )));
303 break;
304 case ARG_SEGSTR:
305 args16 -= sizeof(SEGPTR);
306 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
307 debugstr_a( MapSL(*(SEGPTR *)args16 )) );
308 break;
309 default:
310 break;
315 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
316 VA_END16( args16 );
318 if (call->arg_types[0] & ARG_REGISTER)
319 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
320 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
321 (WORD)context->Edx, (WORD)context->Esi, (WORD)context->Edi,
322 (WORD)context->SegEs, context->EFlags );
324 SYSLEVEL_CheckNotLevel( 2 );
328 /***********************************************************************
329 * RELAY_DebugCallFrom16Ret
331 void RELAY_DebugCallFrom16Ret( CONTEXT86 *context, int ret_val )
333 STACK16FRAME *frame;
334 WORD ordinal;
335 char funstr[80];
336 const CALLFROM16 *call;
338 if (!TRACE_ON(relay)) return;
339 frame = CURRENT_STACK16;
340 call = get_entry_point( frame, funstr, &ordinal );
341 if (!call) return;
342 if (!RELAY_ShowDebugmsgRelay(funstr)) return;
343 DPRINTF( "%04lx:Ret %s() ",GetCurrentThreadId(),funstr);
345 if (call->arg_types[0] & ARG_REGISTER)
347 DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
348 (WORD)context->SegCs, LOWORD(context->Eip), (WORD)context->SegDs);
349 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
350 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
351 (WORD)context->Edx, (WORD)context->Esi, (WORD)context->Edi,
352 (WORD)context->SegEs, context->EFlags );
354 else if (call->arg_types[0] & ARG_RET16)
356 DPRINTF( "retval=%04x ret=%04x:%04x ds=%04x\n",
357 ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
359 else
361 DPRINTF( "retval=%08x ret=%04x:%04x ds=%04x\n",
362 ret_val, frame->cs, frame->ip, frame->ds );
364 SYSLEVEL_CheckNotLevel( 2 );