📄 gdb.c
字号:
/************************************************************************ * * gdb.c * * Shell gdb command * * gdb [-v][-c] [. <args>] * * ###################################################################### * * mips_start_of_legal_notice * * Copyright (c) 2004 MIPS Technologies, Inc. All rights reserved. * * * Unpublished rights (if any) reserved under the copyright laws of the * United States of America and other countries. * * This code is proprietary to MIPS Technologies, Inc. ("MIPS * Technologies"). Any copying, reproducing, modifying or use of this code * (in whole or in part) that is not expressly permitted in writing by MIPS * Technologies or an authorized third party is strictly prohibited. At a * minimum, this code is protected under unfair competition and copyright * laws. Violations thereof may result in criminal penalties and fines. * * MIPS Technologies reserves the right to change this code to improve * function, design or otherwise. MIPS Technologies does not assume any * liability arising out of the application or use of this code, or of any * error or omission in such code. Any warranties, whether express, * statutory, implied or otherwise, including but not limited to the implied * warranties of merchantability or fitness for a particular purpose, are * excluded. Except as expressly provided in any written license agreement * from MIPS Technologies or an authorized third party, the furnishing of * this code does not give recipient any license to any intellectual * property rights, including any patent rights, that cover this code. * * This code shall not be exported, reexported, transferred, or released, * directly or indirectly, in violation of the law of any country or * international law, regulation, treaty, Executive Order, statute, * amendments or supplements thereto. Should a conflict arise regarding the * export, reexport, transfer, or release of this code, the laws of the * United States of America shall be the governing law. * * This code constitutes one or more of the following: commercial computer * software, commercial computer software documentation or other commercial * items. If the user of this code, or any related documentation of any * kind, including related technical data or manuals, is an agency, * department, or other entity of the United States government * ("Government"), the use, duplication, reproduction, release, * modification, disclosure, or transfer of this code, or any related * documentation of any kind, is restricted in accordance with Federal * Acquisition Regulation 12.212 for civilian agencies and Defense Federal * Acquisition Regulation Supplement 227.7202 for military agencies. The use * of this code by the Government is further restricted in accordance with * the terms of the license agreement(s) and/or applicable contract terms * and conditions covering this code from MIPS Technologies or an authorized * third party. * * * * * mips_end_of_legal_notice * * ************************************************************************//************************************************************************ * Include files ************************************************************************/#include <sysdefs.h>#include <syserror.h>#include <shell_api.h>#include <shell.h>#include <gdb_stub.h>#include <string.h>#include <mips.h>#include <syscon_api.h>#include <sys_api.h>#include <excep_api.h>#include <ctype.h>#include <stdio.h>#include <mips.h>/************************************************************************ * Definitions ************************************************************************/#define GDB_OK 0#define GDB_BREAK 1#define GDB_ERROR_CSUM 2#define GDB_CONTINUE 3#define GDB_KILL 4#define GDB_ERROR_GENERAL 5#define GDB_ERROR_ADDRESS 6#define GDB_APPL_DONE 7/************************************************************************ * Public variables ************************************************************************/extern t_gdb_regs appl_context;/************************************************************************ * Static variables ************************************************************************//* OPTIONS */static t_cmd_option options[] ={ #define OPTION_VERBOSE 0 { "v", "Display messages from and to GDB host" },#define OPTION_CHECKSUM_OFF 1 { "c", "Disable check of GDB checksum" }};#define OPTION_COUNT (sizeof(options)/sizeof(t_cmd_option))/* Port used for gdb connection to host */static UINT32 gdb_port;/* Arguments for application */static char *gdb_string = "gdb";#define MAXBUF 2048static char input_buffer[MAXBUF];static char output_buffer[MAXBUF];/* Single stepping */static UINT32 ss_count;static UINT32 ss_addr[2];static UINT32 ss_instr[2]; /* Verbose or not */static bool verbose;/* Enable checksum verification */static bool checksum_on;/* Extended protocol */static bool extended;/* Error codes */static char *msg_e01 = "$E01 : Illegal format.";static char *msg_e02 = "$E02 : Illegal address.";static char *msg_e03 = "$E03 : Structural error in GDB-stub.";/************************************************************************ * Static function prototypes ************************************************************************//************************************************************************ * Implementation : Static functions ************************************************************************/static UINT64*shell_get_epc( t_gdb_regs *regs );static UINT32 get_options( UINT32 argc, char **argv );static UINT32get_packet( char *buf );static voidinit_data( void );static UINT32determine_reply( char *in_buf, char *out_buf, bool *ss );static voidput_packet( char *buf );static boolsetup_single_step( void );static boolcleanup_single_step( void );static charget_char( bool *ctrl_c );static boolvalid_hex( char ch );static UINT64gethex( char *in_buf, char **ptr, UINT32 *rc, bool endian_swap );static UINT32getval( char **in_buf, UINT64 *val, UINT8 nibble_count, bool strict, bool endian_swap );static voidsetval( char **out_buf, UINT64 val, UINT8 size, bool endian_swap );static UINT8char2hex( char ch );static charhex2char( UINT8 val );static UINT32write_regs( char *in_buf, UINT32 size );static voidread_regs( char *out_buf, UINT32 size );/************************************************************************ * gdb ************************************************************************/static MON_FUNC(gdb){ bool ss; UINT32 rc; bool first; extended = FALSE; first = TRUE; rc = get_options( argc, argv ); if( rc != OK ) return rc; printf( "Press Ctrl-C to return to YAMON\n" ); /* Flush input UART fifo */ while( GETCHAR( gdb_port, input_buffer ) ); /* Remote unit may have an output fifo, so * give it a chance to empty it and flush again. */ sys_wait_ms(100); while( GETCHAR( gdb_port, input_buffer ) ); init_data(); do { /* Get packet from GDB */ rc = get_packet( input_buffer ); switch( rc ) { case GDB_OK : put_packet( "+" ); /* Transfer OK */ rc = determine_reply( input_buffer, output_buffer, &ss ); switch( rc ) { case GDB_ERROR_GENERAL : strcpy( output_buffer, msg_e01 ); break; case GDB_ERROR_ADDRESS : strcpy( output_buffer, msg_e02 ); break; case GDB_KILL : case GDB_OK : /* output_buffer was written by determine_reply() */ break; case GDB_CONTINUE : /* Shift to user context */ if( shell_shift_to_user( TRUE, first ) ) { /* Application ended */ sprintf( output_buffer, "$W%02x", (UINT8)SYS_CPUREG(&appl_context, SYS_CPUREG_A0) ); rc = GDB_APPL_DONE; } else { /* Exception */ if( ((UINT32)appl_context.cp0_cause & M_CauseExcCode) >> S_CauseExcCode == EX_BP ) { /* Breakpoint */ strcpy( output_buffer, "$S05" ); } else { /* Other exception */ strcpy( output_buffer, "$X0f" ); } } if( ss ) { /* Break due to single step */ if( !cleanup_single_step() ) { strcpy( output_buffer, msg_e02 ); } } first = FALSE; break; default : /* Should not happen */ strcpy( output_buffer, msg_e03 ); break; } break; case GDB_ERROR_CSUM : case GDB_ERROR_GENERAL : strcpy( output_buffer, "-$" ); break; case GDB_BREAK : default : /* Should not happen */ break; } if( rc != GDB_BREAK ) { /* Write reply */ put_packet( output_buffer ); } } while( (rc != GDB_BREAK) && (rc != GDB_KILL) && (rc != GDB_APPL_DONE) ); if( verbose ) printf( "\n" ); return OK;}/************************************************************************ * get_options ************************************************************************/static UINT32get_options( UINT32 argc, char **argv ){ UINT32 arg; bool ok = TRUE; UINT32 error = SHELL_ERROR_SYNTAX; bool args = FALSE; char *token; /* Setup defaults */ verbose = FALSE; checksum_on = TRUE; shell_argc_appl = 1; shell_argv_appl[0] = gdb_string; for( arg = 1; ok && (arg < argc) && (token = argv[arg]); arg++ ) { if( args ) { if( shell_argc_appl == SHELL_APPL_MAX_ARGS ) { return SHELL_ERROR_ARGV; } else { shell_argv_appl[shell_argc_appl] = token; shell_argc_appl++; } } else { if( strcmp( token, "-v" ) == 0 ) { verbose = TRUE; } else if( strcmp( token, "-c" ) == 0 ) { checksum_on = FALSE; } else if( *token == '-' ) { error = SHELL_ERROR_OPTION; shell_error_data = token; ok = FALSE; } else if( strcmp( token, "." ) == 0 ) { args = TRUE; } else ok = FALSE; } } return ok ? OK : error;}/************************************************************************ * init_data ************************************************************************/static voidinit_data( void ){ /* Setup initial context including EPC */ shell_setup_default_cpu( &appl_context, shell_addr_go_valid ? (UINT32)shell_addr_go : 0 );}/************************************************************************ * get_packet ************************************************************************/static UINT32get_packet( char *buf ){ char ch[2]; char *rcv; UINT8 csum_calc, csum_rcv; UINT8 i; bool ctrl_c; UINT32 rc = GDB_OK; /* Wait for '$' */ do { ch[0] = get_char( &ctrl_c ); if( ctrl_c ) { rc = GDB_BREAK; break; } } while( (ch[0] != '$') ); /* Get packet contents, excluding cheksum bytes */ if( rc == GDB_OK ) { rcv = buf; csum_calc = 0; do { ch[0] = get_char( &ctrl_c ); if( ctrl_c ) { rc = GDB_BREAK; break; } switch( ch[0] ) { case '$' : /* Start again */ rcv = buf; csum_calc = 0; break; case '#' : /* End */ *rcv = '\0'; break; default : /* Packet */ *rcv = ch[0]; csum_calc += (UINT8)ch[0]; rcv++; break; } } while( ch[0] != '#' ); } /* Get checksum bytes */ if( rc == GDB_OK ) { for(i=0; i<2; i++) { ch[i] = get_char( &ctrl_c ); if( ctrl_c ) { rc = GDB_BREAK; break; } } } /* Validate checksum (if not disabled by the -c command option) */ if( (rc == GDB_OK) && checksum_on ) { csum_rcv = 0; for(i=0; i<2; i++) { csum_rcv <<= 4; if( !valid_hex( ch[i] ) ) { rc = GDB_ERROR_GENERAL; break; } csum_rcv += char2hex( ch[i] ); } } if( verbose ) { if( rc == GDB_BREAK ) { printf( "(Break)" ); } else { printf( "(From Host : $%s#%c%c)", buf, ch[0], ch[1] ); } } if( (rc == GDB_OK) && checksum_on && (csum_rcv != csum_calc) ) rc = GDB_ERROR_CSUM; return rc;}/************************************************************************ * determine_reply ************************************************************************/static UINT32determine_reply( char *in_buf, char *out_buf, bool *ss ){ int i; UINT32 count; UINT64 val; char ch; UINT32 address, addr_kseg0; char *ptr; UINT32 reg, size; UINT32 rc; /* Defaults */ *ss = FALSE; /* Start sequence */ strcpy( out_buf, "$" ); out_buf+=1; ch = *in_buf; in_buf++; switch( ch ) { case '!' : /* * extended ops ! Use the extended remote protocol. * Sticky -- only needs to be set once. */ extended = TRUE; strcpy( out_buf, "OK" ); break; case '?' : /* * last signal ? Reply the current reason for stopping. * This is the same reply as is generated * for step or cont : SAA where AA is the * signal number. */ /* We always indicate TRAP */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -