]>
Commit | Line | Data |
---|---|---|
72dd16ea ILT |
1 | /* i386-nlmstub.c -- NLM debugging stub for the i386. |
2 | ||
3 | This is originally based on an m68k software stub written by Glenn | |
4 | Engel at HP, but has changed quite a bit. It was modified for the | |
5 | i386 by Jim Kingdon, Cygnus Support. It was modified to run under | |
6 | NetWare by Ian Lance Taylor, Cygnus Support. | |
7 | ||
8 | This code is intended to produce an NLM (a NetWare Loadable Module) | |
9 | to run under NetWare on an i386 platform. To create the NLM, | |
10 | compile this code into an object file using the NLM SDK on any i386 | |
11 | host, and use the nlmconv program (available in the GNU binutils) | |
12 | to transform the resulting object file into an NLM. */ | |
13 | ||
14 | /**************************************************************************** | |
15 | ||
16 | THIS SOFTWARE IS NOT COPYRIGHTED | |
17 | ||
18 | HP offers the following for use in the public domain. HP makes no | |
19 | warranty with regard to the software or it's performance and the | |
20 | user accepts the software "AS IS" with all faults. | |
21 | ||
22 | HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD | |
23 | TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
24 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
25 | ||
26 | ****************************************************************************/ | |
27 | ||
28 | /**************************************************************************** | |
29 | * | |
30 | * The following gdb commands are supported: | |
31 | * | |
32 | * command function Return value | |
33 | * | |
34 | * g return the value of the CPU registers hex data or ENN | |
35 | * G set the value of the CPU registers OK or ENN | |
36 | * | |
37 | * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN | |
38 | * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN | |
39 | * | |
40 | * c Resume at current address SNN ( signal NN) | |
41 | * cAA..AA Continue at address AA..AA SNN | |
42 | * | |
43 | * s Step one instruction SNN | |
44 | * sAA..AA Step one instruction from AA..AA SNN | |
45 | * | |
46 | * k kill | |
47 | * | |
48 | * ? What was the last sigval ? SNN (signal NN) | |
49 | * | |
50 | * All commands and responses are sent with a packet which includes a | |
51 | * checksum. A packet consists of | |
52 | * | |
53 | * $<packet info>#<checksum>. | |
54 | * | |
55 | * where | |
56 | * <packet info> :: <characters representing the command or response> | |
57 | * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>> | |
58 | * | |
59 | * When a packet is received, it is first acknowledged with either '+' or '-'. | |
60 | * '+' indicates a successful transfer. '-' indicates a failed transfer. | |
61 | * | |
62 | * Example: | |
63 | * | |
64 | * Host: Reply: | |
65 | * $m0,10#2a +$00010203040506070809101112131415#42 | |
66 | * | |
67 | ****************************************************************************/ | |
68 | ||
69 | #include <dfs.h> | |
70 | #include <stdio.h> | |
71 | #include <string.h> | |
72 | #include <stdlib.h> | |
73 | #include <ctype.h> | |
74 | #include <time.h> | |
75 | #include <aio.h> | |
76 | #include <conio.h> | |
77 | #include <advanced.h> | |
78 | #include <debugapi.h> | |
79 | #include <process.h> | |
80 | ||
81 | /************************************************************************/ | |
82 | /***************************************************************************** | |
83 | * | |
84 | * (C) Copyright 1988-1993 Novell, Inc. | |
85 | * All Rights Reserved. | |
86 | * | |
87 | * This program is an unpublished copyrighted work which is proprietary | |
88 | * to Novell, Inc. and contains confidential information that is not | |
89 | * to be reproduced or disclosed to any other person or entity without | |
90 | * prior written consent from Novell, Inc. in each and every instance. | |
91 | * | |
92 | * WARNING: Unauthorized reproduction of this program as well as | |
93 | * unauthorized preparation of derivative works based upon the | |
94 | * program or distribution of copies by sale, rental, lease or | |
95 | * lending are violations of federal copyright laws and state trade | |
96 | * secret laws, punishable by civil and criminal penalties. | |
97 | * | |
98 | * $release$ | |
99 | * $modname: loadstuff.h$ | |
100 | * $version: 1.37$ | |
101 | * $date: Fri, Jan 15, 1993$ | |
102 | * | |
103 | ****************************************************************************/ | |
104 | ||
105 | ||
106 | /* WARNING: THIS IS NOT A COMPLETE OS HEADER FILE - DON'T GET CONFUSED | |
107 | *********************************************************************** | |
108 | * The information is this file is a subset of the OS LOADER.H. | |
109 | * This file was created to reveal the LoadDefinitionStrucutre and some | |
110 | * associated information to Cygnus Support to assist them in their | |
111 | * efforts to develop GNU netware utilities. Don't confuse this file | |
112 | * with LOADER.H or any other actually supported NetWare header. | |
113 | ||
114 | ************************************************************************/ | |
115 | ||
116 | struct LoadDefinitionStructure | |
117 | { | |
118 | struct LoadDefinitionStructure *LDLink; | |
119 | struct LoadDefinitionStructure *LDKillLink; | |
120 | struct LoadDefinitionStructure *LDScanLink; | |
121 | struct ResourceTagStructure *LDResourceList; | |
122 | LONG LDIdentificationNumber; | |
123 | LONG LDCodeImageOffset; | |
124 | LONG LDCodeImageLength; | |
125 | LONG LDDataImageOffset; | |
126 | LONG LDDataImageLength; | |
127 | LONG LDUninitializedDataLength; | |
128 | LONG LDCustomDataOffset; | |
129 | LONG LDCustomDataSize; | |
130 | LONG LDFlags; | |
131 | LONG LDType; | |
132 | LONG (*LDInitializationProcedure)( | |
133 | struct LoadDefinitionStructure *LoadRecord, | |
134 | struct ScreenStruct *screenID, | |
135 | BYTE *CommandLine, | |
136 | BYTE *loadDirectoryPath, | |
137 | LONG uninitializedDataLength, | |
138 | LONG fileHandle, | |
139 | LONG (*ReadRoutine)( | |
140 | LONG fileHandle, | |
141 | LONG offset, | |
142 | void *buffer, | |
143 | LONG numberOfBytes), | |
144 | LONG customDataOffset, | |
145 | LONG customDataSize); | |
146 | void (*LDExitProcedure)(void); | |
147 | LONG (*LDCheckUnloadProcedure)( | |
148 | struct ScreenStruct *screenID); | |
149 | struct ExternalPublicDefinitionStructure *LDPublics; | |
150 | BYTE LDFileName[36]; | |
151 | BYTE LDName[128]; | |
152 | LONG *LDCLIBLoadStructure; | |
153 | LONG *LDNLMDebugger; | |
154 | LONG LDParentID; | |
155 | LONG LDReservedForCLIB; | |
156 | LONG Reserved0; | |
157 | LONG Reserved1; | |
158 | void *LDModuleObjectHandle; /* If Instrumented BEW 10/16/90 */ | |
159 | LONG LDMajorVersion; | |
160 | LONG LDMinorVersion; | |
161 | LONG LDRevision; | |
162 | LONG LDYear; | |
163 | LONG LDMonth; | |
164 | LONG LDDay; | |
165 | BYTE *LDCopyright; | |
166 | LONG LDAllocAvailBytes; | |
167 | LONG LDAllocFreeCount; | |
168 | LONG LDLastGarbCollect; | |
169 | LONG LDAlloc16Lists[64]; | |
170 | LONG LDAlloc256Lists[12]; | |
171 | LONG LDAlloc4kList; | |
172 | struct DomainStructure *LDDomainID; /* This must be non-zero for the Alloc Hunt code to work right. */ | |
173 | /* It also points to the domain structure. */ | |
174 | struct LoadDefinitionStructure *LDEnvLink; | |
175 | void *LDAllocPagesListHead; | |
176 | struct ExternalPublicDefinitionStructure *LDTempPublicList; | |
177 | LONG LDMessageLanguage; /* for enabling */ | |
178 | BYTE **LDMessages; /* for enabling */ | |
179 | LONG LDMessageCount; /* for enabling */ | |
180 | BYTE *LDHelpFile; /* for enabling */ | |
181 | LONG LDMessageBufferSize; /* for enabling */ | |
182 | LONG LDHelpBufferSize; /* for enabling */ | |
183 | LONG LDSharedCodeOffset; /* for protection */ | |
184 | LONG LDSharedCodeLength; /* for protection */ | |
185 | LONG LDSharedDataOffset; /* for protection */ | |
186 | LONG LDSharedDataLength; /* for protection */ | |
187 | LONG (*LDSharedInitProcedure)( | |
188 | struct LoadDefinitionStructure *LoadRecord, | |
189 | struct ScreenStruct *screenID, | |
190 | BYTE *CommandLine); | |
191 | void (*LDSharedExitProcedure)(void); | |
192 | LONG LDRPCDataTable; | |
193 | LONG LDRealRPCDataTable; | |
194 | LONG LDRPCDataTableSize; | |
195 | LONG LDNumberOfReferencedPublics; | |
196 | struct ExternalPublicDefinitionStructure **LDReferencedPublics; | |
197 | LONG LDNumberOfReferencedExports; | |
198 | }; | |
199 | ||
200 | ||
201 | /* define the LDFlags. */ | |
202 | ||
203 | #define LDModuleIsReEntrantBit 0x00000001 | |
204 | #define LDModuleCanBeMultiplyLoadedBit 0x00000002 | |
205 | #define LDSynchronizeStart 0x00000004 | |
206 | #define LDPseudoPreemptionBit 0x00000008 | |
207 | #define LDLoadInOSDomain 0x00000010 | |
208 | #define LDDontUnloadBit 0x20000000 | |
209 | #define LDModuleIsBeingDebugged 0x40000000 | |
210 | #define LDMemoryOn4KBoundriesBit 0x80000000 | |
211 | ||
212 | /* LoadModule load options */ | |
213 | #define LO_NORMAL 0x0000 | |
214 | #define LO_STARTUP 0x0001 | |
215 | #define LO_PROTECT 0x0002 | |
216 | #define LO_DEBUG 0x0004 | |
217 | #define LO_AUTO_LOAD 0x0008 | |
218 | #define LO_DONT_PROMPT 0x0010 | |
219 | #define LO_LOAD_LOW 0x0020 | |
220 | #define LO_RETURN_HANDLE 0x0040 | |
221 | #define LO_LOAD_SILENT 0x0080 | |
222 | ||
223 | /* Loader returned error codes */ | |
224 | #define LOAD_COULD_NOT_FIND_FILE 1 | |
225 | #define LOAD_ERROR_READING_FILE 2 | |
226 | #define LOAD_NOT_NLM_FILE_FORMAT 3 | |
227 | #define LOAD_WRONG_NLM_FILE_VERSION 4 | |
228 | #define LOAD_REENTRANT_INITIALIZE_FAILURE 5 | |
229 | #define LOAD_CAN_NOT_LOAD_MULTIPLE_COPIES 6 | |
230 | #define LOAD_ALREADY_IN_PROGRESS 7 | |
231 | #define LOAD_NOT_ENOUGH_MEMORY 8 | |
232 | #define LOAD_INITIALIZE_FAILURE 9 | |
233 | #define LOAD_INCONSISTENT_FILE_FORMAT 10 | |
234 | #define LOAD_CAN_NOT_LOAD_AT_STARTUP 11 | |
235 | #define LOAD_AUTO_LOAD_MODULES_NOT_LOADED 12 | |
236 | #define LOAD_UNRESOLVED_EXTERNAL 13 | |
237 | #define LOAD_PUBLIC_ALREADY_DEFINED 14 | |
238 | #define LOAD_XDC_DATA_ERROR 15 | |
239 | #define LOAD_NOT_OS_DOMAIN 16 | |
240 | ||
241 | /****************************************************************************/ | |
242 | ||
243 | /* The main thread ID. */ | |
244 | static int mainthread; | |
245 | ||
72dd16ea ILT |
246 | /* The LoadDefinitionStructure of the NLM being debugged. */ |
247 | static struct LoadDefinitionStructure *handle; | |
248 | ||
249 | /* Whether we have connected to gdb. */ | |
250 | static int talking; | |
251 | ||
252 | /* The actual first instruction in the program. */ | |
253 | static unsigned char first_insn; | |
254 | ||
255 | /* An error message for the main thread to print. */ | |
256 | static char *error_message; | |
257 | ||
258 | /* The AIO port handle. */ | |
259 | static int AIOhandle; | |
260 | ||
261 | /* The console screen. */ | |
262 | static int console_screen; | |
263 | ||
264 | /* BUFMAX defines the maximum number of characters in inbound/outbound | |
265 | buffers. At least NUMREGBYTES*2 are needed for register packets */ | |
266 | #define BUFMAX 400 | |
267 | ||
268 | /* remote_debug > 0 prints ill-formed commands in valid packets and | |
269 | checksum errors. */ | |
270 | static int remote_debug = 1; | |
271 | ||
272 | static const char hexchars[] = "0123456789abcdef"; | |
273 | ||
274 | /* Number of bytes of registers. */ | |
275 | #define NUMREGBYTES 64 | |
276 | enum regnames {EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI, | |
277 | PC /* also known as eip */, | |
278 | PS /* also known as eflags */, | |
279 | CS, SS, DS, ES, FS, GS}; | |
280 | ||
281 | /* Register values. */ | |
282 | static int registers[NUMREGBYTES/4]; | |
283 | ||
284 | /* Read a character from the serial port. This must busy wait, but | |
285 | that's OK because we will be the only thread running anyhow. */ | |
286 | ||
287 | static int | |
288 | getDebugChar () | |
289 | { | |
290 | int err; | |
291 | LONG got; | |
292 | unsigned char ret; | |
293 | ||
294 | do | |
295 | { | |
296 | err = AIOReadData (AIOhandle, (char *) &ret, 1, &got); | |
297 | if (err != 0) | |
298 | { | |
299 | error_message = "AIOReadData failed"; | |
300 | ResumeThread (mainthread); | |
301 | return -1; | |
302 | } | |
303 | } | |
304 | while (got == 0); | |
305 | ||
306 | return ret; | |
307 | } | |
308 | ||
309 | /* Write a character to the serial port. Returns 0 on failure, | |
310 | non-zero on success. */ | |
311 | ||
312 | static int | |
313 | putDebugChar (c) | |
314 | unsigned char c; | |
315 | { | |
316 | int err; | |
317 | LONG put; | |
318 | ||
319 | err = AIOWriteData (AIOhandle, (char *) &c, 1, &put); | |
320 | if (err != 0 || put != 1) | |
321 | { | |
322 | error_message = "AIOWriteData failed"; | |
323 | ResumeThread (mainthread); | |
324 | return 0; | |
325 | } | |
326 | return 1; | |
327 | } | |
328 | ||
329 | /* Get the registers out of the frame information. */ | |
330 | ||
331 | static void | |
332 | frame_to_registers (frame, regs) | |
333 | T_TSS_StackFrame *frame; | |
334 | int *regs; | |
335 | { | |
336 | regs[EAX] = frame->ExceptionEAX; | |
337 | regs[ECX] = frame->ExceptionECX; | |
338 | regs[EDX] = frame->ExceptionEDX; | |
339 | regs[EBX] = frame->ExceptionEBX; | |
340 | regs[ESP] = frame->ExceptionESP; | |
341 | regs[EBP] = frame->ExceptionEBP; | |
342 | regs[ESI] = frame->ExceptionESI; | |
343 | regs[EDI] = frame->ExceptionEDI; | |
344 | regs[PC] = frame->ExceptionEIP; | |
345 | regs[PS] = frame->ExceptionSystemFlags; | |
346 | regs[CS] = frame->ExceptionCS[0]; | |
347 | regs[SS] = frame->ExceptionSS[0]; | |
348 | regs[DS] = frame->ExceptionDS[0]; | |
349 | regs[ES] = frame->ExceptionES[0]; | |
350 | regs[FS] = frame->ExceptionFS[0]; | |
351 | regs[GS] = frame->ExceptionGS[0]; | |
352 | } | |
353 | ||
354 | /* Put the registers back into the frame information. */ | |
355 | ||
356 | static void | |
357 | registers_to_frame (regs, frame) | |
358 | int *regs; | |
359 | T_TSS_StackFrame *frame; | |
360 | { | |
361 | frame->ExceptionEAX = regs[EAX]; | |
362 | frame->ExceptionECX = regs[ECX]; | |
363 | frame->ExceptionEDX = regs[EDX]; | |
364 | frame->ExceptionEBX = regs[EBX]; | |
365 | frame->ExceptionESP = regs[ESP]; | |
366 | frame->ExceptionEBP = regs[EBP]; | |
367 | frame->ExceptionESI = regs[ESI]; | |
368 | frame->ExceptionEDI = regs[EDI]; | |
369 | frame->ExceptionEIP = regs[PC]; | |
370 | frame->ExceptionSystemFlags = regs[PS]; | |
371 | frame->ExceptionCS[0] = regs[CS]; | |
372 | frame->ExceptionSS[0] = regs[SS]; | |
373 | frame->ExceptionDS[0] = regs[DS]; | |
374 | frame->ExceptionES[0] = regs[ES]; | |
375 | frame->ExceptionFS[0] = regs[FS]; | |
376 | frame->ExceptionGS[0] = regs[GS]; | |
377 | } | |
378 | ||
379 | /* Turn a hex character into a number. */ | |
380 | ||
381 | static int | |
382 | hex (ch) | |
383 | char ch; | |
384 | { | |
385 | if ((ch >= 'a') && (ch <= 'f')) | |
386 | return (ch-'a'+10); | |
387 | if ((ch >= '0') && (ch <= '9')) | |
388 | return (ch-'0'); | |
389 | if ((ch >= 'A') && (ch <= 'F')) | |
390 | return (ch-'A'+10); | |
391 | return (-1); | |
392 | } | |
393 | ||
394 | /* Scan for the sequence $<data>#<checksum>. Returns 0 on failure, | |
395 | non-zero on success. */ | |
396 | ||
397 | static int | |
398 | getpacket (buffer) | |
399 | char * buffer; | |
400 | { | |
401 | unsigned char checksum; | |
402 | unsigned char xmitcsum; | |
403 | int i; | |
404 | int count; | |
405 | int ch; | |
406 | ||
407 | do | |
408 | { | |
409 | /* wait around for the start character, ignore all other characters */ | |
410 | while ((ch = getDebugChar()) != '$') | |
411 | if (ch == -1) | |
412 | return 0; | |
413 | checksum = 0; | |
414 | xmitcsum = -1; | |
415 | ||
416 | count = 0; | |
417 | ||
418 | /* now, read until a # or end of buffer is found */ | |
419 | while (count < BUFMAX) | |
420 | { | |
421 | ch = getDebugChar(); | |
422 | if (ch == -1) | |
423 | return 0; | |
424 | if (ch == '#') | |
425 | break; | |
426 | checksum = checksum + ch; | |
427 | buffer[count] = ch; | |
428 | count = count + 1; | |
429 | } | |
430 | buffer[count] = 0; | |
431 | ||
432 | if (ch == '#') | |
433 | { | |
434 | ch = getDebugChar (); | |
435 | if (ch == -1) | |
436 | return 0; | |
437 | xmitcsum = hex(ch) << 4; | |
438 | ch = getDebugChar (); | |
439 | if (ch == -1) | |
440 | return 0; | |
441 | xmitcsum += hex(ch); | |
442 | if ((remote_debug ) && (checksum != xmitcsum)) | |
443 | { | |
444 | fprintf(stderr,"bad checksum. My count = 0x%x, sent=0x%x. buf=%s\n", | |
445 | checksum,xmitcsum,buffer); | |
446 | } | |
447 | ||
448 | if (checksum != xmitcsum) | |
449 | { | |
450 | /* failed checksum */ | |
451 | if (! putDebugChar('-')) | |
452 | return 0; | |
453 | } | |
454 | else | |
455 | { | |
456 | /* successful transfer */ | |
457 | if (! putDebugChar('+')) | |
458 | return 0; | |
459 | /* if a sequence char is present, reply the sequence ID */ | |
460 | if (buffer[2] == ':') | |
461 | { | |
462 | if (! putDebugChar (buffer[0]) | |
463 | || ! putDebugChar (buffer[1])) | |
464 | return 0; | |
465 | /* remove sequence chars from buffer */ | |
466 | count = strlen(buffer); | |
467 | for (i=3; i <= count; i++) | |
468 | buffer[i-3] = buffer[i]; | |
469 | } | |
470 | } | |
471 | } | |
472 | } | |
473 | while (checksum != xmitcsum); | |
474 | ||
475 | return 1; | |
476 | } | |
477 | ||
478 | /* Send the packet in buffer. Returns 0 on failure, non-zero on | |
479 | success. */ | |
480 | ||
481 | static int | |
482 | putpacket (buffer) | |
483 | char * buffer; | |
484 | { | |
485 | unsigned char checksum; | |
486 | int count; | |
487 | int ch; | |
488 | ||
489 | /* $<packet info>#<checksum>. */ | |
490 | do | |
491 | { | |
492 | if (! putDebugChar('$')) | |
493 | return 0; | |
494 | checksum = 0; | |
495 | count = 0; | |
496 | ||
497 | while (ch=buffer[count]) | |
498 | { | |
499 | if (! putDebugChar(ch)) | |
500 | return 0; | |
501 | checksum += ch; | |
502 | count += 1; | |
503 | } | |
504 | ||
505 | if (! putDebugChar('#') | |
506 | || ! putDebugChar(hexchars[checksum >> 4]) | |
507 | || ! putDebugChar(hexchars[checksum % 16])) | |
508 | return 0; | |
509 | ||
510 | ch = getDebugChar (); | |
511 | if (ch == -1) | |
512 | return 0; | |
513 | } | |
514 | while (ch != '+'); | |
515 | ||
516 | return 1; | |
517 | } | |
518 | ||
519 | static char remcomInBuffer[BUFMAX]; | |
520 | static char remcomOutBuffer[BUFMAX]; | |
521 | static short error; | |
522 | ||
523 | static void | |
524 | debug_error (format, parm) | |
525 | char *format; | |
526 | char *parm; | |
527 | { | |
528 | if (remote_debug) | |
529 | fprintf (stderr, format, parm); | |
530 | } | |
531 | ||
532 | /* Address of a routine to RTE to if we get a memory fault. */ | |
533 | static volatile void (*mem_fault_routine)() = NULL; | |
534 | ||
535 | /* Indicate to caller of mem2hex or hex2mem that there has been an | |
536 | error. */ | |
537 | static volatile int mem_err = 0; | |
538 | ||
539 | static void | |
540 | set_mem_err () | |
541 | { | |
542 | mem_err = 1; | |
543 | } | |
544 | ||
545 | /* These are separate functions so that they are so short and sweet | |
546 | that the compiler won't save any registers (if there is a fault | |
547 | to mem_fault, they won't get restored, so there better not be any | |
548 | saved). */ | |
549 | ||
550 | static int | |
551 | get_char (addr) | |
552 | char *addr; | |
553 | { | |
554 | return *addr; | |
555 | } | |
556 | ||
557 | static void | |
558 | set_char (addr, val) | |
559 | char *addr; | |
560 | int val; | |
561 | { | |
562 | *addr = val; | |
563 | } | |
564 | ||
565 | /* convert the memory pointed to by mem into hex, placing result in buf */ | |
566 | /* return a pointer to the last char put in buf (null) */ | |
567 | /* If MAY_FAULT is non-zero, then we should set mem_err in response to | |
568 | a fault; if zero treat a fault like any other fault in the stub. */ | |
569 | ||
570 | static char * | |
571 | mem2hex (mem, buf, count, may_fault) | |
572 | char *mem; | |
573 | char *buf; | |
574 | int count; | |
575 | int may_fault; | |
576 | { | |
577 | int i; | |
578 | unsigned char ch; | |
579 | ||
580 | if (may_fault) | |
581 | mem_fault_routine = set_mem_err; | |
582 | for (i = 0; i < count; i++) | |
583 | { | |
584 | ch = get_char (mem++); | |
585 | if (may_fault && mem_err) | |
586 | return (buf); | |
587 | *buf++ = hexchars[ch >> 4]; | |
588 | *buf++ = hexchars[ch % 16]; | |
589 | } | |
590 | *buf = 0; | |
591 | if (may_fault) | |
592 | mem_fault_routine = NULL; | |
593 | return(buf); | |
594 | } | |
595 | ||
596 | /* convert the hex array pointed to by buf into binary to be placed in mem */ | |
597 | /* return a pointer to the character AFTER the last byte written */ | |
598 | ||
599 | static char * | |
600 | hex2mem (buf, mem, count, may_fault) | |
601 | char *buf; | |
602 | char *mem; | |
603 | int count; | |
604 | int may_fault; | |
605 | { | |
606 | int i; | |
607 | unsigned char ch; | |
608 | ||
609 | if (may_fault) | |
610 | mem_fault_routine = set_mem_err; | |
611 | for (i=0;i<count;i++) | |
612 | { | |
613 | ch = hex(*buf++) << 4; | |
614 | ch = ch + hex(*buf++); | |
615 | set_char (mem++, ch); | |
616 | if (may_fault && mem_err) | |
617 | return (mem); | |
618 | } | |
619 | if (may_fault) | |
620 | mem_fault_routine = NULL; | |
621 | return(mem); | |
622 | } | |
623 | ||
624 | /* This function takes the 386 exception vector and attempts to | |
625 | translate this number into a unix compatible signal value. */ | |
626 | ||
627 | static int | |
628 | computeSignal (exceptionVector) | |
629 | int exceptionVector; | |
630 | { | |
631 | int sigval; | |
632 | switch (exceptionVector) | |
633 | { | |
634 | case 0 : sigval = 8; break; /* divide by zero */ | |
635 | case 1 : sigval = 5; break; /* debug exception */ | |
636 | case 3 : sigval = 5; break; /* breakpoint */ | |
637 | case 4 : sigval = 16; break; /* into instruction (overflow) */ | |
638 | case 5 : sigval = 16; break; /* bound instruction */ | |
639 | case 6 : sigval = 4; break; /* Invalid opcode */ | |
640 | case 7 : sigval = 8; break; /* coprocessor not available */ | |
641 | case 8 : sigval = 7; break; /* double fault */ | |
642 | case 9 : sigval = 11; break; /* coprocessor segment overrun */ | |
643 | case 10 : sigval = 11; break; /* Invalid TSS */ | |
644 | case 11 : sigval = 11; break; /* Segment not present */ | |
645 | case 12 : sigval = 11; break; /* stack exception */ | |
646 | case 13 : sigval = 11; break; /* general protection */ | |
647 | case 14 : sigval = 11; break; /* page fault */ | |
648 | case 16 : sigval = 7; break; /* coprocessor error */ | |
649 | default: | |
650 | sigval = 7; /* "software generated"*/ | |
651 | } | |
652 | return (sigval); | |
653 | } | |
654 | ||
655 | /**********************************************/ | |
656 | /* WHILE WE FIND NICE HEX CHARS, BUILD AN INT */ | |
657 | /* RETURN NUMBER OF CHARS PROCESSED */ | |
658 | /**********************************************/ | |
659 | static int | |
660 | hexToInt(ptr, intValue) | |
661 | char **ptr; | |
662 | int *intValue; | |
663 | { | |
664 | int numChars = 0; | |
665 | int hexValue; | |
666 | ||
667 | *intValue = 0; | |
668 | ||
669 | while (**ptr) | |
670 | { | |
671 | hexValue = hex(**ptr); | |
672 | if (hexValue >=0) | |
673 | { | |
674 | *intValue = (*intValue <<4) | hexValue; | |
675 | numChars ++; | |
676 | } | |
677 | else | |
678 | break; | |
679 | ||
680 | (*ptr)++; | |
681 | } | |
682 | ||
683 | return (numChars); | |
684 | } | |
685 | ||
686 | /* This function does all command processing for interfacing to gdb. | |
687 | It is called whenever an exception occurs in the module being | |
688 | debugged. */ | |
689 | ||
690 | static LONG | |
691 | handle_exception (T_StackFrame *old_frame) | |
692 | { | |
693 | T_TSS_StackFrame *frame = (T_TSS_StackFrame *) old_frame; | |
694 | int first = 0; | |
695 | int sigval; | |
696 | int addr, length; | |
697 | char * ptr; | |
698 | int newPC; | |
699 | ||
700 | /* Apparently the bell can sometimes be ringing at this point, and | |
701 | should be stopped. */ | |
702 | StopBell (); | |
703 | ||
704 | if (remote_debug) | |
705 | { | |
706 | ConsolePrintf ("vector=%d: %s, sr=0x%x, pc=0x%x, thread=%d\r\n", | |
707 | frame->ExceptionNumber, | |
708 | frame->ExceptionDescription, | |
709 | frame->ExceptionSystemFlags, | |
710 | frame->ExceptionEIP, | |
711 | GetThreadID ()); | |
712 | } | |
713 | ||
714 | /* If the NLM just started, we record the module load information | |
715 | and the thread ID, and set a breakpoint at the first instruction | |
716 | in the program. */ | |
717 | if (frame->ExceptionNumber == START_NLM_EVENT | |
718 | && handle == NULL) | |
719 | { | |
72dd16ea ILT |
720 | handle = (struct LoadDefinitionStructure *) frame->ExceptionErrorCode; |
721 | first_insn = *(char *) handle->LDInitializationProcedure; | |
722 | *(unsigned char *) handle->LDInitializationProcedure = 0xcc; | |
723 | return RETURN_TO_PROGRAM; | |
724 | } | |
725 | ||
d6a99838 ILT |
726 | /* Pass some events on to the next debugger, in case it will handle |
727 | them. */ | |
728 | if (frame->ExceptionNumber == ENTER_DEBUGGER_EVENT | |
729 | || frame->ExceptionNumber == KEYBOARD_BREAK_EVENT) | |
730 | return RETURN_TO_NEXT_DEBUGGER; | |
731 | ||
72dd16ea ILT |
732 | /* At the moment, we don't care about most of the unusual NetWare |
733 | exceptions. */ | |
734 | if (frame->ExceptionNumber != TERMINATE_NLM_EVENT | |
735 | && frame->ExceptionNumber > 31) | |
736 | return RETURN_TO_PROGRAM; | |
737 | ||
738 | /* Reset the initial breakpoint if necessary. */ | |
739 | if (frame->ExceptionNumber == 3 | |
740 | && frame->ExceptionEIP == (LONG) handle->LDInitializationProcedure + 1 | |
741 | && *(unsigned char *) handle->LDInitializationProcedure == 0xcc) | |
742 | { | |
743 | *(char *) handle->LDInitializationProcedure = first_insn; | |
744 | frame->ExceptionEIP = (LONG) handle->LDInitializationProcedure; | |
745 | first = 1; | |
746 | } | |
747 | ||
748 | /* FIXME: How do we know that this exception has anything to do with | |
749 | the program we are debugging? We can check whether the PC is in | |
750 | the range of the module we are debugging, but that doesn't help | |
751 | much since an error could occur in a library routine. */ | |
752 | ||
753 | frame_to_registers (frame, registers); | |
754 | ||
755 | /* reply to host that an exception has occurred */ | |
756 | if (frame->ExceptionNumber == TERMINATE_NLM_EVENT) | |
757 | { | |
758 | /* There is no way to get the exit status. */ | |
759 | remcomOutBuffer[0] = 'W'; | |
760 | remcomOutBuffer[1] = hexchars[0]; | |
761 | remcomOutBuffer[2] = hexchars[0]; | |
762 | remcomOutBuffer[3] = 0; | |
763 | } | |
764 | else | |
765 | { | |
766 | sigval = computeSignal (frame->ExceptionNumber); | |
767 | remcomOutBuffer[0] = 'S'; | |
768 | remcomOutBuffer[1] = hexchars[sigval >> 4]; | |
769 | remcomOutBuffer[2] = hexchars[sigval % 16]; | |
770 | remcomOutBuffer[3] = 0; | |
771 | if (first) | |
772 | { | |
773 | remcomOutBuffer[0] = 'N'; | |
774 | sprintf (remcomOutBuffer + 3, "0x%x;0x%x;0x%x", | |
775 | handle->LDCodeImageOffset, | |
776 | handle->LDDataImageOffset, | |
777 | handle->LDDataImageOffset + handle->LDDataImageLength); | |
778 | } | |
779 | } | |
780 | ||
781 | if (! putpacket(remcomOutBuffer)) | |
782 | return RETURN_TO_NEXT_DEBUGGER; | |
783 | ||
784 | if (frame->ExceptionNumber == TERMINATE_NLM_EVENT) | |
785 | { | |
786 | ResumeThread (mainthread); | |
787 | return RETURN_TO_PROGRAM; | |
788 | } | |
789 | ||
790 | while (1) | |
791 | { | |
792 | error = 0; | |
793 | remcomOutBuffer[0] = 0; | |
794 | if (! getpacket (remcomInBuffer)) | |
795 | return RETURN_TO_NEXT_DEBUGGER; | |
796 | talking = 1; | |
797 | switch (remcomInBuffer[0]) | |
798 | { | |
799 | case '?': | |
800 | sigval = computeSignal (frame->ExceptionNumber); | |
801 | remcomOutBuffer[0] = 'S'; | |
802 | remcomOutBuffer[1] = hexchars[sigval >> 4]; | |
803 | remcomOutBuffer[2] = hexchars[sigval % 16]; | |
804 | remcomOutBuffer[3] = 0; | |
805 | if (first) | |
806 | { | |
807 | remcomOutBuffer[0] = 'N'; | |
808 | sprintf (remcomOutBuffer + 3, "0x%x;0x%x;0x%x", | |
809 | handle->LDCodeImageOffset, | |
810 | handle->LDDataImageOffset, | |
811 | (handle->LDDataImageOffset | |
812 | + handle->LDDataImageLength)); | |
813 | } | |
814 | break; | |
815 | case 'd': | |
816 | remote_debug = !(remote_debug); /* toggle debug flag */ | |
817 | break; | |
818 | case 'g': | |
819 | /* return the value of the CPU registers */ | |
820 | mem2hex((char*) registers, remcomOutBuffer, NUMREGBYTES, 0); | |
821 | break; | |
822 | case 'G': | |
823 | /* set the value of the CPU registers - return OK */ | |
824 | hex2mem(&remcomInBuffer[1], (char*) registers, NUMREGBYTES, 0); | |
825 | strcpy(remcomOutBuffer,"OK"); | |
826 | break; | |
827 | ||
828 | case 'm': | |
829 | /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */ | |
830 | /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */ | |
831 | ptr = &remcomInBuffer[1]; | |
832 | if (hexToInt(&ptr,&addr)) | |
833 | if (*(ptr++) == ',') | |
834 | if (hexToInt(&ptr,&length)) | |
835 | { | |
836 | ptr = 0; | |
837 | mem_err = 0; | |
838 | mem2hex((char*) addr, remcomOutBuffer, length, 1); | |
839 | if (mem_err) | |
840 | { | |
841 | strcpy (remcomOutBuffer, "E03"); | |
842 | debug_error ("memory fault"); | |
843 | } | |
844 | } | |
845 | ||
846 | if (ptr) | |
847 | { | |
848 | strcpy(remcomOutBuffer,"E01"); | |
849 | debug_error("malformed read memory command: %s",remcomInBuffer); | |
850 | } | |
851 | break; | |
852 | ||
853 | case 'M': | |
854 | /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */ | |
855 | /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */ | |
856 | ptr = &remcomInBuffer[1]; | |
857 | if (hexToInt(&ptr,&addr)) | |
858 | if (*(ptr++) == ',') | |
859 | if (hexToInt(&ptr,&length)) | |
860 | if (*(ptr++) == ':') | |
861 | { | |
862 | mem_err = 0; | |
863 | hex2mem(ptr, (char*) addr, length, 1); | |
864 | ||
865 | if (mem_err) | |
866 | { | |
867 | strcpy (remcomOutBuffer, "E03"); | |
868 | debug_error ("memory fault"); | |
869 | } | |
870 | else | |
871 | { | |
872 | strcpy(remcomOutBuffer,"OK"); | |
873 | } | |
874 | ||
875 | ptr = 0; | |
876 | } | |
877 | if (ptr) | |
878 | { | |
879 | strcpy(remcomOutBuffer,"E02"); | |
880 | debug_error("malformed write memory command: %s",remcomInBuffer); | |
881 | } | |
882 | break; | |
883 | ||
884 | case 'c': | |
885 | case 's': | |
886 | /* cAA..AA Continue at address AA..AA(optional) */ | |
887 | /* sAA..AA Step one instruction from AA..AA(optional) */ | |
888 | /* try to read optional parameter, pc unchanged if no parm */ | |
889 | ptr = &remcomInBuffer[1]; | |
890 | if (hexToInt(&ptr,&addr)) | |
891 | registers[ PC ] = addr; | |
892 | ||
893 | newPC = registers[ PC]; | |
894 | ||
895 | /* clear the trace bit */ | |
896 | registers[ PS ] &= 0xfffffeff; | |
897 | ||
898 | /* set the trace bit if we're stepping */ | |
899 | if (remcomInBuffer[0] == 's') registers[ PS ] |= 0x100; | |
900 | ||
901 | registers_to_frame (registers, frame); | |
902 | return RETURN_TO_PROGRAM; | |
903 | ||
904 | case 'k': | |
905 | /* kill the program */ | |
906 | KillMe (handle); | |
907 | ResumeThread (mainthread); | |
908 | return RETURN_TO_PROGRAM; | |
909 | } | |
910 | ||
911 | /* reply to the request */ | |
912 | if (! putpacket(remcomOutBuffer)) | |
913 | return RETURN_TO_NEXT_DEBUGGER; | |
914 | } | |
915 | } | |
916 | ||
917 | /* Start up. The main thread opens the named serial I/O port, loads | |
918 | the named NLM module and then goes to sleep. The serial I/O port | |
919 | is named as a board number and a port number. It would be more DOS | |
920 | like to provide a menu of available serial ports, but I don't want | |
921 | to have to figure out how to do that. */ | |
922 | ||
923 | int | |
924 | main (argc, argv) | |
925 | int argc; | |
926 | char **argv; | |
927 | { | |
928 | int hardware, board, port; | |
929 | LONG err; | |
930 | struct debuggerStructure s; | |
931 | char *cmdlin; | |
932 | int i; | |
933 | ||
934 | /* Create a screen for the debugger. */ | |
935 | console_screen = CreateScreen ("System Console", 0); | |
936 | if (DisplayScreen (console_screen) != ESUCCESS) | |
937 | fprintf (stderr, "DisplayScreen failed\n"); | |
938 | ||
939 | if (argc < 4) | |
940 | { | |
941 | fprintf (stderr, | |
942 | "Usage: load gdbserver board port program [arguments]\n"); | |
943 | exit (1); | |
944 | } | |
945 | ||
946 | hardware = -1; | |
947 | board = strtol (argv[1], (char **) NULL, 0); | |
948 | port = strtol (argv[2], (char **) NULL, 0); | |
949 | ||
950 | err = AIOAcquirePort (&hardware, &board, &port, &AIOhandle); | |
951 | if (err != AIO_SUCCESS) | |
952 | { | |
953 | switch (err) | |
954 | { | |
955 | case AIO_PORT_NOT_AVAILABLE: | |
956 | fprintf (stderr, "Port not available\n"); | |
957 | break; | |
958 | ||
959 | case AIO_BOARD_NUMBER_INVALID: | |
960 | case AIO_PORT_NUMBER_INVALID: | |
961 | fprintf (stderr, "No such port\n"); | |
962 | break; | |
963 | ||
964 | default: | |
965 | fprintf (stderr, "Could not open port: %d\n", err); | |
966 | break; | |
967 | } | |
968 | ||
969 | exit (1); | |
970 | } | |
971 | ||
972 | err = AIOConfigurePort (AIOhandle, AIO_BAUD_9600, AIO_DATA_BITS_8, | |
973 | AIO_STOP_BITS_1, AIO_PARITY_NONE, | |
974 | AIO_HARDWARE_FLOW_CONTROL_OFF); | |
975 | if (err != AIO_SUCCESS) | |
976 | { | |
977 | fprintf (stderr, "Could not configure port: %d\n", err); | |
978 | AIOReleasePort (AIOhandle); | |
979 | exit (1); | |
980 | } | |
981 | ||
982 | /* Register ourselves as an alternate debugger. */ | |
983 | memset (&s, 0, sizeof s); | |
984 | s.DDSResourceTag = ((struct ResourceTagStructure *) | |
985 | AllocateResourceTag (GetNLMHandle (), | |
986 | "gdbserver", | |
987 | DebuggerSignature)); | |
988 | if (s.DDSResourceTag == 0) | |
989 | { | |
990 | fprintf (stderr, "AllocateResourceTag failed\n"); | |
991 | AIOReleasePort (AIOhandle); | |
992 | exit (1); | |
993 | } | |
994 | s.DDSdebuggerEntry = handle_exception; | |
995 | s.DDSFlags = TSS_FRAME_BIT; | |
996 | ||
997 | err = RegisterDebuggerRTag (&s, AT_FIRST); | |
998 | if (err != 0) | |
999 | { | |
1000 | fprintf (stderr, "RegisterDebuggerRTag failed\n"); | |
1001 | AIOReleasePort (AIOhandle); | |
1002 | exit (1); | |
1003 | } | |
1004 | ||
1005 | /* Get the command line we were invoked with, and advance it past | |
1006 | our name and the board and port arguments. */ | |
1007 | cmdlin = getcmd ((char *) NULL); | |
1008 | for (i = 0; i < 2; i++) | |
1009 | { | |
1010 | while (! isspace (*cmdlin)) | |
1011 | ++cmdlin; | |
1012 | while (isspace (*cmdlin)) | |
1013 | ++cmdlin; | |
1014 | } | |
1015 | ||
1016 | /* In case GDB is started before us, ack any packets (presumably | |
1017 | "$?#xx") sitting there. */ | |
1018 | if (! putDebugChar ('+')) | |
1019 | { | |
1020 | fprintf (stderr, "putDebugChar failed\n"); | |
1021 | UnRegisterDebugger (&s); | |
1022 | AIOReleasePort (AIOhandle); | |
1023 | exit (1); | |
1024 | } | |
1025 | ||
1026 | mainthread = GetThreadID (); | |
1027 | handle = NULL; | |
1028 | talking = 0; | |
1029 | ||
1030 | if (remote_debug > 0) | |
1031 | ConsolePrintf ("About to call LoadModule with \"%s\" %d %d\r\n", | |
1032 | cmdlin, console_screen, __GetScreenID (console_screen)); | |
1033 | ||
1034 | /* Start up the module to be debugged. */ | |
1035 | err = LoadModule ((struct ScreenStruct *) __GetScreenID (console_screen), | |
1036 | cmdlin, LO_DEBUG); | |
1037 | if (err != 0) | |
1038 | { | |
1039 | fprintf (stderr, "LoadModule failed: %d\n", err); | |
1040 | UnRegisterDebugger (&s); | |
1041 | AIOReleasePort (AIOhandle); | |
1042 | exit (1); | |
1043 | } | |
1044 | ||
d6a99838 | 1045 | /* Wait for the debugger to wake us up. */ |
72dd16ea | 1046 | if (remote_debug > 0) |
d6a99838 | 1047 | ConsolePrintf ("Suspending main thread (%d)\r\n", mainthread); |
72dd16ea | 1048 | SuspendThread (mainthread); |
d6a99838 ILT |
1049 | if (remote_debug > 0) |
1050 | ConsolePrintf ("Resuming main thread (%d)\r\n", mainthread); | |
72dd16ea ILT |
1051 | |
1052 | /* If we are woken up, print an optional error message, deregister | |
1053 | ourselves and exit. */ | |
1054 | if (error_message != NULL) | |
1055 | fprintf (stderr, "%s\n", error_message); | |
1056 | UnRegisterDebugger (&s); | |
1057 | AIOReleasePort (AIOhandle); | |
1058 | exit (0); | |
1059 | } |