Added some more tests.
[wine.git] / if1632 / relay.c
blob6c6a12bb29668d7907b9c4dda72f6bba3fd10c02
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 <assert.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
25 #include "wine/winbase16.h"
26 #include "winnt.h"
27 #include "module.h"
28 #include "stackframe.h"
29 #include "selectors.h"
30 #include "builtin16.h"
31 #include "syslevel.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(relay);
36 /***********************************************************************
37 * RELAY_Init
39 BOOL RELAY_Init(void)
41 #ifdef __i386__
42 WORD codesel;
44 /* Allocate the code selector for CallTo16 routines */
46 extern void Call16_Ret_Start(), Call16_Ret_End();
47 extern void CallTo16_Ret();
48 extern void CALL32_CBClient_Ret();
49 extern void CALL32_CBClientEx_Ret();
50 extern SEGPTR CallTo16_RetAddr;
51 extern DWORD CallTo16_DataSelector;
52 extern SEGPTR CALL32_CBClient_RetAddr;
53 extern SEGPTR CALL32_CBClientEx_RetAddr;
55 codesel = SELECTOR_AllocBlock( (void *)Call16_Ret_Start,
56 (char *)Call16_Ret_End - (char *)Call16_Ret_Start,
57 WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
58 if (!codesel) return FALSE;
60 /* Patch the return addresses for CallTo16 routines */
62 CallTo16_DataSelector = __get_ds();
63 CallTo16_RetAddr =
64 MAKESEGPTR( codesel, (char*)CallTo16_Ret - (char*)Call16_Ret_Start );
65 CALL32_CBClient_RetAddr =
66 MAKESEGPTR( codesel, (char*)CALL32_CBClient_Ret - (char*)Call16_Ret_Start );
67 CALL32_CBClientEx_RetAddr =
68 MAKESEGPTR( codesel, (char*)CALL32_CBClientEx_Ret - (char*)Call16_Ret_Start );
69 #endif
70 return TRUE;
74 * Stubs for the CallTo16/CallFrom16 routines on non-Intel architectures
75 * (these will never be called but need to be present to satisfy the linker ...)
77 #ifndef __i386__
78 /***********************************************************************
79 * wine_call_to_16_word (KERNEL32.@)
81 WORD WINAPI wine_call_to_16_word( FARPROC16 target, INT nArgs )
83 assert( FALSE );
86 /***********************************************************************
87 * wine_call_to_16_long (KERNEL32.@)
89 LONG WINAPI wine_call_to_16_long( FARPROC16 target, INT nArgs )
91 assert( FALSE );
94 /***********************************************************************
95 * wine_call_to_16_regs_short (KERNEL32.@)
97 void WINAPI wine_call_to_16_regs_short( CONTEXT86 *context, INT nArgs )
99 assert( FALSE );
102 /***********************************************************************
103 * wine_call_to_16_regs_long (KERNEL32.@)
105 void WINAPI wine_call_to_16_regs_long ( CONTEXT86 *context, INT nArgs )
107 assert( FALSE );
110 /***********************************************************************
111 * __wine_call_from_16_word (KERNEL32.@)
113 WORD __wine_call_from_16_word()
115 assert( FALSE );
118 /***********************************************************************
119 * __wine_call_from_16_long (KERNEL32.@)
121 LONG __wine_call_from_16_long()
123 assert( FALSE );
126 /***********************************************************************
127 * __wine_call_from_16_regs (KERNEL32.@)
129 void __wine_call_from_16_regs()
131 assert( FALSE );
134 DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi )
135 { assert( FALSE ); }
137 DWORD WINAPI CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs )
138 { assert( FALSE ); }
139 #endif
142 /* from relay32/relay386.c */
143 extern char **debug_relay_excludelist,**debug_relay_includelist;
144 extern int RELAY_ShowDebugmsgRelay(const char *func);
147 /***********************************************************************
148 * get_entry_point
150 * Return the ordinal, name, and type info corresponding to a CS:IP address.
152 static const CALLFROM16 *get_entry_point( STACK16FRAME *frame, LPSTR name, WORD *pOrd )
154 WORD i, max_offset;
155 register BYTE *p;
156 NE_MODULE *pModule;
157 ET_BUNDLE *bundle;
158 ET_ENTRY *entry;
160 if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16( frame->module_cs ) ))))
161 return NULL;
163 max_offset = 0;
164 *pOrd = 0;
165 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
168 entry = (ET_ENTRY *)((BYTE *)bundle+6);
169 for (i = bundle->first + 1; i <= bundle->last; i++)
171 if ((entry->offs < frame->entry_ip)
172 && (entry->segnum == 1) /* code segment ? */
173 && (entry->offs >= max_offset))
175 max_offset = entry->offs;
176 *pOrd = i;
178 entry++;
180 } while ( (bundle->next)
181 && (bundle = (ET_BUNDLE *)((BYTE *)pModule+bundle->next)));
183 /* Search for the name in the resident names table */
184 /* (built-in modules have no non-resident table) */
186 p = (BYTE *)pModule + pModule->name_table;
187 while (*p)
189 p += *p + 1 + sizeof(WORD);
190 if (*(WORD *)(p + *p + 1) == *pOrd) break;
193 sprintf( name, "%.*s.%d: %.*s",
194 *((BYTE *)pModule + pModule->name_table),
195 (char *)pModule + pModule->name_table + 1,
196 *pOrd, *p, (char *)(p + 1) );
198 /* Retrieve entry point call structure */
199 p = MapSL( MAKESEGPTR( frame->module_cs, frame->callfrom_ip ) );
200 /* p now points to lret, get the start of CALLFROM16 structure */
201 return (CALLFROM16 *)(p - (BYTE *)&((CALLFROM16 *)0)->lret);
205 /***********************************************************************
206 * RELAY_DebugCallFrom16
208 void RELAY_DebugCallFrom16( CONTEXT86 *context )
210 STACK16FRAME *frame;
211 WORD ordinal;
212 char *args16, funstr[80];
213 const CALLFROM16 *call;
214 int i;
216 if (!TRACE_ON(relay)) return;
218 frame = CURRENT_STACK16;
219 call = get_entry_point( frame, funstr, &ordinal );
220 if (!call) return; /* happens for the two snoop register relays */
221 if (!RELAY_ShowDebugmsgRelay(funstr)) return;
222 DPRINTF( "%08lx:Call %s(",GetCurrentThreadId(),funstr);
223 VA_START16( args16 );
225 if (call->lret == 0xcb66) /* cdecl */
227 for (i = 0; i < 20; i++)
229 int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
231 if (type == ARG_NONE) break;
232 if (i) DPRINTF( "," );
233 switch(type)
235 case ARG_WORD:
236 case ARG_SWORD:
237 DPRINTF( "%04x", *(WORD *)args16 );
238 args16 += sizeof(WORD);
239 break;
240 case ARG_LONG:
241 DPRINTF( "%08x", *(int *)args16 );
242 args16 += sizeof(int);
243 break;
244 case ARG_PTR:
245 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
246 args16 += sizeof(SEGPTR);
247 break;
248 case ARG_STR:
249 DPRINTF( "%08x %s", *(int *)args16,
250 debugres_a( MapSL(*(SEGPTR *)args16 )));
251 args16 += sizeof(int);
252 break;
253 case ARG_SEGSTR:
254 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
255 debugres_a( MapSL(*(SEGPTR *)args16 )) );
256 args16 += sizeof(SEGPTR);
257 break;
258 default:
259 break;
263 else /* not cdecl */
265 /* Start with the last arg */
266 args16 += call->nArgs;
267 for (i = 0; i < 20; i++)
269 int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
271 if (type == ARG_NONE) break;
272 if (i) DPRINTF( "," );
273 switch(type)
275 case ARG_WORD:
276 case ARG_SWORD:
277 args16 -= sizeof(WORD);
278 DPRINTF( "%04x", *(WORD *)args16 );
279 break;
280 case ARG_LONG:
281 args16 -= sizeof(int);
282 DPRINTF( "%08x", *(int *)args16 );
283 break;
284 case ARG_PTR:
285 args16 -= sizeof(SEGPTR);
286 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
287 break;
288 case ARG_STR:
289 args16 -= sizeof(int);
290 DPRINTF( "%08x %s", *(int *)args16,
291 debugres_a( MapSL(*(SEGPTR *)args16 )));
292 break;
293 case ARG_SEGSTR:
294 args16 -= sizeof(SEGPTR);
295 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
296 debugres_a( MapSL(*(SEGPTR *)args16 )) );
297 break;
298 default:
299 break;
304 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
305 VA_END16( args16 );
307 if (call->arg_types[0] & ARG_REGISTER)
308 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
309 AX_reg(context), BX_reg(context), CX_reg(context),
310 DX_reg(context), SI_reg(context), DI_reg(context),
311 (WORD)context->SegEs, context->EFlags );
313 SYSLEVEL_CheckNotLevel( 2 );
317 /***********************************************************************
318 * RELAY_DebugCallFrom16Ret
320 void RELAY_DebugCallFrom16Ret( CONTEXT86 *context, int ret_val )
322 STACK16FRAME *frame;
323 WORD ordinal;
324 char funstr[80];
325 const CALLFROM16 *call;
327 if (!TRACE_ON(relay)) return;
328 frame = CURRENT_STACK16;
329 call = get_entry_point( frame, funstr, &ordinal );
330 if (!call) return;
331 if (!RELAY_ShowDebugmsgRelay(funstr)) return;
332 DPRINTF( "%08lx:Ret %s() ",GetCurrentThreadId(),funstr);
334 if (call->arg_types[0] & ARG_REGISTER)
336 DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
337 (WORD)context->SegCs, LOWORD(context->Eip), (WORD)context->SegDs);
338 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
339 AX_reg(context), BX_reg(context), CX_reg(context),
340 DX_reg(context), SI_reg(context), DI_reg(context),
341 (WORD)context->SegEs, context->EFlags );
343 else if (call->arg_types[0] & ARG_RET16)
345 DPRINTF( "retval=%04x ret=%04x:%04x ds=%04x\n",
346 ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
348 else
350 DPRINTF( "retval=%08x ret=%04x:%04x ds=%04x\n",
351 ret_val, frame->cs, frame->ip, frame->ds );
353 SYSLEVEL_CheckNotLevel( 2 );
357 /***********************************************************************
358 * RELAY_DebugCallTo16
360 * 'target' contains either the function to call (normal CallTo16)
361 * or a pointer to the CONTEXT86 struct (register CallTo16).
362 * 'nb_args' is the number of argument bytes on the 16-bit stack;
363 * 'reg_func' specifies whether we have a register CallTo16 or not.
365 void RELAY_DebugCallTo16( LPVOID target, int nb_args, BOOL reg_func )
367 WORD *stack16;
368 TEB *teb;
370 if (!TRACE_ON(relay)) return;
371 teb = NtCurrentTeb();
372 stack16 = (WORD *)THREAD_STACK16(teb);
374 nb_args /= sizeof(WORD);
376 if ( reg_func )
378 CONTEXT86 *context = (CONTEXT86 *)target;
380 DPRINTF("%08lx:CallTo16(func=%04lx:%04x,ds=%04lx",
381 GetCurrentThreadId(),
382 context->SegCs, LOWORD(context->Eip), context->SegDs );
383 while (nb_args--) DPRINTF( ",%04x", *--stack16 );
384 DPRINTF(") ss:sp=%04x:%04x", SELECTOROF(teb->cur_stack),
385 OFFSETOF(teb->cur_stack) );
386 DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x si=%04x di=%04x bp=%04x es=%04x fs=%04x\n",
387 AX_reg(context), BX_reg(context), CX_reg(context),
388 DX_reg(context), SI_reg(context), DI_reg(context),
389 BP_reg(context), (WORD)context->SegEs, (WORD)context->SegFs );
391 else
393 DPRINTF("%08lx:CallTo16(func=%04x:%04x,ds=%04x",
394 GetCurrentThreadId(),
395 HIWORD(target), LOWORD(target), SELECTOROF(teb->cur_stack) );
396 while (nb_args--) DPRINTF( ",%04x", *--stack16 );
397 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb->cur_stack),
398 OFFSETOF(teb->cur_stack) );
401 SYSLEVEL_CheckNotLevel( 2 );
405 /***********************************************************************
406 * RELAY_DebugCallTo16Ret
408 void RELAY_DebugCallTo16Ret( BOOL reg_func, int ret_val )
410 if (!TRACE_ON(relay)) return;
412 if (!reg_func)
414 DPRINTF("%08lx:RetFrom16() ss:sp=%04x:%04x retval=%08x\n",
415 GetCurrentThreadId(),
416 SELECTOROF(NtCurrentTeb()->cur_stack),
417 OFFSETOF(NtCurrentTeb()->cur_stack), ret_val);
419 else
421 CONTEXT86 *context = (CONTEXT86 *)ret_val;
423 DPRINTF("%08lx:RetFrom16() ss:sp=%04x:%04x ",
424 GetCurrentThreadId(),
425 SELECTOROF(NtCurrentTeb()->cur_stack),
426 OFFSETOF(NtCurrentTeb()->cur_stack));
427 DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x bp=%04x sp=%04x\n",
428 AX_reg(context), BX_reg(context), CX_reg(context),
429 DX_reg(context), BP_reg(context), LOWORD(context->Esp));
432 SYSLEVEL_CheckNotLevel( 2 );