1 /* i386-nlmstub.c -- NLM debugging stub for the i386.
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.
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. */
14 /****************************************************************************
16 THIS SOFTWARE IS NOT COPYRIGHTED
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.
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.
26 ****************************************************************************/
28 /****************************************************************************
30 * The following gdb commands are supported:
32 * command function Return value
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
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
40 * c Resume at current address SNN ( signal NN)
41 * cAA..AA Continue at address AA..AA SNN
43 * s Step one instruction SNN
44 * sAA..AA Step one instruction from AA..AA SNN
48 * ? What was the last sigval ? SNN (signal NN)
50 * All commands and responses are sent with a packet which includes a
51 * checksum. A packet consists of
53 * $<packet info>#<checksum>.
56 * <packet info> :: <characters representing the command or response>
57 * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
59 * When a packet is received, it is first acknowledged with either '+' or '-'.
60 * '+' indicates a successful transfer. '-' indicates a failed transfer.
65 * $m0,10#2a +$00010203040506070809101112131415#42
67 ****************************************************************************/
81 /************************************************************************/
82 /*****************************************************************************
84 * (C) Copyright 1988-1993 Novell, Inc.
85 * All Rights Reserved.
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.
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.
99 * $modname: loadstuff.h$
101 * $date: Fri, Jan 15, 1993$
103 ****************************************************************************/
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.
114 ************************************************************************/
116 struct LoadDefinitionStructure
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
;
132 LONG (*LDInitializationProcedure
)(
133 struct LoadDefinitionStructure
*LoadRecord
,
134 struct ScreenStruct
*screenID
,
136 BYTE
*loadDirectoryPath
,
137 LONG uninitializedDataLength
,
144 LONG customDataOffset
,
145 LONG customDataSize
);
146 void (*LDExitProcedure
)(void);
147 LONG (*LDCheckUnloadProcedure
)(
148 struct ScreenStruct
*screenID
);
149 struct ExternalPublicDefinitionStructure
*LDPublics
;
152 LONG
*LDCLIBLoadStructure
;
155 LONG LDReservedForCLIB
;
158 void *LDModuleObjectHandle
; /* If Instrumented BEW 10/16/90 */
166 LONG LDAllocAvailBytes
;
167 LONG LDAllocFreeCount
;
168 LONG LDLastGarbCollect
;
169 LONG LDAlloc16Lists
[64];
170 LONG LDAlloc256Lists
[12];
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
,
191 void (*LDSharedExitProcedure
)(void);
193 LONG LDRealRPCDataTable
;
194 LONG LDRPCDataTableSize
;
195 LONG LDNumberOfReferencedPublics
;
196 struct ExternalPublicDefinitionStructure
**LDReferencedPublics
;
197 LONG LDNumberOfReferencedExports
;
201 /* define the LDFlags. */
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
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
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
241 /****************************************************************************/
243 /* The main thread ID. */
244 static int mainthread
;
246 /* The LoadDefinitionStructure of the NLM being debugged. */
247 static struct LoadDefinitionStructure
*handle
;
249 /* Whether we have connected to gdb. */
252 /* The actual first instruction in the program. */
253 static unsigned char first_insn
;
255 /* An error message for the main thread to print. */
256 static char *error_message
;
258 /* The AIO port handle. */
259 static int AIOhandle
;
261 /* The console screen. */
262 static int console_screen
;
264 /* BUFMAX defines the maximum number of characters in inbound/outbound
265 buffers. At least NUMREGBYTES*2 are needed for register packets */
268 /* remote_debug > 0 prints ill-formed commands in valid packets and
270 static int remote_debug
= 1;
272 static const char hexchars
[] = "0123456789abcdef";
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
};
281 /* Register values. */
282 static int registers
[NUMREGBYTES
/4];
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. */
296 err
= AIOReadData (AIOhandle
, (char *) &ret
, 1, &got
);
299 error_message
= "AIOReadData failed";
300 ResumeThread (mainthread
);
309 /* Write a character to the serial port. Returns 0 on failure,
310 non-zero on success. */
319 err
= AIOWriteData (AIOhandle
, (char *) &c
, 1, &put
);
320 if (err
!= 0 || put
!= 1)
322 error_message
= "AIOWriteData failed";
323 ResumeThread (mainthread
);
329 /* Get the registers out of the frame information. */
332 frame_to_registers (frame
, regs
)
333 T_TSS_StackFrame
*frame
;
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];
354 /* Put the registers back into the frame information. */
357 registers_to_frame (regs
, frame
)
359 T_TSS_StackFrame
*frame
;
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
];
379 /* Turn a hex character into a number. */
385 if ((ch
>= 'a') && (ch
<= 'f'))
387 if ((ch
>= '0') && (ch
<= '9'))
389 if ((ch
>= 'A') && (ch
<= 'F'))
394 /* Scan for the sequence $<data>#<checksum>. Returns 0 on failure,
395 non-zero on success. */
401 unsigned char checksum
;
402 unsigned char xmitcsum
;
409 /* wait around for the start character, ignore all other characters */
410 while ((ch
= getDebugChar()) != '$')
418 /* now, read until a # or end of buffer is found */
419 while (count
< BUFMAX
)
426 checksum
= checksum
+ ch
;
434 ch
= getDebugChar ();
437 xmitcsum
= hex(ch
) << 4;
438 ch
= getDebugChar ();
442 if ((remote_debug
) && (checksum
!= xmitcsum
))
444 fprintf(stderr
,"bad checksum. My count = 0x%x, sent=0x%x. buf=%s\n",
445 checksum
,xmitcsum
,buffer
);
448 if (checksum
!= xmitcsum
)
450 /* failed checksum */
451 if (! putDebugChar('-'))
456 /* successful transfer */
457 if (! putDebugChar('+'))
459 /* if a sequence char is present, reply the sequence ID */
460 if (buffer
[2] == ':')
462 if (! putDebugChar (buffer
[0])
463 || ! putDebugChar (buffer
[1]))
465 /* remove sequence chars from buffer */
466 count
= strlen(buffer
);
467 for (i
=3; i
<= count
; i
++)
468 buffer
[i
-3] = buffer
[i
];
473 while (checksum
!= xmitcsum
);
476 ConsolePrintf ("Received packet \"%s\"\r\n", buffer
);
481 /* Send the packet in buffer. Returns 0 on failure, non-zero on
488 unsigned char checksum
;
493 ConsolePrintf ("Sending packet \"%s\"\r\n", buffer
);
495 /* $<packet info>#<checksum>. */
498 if (! putDebugChar('$'))
503 while (ch
=buffer
[count
])
505 if (! putDebugChar(ch
))
511 if (! putDebugChar('#')
512 || ! putDebugChar(hexchars
[checksum
>> 4])
513 || ! putDebugChar(hexchars
[checksum
% 16]))
516 ch
= getDebugChar ();
525 static char remcomInBuffer
[BUFMAX
];
526 static char remcomOutBuffer
[BUFMAX
];
530 debug_error (format
, parm
)
536 fprintf (stderr
, format
, parm
);
537 fprintf (stderr
, "\n");
541 /* This is set if we could get a memory access fault. */
542 static int mem_may_fault
;
544 /* Indicate to caller of mem2hex or hex2mem that there has been an
546 static volatile int mem_err
= 0;
548 /* These are separate functions so that they are so short and sweet
549 that the compiler won't save any registers (if there is a fault
550 to mem_fault, they won't get restored, so there better not be any
568 /* This bit of assembly language just returns from a function. If a
569 memory error occurs within get_char or set_char, the debugger
570 handler points EIP at these instructions to get out. */
572 extern void just_return ();
573 asm (".globl just_return");
574 asm (".globl _just_return");
575 asm ("just_return:");
576 asm ("_just_return:");
580 /* convert the memory pointed to by mem into hex, placing result in buf */
581 /* return a pointer to the last char put in buf (null) */
582 /* If MAY_FAULT is non-zero, then we should set mem_err in response to
583 a fault; if zero treat a fault like any other fault in the stub. */
586 mem2hex (mem
, buf
, count
, may_fault
)
595 mem_may_fault
= may_fault
;
596 for (i
= 0; i
< count
; i
++)
598 ch
= get_char (mem
++);
599 if (may_fault
&& mem_err
)
601 *buf
++ = hexchars
[ch
>> 4];
602 *buf
++ = hexchars
[ch
% 16];
609 /* convert the hex array pointed to by buf into binary to be placed in mem */
610 /* return a pointer to the character AFTER the last byte written */
613 hex2mem (buf
, mem
, count
, may_fault
)
622 mem_may_fault
= may_fault
;
623 for (i
=0;i
<count
;i
++)
625 ch
= hex(*buf
++) << 4;
626 ch
= ch
+ hex(*buf
++);
627 set_char (mem
++, ch
);
628 if (may_fault
&& mem_err
)
635 /* This function takes the 386 exception vector and attempts to
636 translate this number into a unix compatible signal value. */
639 computeSignal (exceptionVector
)
643 switch (exceptionVector
)
645 case 0 : sigval
= 8; break; /* divide by zero */
646 case 1 : sigval
= 5; break; /* debug exception */
647 case 3 : sigval
= 5; break; /* breakpoint */
648 case 4 : sigval
= 16; break; /* into instruction (overflow) */
649 case 5 : sigval
= 16; break; /* bound instruction */
650 case 6 : sigval
= 4; break; /* Invalid opcode */
651 case 7 : sigval
= 8; break; /* coprocessor not available */
652 case 8 : sigval
= 7; break; /* double fault */
653 case 9 : sigval
= 11; break; /* coprocessor segment overrun */
654 case 10 : sigval
= 11; break; /* Invalid TSS */
655 case 11 : sigval
= 11; break; /* Segment not present */
656 case 12 : sigval
= 11; break; /* stack exception */
657 case 13 : sigval
= 11; break; /* general protection */
658 case 14 : sigval
= 11; break; /* page fault */
659 case 16 : sigval
= 7; break; /* coprocessor error */
661 sigval
= 7; /* "software generated"*/
666 /**********************************************/
667 /* WHILE WE FIND NICE HEX CHARS, BUILD AN INT */
668 /* RETURN NUMBER OF CHARS PROCESSED */
669 /**********************************************/
671 hexToInt(ptr
, intValue
)
682 hexValue
= hex(**ptr
);
685 *intValue
= (*intValue
<<4) | hexValue
;
697 /* This function does all command processing for interfacing to gdb.
698 It is called whenever an exception occurs in the module being
702 handle_exception (T_StackFrame
*old_frame
)
704 T_TSS_StackFrame
*frame
= (T_TSS_StackFrame
*) old_frame
;
710 /* Apparently the bell can sometimes be ringing at this point, and
711 should be stopped. */
716 ConsolePrintf ("vector=%d: %s, sr=0x%x, pc=0x%x, thread=%d\r\n",
717 frame
->ExceptionNumber
,
718 frame
->ExceptionDescription
,
719 frame
->ExceptionSystemFlags
,
724 /* If the NLM just started, we record the module load information
725 and the thread ID, and set a breakpoint at the first instruction
727 if (frame
->ExceptionNumber
== START_NLM_EVENT
730 handle
= (struct LoadDefinitionStructure
*) frame
->ExceptionErrorCode
;
731 first_insn
= *(char *) handle
->LDInitializationProcedure
;
732 *(unsigned char *) handle
->LDInitializationProcedure
= 0xcc;
733 return RETURN_TO_PROGRAM
;
736 /* After we've reached the initial breakpoint, reset it. */
737 if (frame
->ExceptionEIP
== (LONG
) handle
->LDInitializationProcedure
+ 1
738 && *(unsigned char *) handle
->LDInitializationProcedure
== 0xcc)
740 *(char *) handle
->LDInitializationProcedure
= first_insn
;
741 frame
->ExceptionEIP
= (LONG
) handle
->LDInitializationProcedure
;
744 /* Pass some events on to the next debugger, in case it will handle
746 if (frame
->ExceptionNumber
== ENTER_DEBUGGER_EVENT
747 || frame
->ExceptionNumber
== KEYBOARD_BREAK_EVENT
)
748 return RETURN_TO_NEXT_DEBUGGER
;
750 /* At the moment, we don't care about most of the unusual NetWare
752 if (frame
->ExceptionNumber
!= TERMINATE_NLM_EVENT
753 && frame
->ExceptionNumber
> 31)
754 return RETURN_TO_PROGRAM
;
756 /* If we get a GP fault, and mem_may_fault is set, and the
757 instruction pointer is near set_char or get_char, then we caused
758 the fault ourselves accessing an illegal memory location. */
760 && (frame
->ExceptionNumber
== 11
761 || frame
->ExceptionNumber
== 13
762 || frame
->ExceptionNumber
== 14)
763 && ((frame
->ExceptionEIP
>= (long) &set_char
764 && frame
->ExceptionEIP
< (long) &set_char
+ 50)
765 || (frame
->ExceptionEIP
>= (long) &get_char
766 && frame
->ExceptionEIP
< (long) &get_char
+ 50)))
769 /* Point the instruction pointer at an assembly language stub
770 which just returns from the function. */
771 frame
->ExceptionEIP
= (long) &just_return
;
772 /* Keep going. This will act as though it returned from
773 set_char or get_char. The calling routine will check
774 mem_err, and do the right thing. */
775 return RETURN_TO_PROGRAM
;
778 /* FIXME: How do we know that this exception has anything to do with
779 the program we are debugging? We can check whether the PC is in
780 the range of the module we are debugging, but that doesn't help
781 much since an error could occur in a library routine. */
783 frame_to_registers (frame
, registers
);
785 /* reply to host that an exception has occurred */
786 if (frame
->ExceptionNumber
== TERMINATE_NLM_EVENT
)
788 /* There is no way to get the exit status. */
789 remcomOutBuffer
[0] = 'W';
790 remcomOutBuffer
[1] = hexchars
[0];
791 remcomOutBuffer
[2] = hexchars
[0];
792 remcomOutBuffer
[3] = 0;
796 sigval
= computeSignal (frame
->ExceptionNumber
);
797 remcomOutBuffer
[0] = 'N';
798 remcomOutBuffer
[1] = hexchars
[sigval
>> 4];
799 remcomOutBuffer
[2] = hexchars
[sigval
% 16];
800 sprintf (remcomOutBuffer
+ 3, "0x%x;0x%x;0x%x",
801 handle
->LDCodeImageOffset
,
802 handle
->LDDataImageOffset
,
803 handle
->LDDataImageOffset
+ handle
->LDDataImageLength
);
806 if (! putpacket(remcomOutBuffer
))
807 return RETURN_TO_NEXT_DEBUGGER
;
809 if (frame
->ExceptionNumber
== TERMINATE_NLM_EVENT
)
811 ResumeThread (mainthread
);
812 return RETURN_TO_PROGRAM
;
818 remcomOutBuffer
[0] = 0;
819 if (! getpacket (remcomInBuffer
))
820 return RETURN_TO_NEXT_DEBUGGER
;
822 switch (remcomInBuffer
[0])
825 sigval
= computeSignal (frame
->ExceptionNumber
);
826 remcomOutBuffer
[0] = 'N';
827 remcomOutBuffer
[1] = hexchars
[sigval
>> 4];
828 remcomOutBuffer
[2] = hexchars
[sigval
% 16];
829 sprintf (remcomOutBuffer
+ 3, "0x%x;0x%x;0x%x",
830 handle
->LDCodeImageOffset
,
831 handle
->LDDataImageOffset
,
832 handle
->LDDataImageOffset
+ handle
->LDDataImageLength
);
835 remote_debug
= !(remote_debug
); /* toggle debug flag */
838 /* return the value of the CPU registers */
839 mem2hex((char*) registers
, remcomOutBuffer
, NUMREGBYTES
, 0);
842 /* set the value of the CPU registers - return OK */
843 hex2mem(&remcomInBuffer
[1], (char*) registers
, NUMREGBYTES
, 0);
844 strcpy(remcomOutBuffer
,"OK");
848 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
849 /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */
850 ptr
= &remcomInBuffer
[1];
851 if (hexToInt(&ptr
,&addr
))
853 if (hexToInt(&ptr
,&length
))
857 mem2hex((char*) addr
, remcomOutBuffer
, length
, 1);
860 strcpy (remcomOutBuffer
, "E03");
861 debug_error ("memory fault");
867 strcpy(remcomOutBuffer
,"E01");
868 debug_error("malformed read memory command: %s",remcomInBuffer
);
873 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
874 /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */
875 ptr
= &remcomInBuffer
[1];
876 if (hexToInt(&ptr
,&addr
))
878 if (hexToInt(&ptr
,&length
))
882 hex2mem(ptr
, (char*) addr
, length
, 1);
886 strcpy (remcomOutBuffer
, "E03");
887 debug_error ("memory fault");
891 strcpy(remcomOutBuffer
,"OK");
898 strcpy(remcomOutBuffer
,"E02");
899 debug_error("malformed write memory command: %s",remcomInBuffer
);
905 /* cAA..AA Continue at address AA..AA(optional) */
906 /* sAA..AA Step one instruction from AA..AA(optional) */
907 /* try to read optional parameter, pc unchanged if no parm */
908 ptr
= &remcomInBuffer
[1];
909 if (hexToInt(&ptr
,&addr
))
910 registers
[ PC
] = addr
;
912 newPC
= registers
[ PC
];
914 /* clear the trace bit */
915 registers
[ PS
] &= 0xfffffeff;
917 /* set the trace bit if we're stepping */
918 if (remcomInBuffer
[0] == 's') registers
[ PS
] |= 0x100;
920 registers_to_frame (registers
, frame
);
921 return RETURN_TO_PROGRAM
;
924 /* kill the program */
926 ResumeThread (mainthread
);
927 return RETURN_TO_PROGRAM
;
930 /* reply to the request */
931 if (! putpacket(remcomOutBuffer
))
932 return RETURN_TO_NEXT_DEBUGGER
;
936 /* Start up. The main thread opens the named serial I/O port, loads
937 the named NLM module and then goes to sleep. The serial I/O port
938 is named as a board number and a port number. It would be more DOS
939 like to provide a menu of available serial ports, but I don't want
940 to have to figure out how to do that. */
947 int hardware
, board
, port
;
949 struct debuggerStructure s
;
953 /* Create a screen for the debugger. */
954 console_screen
= CreateScreen ("System Console", 0);
955 if (DisplayScreen (console_screen
) != ESUCCESS
)
956 fprintf (stderr
, "DisplayScreen failed\n");
961 "Usage: load gdbserver board port program [arguments]\n");
966 board
= strtol (argv
[1], (char **) NULL
, 0);
967 port
= strtol (argv
[2], (char **) NULL
, 0);
969 err
= AIOAcquirePort (&hardware
, &board
, &port
, &AIOhandle
);
970 if (err
!= AIO_SUCCESS
)
974 case AIO_PORT_NOT_AVAILABLE
:
975 fprintf (stderr
, "Port not available\n");
978 case AIO_BOARD_NUMBER_INVALID
:
979 case AIO_PORT_NUMBER_INVALID
:
980 fprintf (stderr
, "No such port\n");
984 fprintf (stderr
, "Could not open port: %d\n", err
);
991 err
= AIOConfigurePort (AIOhandle
, AIO_BAUD_9600
, AIO_DATA_BITS_8
,
992 AIO_STOP_BITS_1
, AIO_PARITY_NONE
,
993 AIO_HARDWARE_FLOW_CONTROL_OFF
);
994 if (err
!= AIO_SUCCESS
)
996 fprintf (stderr
, "Could not configure port: %d\n", err
);
997 AIOReleasePort (AIOhandle
);
1001 /* Register ourselves as an alternate debugger. */
1002 memset (&s
, 0, sizeof s
);
1003 s
.DDSResourceTag
= ((struct ResourceTagStructure
*)
1004 AllocateResourceTag (GetNLMHandle (),
1006 DebuggerSignature
));
1007 if (s
.DDSResourceTag
== 0)
1009 fprintf (stderr
, "AllocateResourceTag failed\n");
1010 AIOReleasePort (AIOhandle
);
1013 s
.DDSdebuggerEntry
= handle_exception
;
1014 s
.DDSFlags
= TSS_FRAME_BIT
;
1016 err
= RegisterDebuggerRTag (&s
, AT_FIRST
);
1019 fprintf (stderr
, "RegisterDebuggerRTag failed\n");
1020 AIOReleasePort (AIOhandle
);
1024 /* Get the command line we were invoked with, and advance it past
1025 our name and the board and port arguments. */
1026 cmdlin
= getcmd ((char *) NULL
);
1027 for (i
= 0; i
< 2; i
++)
1029 while (! isspace (*cmdlin
))
1031 while (isspace (*cmdlin
))
1035 /* In case GDB is started before us, ack any packets (presumably
1036 "$?#xx") sitting there. */
1037 if (! putDebugChar ('+'))
1039 fprintf (stderr
, "putDebugChar failed\n");
1040 UnRegisterDebugger (&s
);
1041 AIOReleasePort (AIOhandle
);
1045 mainthread
= GetThreadID ();
1049 if (remote_debug
> 0)
1050 ConsolePrintf ("About to call LoadModule with \"%s\" %d %d\r\n",
1051 cmdlin
, console_screen
, __GetScreenID (console_screen
));
1053 /* Start up the module to be debugged. */
1054 err
= LoadModule ((struct ScreenStruct
*) __GetScreenID (console_screen
),
1058 fprintf (stderr
, "LoadModule failed: %d\n", err
);
1059 UnRegisterDebugger (&s
);
1060 AIOReleasePort (AIOhandle
);
1064 /* Wait for the debugger to wake us up. */
1065 if (remote_debug
> 0)
1066 ConsolePrintf ("Suspending main thread (%d)\r\n", mainthread
);
1067 SuspendThread (mainthread
);
1068 if (remote_debug
> 0)
1069 ConsolePrintf ("Resuming main thread (%d)\r\n", mainthread
);
1071 /* If we are woken up, print an optional error message, deregister
1072 ourselves and exit. */
1073 if (error_message
!= NULL
)
1074 fprintf (stderr
, "%s\n", error_message
);
1075 UnRegisterDebugger (&s
);
1076 AIOReleasePort (AIOhandle
);