📄 server.c
字号:
/* Main code for remote server for GDB. Copyright (C) 1989, 1993 Free Software Foundation, Inc.This file is part of GDB.This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include "server.h"#include "config.h"#include "sysdeps.h"#include "shared.h"#include "memory.h"#include "custom.h"#include "newcpu.h"#define REGISTER_BYTES 72extern int gdb_fd;unsigned long text_offset;unsigned long data_offset;unsigned long bss_offset;int inferior_pid;jmp_buf toplevel;#define MAX_BP 5static unsigned long currentBP=0;//tanghaostatic unsigned long breakp[MAX_BP];intgdb (){ if (setjmp(toplevel)) { printf("Poof!\n"); exit(1); } bzero(breakp,sizeof(breakp)); gdb_fd = open("/dev/ptype",O_RDWR | O_SYNC); inferior_pid = 1; create_inferior (inferior_pid); puts("Attached to process. Start debugger");}static int first = 0;gdb_break(unsigned long pc, unsigned long ps, unsigned long *D, unsigned long *A){ char ch, status, own_buf[200], mem_buf[200]; int i; unsigned char signal; unsigned int type, mem_addr, len; fprintf(stderr, "..................................................I get it.\n"); for (i=0; i<8; i++) registers[0 + i] = htonl((Shptr->regs).d[i]); for (i=0; i<8; i++) registers[8 + i] = htonl((Shptr->regs).a[i]); registers[16] = htonl(ps); registers[17] = htonl(pc);fprintf(stderr, "%s,%d - pc=0x%x(0x%x)\n", __FILE__, __LINE__, pc, htonl(pc));fflush(stderr);sleep(1); /* setup offsets */ if (!(Shptr->regs).s) { mem_addr = (Shptr->regs).a[5]; //tanghao some thing maybe wrong here text_offset=get_long(mem_addr + 0) - 0x10000; /* gdb adds 0x10000 */ data_offset=get_long(mem_addr + 4); bss_offset= get_long(mem_addr + 8);// text_offset = (Shptr->regs).pc - (Shptr->regs).d[0];// data_offset = (Shptr->regs).a[5];// bss_offset = (Shptr->regs).a[5]; } i = 0;fprintf(stderr, "%s,%d - %s\n", __FILE__, __LINE__, __FUNCTION__); signal = mywait (&status); /* Wait till we are at 1st instr in prog */ /* We are now stopped at the first instruction of the target process */fprintf(stderr, "%s,%d - %s\n", __FILE__, __LINE__, __FUNCTION__); if (first) {fprintf(stderr, "%s,%d - %s\n", __FILE__, __LINE__, __FUNCTION__); sprintf(own_buf,"S05"); putpkt (own_buf); } first = 1; while (1) { setjmp(toplevel); while (getpkt (own_buf) > 0) { unsigned char sig; i = 0; ch = own_buf[i++]; fprintf(stderr, "ch=%x,%c\n",ch,ch);//tanghao fflush(stderr);//tanghao switch (ch) { case '?': prepare_resume_reply (own_buf, status, signal); break;#if 0 case 'q': if((own_buf[1]=='O') && (own_buf[2]=='f') && (own_buf[3]=='f') && (own_buf[4]=='s') && (own_buf[5]=='e') && (own_buf[6]=='t') && (own_buf[7]=='s') ) { if ((Shptr->regs).s) { sprintf(own_buf,"Text=%.8x;Data=%.8x;Bss=%.8x", 0, 0, 0); /* no offset for the kernel */ } else { sprintf(own_buf,"Text=%.8x;Data=%.8x;Bss=%.8x", text_offset, data_offset, bss_offset); /* user space is offset */ } } else { own_buf[0] = '\0'; } break;#endif case 'g': convert_int_to_ascii (registers, own_buf, REGISTER_BYTES); break; case 'G': convert_ascii_to_int (&own_buf[1], registers, REGISTER_BYTES); store_inferior_registers (-1); write_ok (own_buf); break; case 'm': decode_m_packet (&own_buf[1], &mem_addr, &len); fprintf(stderr, "C%x",len); {// int i;// for(i=0;i++;i<len) { fprintf(stderr, "%s ",own_buf); } } read_inferior_memory (mem_addr, mem_buf, len); convert_int_to_ascii (mem_buf, own_buf, len); break; case 'M': decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); fprintf(stderr, "mem_addr:%x,mem_buf:=%x",mem_addr,*((char *)mem_buf));//tanghao#if 0// if ((mem_addr > 0x10c0000) && (mem_addr < 0x10e00000)) //tanghao {// if (mem_buf[0] == 0x4e && mem_buf[1] == 0x41) if (mem_buf[0] == 0x4e)// && mem_buf[1] == 0x41) //tanghao { ins_break(mem_addr); fprintf(stderr, "here to ins_breakpoint\n");//tanghao write_ok(own_buf); //tnaghoa // break; //tanghao } else if(mem_buf[1] == 0x41) { del_break(mem_addr); fprintf(stderr, "here to del_breakpoint\n");//tanghao write_ok(own_buf); //tnahgoa // break; //tanghao }// write_ok(own_buf); //tanghao// break; //tanghao }#endif //0 fprintf(stderr, "here to write\n");//tanghao if (write_inferior_memory (mem_addr, mem_buf, len) == 0) write_ok (own_buf); else write_enn (own_buf); break; case 'S': case 's': return 1; case 'C': return 0; case 'c': return 0;// Shptr->CpuReq = cpuStart;// write_ok(own_buf); //tnaghoa // break; case 'k': return 0; case 'z': decode_z_packet (&own_buf[1], &type, &mem_addr, &len); fprintf(stderr, "here to by z del_breakpoint\n");//tanghao if(del_break(mem_addr)==1) write_ok(own_buf); //tnaghoa else write_enn (own_buf); break; case 'Z': fprintf(stderr, "here to before by Z ins_breakpoint\n");//tanghao decode_Z_packet (&own_buf[1], &type, &mem_addr, &len); fprintf(stderr, "here to by Z ins_breakpoint\n");//tanghao if(ins_break(mem_addr)==1) write_ok(own_buf); //tnaghoa else write_enn (own_buf); break; default: /* It is a request we don't understand. Respond with an empty packet so that gdb knows that we don't support this request. */ own_buf[0] = '\0'; break; } putpkt (own_buf); } }}del_break(unsigned long pc){ int i; for (i=0;i<(MAX_BP);i++) if (pc == breakp[i]) { breakp[i] = 0; return 1; } return 0;}ins_break(unsigned long pc){ int i; for (i=0;i<(MAX_BP);i++) if (!breakp[i]) { breakp[i] = pc; return 1; } return 0;// breakp[currentBP++] = pc;// return 1;}check_break(unsigned long pc){ int i;// fprintf(stderr, "in check_break");//tanghao for (i=0;i<(MAX_BP);i++) // for (i=0;i<(currentBP);i++) //tanghao{// fprintf(stderr, "if in break,pc=%x,break[%d]=%x\n\r",pc,i,breakp[i]);//tanghao if (pc == breakp[i]) { fprintf(stderr, "get into break,pc=%x",pc);//tanghao// Shptr->CpuReq = cpuStop;// MC68000_setstopped(1); return 1; }} return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -