📄 rem-multi.shar
字号:
perror_with_name ("ptrace"); ptrace (PTRACE_SETFPREGS, inferior_pid, &inferior_fp_registers); if (errno) perror_with_name ("ptrace");}#endif /* not NEW_SUN_PTRACE *//* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory in the NEW_SUN_PTRACE case. It ought to be straightforward. But it appears that writing did not write the data that I specified. I cannot understand where it got the data that it actually did write. *//* Copy LEN bytes from inferior's memory starting at MEMADDR to debugger memory starting at MYADDR. */read_inferior_memory (memaddr, myaddr, len) CORE_ADDR memaddr; char *myaddr; int len;{ register int i; /* Round starting address down to longword boundary. */ register CORE_ADDR addr = memaddr & - sizeof (int); /* Round ending address up; get number of longwords that makes. */ register int count = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); /* Allocate buffer of that many longwords. */ register int *buffer = (int *) alloca (count * sizeof (int)); /* Read all the longwords */ for (i = 0; i < count; i++, addr += sizeof (int)) { buffer[i] = ptrace (1, inferior_pid, addr, 0); } /* Copy appropriate bytes out of the buffer. */ bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);}/* Copy LEN bytes of data from debugger memory at MYADDR to inferior's memory at MEMADDR. On failure (cannot write the inferior) returns the value of errno. */intwrite_inferior_memory (memaddr, myaddr, len) CORE_ADDR memaddr; char *myaddr; int len;{ register int i; /* Round starting address down to longword boundary. */ register CORE_ADDR addr = memaddr & - sizeof (int); /* Round ending address up; get number of longwords that makes. */ register int count = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); /* Allocate buffer of that many longwords. */ register int *buffer = (int *) alloca (count * sizeof (int)); extern int errno; /* Fill start and end extra bytes of buffer with existing memory data. */ buffer[0] = ptrace (1, inferior_pid, addr, 0); if (count > 1) { buffer[count - 1] = ptrace (1, inferior_pid, addr + (count - 1) * sizeof (int), 0); } /* Copy data to be written over corresponding part of buffer */ bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); /* Write the entire buffer. */ for (i = 0; i < count; i++, addr += sizeof (int)) { errno = 0; ptrace (4, inferior_pid, addr, buffer[i]); if (errno) return errno; } return 0;}voidtry_writing_regs_command (){ register int i; register int value; extern int errno; if (inferior_pid == 0) error ("There is no inferior process now."); fetch_inferior_registers(); for (i = 0;i<18 ; i ++) { QUIT; errno = 0; value = read_register(i); write_register ( i, value); if (errno == 0) { printf (" Succeeded with register %d; value 0x%x (%d).\n", i, value, value); } else printf (" Failed with register %d.\n", i); }}voidinitialize (){ inferior_pid = 0;}/* Return the contents of register REGNO, regarding it as an integer. */CORE_ADDRread_register (regno) int regno;{ /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */ return *(int *) ®isters[REGISTER_BYTE (regno)];}/* Store VALUE in the register number REGNO, regarded as an integer. */voidwrite_register (regno, val) int regno, val;{ /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */ *(int *) ®isters[REGISTER_BYTE (regno)] = val; if (have_inferior_p ()) store_inferior_registers (regno);}inthave_inferior_p (){ return inferior_pid != 0;}print_some_registers(regs)int regs[];{ register int i; for (i = 0; i < 18; i++) { printf("reg[%d] = %x\n", i, regs[i]); }}SHAR_EOFcat << \SHAR_EOF > remote_server.c/* Main code for remote server for GDB, the GNU Debugger. Copyright (C) 1989 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 "param.h"#include <stdio.h>void read_inferior_memory(), fetch_inferior_registers(); unsigned char resume(); void kill_inferior(); void initialize(), try_writing_regs_command(); int create_inferior(), read_register(); extern char registers[]; int inferior_pid; extern char **environ; /* Descriptor for I/O to remote machine. */int remote_desc;int kiodebug = 0;int remote_debugging; void remote_send ();void putpkt ();void getpkt ();void remote_open(); void write_ok(); void write_enn(); void convert_ascii_to_int(); void convert_int_to_ascii(); void prepare_resume_reply(); void decode_m_packet(); void decode_M_packet(); main(argc,argv)int argc; char *argv[]; { char ch,status, own_buf[2000], mem_buf[2000]; int i=0; unsigned char signal; unsigned int mem_addr, len; initialize(); printf("\nwill open serial link\n"); remote_open("/dev/ttya",0); if(argc < 2) { printf("Enter name of program to be run with command line args\n"); gets(own_buf); inferior_pid = create_inferior(own_buf,environ); printf("\nProcess %s created; pid = %d\n",own_buf,inferior_pid); } else { inferior_pid = create_inferior(argv[1],environ); printf("\nProcess %s created; pid = %d\n",argv[1],inferior_pid); } do { getpkt(own_buf); printf("\nPacket received is>:%s\n",own_buf); i = 0; ch = own_buf[i++]; switch (ch) { case 'h': /**********This is only for tweaking the gdb+ program *******/ signal = resume(1,0,&status); prepare_resume_reply(own_buf,status,signal); break; /*************end tweak*************************************/ case 'g': fetch_inferior_registers(); convert_int_to_ascii(registers,own_buf,REGISTER_BYTES); break; case 'G': convert_ascii_to_int(&own_buf[1],registers,REGISTER_BYTES); if(store_inferior_registers(-1)==0) write_ok(own_buf); else write_enn(own_buf); break; case 'm': decode_m_packet(&own_buf[1],&mem_addr,&len); 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); if(write_inferior_memory(mem_addr,mem_buf,len)==0) write_ok(own_buf); else write_enn(own_buf); break; case 'c': signal = resume(0,0,&status); printf("\nSignal received is >: %0x \n",signal); prepare_resume_reply(own_buf,status,signal); break; case 's': signal = resume(1,0,&status); prepare_resume_reply(own_buf,status,signal); break; case 'k': kill_inferior(); sprintf(own_buf,"q"); putpkt(own_buf); printf("\nObtained kill request...terminating\n"); close(remote_desc); exit(0); case 't': try_writing_regs_command(); own_buf[0] = '\0'; break; default : printf("\nUnknown option chosen by master\n"); write_enn(own_buf); break; } putpkt(own_buf); } while(1) ; close(remote_desc); /** now get out of here**/ printf("\nFinished reading data from serial link - Bye!\n"); exit(0);}SHAR_EOFcat << \SHAR_EOF > remote_utils.c/* Remote utility routines for the remote server for GDB, the GNU debugger. Copyright (C) 1986, 1989 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 "param.h"#include <stdio.h>#include <signal.h>#include <sys/wait.h>#include <sys/ioctl.h>#include <a.out.h>#include <sys/file.h>#include <sgtty.h> extern int remote_desc; extern int remote_debugging; extern int kiodebug; void remote_open(); void remote_send(); void putpkt(); void getpkt(); void write_ok(); void write_enn(); void convert_ascii_to_int(); void convert_int_to_ascii(); void prepare_resume_reply(); /* Open a connection to a remote debugger. NAME is the filename used for communication. */voidremote_open (name, from_tty) char *name; int from_tty;{ struct sgttyb sg; remote_debugging = 0; remote_desc = open (name, O_RDWR); if (remote_desc < 0) printf("\ncould not open remote device\n"); ioctl (remote_desc, TIOCGETP, &sg); sg.sg_flags = RAW; ioctl (remote_desc, TIOCSETP, &sg); if (from_tty) printf ("Remote debugging using %s\n", name); remote_debugging = 1;}/* Convert hex digit A to a number. */static intfromhex (a) int a;{ if (a >= '0' && a <= '9') return a - '0'; else if (a >= 'a' && a <= 'f') return a - 'a' + 10; else perror ("Reply contains invalid hex digit");}/* Convert number NIB to a hex digit. */static inttohex (nib) int nib;{ if (nib < 10) return '0'+nib; else return 'a'+nib-10;}/* Send the command in BUF to the remote machine, and read the reply into BUF. Report an error if we get an error reply. */voidremote_send (buf) char *buf;{ putpkt (buf); getpkt (buf); if (buf[0] == 'E') perror ("Remote failure reply: %s", buf);}/* Send a packet to the remote machine, with error checking. The data of the packet is in BUF. */voidputpkt (buf) char *buf;{ int i; unsigned char csum = 0; char buf2[500]; char buf3[1]; int cnt = strlen (buf); char *p; if (kiodebug) fprintf (stderr, "Sending packet: %s\n", buf); /* Copy the packet into buffer BUF2, encapsulating it and giving it a checksum. */ p = buf2; *p++ = '$'; for (i = 0; i < cnt; i++) { csum += buf[i]; *p++ = buf[i]; } *p++ = '#'; *p++ = tohex ((csum >> 4) & 0xf); *p++ = tohex (csum & 0xf); /* Send it over and over until we get a positive ack. */ do { write (remote_desc, buf2, p - buf2); read (remote_desc, buf3, 1); } while (buf3[0] != '+');}static intreadchar (){ char buf[1]; while (read (remote_desc, buf, 1) != 1) ; return buf[0] & 0x7f;}/* Read a packet from the remote machine, with error checking, and store it in BUF. */voidgetpkt (buf) char *buf;{ char *bp; unsigned char csum, c, c1, c2; extern kiodebug; while (1) { csum = 0; while ((c = readchar()) != '$'); bp = buf; while (1) { c = readchar (); if (c == '#') break; *bp++ = c; csum += c; } *bp = 0; c1 = fromhex (readchar ()); c2 = fromhex (readchar ()); if (csum == (c1 << 4) + c2) break; printf ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n", (c1 << 4) + c2, csum, buf); write (remote_desc, "-", 1); } write (remote_desc, "+", 1); if (kiodebug) fprintf (stderr,"Packet received :%s\n", buf);}void write_ok(buf) char *buf; { buf[0] = 'O'; buf[1] = 'k'; buf[2] = '\0';}void write_enn(buf) char *buf; { buf[0] = 'E'; buf[1] = 'N'; buf[2] = 'N'; buf[3] = '\0';}voidconvert_int_to_ascii(from,to,n)char *from, *to; int n; { int nib ; char ch; while( n-- ) { ch = *from++; nib = ((ch & 0xf0) >> 4)& 0x0f; *to++ = tohex(nib); nib = ch & 0x0f; *to++ = tohex(nib); } *to++ = 0; }voidconvert_ascii_to_int(from,to,n)char *from, *to; int n; { int nib1,nib2 ; while( n-- ) { nib1 = fromhex(*from++); nib2 = fromhex(*from++); *to++ = (((nib1 & 0x0f)<< 4)& 0xf0) | (nib2 & 0x0f); } }voidprepare_resume_reply(buf,status,signal)char *buf ,status; unsigned char signal; { int nib; char ch; *buf++ = 'S'; *buf++ = status; nib = ((signal & 0xf0) >> 4) ; *buf++ = tohex(nib); nib = signal & 0x0f; *buf++ = tohex(nib); *buf++ = 0; }void decode_m_packet(from,mem_addr_ptr,len_ptr)char *from;unsigned int *mem_addr_ptr, *len_ptr; { int i = 0, j = 0 ; char ch; *mem_addr_ptr = *len_ptr = 0; /************debugging begin************/ printf("\nIn decode_m_packet"); /************debugging end************/ while((ch = from[i++]) != ',') { *mem_addr_ptr = *mem_addr_ptr << 4; *mem_addr_ptr |= fromhex(ch) & 0x0f; } /************debugging begin************/ printf("\nFinished mem_addr part"); /************debugging end************/ for(j=0; j < 4; j++) { if((ch = from[i++]) == 0) break; *len_ptr = *len_ptr << 4; *len_ptr |= fromhex(ch) & 0x0f; } /************debugging begin************/ printf("\nFinished len_ptr part"); /************debugging end************/ }void decode_M_packet(from,mem_addr_ptr,len_ptr,to)char *from, *to;unsigned int *mem_addr_ptr, *len_ptr; { int i = 0, j = 0 ; char ch; *mem_addr_ptr = *len_ptr = 0; /************debugging begin************/ printf("\nIn decode_M_packet"); /************debugging end************/ while((ch = from[i++]) != ',') { *mem_addr_ptr = *mem_addr_ptr << 4; *mem_addr_ptr |= fromhex(ch) & 0x0f; } /************debugging begin************/ printf("\nFinished mem_addr part: memaddr = %x",*mem_addr_ptr); /************debugging end************/ while((ch = from[i++]) != ':') { *len_ptr = *len_ptr << 4; *len_ptr |= fromhex(ch) & 0x0f; } /************debugging begin************/ printf("\nFinished len_ptr part: len = %d",*len_ptr); /************debugging end************/ convert_ascii_to_int(&from[i++],to,*len_ptr); /************debugging begin************/ printf("\nmembuf : %x",*(int *)to); /************debugging end************/ }SHAR_EOF# End of shell archiveexit 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -