2 Private include file for GDB stub
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #ifndef __GDB_STUB_INTERNAL__
17 #define __GDB_STUB_INTERNAL__
20 #include <Library/BaseLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/UefiLib.h>
25 #include <Library/UefiBootServicesTableLib.h>
26 #include <Library/PcdLib.h>
27 #include <Library/GdbSerialLib.h>
28 #include <Library/PrintLib.h>
30 #include <Protocol/DebugSupport.h>
31 #include <Protocol/SerialIo.h>
32 #include <Protocol/LoadedImage.h>
33 #include <Protocol/LoadedImage.h>
34 #include <Guid/DebugImageInfoTable.h>
35 #include <IndustryStandard/PeImage.h>
37 extern CONST CHAR8 mHexToStr
[];
39 // maximum size of input and output buffers
40 // This value came from the show remote command of the gdb we tested against
41 #define MAX_BUF_SIZE 2000
43 // maximum size of address buffer
44 #define MAX_ADDR_SIZE 32
46 // maximum size of register number buffer
47 #define MAX_REG_NUM_BUF_SIZE 32
49 // maximum size of length buffer
50 #define MAX_LENGTH_SIZE 32
52 // maximum size of T signal members
53 #define MAX_T_SIGNAL_SIZE 64
55 // the mask used to clear all the cache
56 #define TF_BIT 0x00000100
60 // GDB Signal definitions - generic names for interrupts
62 #define GDB_SIGILL 4 // Illegal instruction
63 #define GDB_SIGTRAP 5 // Trace Trap (Breakpoint and SingleStep)
64 #define GDB_SIGEMT 7 // Emulator Trap
65 #define GDB_SIGFPE 8 // Floating point exception
66 #define GDB_SIGSEGV 11 // Segment violation, page fault
70 // GDB File I/O Error values, zero means no error
71 // Includes all general GDB Unix like error values
73 #define GDB_EBADMEMADDRBUFSIZE 11 // the buffer that stores memory Address to be read from/written to is not the right size
74 #define GDB_EBADMEMLENGBUFSIZE 12 // the buffer that stores Length is not the right size
75 #define GDB_EBADMEMLENGTH 13 // Length, the given number of bytes to read or write, is not the right size
76 #define GDB_EBADMEMDATA 14 // one of the bytes or nibbles of the memory is leess than 0
77 #define GDB_EBADMEMDATASIZE 15 // the memory data, 'XX..', is too short or too long
78 #define GDB_EBADBUFSIZE 21 // the buffer created is not the correct size
79 #define GDB_EINVALIDARG 31 // argument is invalid
80 #define GDB_ENOSPACE 41 //
81 #define GDB_EINVALIDBRKPOINTTYPE 51 // the breakpoint type is not recognized
82 #define GDB_EINVALIDREGNUM 61 // given register number is not valid: either <0 or >=Number of Registers
83 #define GDB_EUNKNOWN 255 // unknown
87 // These devices are open by GDB so we can just read and write to them
89 #define GDB_STDIN 0x00
90 #define GDB_STDOUT 0x01
91 #define GDB_STDERR 0x02
94 //Define Register size for different architectures
96 #if defined (MDE_CPU_IA32)
98 #elif defined (MDE_CPU_X64)
100 #elif defined (MDE_CPU_ARM)
104 #define GDB_SERIAL_DEV_SIGNATURE SIGNATURE_32 ('g', 'd', 'b', 's')
107 VENDOR_DEVICE_PATH VendorDevice
;
108 UINT32 Index
; // Suport more than one
109 EFI_DEVICE_PATH_PROTOCOL End
;
110 } GDB_SERIAL_DEVICE_PATH
;
114 // Purpose: To provide device specific information
116 // Signature UINTN: The identity of the serial device
117 // SerialIo SERIAL_IO_PROTOCOL: Serial I/O protocol interface
118 // SerialMode SERIAL_IO_MODE:
119 // DevicePath EFI_DEVICE_PATH_PROTOCOL *: Device path of the serial device
124 EFI_SERIAL_IO_PROTOCOL SerialIo
;
125 EFI_SERIAL_IO_MODE SerialMode
;
126 GDB_SERIAL_DEVICE_PATH DevicePath
;
127 INTN InFileDescriptor
;
128 INTN OutFileDescriptor
;
132 #define GDB_SERIAL_DEV_FROM_THIS(a) CR (a, GDB_SERIAL_DEV, SerialIo, GDB_SERIAL_DEV_SIGNATURE)
136 EFI_EXCEPTION_TYPE Exception
;
138 } EFI_EXCEPTION_TYPE_ENTRY
;
141 #if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)
144 // Byte packed structure for DR6
146 // 64-bits on X64. The upper 32-bits on X64 are reserved
150 UINT32 B0
:1; // Breakpoint condition detected
151 UINT32 B1
:1; // Breakpoint condition detected
152 UINT32 B2
:1; // Breakpoint condition detected
153 UINT32 B3
:1; // Breakpoint condition detected
154 UINT32 Reserved_1
:9; // Reserved
155 UINT32 BD
:1; // Debug register access detected
156 UINT32 BS
:1; // Single step
157 UINT32 BT
:1; // Task switch
158 UINT32 Reserved_2
:16; // Reserved
164 // Byte packed structure for DR7
166 // 64-bits on X64. The upper 32-bits on X64 are reserved
170 UINT32 L0
:1; // Local breakpoint enable
171 UINT32 G0
:1; // Global breakpoint enable
172 UINT32 L1
:1; // Local breakpoint enable
173 UINT32 G1
:1; // Global breakpoint enable
174 UINT32 L2
:1; // Local breakpoint enable
175 UINT32 G2
:1; // Global breakpoint enable
176 UINT32 L3
:1; // Local breakpoint enable
177 UINT32 G3
:1; // Global breakpoint enable
178 UINT32 LE
:1; // Local exact breakpoint enable
179 UINT32 GE
:1; // Global exact breakpoint enable
180 UINT32 Reserved_1
:3; // Reserved
181 UINT32 GD
:1; // Global detect enable
182 UINT32 Reserved_2
:2; // Reserved
183 UINT32 RW0
:2; // Read/Write field
184 UINT32 LEN0
:2; // Length field
185 UINT32 RW1
:2; // Read/Write field
186 UINT32 LEN1
:2; // Length field
187 UINT32 RW2
:2; // Read/Write field
188 UINT32 LEN2
:2; // Length field
189 UINT32 RW3
:2; // Read/Write field
190 UINT32 LEN3
:2; // Length field
195 #endif /* if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64) */
198 InstructionExecution
, //Hardware breakpoint
201 DataReadWrite
, //awatch
202 SoftwareBreakpoint
, //Software breakpoint
207 // Array of exception types that need to be hooked by the debugger
209 extern EFI_EXCEPTION_TYPE_ENTRY gExceptionType
[];
212 // Set TRUE if F Reply package signals a ctrl-c. We can not process the Ctrl-c
213 // here we need to wait for the periodic callback to do this.
215 extern BOOLEAN gCtrlCBreakFlag
;
218 // If the periodic callback is called while we are processing an F packet we need
219 // to let the callback know to not read from the serail stream as it could steal
220 // characters from the F reponse packet
222 extern BOOLEAN gProcessingFPacket
;
225 // The offsets of registers SystemContext.
226 // The fields in the array are in the gdb ordering.
228 extern UINTN gRegisterOffsets
[];
231 Return the number of entries in the gExceptionType[]
233 @retval UINTN, the number of entries in the gExceptionType[] array.
242 Return the number of entries in the gRegisters[]
244 @retval UINTN, the number of entries (registers) in the gRegisters[] array.
253 Check to see if the ISA is supported.
254 ISA = Instruction Set Architecture
256 @retval TRUE if Isa is supported,
261 IN EFI_INSTRUCTION_SET_ARCHITECTURE Isa
266 Send the T signal with the given exception type (in gdb order) and possibly with n:r pairs related to the watchpoints
268 @param SystemContext Register content at time of the exception
269 @param GdbExceptionType GDB exception type
274 IN EFI_SYSTEM_CONTEXT SystemContext
,
275 IN UINT8 GdbExceptionType
280 Translates the EFI mapping to GDB mapping
282 @param EFIExceptionType EFI Exception that is being processed
283 @retval UINTN that corresponds to EFIExceptionType's GDB exception type number
286 ConvertEFItoGDBtype (
287 IN EFI_EXCEPTION_TYPE EFIExceptionType
292 Empties the given buffer
293 @param *Buf pointer to the first element in buffer to be emptied
302 Converts an 8-bit Hex Char into a INTN.
304 @param Char - the hex character to be converted into UINTN
305 @retval a INTN, from 0 to 15, that corressponds to Char
306 -1 if Char is not a hex character
315 Send an error with the given error number after converting to hex.
316 The error number is put into the buffer in hex. '255' is the biggest errno we can send.
317 ex: 162 will be sent as A2.
319 @param errno the error number that will be sent
329 Send 'OK' when the function is done executing successfully.
338 Send empty packet to specify that particular command/functionality is not supported.
346 Reads the n-th register's value into an output buffer and sends it as a packet
347 @param SystemContext Register content at time of the exception
348 @param InBuffer This is the input buffer received from gdb server
352 IN EFI_SYSTEM_CONTEXT SystemContext
,
358 Reads the general registers into an output buffer and sends it as a packet
359 @param SystemContext Register content at time of the exception
362 ReadGeneralRegisters (
363 IN EFI_SYSTEM_CONTEXT SystemContext
367 /** ‘P n...=r...’
368 Writes the new value of n-th register received into the input buffer to the n-th register
369 @param SystemContext Register content at time of the exception
370 @param InBuffer This is the input buffer received from gdb server
374 IN EFI_SYSTEM_CONTEXT SystemContext
,
380 Writes the new values received into the input buffer to the general registers
381 @param SystemContext Register content at time of the exception
382 @param InBuffer Pointer to the input buffer received from gdb server
386 WriteGeneralRegisters (
387 IN EFI_SYSTEM_CONTEXT SystemContext
,
392 /** ‘m addr,length ’
393 Find the Length of the area to read and the start addres. Finally, pass them to
394 another function, TransferFromMemToOutBufAndSend, that will read from that memory space and
397 @param *PacketData Pointer to Payload data for the packet
405 /** ‘M addr,length :XX...’
406 Find the Length of the area in bytes to write and the start addres. Finally, pass them to
407 another function, TransferFromInBufToMem, that will write to that memory space the info in
410 @param PacketData Pointer to Payload data for the packet
419 Continue. addr is Address to resume. If addr is omitted, resume at current
422 @param SystemContext Register content at time of the exception
423 @param *PacketData Pointer to PacketData
428 IN EFI_SYSTEM_CONTEXT SystemContext
,
434 Single step. addr is the Address at which to resume. If addr is omitted, resume
437 @param SystemContext Register content at time of the exception
438 @param PacketData Pointer to Payload data for the packet
442 IN EFI_SYSTEM_CONTEXT SystemContext
,
447 Insert Single Step in the SystemContext
449 @param SystemContext Register content at time of the exception
453 IN EFI_SYSTEM_CONTEXT SystemContext
457 Remove Single Step in the SystemContext
459 @param SystemContext Register content at time of the exception
463 IN EFI_SYSTEM_CONTEXT SystemContext
468 ‘Z1, [addr], [length]’
469 ‘Z2, [addr], [length]’
470 ‘Z3, [addr], [length]’
471 ‘Z4, [addr], [length]’
473 Insert hardware breakpoint/watchpoint at address addr of size length
475 @param SystemContext Register content at time of the exception
476 @param *PacketData Pointer to the Payload data for the packet
482 IN EFI_SYSTEM_CONTEXT SystemContext
,
488 ‘z1, [addr], [length]’
489 ‘z2, [addr], [length]’
490 ‘z3, [addr], [length]’
491 ‘z4, [addr], [length]’
493 Remove hardware breakpoint/watchpoint at address addr of size length
495 @param SystemContext Register content at time of the exception
496 @param *PacketData Pointer to the Payload data for the packet
502 IN EFI_SYSTEM_CONTEXT SystemContext
,
508 Exception Hanldler for GDB. It will be called for all exceptions
509 registered via the gExceptionType[] array.
511 @param ExceptionType Exception that is being processed
512 @param SystemContext Register content at time of the exception
517 GdbExceptionHandler (
518 IN EFI_EXCEPTION_TYPE ExceptionType
,
519 IN OUT EFI_SYSTEM_CONTEXT SystemContext
524 Periodic callback for GDB. This function is used to catch a ctrl-c or other
525 break in type command from GDB.
527 @param SystemContext Register content at time of the call
532 GdbPeriodicCallBack (
533 IN OUT EFI_SYSTEM_CONTEXT SystemContext
538 Make two serail consoles: 1) StdIn and StdOut via GDB. 2) StdErr via GDB.
540 These console show up on the remote system running GDB
545 GdbInitializeSerialConsole (
551 Send a GDB Remote Serial Protocol Packet
553 $PacketData#checksum PacketData is passed in and this function adds the packet prefix '$',
554 the packet teminating character '#' and the two digit checksum.
556 If an ack '+' is not sent resend the packet, but timeout eventually so we don't end up
557 in an infinit loop. This is so if you unplug the debugger code just keeps running
559 @param PacketData Payload data for the packet
561 @retval Number of bytes of packet data sent.
571 Receive a GDB Remote Serial Protocol Packet
573 $PacketData#checksum PacketData is passed in and this function adds the packet prefix '$',
574 the packet teminating character '#' and the two digit checksum.
576 If host re-starts sending a packet without ending the previous packet, only the last valid packet is processed.
577 (In other words, if received packet is '$12345$12345$123456#checksum', only '$123456#checksum' will be processed.)
579 If an ack '+' is not sent resend the packet
581 @param PacketData Payload data for the packet
583 @retval Number of bytes of packet data received.
588 OUT CHAR8
*PacketData
,
589 IN UINTN PacketDataSize
594 Read data from a FileDescriptor. On success number of bytes read is returned. Zero indicates
595 the end of a file. On error -1 is returned. If count is zero, GdbRead returns zero.
597 @param FileDescriptor Device to talk to.
598 @param Buffer Buffer to hold Count bytes that were read
599 @param Count Number of bytes to transfer.
602 @retval {other} Number of bytes read.
607 IN INTN FileDescriptor
,
614 Write data to a FileDescriptor. On success number of bytes written is returned. Zero indicates
615 nothing was written. On error -1 is returned.
617 @param FileDescriptor Device to talk to.
618 @param Buffer Buffer to hold Count bytes that are to be written
619 @param Count Number of bytes to transfer.
622 @retval {other} Number of bytes written.
627 IN INTN FileDescriptor
,
628 OUT CONST VOID
*Buffer
,
633 FindPointerToRegister (
634 IN EFI_SYSTEM_CONTEXT SystemContext
,
640 IN EFI_SYSTEM_CONTEXT SystemContext
,
646 TransferFromInBufToMem (
653 TransferFromMemToOutBufAndSend (
660 IN EFI_SYSTEM_CONTEXT SystemContext
,
667 EFI_SYSTEM_CONTEXT SystemContext
671 ParseBreakpointPacket (
672 IN CHAR8
*PacketData
,
679 GetBreakpointDataAddress (
680 IN EFI_SYSTEM_CONTEXT SystemContext
,
681 IN UINTN BreakpointNumber
685 GetBreakpointDetected (
686 IN EFI_SYSTEM_CONTEXT SystemContext
691 IN EFI_SYSTEM_CONTEXT SystemContext
,
692 IN UINTN BreakpointNumber
701 FindNextFreeDebugRegister (
702 IN EFI_SYSTEM_CONTEXT SystemContext
,
707 EnableDebugRegister (
708 IN EFI_SYSTEM_CONTEXT SystemContext
,
716 FindMatchingDebugRegister (
717 IN EFI_SYSTEM_CONTEXT SystemContext
,
725 DisableDebugRegister (
726 IN EFI_SYSTEM_CONTEXT SystemContext
,
731 InitializeProcessor (
742 IN EFI_EXCEPTION_TYPE ExceptionType
,
743 IN OUT EFI_SYSTEM_CONTEXT SystemContext