#include "defs.h"
#include "gdb_string.h"
#include <ctype.h>
#include <fcntl.h>
#include "frame.h"
#include "inferior.h"
#include "bfd.h"
#include "symfile.h"
#include "target.h"
#include "gdb_wait.h"
#include "gdbcmd.h"
#include "objfiles.h"
#include "gdb-stabs.h"
#include "gdbthread.h"
#include "remote.h"
#include <ctype.h>
#include <sys/time.h>
#ifdef USG
#include <sys/types.h>
#endif
#include "event-loop.h"
#include "event-top.h"
#include "inf-loop.h"
#include <signal.h>
#include "serial.h"
static void cleanup_sigint_signal_handler (void *dummy);
static void initialize_sigint_signal_handler (void);
static int getpkt_sane (char *buf, long sizeof_buf, int forever);
static void handle_remote_sigint (int);
static void handle_remote_sigint_twice (int);
static void async_remote_interrupt (gdb_client_data);
void async_remote_interrupt_twice (gdb_client_data);
static void build_remote_gdbarch_data (void);
static int remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len);
static int remote_read_bytes (CORE_ADDR memaddr, char *myaddr, int len);
static void remote_files_info (struct target_ops *ignore);
static int remote_xfer_memory (CORE_ADDR memaddr, char *myaddr,
int len, int should_write,
struct target_ops *target);
static void remote_prepare_to_store (void);
static void remote_fetch_registers (int regno);
static void remote_resume (int pid, int step, enum target_signal siggnal);
static void remote_async_resume (int pid, int step,
enum target_signal siggnal);
static int remote_start_remote (PTR);
static void remote_open (char *name, int from_tty);
static void remote_async_open (char *name, int from_tty);
static void extended_remote_open (char *name, int from_tty);
static void extended_remote_async_open (char *name, int from_tty);
static void remote_open_1 (char *, int, struct target_ops *, int extended_p);
static void remote_async_open_1 (char *, int, struct target_ops *,
int extended_p);
static void remote_close (int quitting);
static void remote_store_registers (int regno);
static void remote_mourn (void);
static void remote_async_mourn (void);
static void extended_remote_restart (void);
static void extended_remote_mourn (void);
static void extended_remote_create_inferior (char *, char *, char **);
static void extended_remote_async_create_inferior (char *, char *, char **);
static void remote_mourn_1 (struct target_ops *);
static void remote_send (char *buf, long sizeof_buf);
static int readchar (int timeout);
static int remote_wait (int pid, struct target_waitstatus *status);
static int remote_async_wait (int pid, struct target_waitstatus *status);
static void remote_kill (void);
static void remote_async_kill (void);
static int tohex (int nib);
static void remote_detach (char *args, int from_tty);
static void remote_async_detach (char *args, int from_tty);
static void remote_interrupt (int signo);
static void remote_interrupt_twice (int signo);
static void interrupt_query (void);
static void set_thread (int, int);
static int remote_thread_alive (int);
static void get_offsets (void);
static long read_frame (char *buf, long sizeof_buf);
static int remote_insert_breakpoint (CORE_ADDR, char *);
static int remote_remove_breakpoint (CORE_ADDR, char *);
static int hexnumlen (ULONGEST num);
static void init_remote_ops (void);
static void init_extended_remote_ops (void);
static void init_remote_cisco_ops (void);
static struct target_ops remote_cisco_ops;
static void remote_stop (void);
static int ishex (int ch, int *val);
static int stubhex (int ch);
static int remote_query (int , char *, char *, int *);
static int hexnumstr (char *, ULONGEST);
static int hexnumnstr (char *, ULONGEST, int);
static CORE_ADDR remote_address_masked (CORE_ADDR);
static void print_packet (char *);
static unsigned long crc32 (unsigned char *, int, unsigned int);
static void compare_sections_command (char *, int);
static void packet_command (char *, int);
static int stub_unpack_int (char *buff, int fieldlength);
static int remote_current_thread (int oldpid);
static void remote_find_new_threads (void);
static void record_currthread (int currthread);
extern int fromhex (int a);
static int putpkt_binary (char *buf, int cnt);
static void check_binary_download (CORE_ADDR addr);
struct packet_config;
static void show_packet_config_cmd (struct packet_config *config);
static void update_packet_config (struct packet_config *config);
void open_remote_target (char *, int, struct target_ops *, int);
void _initialize_remote (void);
static struct target_ops remote_ops;
static struct target_ops extended_remote_ops;
static struct target_ops remote_async_ops;
static struct target_ops extended_async_remote_ops;
static int wait_forever_enabled_p = 1;
static int remote_break;
static serial_t remote_desc = NULL;
static int cisco_kernel_mode = 0;
static int remote_address_size;
static int remote_async_terminal_ours_p;
static long actual_register_packet_size;
static long remote_packet_size;
#define PBUFSIZ (remote_packet_size)
struct memory_packet_config
{
char *name;
long size;
int fixed_p;
};
static long
get_memory_packet_size (struct memory_packet_config *config)
{
#ifndef MAX_REMOTE_PACKET_SIZE
#define MAX_REMOTE_PACKET_SIZE 16384
#endif
#ifndef MIN_REMOTE_PACKET_SIZE
#define MIN_REMOTE_PACKET_SIZE 16
#endif
long what_they_get;
if (config->fixed_p)
{
if (config->size <= 0)
what_they_get = MAX_REMOTE_PACKET_SIZE;
else
what_they_get = config->size;
}
else
{
what_they_get = remote_packet_size;
if (config->size > 0
&& what_they_get > config->size)
what_they_get = config->size;
if (actual_register_packet_size > 0
&& what_they_get > actual_register_packet_size)
what_they_get = actual_register_packet_size;
}
if (what_they_get > MAX_REMOTE_PACKET_SIZE)
what_they_get = MAX_REMOTE_PACKET_SIZE;
if (what_they_get < MIN_REMOTE_PACKET_SIZE)
what_they_get = MIN_REMOTE_PACKET_SIZE;
return what_they_get;
}
static void
set_memory_packet_size (char *args, struct memory_packet_config *config)
{
int fixed_p = config->fixed_p;
long size = config->size;
if (args == NULL)
error ("Argument required (integer, `fixed' or `limited').");
else if (strcmp (args, "hard") == 0
|| strcmp (args, "fixed") == 0)
fixed_p = 1;
else if (strcmp (args, "soft") == 0
|| strcmp (args, "limit") == 0)
fixed_p = 0;
else
{
char *end;
size = strtoul (args, &end, 0);
if (args == end)
error ("Invalid %s (bad syntax).", config->name);
#if 0
if (size > MAX_REMOTE_PACKET_SIZE)
error ("Invalid %s (too large).", config->name);
#endif
}
if (fixed_p && !config->fixed_p)
{
if (! query ("The target may not be able to correctly handle a %s\n"
"of %ld bytes. Change the packet size? ",
config->name, size))
error ("Packet size not changed.");
}
config->fixed_p = fixed_p;
config->size = size;
}
static void
show_memory_packet_size (struct memory_packet_config *config)
{
printf_filtered ("The %s is %ld. ", config->name, config->size);
if (config->fixed_p)
printf_filtered ("Packets are fixed at %ld bytes.\n",
get_memory_packet_size (config));
else
printf_filtered ("Packets are limited to %ld bytes.\n",
get_memory_packet_size (config));
}
static struct memory_packet_config memory_write_packet_config =
{
"memory-write-packet-size",
};
static void
set_memory_write_packet_size (char *args, int from_tty)
{
set_memory_packet_size (args, &memory_write_packet_config);
}
static void
show_memory_write_packet_size (char *args, int from_tty)
{
show_memory_packet_size (&memory_write_packet_config);
}
static long
get_memory_write_packet_size (void)
{
return get_memory_packet_size (&memory_write_packet_config);
}
static struct memory_packet_config memory_read_packet_config =
{
"memory-read-packet-size",
};
static void
set_memory_read_packet_size (char *args, int from_tty)
{
set_memory_packet_size (args, &memory_read_packet_config);
}
static void
show_memory_read_packet_size (char *args, int from_tty)
{
show_memory_packet_size (&memory_read_packet_config);
}
static long
get_memory_read_packet_size (void)
{
long size = get_memory_packet_size (&memory_read_packet_config);
if (size > PBUFSIZ)
size = PBUFSIZ;
return size;
}
static void
register_remote_packet_sizes (void)
{
REGISTER_GDBARCH_SWAP (remote_packet_size);
REGISTER_GDBARCH_SWAP (actual_register_packet_size);
}
static void
build_remote_packet_sizes (void)
{
remote_packet_size = 400 - 1;
if (REGISTER_BYTES > ((remote_packet_size - 32) / 2))
remote_packet_size = (REGISTER_BYTES * 2 + 32);
actual_register_packet_size = 0;
}
enum packet_support
{
PACKET_SUPPORT_UNKNOWN = 0,
PACKET_ENABLE,
PACKET_DISABLE
};
struct packet_config
{
char *name;
char *title;
enum cmd_auto_boolean detect;
enum packet_support support;
};
enum packet_result
{
PACKET_ERROR,
PACKET_OK,
PACKET_UNKNOWN
};
static void
update_packet_config (struct packet_config *config)
{
switch (config->detect)
{
case CMD_AUTO_BOOLEAN_TRUE:
config->support = PACKET_ENABLE;
break;
case CMD_AUTO_BOOLEAN_FALSE:
config->support = PACKET_DISABLE;
break;
case CMD_AUTO_BOOLEAN_AUTO:
config->support = PACKET_SUPPORT_UNKNOWN;
break;
}
}
static void
show_packet_config_cmd (struct packet_config *config)
{
char *support = "internal-error";
switch (config->support)
{
case PACKET_ENABLE:
support = "enabled";
break;
case PACKET_DISABLE:
support = "disabled";
break;
case PACKET_SUPPORT_UNKNOWN:
support = "unknown";
break;
}
switch (config->detect)
{
case CMD_AUTO_BOOLEAN_AUTO:
printf_filtered ("Support for remote protocol `%s' (%s) packet is auto-detected, currently %s.\n",
config->name, config->title, support);
break;
case CMD_AUTO_BOOLEAN_TRUE:
case CMD_AUTO_BOOLEAN_FALSE:
printf_filtered ("Support for remote protocol `%s' (%s) packet is currently %s.\n",
config->name, config->title, support);
break;
}
}
static void
add_packet_config_cmd (struct packet_config *config,
char *name,
char *title,
void (*set_func) (char *args, int from_tty,
struct cmd_list_element *
c),
void (*show_func) (char *name,
int from_tty),
struct cmd_list_element **set_remote_list,
struct cmd_list_element **show_remote_list,
int legacy)
{
struct cmd_list_element *set_cmd;
struct cmd_list_element *show_cmd;
char *set_doc;
char *show_doc;
char *cmd_name;
config->name = name;
config->title = title;
config->detect = CMD_AUTO_BOOLEAN_AUTO;
config->support = PACKET_SUPPORT_UNKNOWN;
asprintf (&set_doc, "Set use of remote protocol `%s' (%s) packet",
name, title);
asprintf (&show_doc, "Show current use of remote protocol `%s' (%s) packet",
name, title);
asprintf (&cmd_name, "%s-packet", title);
set_cmd = add_set_auto_boolean_cmd (cmd_name, class_obscure,
&config->detect, set_doc,
set_remote_list);
set_cmd->function.sfunc = set_func;
show_cmd = add_cmd (cmd_name, class_obscure, show_func, show_doc,
show_remote_list);
if (legacy)
{
char *legacy_name;
asprintf (&legacy_name, "%s-packet", name);
add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
set_remote_list);
add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
show_remote_list);
}
}
static enum packet_result
packet_ok (const char *buf, struct packet_config *config)
{
if (buf[0] != '\0')
{
switch (config->support)
{
case PACKET_SUPPORT_UNKNOWN:
if (remote_debug)
fprintf_unfiltered (gdb_stdlog,
"Packet %s (%s) is supported\n",
config->name, config->title);
config->support = PACKET_ENABLE;
break;
case PACKET_DISABLE:
internal_error ("packet_ok: attempt to use a disabled packet");
break;
case PACKET_ENABLE:
break;
}
if (buf[0] == 'O' && buf[1] == 'K' && buf[2] == '\0')
return PACKET_OK;
if (buf[0] == 'E'
&& isxdigit (buf[1]) && isxdigit (buf[2])
&& buf[3] == '\0')
return PACKET_ERROR;
return PACKET_OK;
}
else
{
switch (config->support)
{
case PACKET_ENABLE:
if (config->detect == CMD_AUTO_BOOLEAN_AUTO)
error ("Protocol error: %s (%s) conflicting enabled responses.",
config->name, config->title);
else
error ("Enabled packet %s (%s) not recognized by stub",
config->name, config->title);
break;
case PACKET_SUPPORT_UNKNOWN:
if (remote_debug)
fprintf_unfiltered (gdb_stdlog,
"Packet %s (%s) is NOT supported\n",
config->name, config->title);
config->support = PACKET_DISABLE;
break;
case PACKET_DISABLE:
break;
}
return PACKET_UNKNOWN;
}
}
static struct packet_config remote_protocol_P;
static void
set_remote_protocol_P_packet_cmd (char *args, int from_tty,
struct cmd_list_element *c)
{
update_packet_config (&remote_protocol_P);
}
static void
show_remote_protocol_P_packet_cmd (char *args, int from_tty)
{
show_packet_config_cmd (&remote_protocol_P);
}
enum Z_packet_type
{
Z_PACKET_SOFTWARE_BP,
Z_PACKET_HARDWARE_BP,
Z_PACKET_WRITE_WP,
Z_PACKET_READ_WP,
Z_PACKET_ACCESS_WP,
NR_Z_PACKET_TYPES
};
static struct packet_config remote_protocol_Z[NR_Z_PACKET_TYPES];
static void
set_remote_protocol_Z_software_bp_packet_cmd (char *args, int from_tty,
struct cmd_list_element *c)
{
update_packet_config (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP]);
}
static void
show_remote_protocol_Z_software_bp_packet_cmd (char *args, int from_tty)
{
show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP]);
}
static void
set_remote_protocol_Z_hardware_bp_packet_cmd (char *args, int from_tty,
struct cmd_list_element *c)
{
update_packet_config (&remote_protocol_Z[Z_PACKET_HARDWARE_BP]);
}
static void
show_remote_protocol_Z_hardware_bp_packet_cmd (char *args, int from_tty)
{
show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_HARDWARE_BP]);
}
static void
set_remote_protocol_Z_write_wp_packet_cmd (char *args, int from_tty,
struct cmd_list_element *c)
{
update_packet_config (&remote_protocol_Z[Z_PACKET_WRITE_WP]);
}
static void
show_remote_protocol_Z_write_wp_packet_cmd (char *args, int from_tty)
{
show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_WRITE_WP]);
}
static void
set_remote_protocol_Z_read_wp_packet_cmd (char *args, int from_tty,
struct cmd_list_element *c)
{
update_packet_config (&remote_protocol_Z[Z_PACKET_READ_WP]);
}
static void
show_remote_protocol_Z_read_wp_packet_cmd (char *args, int from_tty)
{
show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_READ_WP]);
}
static void
set_remote_protocol_Z_access_wp_packet_cmd (char *args, int from_tty,
struct cmd_list_element *c)
{
update_packet_config (&remote_protocol_Z[Z_PACKET_ACCESS_WP]);
}
static void
show_remote_protocol_Z_access_wp_packet_cmd (char *args, int from_tty)
{
show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_ACCESS_WP]);
}
static enum cmd_auto_boolean remote_Z_packet_detect;
static void
set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
struct cmd_list_element *c)
{
int i;
for (i = 0; i < NR_Z_PACKET_TYPES; i++)
{
remote_protocol_Z[i].detect = remote_Z_packet_detect;
update_packet_config (&remote_protocol_Z[i]);
}
}
static void
show_remote_protocol_Z_packet_cmd (char *args, int from_tty)
{
int i;
for (i = 0; i < NR_Z_PACKET_TYPES; i++)
{
show_packet_config_cmd (&remote_protocol_Z[i]);
}
}
static struct packet_config remote_protocol_binary_download;
static int use_threadinfo_query;
static int use_threadextra_query;
static void
set_remote_protocol_binary_download_cmd (char *args,
int from_tty,
struct cmd_list_element *c)
{
update_packet_config (&remote_protocol_binary_download);
}
static void
show_remote_protocol_binary_download_cmd (char *args,
int from_tty)
{
show_packet_config_cmd (&remote_protocol_binary_download);
}
PTR sigint_remote_twice_token;
PTR sigint_remote_token;
void (*target_resume_hook) (void);
void (*target_wait_loop_hook) (void);
static int general_thread;
static int continue_thread;
static void
record_currthread (int currthread)
{
general_thread = currthread;
if (!in_thread_list (currthread))
{
add_thread (currthread);
#ifdef UI_OUT
ui_out_text (uiout, "[New ");
ui_out_text (uiout, target_pid_to_str (currthread));
ui_out_text (uiout, "]\n");
#else
printf_filtered ("[New %s]\n", target_pid_to_str (currthread));
#endif
}
}
#define MAGIC_NULL_PID 42000
static void
set_thread (int th, int gen)
{
char *buf = alloca (PBUFSIZ);
int state = gen ? general_thread : continue_thread;
if (state == th)
return;
buf[0] = 'H';
buf[1] = gen ? 'g' : 'c';
if (th == MAGIC_NULL_PID)
{
buf[2] = '0';
buf[3] = '\0';
}
else if (th < 0)
sprintf (&buf[2], "-%x", -th);
else
sprintf (&buf[2], "%x", th);
putpkt (buf);
getpkt (buf, PBUFSIZ, 0);
if (gen)
general_thread = th;
else
continue_thread = th;
}
static int
remote_thread_alive (int tid)
{
char buf[16];
if (tid < 0)
sprintf (buf, "T-%08x", -tid);
else
sprintf (buf, "T%08x", tid);
putpkt (buf);
getpkt (buf, sizeof (buf), 0);
return (buf[0] == 'O' && buf[1] == 'K');
}
#define OPAQUETHREADBYTES 8
typedef unsigned char threadref[OPAQUETHREADBYTES];
typedef int gdb_threadref;
struct gdb_ext_thread_info
{
threadref threadid;
int active;
char display[256];
char shortname[32];
char more_display[256];
};
#define TAG_THREADID 1
#define TAG_EXISTS 2
#define TAG_DISPLAY 4
#define TAG_THREADNAME 8
#define TAG_MOREDISPLAY 16
#define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES*2)
char *unpack_varlen_hex (char *buff, int *result);
static char *unpack_nibble (char *buf, int *val);
static char *pack_nibble (char *buf, int nibble);
static char *pack_hex_byte (char *pkt, int byte);
static char *unpack_byte (char *buf, int *value);
static char *pack_int (char *buf, int value);
static char *unpack_int (char *buf, int *value);
static char *unpack_string (char *src, char *dest, int length);
static char *pack_threadid (char *pkt, threadref * id);
static char *unpack_threadid (char *inbuf, threadref * id);
void int_to_threadref (threadref * id, int value);
static int threadref_to_int (threadref * ref);
static void copy_threadref (threadref * dest, threadref * src);
static int threadmatch (threadref * dest, threadref * src);
static char *pack_threadinfo_request (char *pkt, int mode, threadref * id);
static int remote_unpack_thread_info_response (char *pkt,
threadref * expectedref,
struct gdb_ext_thread_info
*info);
static int remote_get_threadinfo (threadref * threadid, int fieldset,
struct gdb_ext_thread_info *info);
static int adapt_remote_get_threadinfo (gdb_threadref * ref,
int selection,
struct gdb_ext_thread_info *info);
static char *pack_threadlist_request (char *pkt, int startflag,
int threadcount,
threadref * nextthread);
static int parse_threadlist_response (char *pkt,
int result_limit,
threadref * original_echo,
threadref * resultlist, int *doneflag);
static int remote_get_threadlist (int startflag,
threadref * nextthread,
int result_limit,
int *done,
int *result_count, threadref * threadlist);
typedef int (*rmt_thread_action) (threadref * ref, void *context);
static int remote_threadlist_iterator (rmt_thread_action stepfunction,
void *context, int looplimit);
static int remote_newthread_step (threadref * ref, void *context);
static const char hexchars[] = "0123456789abcdef";
static int
ishex (int ch, int *val)
{
if ((ch >= 'a') && (ch <= 'f'))
{
*val = ch - 'a' + 10;
return 1;
}
if ((ch >= 'A') && (ch <= 'F'))
{
*val = ch - 'A' + 10;
return 1;
}
if ((ch >= '0') && (ch <= '9'))
{
*val = ch - '0';
return 1;
}
return 0;
}
static int
stubhex (int ch)
{
if (ch >= 'a' && ch <= 'f')
return ch - 'a' + 10;
if (ch >= '0' && ch <= '9')
return ch - '0';
if (ch >= 'A' && ch <= 'F')
return ch - 'A' + 10;
return -1;
}
static int
stub_unpack_int (char *buff, int fieldlength)
{
int nibble;
int retval = 0;
while (fieldlength)
{
nibble = stubhex (*buff++);
retval |= nibble;
fieldlength--;
if (fieldlength)
retval = retval << 4;
}
return retval;
}
char *
unpack_varlen_hex (char *buff,
int *result)
{
int nibble;
int retval = 0;
while (ishex (*buff, &nibble))
{
buff++;
retval = retval << 4;
retval |= nibble & 0x0f;
}
*result = retval;
return buff;
}
static char *
unpack_nibble (char *buf, int *val)
{
ishex (*buf++, val);
return buf;
}
static char *
pack_nibble (char *buf, int nibble)
{
*buf++ = hexchars[(nibble & 0x0f)];
return buf;
}
static char *
pack_hex_byte (char *pkt, int byte)
{
*pkt++ = hexchars[(byte >> 4) & 0xf];
*pkt++ = hexchars[(byte & 0xf)];
return pkt;
}
static char *
unpack_byte (char *buf, int *value)
{
*value = stub_unpack_int (buf, 2);
return buf + 2;
}
static char *
pack_int (char *buf, int value)
{
buf = pack_hex_byte (buf, (value >> 24) & 0xff);
buf = pack_hex_byte (buf, (value >> 16) & 0xff);
buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
buf = pack_hex_byte (buf, (value & 0xff));
return buf;
}
static char *
unpack_int (char *buf, int *value)
{
*value = stub_unpack_int (buf, 8);
return buf + 8;
}
#if 0
static char *pack_string (char *pkt, char *string);
static char *
pack_string (char *pkt, char *string)
{
char ch;
int len;
len = strlen (string);
if (len > 200)
len = 200;
pkt = pack_hex_byte (pkt, len);
while (len-- > 0)
{
ch = *string++;
if ((ch == '\0') || (ch == '#'))
ch = '*';
*pkt++ = ch;
}
return pkt;
}
#endif
static char *
unpack_string (char *src, char *dest, int length)
{
while (length--)
*dest++ = *src++;
*dest = '\0';
return src;
}
static char *
pack_threadid (char *pkt, threadref *id)
{
char *limit;
unsigned char *altid;
altid = (unsigned char *) id;
limit = pkt + BUF_THREAD_ID_SIZE;
while (pkt < limit)
pkt = pack_hex_byte (pkt, *altid++);
return pkt;
}
static char *
unpack_threadid (char *inbuf, threadref *id)
{
char *altref;
char *limit = inbuf + BUF_THREAD_ID_SIZE;
int x, y;
altref = (char *) id;
while (inbuf < limit)
{
x = stubhex (*inbuf++);
y = stubhex (*inbuf++);
*altref++ = (x << 4) | y;
}
return inbuf;
}
void
int_to_threadref (threadref *id, int value)
{
unsigned char *scan;
scan = (unsigned char *) id;
{
int i = 4;
while (i--)
*scan++ = 0;
}
*scan++ = (value >> 24) & 0xff;
*scan++ = (value >> 16) & 0xff;
*scan++ = (value >> 8) & 0xff;
*scan++ = (value & 0xff);
}
static int
threadref_to_int (threadref *ref)
{
int i, value = 0;
unsigned char *scan;
scan = (char *) ref;
scan += 4;
i = 4;
while (i-- > 0)
value = (value << 8) | ((*scan++) & 0xff);
return value;
}
static void
copy_threadref (threadref *dest, threadref *src)
{
int i;
unsigned char *csrc, *cdest;
csrc = (unsigned char *) src;
cdest = (unsigned char *) dest;
i = 8;
while (i--)
*cdest++ = *csrc++;
}
static int
threadmatch (threadref *dest, threadref *src)
{
#if 0
unsigned char *srcp, *destp;
int i, result;
srcp = (char *) src;
destp = (char *) dest;
result = 1;
while (i-- > 0)
result &= (*srcp++ == *destp++) ? 1 : 0;
return result;
#endif
return 1;
}
static char *
pack_threadinfo_request (char *pkt, int mode, threadref *id)
{
*pkt++ = 'q';
*pkt++ = 'P';
pkt = pack_int (pkt, mode);
pkt = pack_threadid (pkt, id);
*pkt = '\0';
return pkt;
}
#define TAG_THREADID 1
#define TAG_EXISTS 2
#define TAG_DISPLAY 4
#define TAG_THREADNAME 8
#define TAG_MOREDISPLAY 16
static int
remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
struct gdb_ext_thread_info *info)
{
int mask, length;
unsigned int tag;
threadref ref;
char *limit = pkt + PBUFSIZ;
int retval = 1;
info->active = 0;
info->display[0] = '\0';
info->shortname[0] = '\0';
info->more_display[0] = '\0';
pkt = unpack_int (pkt, &mask);
pkt = unpack_threadid (pkt, &ref);
if (mask == 0)
warning ("Incomplete response to threadinfo request\n");
if (!threadmatch (&ref, expectedref))
{
warning ("ERROR RMT Thread info mismatch\n");
return 0;
}
copy_threadref (&info->threadid, &ref);
while ((pkt < limit) && mask && *pkt)
{
pkt = unpack_int (pkt, &tag);
pkt = unpack_byte (pkt, &length);
if (!(tag & mask))
{
warning ("ERROR RMT: threadinfo tag mismatch\n");
retval = 0;
break;
}
if (tag == TAG_THREADID)
{
if (length != 16)
{
warning ("ERROR RMT: length of threadid is not 16\n");
retval = 0;
break;
}
pkt = unpack_threadid (pkt, &ref);
mask = mask & ~TAG_THREADID;
continue;
}
if (tag == TAG_EXISTS)
{
info->active = stub_unpack_int (pkt, length);
pkt += length;
mask = mask & ~(TAG_EXISTS);
if (length > 8)
{
warning ("ERROR RMT: 'exists' length too long\n");
retval = 0;
break;
}
continue;
}
if (tag == TAG_THREADNAME)
{
pkt = unpack_string (pkt, &info->shortname[0], length);
mask = mask & ~TAG_THREADNAME;
continue;
}
if (tag == TAG_DISPLAY)
{
pkt = unpack_string (pkt, &info->display[0], length);
mask = mask & ~TAG_DISPLAY;
continue;
}
if (tag == TAG_MOREDISPLAY)
{
pkt = unpack_string (pkt, &info->more_display[0], length);
mask = mask & ~TAG_MOREDISPLAY;
continue;
}
warning ("ERROR RMT: unknown thread info tag\n");
break;
}
return retval;
}
static int
remote_get_threadinfo (threadref *threadid, int fieldset,
struct gdb_ext_thread_info *info)
{
int result;
char *threadinfo_pkt = alloca (PBUFSIZ);
pack_threadinfo_request (threadinfo_pkt, fieldset, threadid);
putpkt (threadinfo_pkt);
getpkt (threadinfo_pkt, PBUFSIZ, 0);
result = remote_unpack_thread_info_response (threadinfo_pkt + 2, threadid,
info);
return result;
}
static int
adapt_remote_get_threadinfo (gdb_threadref *ref, int selection,
struct gdb_ext_thread_info *info)
{
threadref lclref;
int_to_threadref (&lclref, *ref);
return remote_get_threadinfo (&lclref, selection, info);
}
static char *
pack_threadlist_request (char *pkt, int startflag, int threadcount,
threadref *nextthread)
{
*pkt++ = 'q';
*pkt++ = 'L';
pkt = pack_nibble (pkt, startflag);
pkt = pack_hex_byte (pkt, threadcount);
pkt = pack_threadid (pkt, nextthread);
*pkt = '\0';
return pkt;
}
static int
parse_threadlist_response (char *pkt, int result_limit,
threadref *original_echo, threadref *resultlist,
int *doneflag)
{
char *limit;
int count, resultcount, done;
resultcount = 0;
limit = pkt + (PBUFSIZ - BUF_THREAD_ID_SIZE);
pkt = unpack_byte (pkt, &count);
pkt = unpack_nibble (pkt, &done);
pkt = unpack_threadid (pkt, original_echo);
while ((count-- > 0) && (pkt < limit))
{
pkt = unpack_threadid (pkt, resultlist++);
if (resultcount++ >= result_limit)
break;
}
if (doneflag)
*doneflag = done;
return resultcount;
}
static int
remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
int *done, int *result_count, threadref *threadlist)
{
static threadref echo_nextthread;
char *threadlist_packet = alloca (PBUFSIZ);
char *t_response = alloca (PBUFSIZ);
int result = 1;
if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= PBUFSIZ)
result_limit = (PBUFSIZ / BUF_THREAD_ID_SIZE) - 2;
pack_threadlist_request (threadlist_packet,
startflag, result_limit, nextthread);
putpkt (threadlist_packet);
getpkt (t_response, PBUFSIZ, 0);
*result_count =
parse_threadlist_response (t_response + 2, result_limit, &echo_nextthread,
threadlist, done);
if (!threadmatch (&echo_nextthread, nextthread))
{
warning ("HMM: threadlist did not echo arg thread, dropping it\n");
return 0;
}
if (*result_count <= 0)
{
if (*done != 1)
{
warning ("RMT ERROR : failed to get remote thread list\n");
result = 0;
}
return result;
}
if (*result_count > result_limit)
{
*result_count = 0;
warning ("RMT ERROR: threadlist response longer than requested\n");
return 0;
}
return result;
}
#define MAXTHREADLISTRESULTS 32
static int
remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
int looplimit)
{
int done, i, result_count;
int startflag = 1;
int result = 1;
int loopcount = 0;
static threadref nextthread;
static threadref resultthreadlist[MAXTHREADLISTRESULTS];
done = 0;
while (!done)
{
if (loopcount++ > looplimit)
{
result = 0;
warning ("Remote fetch threadlist -infinite loop-\n");
break;
}
if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS,
&done, &result_count, resultthreadlist))
{
result = 0;
break;
}
startflag = 0;
if (result_count >= 1)
copy_threadref (&nextthread, &resultthreadlist[result_count - 1]);
i = 0;
while (result_count--)
if (!(result = (*stepfunction) (&resultthreadlist[i++], context)))
break;
}
return result;
}
static int
remote_newthread_step (threadref *ref, void *context)
{
int pid;
pid = threadref_to_int (ref);
if (!in_thread_list (pid))
add_thread (pid);
return 1;
}
#define CRAZY_MAX_THREADS 1000
static int
remote_current_thread (int oldpid)
{
char *buf = alloca (PBUFSIZ);
putpkt ("qC");
getpkt (buf, PBUFSIZ, 0);
if (buf[0] == 'Q' && buf[1] == 'C')
return strtol (&buf[2], NULL, 16);
else
return oldpid;
}
static void
remote_find_new_threads (void)
{
remote_threadlist_iterator (remote_newthread_step, 0,
CRAZY_MAX_THREADS);
if (inferior_pid == MAGIC_NULL_PID)
inferior_pid = remote_current_thread (inferior_pid);
}
static void
remote_threads_info (void)
{
char *buf = alloca (PBUFSIZ);
char *bufp;
int tid;
if (remote_desc == 0)
error ("Command can only be used when connected to the remote target.");
if (use_threadinfo_query)
{
putpkt ("qfThreadInfo");
bufp = buf;
getpkt (bufp, PBUFSIZ, 0);
if (bufp[0] != '\0')
{
while (*bufp++ == 'm')
{
do
{
tid = strtol (bufp, &bufp, 16);
if (tid != 0 && !in_thread_list (tid))
add_thread (tid);
}
while (*bufp++ == ',');
putpkt ("qsThreadInfo");
bufp = buf;
getpkt (bufp, PBUFSIZ, 0);
}
return;
}
}
use_threadinfo_query = 0;
remote_find_new_threads ();
return;
}
static char *
remote_threads_extra_info (struct thread_info *tp)
{
int result;
int set;
threadref id;
struct gdb_ext_thread_info threadinfo;
static char display_buf[100];
char *bufp = alloca (PBUFSIZ);
int n = 0;
if (remote_desc == 0)
internal_error ("remote_threads_extra_info");
if (use_threadextra_query)
{
sprintf (bufp, "qThreadExtraInfo,%x", tp->pid);
putpkt (bufp);
getpkt (bufp, PBUFSIZ, 0);
if (bufp[0] != 0)
{
char *p;
for (p = display_buf;
p < display_buf + sizeof(display_buf) - 1 &&
bufp[0] != 0 &&
bufp[1] != 0;
p++, bufp+=2)
{
*p = fromhex (bufp[0]) * 16 + fromhex (bufp[1]);
}
*p = 0;
return display_buf;
}
}
use_threadextra_query = 0;
set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
| TAG_MOREDISPLAY | TAG_DISPLAY;
int_to_threadref (&id, tp->pid);
if (remote_get_threadinfo (&id, set, &threadinfo))
if (threadinfo.active)
{
if (*threadinfo.shortname)
n += sprintf(&display_buf[0], " Name: %s,", threadinfo.shortname);
if (*threadinfo.display)
n += sprintf(&display_buf[n], " State: %s,", threadinfo.display);
if (*threadinfo.more_display)
n += sprintf(&display_buf[n], " Priority: %s",
threadinfo.more_display);
if (n > 0)
{
if (',' == display_buf[n-1])
display_buf[n-1] = ' ';
return display_buf;
}
}
return NULL;
}
static void
extended_remote_restart (void)
{
char *buf = alloca (PBUFSIZ);
buf[0] = 'R';
sprintf (&buf[1], "%x", 0);
putpkt (buf);
putpkt ("?");
getpkt (buf, PBUFSIZ, 0);
}
static void
remote_close (int quitting)
{
if (remote_desc)
SERIAL_CLOSE (remote_desc);
remote_desc = NULL;
}
static void
get_offsets (void)
{
char *buf = alloca (PBUFSIZ);
char *ptr;
int lose;
CORE_ADDR text_addr, data_addr, bss_addr;
struct section_offsets *offs;
putpkt ("qOffsets");
getpkt (buf, PBUFSIZ, 0);
if (buf[0] == '\000')
return;
if (buf[0] == 'E')
{
warning ("Remote failure reply: %s", buf);
return;
}
text_addr = data_addr = bss_addr = 0;
ptr = buf;
lose = 0;
if (strncmp (ptr, "Text=", 5) == 0)
{
ptr += 5;
while (*ptr && *ptr != ';')
text_addr = (text_addr << 4) + fromhex (*ptr++);
}
else
lose = 1;
if (!lose && strncmp (ptr, ";Data=", 6) == 0)
{
ptr += 6;
while (*ptr && *ptr != ';')
data_addr = (data_addr << 4) + fromhex (*ptr++);
}
else
lose = 1;
if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
{
ptr += 5;
while (*ptr && *ptr != ';')
bss_addr = (bss_addr << 4) + fromhex (*ptr++);
}
else
lose = 1;
if (lose)
error ("Malformed response to offset query, %s", buf);
if (symfile_objfile == NULL)
return;
offs = (struct section_offsets *) alloca (SIZEOF_SECTION_OFFSETS);
memcpy (offs, symfile_objfile->section_offsets, SIZEOF_SECTION_OFFSETS);
offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
objfile_relocate (symfile_objfile, offs);
}
static int
remote_cisco_section_offsets (bfd_vma text_addr,
bfd_vma data_addr,
bfd_vma bss_addr,
bfd_signed_vma *text_offs,
bfd_signed_vma *data_offs,
bfd_signed_vma *bss_offs)
{
bfd_vma text_base, data_base, bss_base;
struct minimal_symbol *start;
asection *sect;
bfd *abfd;
int len;
char *p;
if (symfile_objfile == NULL)
return -1;
start = lookup_minimal_symbol ("_start", NULL, NULL);
if (start == NULL)
return -1;
data_base = bss_base = 0;
text_base = SYMBOL_VALUE_ADDRESS (start);
abfd = symfile_objfile->obfd;
for (sect = abfd->sections;
sect != 0;
sect = sect->next)
{
p = (unsigned char *) bfd_get_section_name (abfd, sect);
len = strlen (p);
if (strcmp (p + len - 4, "data") == 0)
if (data_base == 0 ||
data_base > bfd_get_section_vma (abfd, sect))
data_base = bfd_get_section_vma (abfd, sect);
if (strcmp (p + len - 3, "bss") == 0)
if (bss_base == 0 ||
bss_base > bfd_get_section_vma (abfd, sect))
bss_base = bfd_get_section_vma (abfd, sect);
}
*text_offs = text_addr - text_base;
*data_offs = data_addr - data_base;
*bss_offs = bss_addr - bss_base;
if (remote_debug)
{
char tmp[128];
sprintf (tmp, "VMA: text = 0x");
sprintf_vma (tmp + strlen (tmp), text_addr);
sprintf (tmp + strlen (tmp), " data = 0x");
sprintf_vma (tmp + strlen (tmp), data_addr);
sprintf (tmp + strlen (tmp), " bss = 0x");
sprintf_vma (tmp + strlen (tmp), bss_addr);
fprintf_filtered (gdb_stdlog, tmp);
fprintf_filtered (gdb_stdlog,
"Reloc offset: text = 0x%s data = 0x%s bss = 0x%s\n",
paddr_nz (*text_offs),
paddr_nz (*data_offs),
paddr_nz (*bss_offs));
}
return 0;
}
void
remote_cisco_objfile_relocate (bfd_signed_vma text_off, bfd_signed_vma data_off,
bfd_signed_vma bss_off)
{
struct section_offsets *offs;
if (text_off != 0 || data_off != 0 || bss_off != 0)
{
offs = (struct section_offsets *) alloca (SIZEOF_SECTION_OFFSETS);
memcpy (offs, symfile_objfile->section_offsets, SIZEOF_SECTION_OFFSETS);
offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_off;
offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_off;
offs->offsets[SECT_OFF_BSS (symfile_objfile)] = bss_off;
objfile_relocate (symfile_objfile, offs);
exec_set_section_offsets (text_off, data_off, bss_off);
}
}
static int
remote_start_remote_dummy (void *dummy)
{
start_remote ();
return 1;
}
static int
remote_start_remote (PTR dummy)
{
immediate_quit++;
SERIAL_WRITE (remote_desc, "+", 1);
set_thread (-1, 0);
inferior_pid = remote_current_thread (inferior_pid);
get_offsets ();
putpkt ("?");
immediate_quit--;
return remote_start_remote_dummy (dummy);
}
static void
remote_open (char *name, int from_tty)
{
remote_open_1 (name, from_tty, &remote_ops, 0);
}
static void
remote_async_open (char *name, int from_tty)
{
remote_async_open_1 (name, from_tty, &remote_async_ops, 0);
}
static void
extended_remote_open (char *name, int from_tty)
{
remote_open_1 (name, from_tty, &extended_remote_ops, 1 );
}
static void
extended_remote_async_open (char *name, int from_tty)
{
remote_async_open_1 (name, from_tty, &extended_async_remote_ops, 1 );
}
static void
init_all_packet_configs (void)
{
int i;
update_packet_config (&remote_protocol_P);
for (i = 0; i < NR_Z_PACKET_TYPES; i++)
update_packet_config (&remote_protocol_Z[i]);
update_packet_config (&remote_protocol_binary_download);
}
static void
remote_open_1 (char *name, int from_tty, struct target_ops *target,
int extended_p)
{
if (name == 0)
error ("To open a remote debug connection, you need to specify what\n\
serial device is attached to the remote system\n\
(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
wait_forever_enabled_p = 1;
target_preopen (from_tty);
unpush_target (target);
remote_desc = SERIAL_OPEN (name);
if (!remote_desc)
perror_with_name (name);
if (baud_rate != -1)
{
if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
{
SERIAL_CLOSE (remote_desc);
perror_with_name (name);
}
}
SERIAL_RAW (remote_desc);
SERIAL_FLUSH_INPUT (remote_desc);
if (from_tty)
{
puts_filtered ("Remote debugging using ");
puts_filtered (name);
puts_filtered ("\n");
}
push_target (target);
init_all_packet_configs ();
general_thread = -2;
continue_thread = -2;
use_threadinfo_query = 1;
use_threadextra_query = 1;
inferior_pid = MAGIC_NULL_PID;
if (!catch_errors (remote_start_remote, NULL,
"Couldn't establish connection to remote target\n",
RETURN_MASK_ALL))
{
pop_target ();
return;
}
if (extended_p)
{
char *buf = alloca (PBUFSIZ);
putpkt ("!");
getpkt (buf, PBUFSIZ, 0);
}
}
static void
remote_async_open_1 (char *name, int from_tty, struct target_ops *target,
int extended_p)
{
if (name == 0)
error ("To open a remote debug connection, you need to specify what\n\
serial device is attached to the remote system\n\
(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
target_preopen (from_tty);
unpush_target (target);
remote_desc = SERIAL_OPEN (name);
if (!remote_desc)
perror_with_name (name);
if (baud_rate != -1)
{
if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
{
SERIAL_CLOSE (remote_desc);
perror_with_name (name);
}
}
SERIAL_RAW (remote_desc);
SERIAL_FLUSH_INPUT (remote_desc);
if (from_tty)
{
puts_filtered ("Remote debugging using ");
puts_filtered (name);
puts_filtered ("\n");
}
push_target (target);
init_all_packet_configs ();
general_thread = -2;
continue_thread = -2;
use_threadinfo_query = 1;
use_threadextra_query = 1;
inferior_pid = MAGIC_NULL_PID;
remote_async_terminal_ours_p = 1;
wait_forever_enabled_p = 0;
if (!catch_errors (remote_start_remote, NULL,
"Couldn't establish connection to remote target\n",
RETURN_MASK_ALL))
{
pop_target ();
wait_forever_enabled_p = 1;
return;
}
wait_forever_enabled_p = 1;
if (extended_p)
{
char *buf = alloca (PBUFSIZ);
putpkt ("!");
getpkt (buf, PBUFSIZ, 0);
}
}
static void
remote_detach (char *args, int from_tty)
{
char *buf = alloca (PBUFSIZ);
if (args)
error ("Argument given to \"detach\" when remotely debugging.");
strcpy (buf, "D");
remote_send (buf, PBUFSIZ);
target_mourn_inferior ();
if (from_tty)
puts_filtered ("Ending remote debugging.\n");
}
static void
remote_async_detach (char *args, int from_tty)
{
char *buf = alloca (PBUFSIZ);
if (args)
error ("Argument given to \"detach\" when remotely debugging.");
strcpy (buf, "D");
remote_send (buf, PBUFSIZ);
if (target_is_async_p ())
SERIAL_ASYNC (remote_desc, NULL, 0);
target_mourn_inferior ();
if (from_tty)
puts_filtered ("Ending remote debugging.\n");
}
int
fromhex (int a)
{
if (a >= '0' && a <= '9')
return a - '0';
else if (a >= 'a' && a <= 'f')
return a - 'a' + 10;
else if (a >= 'A' && a <= 'F')
return a - 'A' + 10;
else
error ("Reply contains invalid hex digit %d", a);
}
static int
tohex (int nib)
{
if (nib < 10)
return '0' + nib;
else
return 'a' + nib - 10;
}
static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
static int last_sent_step;
static void
remote_resume (int pid, int step, enum target_signal siggnal)
{
char *buf = alloca (PBUFSIZ);
if (pid == -1)
set_thread (0, 0);
else
set_thread (pid, 0);
last_sent_signal = siggnal;
last_sent_step = step;
if (target_resume_hook)
(*target_resume_hook) ();
if (siggnal != TARGET_SIGNAL_0)
{
buf[0] = step ? 'S' : 'C';
buf[1] = tohex (((int) siggnal >> 4) & 0xf);
buf[2] = tohex ((int) siggnal & 0xf);
buf[3] = '\0';
}
else
strcpy (buf, step ? "s" : "c");
putpkt (buf);
}
static void
remote_async_resume (int pid, int step, enum target_signal siggnal)
{
char *buf = alloca (PBUFSIZ);
if (pid == -1)
set_thread (0, 0);
else
set_thread (pid, 0);
last_sent_signal = siggnal;
last_sent_step = step;
if (target_resume_hook)
(*target_resume_hook) ();
if (siggnal != TARGET_SIGNAL_0)
{
buf[0] = step ? 'S' : 'C';
buf[1] = tohex (((int) siggnal >> 4) & 0xf);
buf[2] = tohex ((int) siggnal & 0xf);
buf[3] = '\0';
}
else
strcpy (buf, step ? "s" : "c");
if (event_loop_p && target_can_async_p ())
target_async (inferior_event_handler, 0);
if (target_is_async_p ())
target_executing = 1;
putpkt (buf);
}
static void
initialize_sigint_signal_handler (void)
{
sigint_remote_token =
create_async_signal_handler (async_remote_interrupt, NULL);
signal (SIGINT, handle_remote_sigint);
}
static void
handle_remote_sigint (int sig)
{
signal (sig, handle_remote_sigint_twice);
sigint_remote_twice_token =
create_async_signal_handler (async_remote_interrupt_twice, NULL);
mark_async_signal_handler_wrapper (sigint_remote_token);
}
static void
handle_remote_sigint_twice (int sig)
{
signal (sig, handle_sigint);
sigint_remote_twice_token =
create_async_signal_handler (inferior_event_handler_wrapper, NULL);
mark_async_signal_handler_wrapper (sigint_remote_twice_token);
}
static void
async_remote_interrupt (gdb_client_data arg)
{
if (remote_debug)
fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
target_stop ();
}
void
async_remote_interrupt_twice (gdb_client_data arg)
{
if (remote_debug)
fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n");
if (target_executing)
{
interrupt_query ();
signal (SIGINT, handle_remote_sigint);
}
}
static void
cleanup_sigint_signal_handler (void *dummy)
{
signal (SIGINT, handle_sigint);
if (sigint_remote_twice_token)
delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_twice_token);
if (sigint_remote_token)
delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_token);
}
static void (*ofunc) (int);
static void
remote_interrupt (int signo)
{
signal (signo, remote_interrupt_twice);
if (remote_debug)
fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
target_stop ();
}
static void
remote_interrupt_twice (int signo)
{
signal (signo, ofunc);
interrupt_query ();
signal (signo, remote_interrupt);
}
static void
remote_stop (void)
{
if (remote_debug)
fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
if (remote_break)
SERIAL_SEND_BREAK (remote_desc);
else
SERIAL_WRITE (remote_desc, "\003", 1);
}
static void
interrupt_query (void)
{
target_terminal_ours ();
if (query ("Interrupted while waiting for the program.\n\
Give up (and stop debugging it)? "))
{
target_mourn_inferior ();
return_to_top_level (RETURN_QUIT);
}
target_terminal_inferior ();
}
static void
remote_async_terminal_inferior (void)
{
if (!sync_execution)
return;
if (!remote_async_terminal_ours_p)
return;
delete_file_handler (input_fd);
remote_async_terminal_ours_p = 0;
initialize_sigint_signal_handler ();
}
static void
remote_async_terminal_ours (void)
{
if (!sync_execution)
return;
if (remote_async_terminal_ours_p)
return;
cleanup_sigint_signal_handler (NULL);
add_file_handler (input_fd, stdin_event_handler, 0);
remote_async_terminal_ours_p = 1;
}
int kill_kludge;
void
remote_console_output (char *msg)
{
char *p;
for (p = msg; p[0] && p[1]; p += 2)
{
char tb[2];
char c = fromhex (p[0]) * 16 + fromhex (p[1]);
tb[0] = c;
tb[1] = 0;
fputs_unfiltered (tb, gdb_stdtarg);
}
gdb_flush (gdb_stdtarg);
}
static int
remote_wait (int pid, struct target_waitstatus *status)
{
unsigned char *buf = alloca (PBUFSIZ);
int thread_num = -1;
status->kind = TARGET_WAITKIND_EXITED;
status->value.integer = 0;
while (1)
{
unsigned char *p;
ofunc = signal (SIGINT, remote_interrupt);
getpkt (buf, PBUFSIZ, 1);
signal (SIGINT, ofunc);
if (target_wait_loop_hook)
(*target_wait_loop_hook) ();
switch (buf[0])
{
case 'E':
warning ("Remote failure reply: %s", buf);
continue;
case 'T':
{
int i;
long regno;
char regs[MAX_REGISTER_RAW_SIZE];
p = &buf[3];
while (*p)
{
unsigned char *p1;
char *p_temp;
regno = strtol ((const char *) p, &p_temp, 16);
p1 = (unsigned char *) p_temp;
if (p1 == p)
{
p1 = (unsigned char *) strchr ((const char *) p, ':');
if (p1 == NULL)
warning ("Malformed packet(a) (missing colon): %s\n\
Packet: '%s'\n",
p, buf);
if (strncmp ((const char *) p, "thread", p1 - p) == 0)
{
p_temp = unpack_varlen_hex (++p1, &thread_num);
record_currthread (thread_num);
p = (unsigned char *) p_temp;
}
}
else
{
p = p1;
if (*p++ != ':')
warning ("Malformed packet(b) (missing colon): %s\n\
Packet: '%s'\n",
p, buf);
if (regno >= NUM_REGS)
warning ("Remote sent bad register number %ld: %s\n\
Packet: '%s'\n",
regno, p, buf);
for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
{
if (p[0] == 0 || p[1] == 0)
warning ("Remote reply is too short: %s", buf);
regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
p += 2;
}
supply_register (regno, regs);
}
if (*p++ != ';')
{
warning ("Remote register badly formatted: %s", buf);
warning (" here: %s", p);
}
}
}
case 'S':
status->kind = TARGET_WAITKIND_STOPPED;
status->value.sig = (enum target_signal)
(((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
if (buf[3] == 'p')
{
if (cisco_kernel_mode == 1)
set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
value_from_string ("PDEBUG-"));
cisco_kernel_mode = 0;
thread_num = strtol ((const char *) &buf[4], NULL, 16);
record_currthread (thread_num);
}
else if (buf[3] == 'k')
{
if (cisco_kernel_mode == 1)
set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
value_from_string ("KDEBUG-"));
cisco_kernel_mode = 1;
}
goto got_status;
case 'N':
{
bfd_vma text_addr, data_addr, bss_addr;
bfd_signed_vma text_off, data_off, bss_off;
unsigned char *p1;
status->kind = TARGET_WAITKIND_STOPPED;
status->value.sig = (enum target_signal)
(((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
if (symfile_objfile == NULL)
{
warning ("Relocation packet received with no symbol file. \
Packet Dropped");
goto got_status;
}
p = &buf[3];
text_addr = strtoul (p, (char **) &p1, 16);
if (p1 == p || *p1 != ';')
warning ("Malformed relocation packet: Packet '%s'", buf);
p = p1 + 1;
data_addr = strtoul (p, (char **) &p1, 16);
if (p1 == p || *p1 != ';')
warning ("Malformed relocation packet: Packet '%s'", buf);
p = p1 + 1;
bss_addr = strtoul (p, (char **) &p1, 16);
if (p1 == p)
warning ("Malformed relocation packet: Packet '%s'", buf);
if (remote_cisco_section_offsets (text_addr, data_addr, bss_addr,
&text_off, &data_off, &bss_off)
== 0)
if (text_off != 0 || data_off != 0 || bss_off != 0)
remote_cisco_objfile_relocate (text_off, data_off, bss_off);
goto got_status;
}
case 'W':
{
status->kind = TARGET_WAITKIND_EXITED;
status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
goto got_status;
}
case 'X':
status->kind = TARGET_WAITKIND_SIGNALLED;
status->value.sig = (enum target_signal)
(((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
kill_kludge = 1;
goto got_status;
case 'O':
remote_console_output (buf + 1);
continue;
case '\0':
if (last_sent_signal != TARGET_SIGNAL_0)
{
target_terminal_ours_for_output ();
printf_filtered
("Can't send signals to this remote system. %s not sent.\n",
target_signal_to_name (last_sent_signal));
last_sent_signal = TARGET_SIGNAL_0;
target_terminal_inferior ();
strcpy ((char *) buf, last_sent_step ? "s" : "c");
putpkt ((char *) buf);
continue;
}
default:
warning ("Invalid remote reply: %s", buf);
continue;
}
}
got_status:
if (thread_num != -1)
{
return thread_num;
}
return inferior_pid;
}
static int
remote_async_wait (int pid, struct target_waitstatus *status)
{
unsigned char *buf = alloca (PBUFSIZ);
int thread_num = -1;
status->kind = TARGET_WAITKIND_EXITED;
status->value.integer = 0;
while (1)
{
unsigned char *p;
if (!target_is_async_p ())
ofunc = signal (SIGINT, remote_interrupt);
getpkt (buf, PBUFSIZ, wait_forever_enabled_p);
if (!target_is_async_p ())
signal (SIGINT, ofunc);
if (target_wait_loop_hook)
(*target_wait_loop_hook) ();
switch (buf[0])
{
case 'E':
warning ("Remote failure reply: %s", buf);
continue;
case 'T':
{
int i;
long regno;
char regs[MAX_REGISTER_RAW_SIZE];
p = &buf[3];
while (*p)
{
unsigned char *p1;
char *p_temp;
regno = strtol ((const char *) p, &p_temp, 16);
p1 = (unsigned char *) p_temp;
if (p1 == p)
{
p1 = (unsigned char *) strchr ((const char *) p, ':');
if (p1 == NULL)
warning ("Malformed packet(a) (missing colon): %s\n\
Packet: '%s'\n",
p, buf);
if (strncmp ((const char *) p, "thread", p1 - p) == 0)
{
p_temp = unpack_varlen_hex (++p1, &thread_num);
record_currthread (thread_num);
p = (unsigned char *) p_temp;
}
}
else
{
p = p1;
if (*p++ != ':')
warning ("Malformed packet(b) (missing colon): %s\n\
Packet: '%s'\n",
p, buf);
if (regno >= NUM_REGS)
warning ("Remote sent bad register number %ld: %s\n\
Packet: '%s'\n",
regno, p, buf);
for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
{
if (p[0] == 0 || p[1] == 0)
warning ("Remote reply is too short: %s", buf);
regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
p += 2;
}
supply_register (regno, regs);
}
if (*p++ != ';')
{
warning ("Remote register badly formatted: %s", buf);
warning (" here: %s", p);
}
}
}
case 'S':
status->kind = TARGET_WAITKIND_STOPPED;
status->value.sig = (enum target_signal)
(((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
if (buf[3] == 'p')
{
if (cisco_kernel_mode == 1)
set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
value_from_string ("PDEBUG-"));
cisco_kernel_mode = 0;
thread_num = strtol ((const char *) &buf[4], NULL, 16);
record_currthread (thread_num);
}
else if (buf[3] == 'k')
{
if (cisco_kernel_mode == 1)
set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
value_from_string ("KDEBUG-"));
cisco_kernel_mode = 1;
}
goto got_status;
case 'N':
{
bfd_vma text_addr, data_addr, bss_addr;
bfd_signed_vma text_off, data_off, bss_off;
unsigned char *p1;
status->kind = TARGET_WAITKIND_STOPPED;
status->value.sig = (enum target_signal)
(((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
if (symfile_objfile == NULL)
{
warning ("Relocation packet recieved with no symbol file. \
Packet Dropped");
goto got_status;
}
p = &buf[3];
text_addr = strtoul (p, (char **) &p1, 16);
if (p1 == p || *p1 != ';')
warning ("Malformed relocation packet: Packet '%s'", buf);
p = p1 + 1;
data_addr = strtoul (p, (char **) &p1, 16);
if (p1 == p || *p1 != ';')
warning ("Malformed relocation packet: Packet '%s'", buf);
p = p1 + 1;
bss_addr = strtoul (p, (char **) &p1, 16);
if (p1 == p)
warning ("Malformed relocation packet: Packet '%s'", buf);
if (remote_cisco_section_offsets (text_addr, data_addr, bss_addr,
&text_off, &data_off, &bss_off)
== 0)
if (text_off != 0 || data_off != 0 || bss_off != 0)
remote_cisco_objfile_relocate (text_off, data_off, bss_off);
goto got_status;
}
case 'W':
{
status->kind = TARGET_WAITKIND_EXITED;
status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
goto got_status;
}
case 'X':
status->kind = TARGET_WAITKIND_SIGNALLED;
status->value.sig = (enum target_signal)
(((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
kill_kludge = 1;
goto got_status;
case 'O':
remote_console_output (buf + 1);
status->kind = TARGET_WAITKIND_IGNORE;
goto got_status;
case '\0':
if (last_sent_signal != TARGET_SIGNAL_0)
{
target_terminal_ours_for_output ();
printf_filtered
("Can't send signals to this remote system. %s not sent.\n",
target_signal_to_name (last_sent_signal));
last_sent_signal = TARGET_SIGNAL_0;
target_terminal_inferior ();
strcpy ((char *) buf, last_sent_step ? "s" : "c");
putpkt ((char *) buf);
continue;
}
default:
warning ("Invalid remote reply: %s", buf);
continue;
}
}
got_status:
if (thread_num != -1)
{
return thread_num;
}
return inferior_pid;
}
static int register_bytes_found;
static void
remote_fetch_registers (int regno)
{
char *buf = alloca (PBUFSIZ);
int i;
char *p;
char regs[REGISTER_BYTES];
set_thread (inferior_pid, 1);
sprintf (buf, "g");
remote_send (buf, PBUFSIZ);
if (actual_register_packet_size == 0)
actual_register_packet_size = strlen (buf);
memset (regs, 0, REGISTER_BYTES);
while ((buf[0] < '0' || buf[0] > '9')
&& (buf[0] < 'a' || buf[0] > 'f')
&& buf[0] != 'x')
{
if (remote_debug)
fprintf_unfiltered (gdb_stdlog,
"Bad register packet; fetching a new packet\n");
getpkt (buf, PBUFSIZ, 0);
}
p = buf;
for (i = 0; i < REGISTER_BYTES; i++)
{
if (p[0] == 0)
break;
if (p[1] == 0)
{
warning ("Remote reply is of odd length: %s", buf);
goto supply_them;
}
if (p[0] == 'x' && p[1] == 'x')
regs[i] = 0;
else
regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
p += 2;
}
if (i != register_bytes_found)
{
register_bytes_found = i;
#ifdef REGISTER_BYTES_OK
if (!REGISTER_BYTES_OK (i))
warning ("Remote reply is too short: %s", buf);
#endif
}
supply_them:
for (i = 0; i < NUM_REGS; i++)
{
supply_register (i, ®s[REGISTER_BYTE (i)]);
if (buf[REGISTER_BYTE (i) * 2] == 'x')
register_valid[i] = -1;
}
}
static void
remote_prepare_to_store (void)
{
switch (remote_protocol_P.support)
{
case PACKET_DISABLE:
case PACKET_SUPPORT_UNKNOWN:
read_register_bytes (0, (char *) NULL, REGISTER_BYTES);
break;
case PACKET_ENABLE:
break;
}
}
static int
store_register_using_P (int regno)
{
char *buf = alloca (PBUFSIZ);
char *regp;
char *p;
int i;
sprintf (buf, "P%x=", regno);
p = buf + strlen (buf);
regp = ®isters[REGISTER_BYTE (regno)];
for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i)
{
*p++ = tohex ((regp[i] >> 4) & 0xf);
*p++ = tohex (regp[i] & 0xf);
}
*p = '\0';
remote_send (buf, PBUFSIZ);
return buf[0] != '\0';
}
static void
remote_store_registers (int regno)
{
char *buf = alloca (PBUFSIZ);
int i;
char *p;
set_thread (inferior_pid, 1);
if (regno >= 0)
{
switch (remote_protocol_P.support)
{
case PACKET_DISABLE:
break;
case PACKET_ENABLE:
if (store_register_using_P (regno))
return;
else
error ("Protocol error: P packet not recognized by stub");
case PACKET_SUPPORT_UNKNOWN:
if (store_register_using_P (regno))
{
remote_protocol_P.support = PACKET_ENABLE;
return;
}
else
{
remote_protocol_P.support = PACKET_DISABLE;
break;
}
}
}
buf[0] = 'G';
p = buf + 1;
for (i = 0; i < register_bytes_found; i++)
{
*p++ = tohex ((registers[i] >> 4) & 0xf);
*p++ = tohex (registers[i] & 0xf);
}
*p = '\0';
remote_send (buf, PBUFSIZ);
}
static int
hexnumlen (ULONGEST num)
{
int i;
for (i = 0; num != 0; i++)
num >>= 4;
return max (i, 1);
}
static int
hexnumstr (char *buf, ULONGEST num)
{
int len = hexnumlen (num);
return hexnumnstr (buf, num, len);
}
static int
hexnumnstr (char *buf, ULONGEST num, int width)
{
int i;
buf[width] = '\0';
for (i = width - 1; i >= 0; i--)
{
buf[i] = "0123456789abcdef"[(num & 0xf)];
num >>= 4;
}
return width;
}
static CORE_ADDR
remote_address_masked (CORE_ADDR addr)
{
if (remote_address_size > 0
&& remote_address_size < (sizeof (ULONGEST) * 8))
{
ULONGEST mask = 1;
mask = (mask << remote_address_size) - 1;
addr &= mask;
}
return addr;
}
static void
check_binary_download (CORE_ADDR addr)
{
switch (remote_protocol_binary_download.support)
{
case PACKET_DISABLE:
break;
case PACKET_ENABLE:
break;
case PACKET_SUPPORT_UNKNOWN:
{
char *buf = alloca (PBUFSIZ);
char *p;
p = buf;
*p++ = 'X';
p += hexnumstr (p, (ULONGEST) addr);
*p++ = ',';
p += hexnumstr (p, (ULONGEST) 0);
*p++ = ':';
*p = '\0';
putpkt_binary (buf, (int) (p - buf));
getpkt (buf, PBUFSIZ, 0);
if (buf[0] == '\0')
{
if (remote_debug)
fprintf_unfiltered (gdb_stdlog,
"binary downloading NOT suppported by target\n");
remote_protocol_binary_download.support = PACKET_DISABLE;
}
else
{
if (remote_debug)
fprintf_unfiltered (gdb_stdlog,
"binary downloading suppported by target\n");
remote_protocol_binary_download.support = PACKET_ENABLE;
}
break;
}
}
}
static int
remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
{
unsigned char *buf;
int max_buf_size;
unsigned char *p;
unsigned char *plen;
long sizeof_buf;
int plenlen;
int todo;
int nr_bytes;
check_binary_download (memaddr);
max_buf_size = get_memory_write_packet_size ();
sizeof_buf = max_buf_size + 1;
buf = alloca (sizeof_buf);
max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
p = buf;
switch (remote_protocol_binary_download.support)
{
case PACKET_ENABLE:
*p++ = 'X';
todo = min (len, max_buf_size);
break;
case PACKET_DISABLE:
*p++ = 'M';
todo = min (len, max_buf_size / 2);
break;
case PACKET_SUPPORT_UNKNOWN:
internal_error ("remote_write_bytes: bad switch");
}
memaddr = remote_address_masked (memaddr);
p += hexnumstr (p, (ULONGEST) memaddr);
*p++ = ',';
plen = p;
plenlen = hexnumstr (p, (ULONGEST) todo);
p += plenlen;
*p++ = ':';
*p = '\0';
switch (remote_protocol_binary_download.support)
{
case PACKET_ENABLE:
for (nr_bytes = 0;
(nr_bytes < todo) && (p - buf) < (max_buf_size - 2);
nr_bytes++)
{
switch (myaddr[nr_bytes] & 0xff)
{
case '$':
case '#':
case 0x7d:
*p++ = 0x7d;
*p++ = (myaddr[nr_bytes] & 0xff) ^ 0x20;
break;
default:
*p++ = myaddr[nr_bytes] & 0xff;
break;
}
}
if (nr_bytes < todo)
{
plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
*plen = ':';
}
break;
case PACKET_DISABLE:
for (nr_bytes = 0; nr_bytes < todo; nr_bytes++)
{
*p++ = tohex ((myaddr[nr_bytes] >> 4) & 0xf);
*p++ = tohex (myaddr[nr_bytes] & 0xf);
}
*p = '\0';
break;
case PACKET_SUPPORT_UNKNOWN:
internal_error ("remote_write_bytes: bad switch");
}
putpkt_binary (buf, (int) (p - buf));
getpkt (buf, sizeof_buf, 0);
if (buf[0] == 'E')
{
errno = EIO;
return 0;
}
return nr_bytes;
}
static int
remote_read_bytes (CORE_ADDR memaddr, char *myaddr, int len)
{
char *buf;
int max_buf_size;
long sizeof_buf;
int origlen;
max_buf_size = get_memory_read_packet_size ();
sizeof_buf = max_buf_size + 1;
buf = alloca (sizeof_buf);
origlen = len;
while (len > 0)
{
char *p;
int todo;
int i;
todo = min (len, max_buf_size / 2);
memaddr = remote_address_masked (memaddr);
p = buf;
*p++ = 'm';
p += hexnumstr (p, (ULONGEST) memaddr);
*p++ = ',';
p += hexnumstr (p, (ULONGEST) todo);
*p = '\0';
putpkt (buf);
getpkt (buf, sizeof_buf, 0);
if (buf[0] == 'E')
{
errno = EIO;
return 0;
}
p = buf;
for (i = 0; i < todo; i++)
{
if (p[0] == 0 || p[1] == 0)
return i + (origlen - len);
myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
p += 2;
}
myaddr += todo;
memaddr += todo;
len -= todo;
}
return origlen;
}
static int
remote_xfer_memory (CORE_ADDR mem_addr, char *buffer, int mem_len,
int should_write, struct target_ops *target)
{
CORE_ADDR targ_addr;
int targ_len;
int res;
REMOTE_TRANSLATE_XFER_ADDRESS (mem_addr, mem_len, &targ_addr, &targ_len);
if (targ_len <= 0)
return 0;
if (should_write)
res = remote_write_bytes (targ_addr, buffer, targ_len);
else
res = remote_read_bytes (targ_addr, buffer, targ_len);
return res;
}
#if 0
void
remote_search (int len, char *data, char *mask, CORE_ADDR startaddr,
int increment, CORE_ADDR lorange, CORE_ADDR hirange,
CORE_ADDR *addr_found, char *data_found)
{
if (increment == -4 && len == 4)
{
long mask_long, data_long;
long data_found_long;
CORE_ADDR addr_we_found;
char *buf = alloca (PBUFSIZ);
long returned_long[2];
char *p;
mask_long = extract_unsigned_integer (mask, len);
data_long = extract_unsigned_integer (data, len);
sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
putpkt (buf);
getpkt (buf, PBUFSIZ, 0);
if (buf[0] == '\0')
{
generic_search (len, data, mask, startaddr, increment, lorange,
hirange, addr_found, data_found);
return;
}
if (buf[0] == 'E')
memory_error (EIO, startaddr);
p = buf;
addr_we_found = 0;
while (*p != '\0' && *p != ',')
addr_we_found = (addr_we_found << 4) + fromhex (*p++);
if (*p == '\0')
error ("Protocol error: short return for search");
data_found_long = 0;
while (*p != '\0' && *p != ',')
data_found_long = (data_found_long << 4) + fromhex (*p++);
if (addr_we_found < lorange || addr_we_found >= hirange)
{
*addr_found = 0;
return;
}
*addr_found = addr_we_found;
*data_found = store_unsigned_integer (data_we_found, len);
return;
}
generic_search (len, data, mask, startaddr, increment, lorange,
hirange, addr_found, data_found);
}
#endif
static void
remote_files_info (struct target_ops *ignore)
{
puts_filtered ("Debugging a target over a serial line.\n");
}
static int
readchar (int timeout)
{
int ch;
ch = SERIAL_READCHAR (remote_desc, timeout);
if (ch >= 0)
return (ch & 0x7f);
switch ((enum serial_rc) ch)
{
case SERIAL_EOF:
target_mourn_inferior ();
error ("Remote connection closed");
case SERIAL_ERROR:
perror_with_name ("Remote communication error");
case SERIAL_TIMEOUT:
break;
}
return ch;
}
static void
remote_send (char *buf,
long sizeof_buf)
{
putpkt (buf);
getpkt (buf, sizeof_buf, 0);
if (buf[0] == 'E')
error ("Remote failure reply: %s", buf);
}
static void
print_packet (char *buf)
{
puts_filtered ("\"");
fputstr_filtered (buf, '"', gdb_stdout);
puts_filtered ("\"");
}
int
putpkt (char *buf)
{
return putpkt_binary (buf, strlen (buf));
}
static int
putpkt_binary (char *buf, int cnt)
{
int i;
unsigned char csum = 0;
char *buf2 = alloca (cnt + 6);
long sizeof_junkbuf = PBUFSIZ;
char *junkbuf = alloca (sizeof_junkbuf);
int ch;
int tcount = 0;
char *p;
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);
while (1)
{
int started_error_output = 0;
if (remote_debug)
{
*p = '\0';
fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
fprintf_unfiltered (gdb_stdlog, "...");
gdb_flush (gdb_stdlog);
}
if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
perror_with_name ("putpkt: write failed");
while (1)
{
ch = readchar (remote_timeout);
if (remote_debug)
{
switch (ch)
{
case '+':
case '-':
case SERIAL_TIMEOUT:
case '$':
if (started_error_output)
{
putchar_unfiltered ('\n');
started_error_output = 0;
}
}
}
switch (ch)
{
case '+':
if (remote_debug)
fprintf_unfiltered (gdb_stdlog, "Ack\n");
return 1;
case '-':
if (remote_debug)
fprintf_unfiltered (gdb_stdlog, "Nak\n");
case SERIAL_TIMEOUT:
tcount++;
if (tcount > 3)
return 0;
break;
case '$':
{
if (remote_debug)
fprintf_unfiltered (gdb_stdlog, "Packet instead of Ack, ignoring it\n");
read_frame (junkbuf, sizeof_junkbuf);
continue;
}
default:
if (remote_debug)
{
if (!started_error_output)
{
started_error_output = 1;
fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
}
fputc_unfiltered (ch & 0177, gdb_stdlog);
}
continue;
}
break;
}
#if 0
if (quit_flag)
{
quit_flag = 0;
interrupt_query ();
}
#endif
}
}
static int remote_cisco_mode;
static long
read_frame (char *buf,
long sizeof_buf)
{
unsigned char csum;
long bc;
int c;
csum = 0;
bc = 0;
while (1)
{
c = readchar (remote_timeout);
switch (c)
{
case SERIAL_TIMEOUT:
if (remote_debug)
fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
return -1;
case '$':
if (remote_debug)
fputs_filtered ("Saw new packet start in middle of old one\n",
gdb_stdlog);
return -1;
case '#':
{
unsigned char pktcsum;
int check_0 = 0;
int check_1 = 0;
buf[bc] = '\0';
check_0 = readchar (remote_timeout);
if (check_0 >= 0)
check_1 = readchar (remote_timeout);
if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
{
if (remote_debug)
fputs_filtered ("Timeout in checksum, retrying\n", gdb_stdlog);
return -1;
}
else if (check_0 < 0 || check_1 < 0)
{
if (remote_debug)
fputs_filtered ("Communication error in checksum\n", gdb_stdlog);
return -1;
}
pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
if (csum == pktcsum)
return bc;
if (remote_debug)
{
fprintf_filtered (gdb_stdlog,
"Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
pktcsum, csum);
fputs_filtered (buf, gdb_stdlog);
fputs_filtered ("\n", gdb_stdlog);
}
return -1;
}
case '*':
{
int repeat;
csum += c;
if (remote_cisco_mode == 0)
{
c = readchar (remote_timeout);
csum += c;
repeat = c - ' ' + 3;
}
else
{
c = readchar (remote_timeout);
csum += c;
repeat = fromhex (c) << 4;
c = readchar (remote_timeout);
csum += c;
repeat += fromhex (c);
}
if (repeat > 0 && repeat <= 255
&& bc > 0
&& bc + repeat < sizeof_buf - 1)
{
memset (&buf[bc], buf[bc - 1], repeat);
bc += repeat;
continue;
}
buf[bc] = '\0';
printf_filtered ("Repeat count %d too large for buffer: ", repeat);
puts_filtered (buf);
puts_filtered ("\n");
return -1;
}
default:
if (bc < sizeof_buf - 1)
{
buf[bc++] = c;
csum += c;
continue;
}
buf[bc] = '\0';
puts_filtered ("Remote packet too long: ");
puts_filtered (buf);
puts_filtered ("\n");
return -1;
}
}
}
void
getpkt (char *buf,
long sizeof_buf,
int forever)
{
int timed_out;
timed_out = getpkt_sane (buf, sizeof_buf, forever);
}
int
getpkt_sane (char *buf,
long sizeof_buf,
int forever)
{
int c;
int tries;
int timeout;
int val;
strcpy (buf, "timeout");
if (forever)
{
timeout = watchdog > 0 ? watchdog : -1;
}
else
timeout = remote_timeout;
#define MAX_TRIES 3
for (tries = 1; tries <= MAX_TRIES; tries++)
{
do
{
c = readchar (timeout);
if (c == SERIAL_TIMEOUT)
{
if (forever)
{
QUIT;
target_mourn_inferior ();
error ("Watchdog has expired. Target detached.\n");
}
if (remote_debug)
fputs_filtered ("Timed out.\n", gdb_stdlog);
goto retry;
}
}
while (c != '$');
val = read_frame (buf, sizeof_buf);
if (val >= 0)
{
if (remote_debug)
{
fprintf_unfiltered (gdb_stdlog, "Packet received: ");
fputstr_unfiltered (buf, 0, gdb_stdlog);
fprintf_unfiltered (gdb_stdlog, "\n");
}
SERIAL_WRITE (remote_desc, "+", 1);
return 0;
}
retry:
SERIAL_WRITE (remote_desc, "-", 1);
}
printf_unfiltered ("Ignoring packet error, continuing...\n");
SERIAL_WRITE (remote_desc, "+", 1);
return 1;
}
static void
remote_kill (void)
{
if (kill_kludge)
{
kill_kludge = 0;
target_mourn_inferior ();
return;
}
catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
target_mourn_inferior ();
}
static void
remote_async_kill (void)
{
if (target_is_async_p ())
SERIAL_ASYNC (remote_desc, NULL, 0);
if (kill_kludge)
{
kill_kludge = 0;
target_mourn_inferior ();
return;
}
catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
target_mourn_inferior ();
}
static void
remote_mourn (void)
{
remote_mourn_1 (&remote_ops);
}
static void
remote_async_mourn (void)
{
remote_mourn_1 (&remote_async_ops);
}
static void
extended_remote_mourn (void)
{
#if 0
remote_mourn_1 (&extended_remote_ops);
#endif
}
static void
remote_mourn_1 (struct target_ops *target)
{
unpush_target (target);
generic_mourn_inferior ();
}
static void
extended_remote_create_inferior (char *exec_file, char *args, char **env)
{
remove_breakpoints ();
extended_remote_restart ();
insert_breakpoints ();
clear_proceed_status ();
proceed (-1, TARGET_SIGNAL_0, 0);
}
static void
extended_remote_async_create_inferior (char *exec_file, char *args, char **env)
{
remove_breakpoints ();
if (event_loop_p && target_can_async_p ())
target_async (inferior_event_handler, 0);
extended_remote_restart ();
insert_breakpoints ();
clear_proceed_status ();
proceed (-1, TARGET_SIGNAL_0, 0);
}
#if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
#define REMOTE_BREAKPOINT
#endif
#ifdef REMOTE_BREAKPOINT
#if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
#define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
#define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
#endif
static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT;
static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT;
#endif
static int
remote_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
{
#ifdef REMOTE_BREAKPOINT
int val;
#endif
int bp_size;
if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE)
{
char *buf = alloca (PBUFSIZ);
char *p = buf;
addr = remote_address_masked (addr);
*(p++) = 'Z';
*(p++) = '0';
*(p++) = ',';
p += hexnumstr (p, (ULONGEST) addr);
BREAKPOINT_FROM_PC (&addr, &bp_size);
sprintf (p, ",%d", bp_size);
putpkt (buf);
getpkt (buf, PBUFSIZ, 0);
switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_SOFTWARE_BP]))
{
case PACKET_ERROR:
return -1;
case PACKET_OK:
return 0;
case PACKET_UNKNOWN:
break;
}
}
#ifdef REMOTE_BREAKPOINT
val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
if (val == 0)
{
if (TARGET_BYTE_ORDER == BIG_ENDIAN)
val = target_write_memory (addr, (char *) big_break_insn,
sizeof big_break_insn);
else
val = target_write_memory (addr, (char *) little_break_insn,
sizeof little_break_insn);
}
return val;
#else
return memory_insert_breakpoint (addr, contents_cache);
#endif
}
static int
remote_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
{
int bp_size;
if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE)
{
char *buf = alloca (PBUFSIZ);
char *p = buf;
*(p++) = 'z';
*(p++) = '0';
*(p++) = ',';
addr = remote_address_masked (addr);
p += hexnumstr (p, (ULONGEST) addr);
BREAKPOINT_FROM_PC (&addr, &bp_size);
sprintf (p, ",%d", bp_size);
putpkt (buf);
getpkt (buf, PBUFSIZ, 0);
return (buf[0] == 'E');
}
#ifdef REMOTE_BREAKPOINT
return target_write_memory (addr, contents_cache, sizeof big_break_insn);
#else
return memory_remove_breakpoint (addr, contents_cache);
#endif
}
static int
watchpoint_to_Z_packet (int type)
{
switch (type)
{
case hw_write:
return 2;
break;
case hw_read:
return 3;
break;
case hw_access:
return 4;
break;
default:
internal_error ("hw_bp_to_z: bad watchpoint type %d", type);
}
}
int
remote_insert_watchpoint (CORE_ADDR addr, int len, int type)
{
char *buf = alloca (PBUFSIZ);
char *p;
enum Z_packet_type packet = watchpoint_to_Z_packet (type);
if (remote_protocol_Z[packet].support == PACKET_DISABLE)
error ("Can't set hardware watchpoints without the '%s' (%s) packet\n",
remote_protocol_Z[packet].name,
remote_protocol_Z[packet].title);
sprintf (buf, "Z%x,", packet);
p = strchr (buf, '\0');
addr = remote_address_masked (addr);
p += hexnumstr (p, (ULONGEST) addr);
sprintf (p, ",%x", len);
putpkt (buf);
getpkt (buf, PBUFSIZ, 0);
switch (packet_ok (buf, &remote_protocol_Z[packet]))
{
case PACKET_ERROR:
case PACKET_UNKNOWN:
return -1;
case PACKET_OK:
return 0;
}
internal_error ("remote_insert_watchpoint: reached end of function");
}
int
remote_remove_watchpoint (CORE_ADDR addr, int len, int type)
{
char *buf = alloca (PBUFSIZ);
char *p;
enum Z_packet_type packet = watchpoint_to_Z_packet (type);
if (remote_protocol_Z[packet].support == PACKET_DISABLE)
error ("Can't clear hardware watchpoints without the '%s' (%s) packet\n",
remote_protocol_Z[packet].name,
remote_protocol_Z[packet].title);
sprintf (buf, "z%x,", packet);
p = strchr (buf, '\0');
addr = remote_address_masked (addr);
p += hexnumstr (p, (ULONGEST) addr);
sprintf (p, ",%x", len);
putpkt (buf);
getpkt (buf, PBUFSIZ, 0);
switch (packet_ok (buf, &remote_protocol_Z[packet]))
{
case PACKET_ERROR:
case PACKET_UNKNOWN:
return -1;
case PACKET_OK:
return 0;
}
internal_error ("remote_remove_watchpoint: reached end of function");
}
int
remote_insert_hw_breakpoint (CORE_ADDR addr, int len)
{
char *buf = alloca (PBUFSIZ);
char *p = buf;
if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE)
error ("Can't set hardware breakpoint without the '%s' (%s) packet\n",
remote_protocol_Z[Z_PACKET_HARDWARE_BP].name,
remote_protocol_Z[Z_PACKET_HARDWARE_BP].title);
*(p++) = 'Z';
*(p++) = '1';
*(p++) = ',';
addr = remote_address_masked (addr);
p += hexnumstr (p, (ULONGEST) addr);
*p = '\0';
putpkt (buf);
getpkt (buf, PBUFSIZ, 0);
switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP]))
{
case PACKET_ERROR:
case PACKET_UNKNOWN:
return -1;
case PACKET_OK:
return 0;
}
internal_error ("remote_remove_watchpoint: reached end of function");
}
int
remote_remove_hw_breakpoint (CORE_ADDR addr, int len)
{
char *buf = alloca (PBUFSIZ);
char *p = buf;
if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE)
error ("Can't clear hardware breakpoint without the '%s' (%s) packet\n",
remote_protocol_Z[Z_PACKET_HARDWARE_BP].name,
remote_protocol_Z[Z_PACKET_HARDWARE_BP].title);
*(p++) = 'z';
*(p++) = '1';
*(p++) = ',';
addr = remote_address_masked (addr);
p += hexnumstr (p, (ULONGEST) addr);
*p = '\0';
putpkt(buf);
getpkt (buf, PBUFSIZ, 0);
switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP]))
{
case PACKET_ERROR:
case PACKET_UNKNOWN:
return -1;
case PACKET_OK:
return 0;
}
internal_error ("remote_remove_watchpoint: reached end of function");
}
void
push_remote_target (char *name, int from_tty)
{
printf_filtered ("Switching to remote protocol\n");
remote_open (name, from_tty);
}
void
open_remote_target (char *name, int from_tty, struct target_ops *target,
int extended_p)
{
printf_filtered ("Selecting the %sremote protocol\n",
(extended_p ? "extended-" : ""));
remote_open_1 (name, from_tty, target, extended_p);
}
static unsigned long crc32_table[256] =
{0, 0};
static unsigned long
crc32 (unsigned char *buf, int len, unsigned int crc)
{
if (!crc32_table[1])
{
int i, j;
unsigned int c;
for (i = 0; i < 256; i++)
{
for (c = i << 24, j = 8; j > 0; --j)
c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
crc32_table[i] = c;
}
}
while (len--)
{
crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
buf++;
}
return crc;
}
static void
compare_sections_command (char *args, int from_tty)
{
asection *s;
unsigned long host_crc, target_crc;
extern bfd *exec_bfd;
struct cleanup *old_chain;
char *tmp;
char *sectdata;
char *sectname;
char *buf = alloca (PBUFSIZ);
bfd_size_type size;
bfd_vma lma;
int matched = 0;
int mismatched = 0;
if (!exec_bfd)
error ("command cannot be used without an exec file");
if (!current_target.to_shortname ||
strcmp (current_target.to_shortname, "remote") != 0)
error ("command can only be used with remote target");
for (s = exec_bfd->sections; s; s = s->next)
{
if (!(s->flags & SEC_LOAD))
continue;
size = bfd_get_section_size_before_reloc (s);
if (size == 0)
continue;
sectname = (char *) bfd_get_section_name (exec_bfd, s);
if (args && strcmp (args, sectname) != 0)
continue;
matched = 1;
lma = s->lma;
sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size);
putpkt (buf);
sectdata = xmalloc (size);
old_chain = make_cleanup (free, sectdata);
bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
getpkt (buf, PBUFSIZ, 0);
if (buf[0] == 'E')
error ("target memory fault, section %s, range 0x%08x -- 0x%08x",
sectname, lma, lma + size);
if (buf[0] != 'C')
error ("remote target does not support this operation");
for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++)
target_crc = target_crc * 16 + fromhex (*tmp);
printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
sectname, paddr (lma), paddr (lma + size));
if (host_crc == target_crc)
printf_filtered ("matched.\n");
else
{
printf_filtered ("MIS-MATCHED!\n");
mismatched++;
}
do_cleanups (old_chain);
}
if (mismatched > 0)
warning ("One or more sections of the remote executable does not match\n\
the loaded file\n");
if (args && !matched)
printf_filtered ("No loaded section named '%s'.\n", args);
}
static int
remote_query (int query_type, char *buf, char *outbuf, int *bufsiz)
{
int i;
char *buf2 = alloca (PBUFSIZ);
char *p2 = &buf2[0];
if (!bufsiz)
error ("null pointer to remote bufer size specified");
if (*bufsiz < PBUFSIZ)
{
*bufsiz = PBUFSIZ;
return -1;
}
if (!remote_desc)
error ("remote query is only available after target open");
if ((query_type < 'A') || (query_type > 'Z'))
error ("invalid remote query type");
if (!buf)
error ("null remote query specified");
if (!outbuf)
error ("remote query requires a buffer to receive data");
outbuf[0] = '\0';
*p2++ = 'q';
*p2++ = query_type;
i = 0;
while (buf[i] && (i < (PBUFSIZ - 8)))
{
if ((!isprint (buf[i])) || (buf[i] == '$') || (buf[i] == '#'))
error ("illegal characters in query string");
*p2++ = buf[i];
i++;
}
*p2 = buf[i];
if (buf[i])
error ("query larger than available buffer");
i = putpkt (buf2);
if (i < 0)
return i;
getpkt (outbuf, *bufsiz, 0);
return 0;
}
static void
remote_rcmd (char *command,
struct ui_file *outbuf)
{
int i;
char *buf = alloca (PBUFSIZ);
char *p = buf;
if (!remote_desc)
error ("remote rcmd is only available after target open");
if (command == NULL)
command = "";
strcpy (buf, "qRcmd,");
p = strchr (buf, '\0');
if ((strlen (buf) + strlen (command) * 2 + 8) > PBUFSIZ)
error ("\"monitor\" command ``%s'' is too long\n", command);
for (i = 0; command[i]; i++)
{
*p++ = tohex ((command[i] >> 4) & 0xf);
*p++ = tohex (command[i] & 0xf);
}
*p = '\0';
if (putpkt (buf) < 0)
error ("Communication problem with target\n");
while (1)
{
buf[0] = '\0';
getpkt (buf, PBUFSIZ, 0);
if (buf[0] == '\0')
error ("Target does not support this command\n");
if (buf[0] == 'O' && buf[1] != 'K')
{
remote_console_output (buf + 1);
continue;
}
if (strcmp (buf, "OK") == 0)
break;
if (strlen (buf) == 3 && buf[0] == 'E'
&& isdigit (buf[1]) && isdigit (buf[2]))
{
error ("Protocol error with Rcmd");
}
for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
{
char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
fputc_unfiltered (c, outbuf);
}
break;
}
}
static void
packet_command (char *args, int from_tty)
{
char *buf = alloca (PBUFSIZ);
if (!remote_desc)
error ("command can only be used with remote target");
if (!args)
error ("remote-packet command requires packet text as argument");
puts_filtered ("sending: ");
print_packet (args);
puts_filtered ("\n");
putpkt (args);
getpkt (buf, PBUFSIZ, 0);
puts_filtered ("received: ");
print_packet (buf);
puts_filtered ("\n");
}
#if 0
static void display_thread_info (struct gdb_ext_thread_info *info);
static void threadset_test_cmd (char *cmd, int tty);
static void threadalive_test (char *cmd, int tty);
static void threadlist_test_cmd (char *cmd, int tty);
int get_and_display_threadinfo (threadref * ref);
static void threadinfo_test_cmd (char *cmd, int tty);
static int thread_display_step (threadref * ref, void *context);
static void threadlist_update_test_cmd (char *cmd, int tty);
static void init_remote_threadtests (void);
#define SAMPLE_THREAD 0x05060708
static void
threadset_test_cmd (char *cmd, int tty)
{
int sample_thread = SAMPLE_THREAD;
printf_filtered ("Remote threadset test\n");
set_thread (sample_thread, 1);
}
static void
threadalive_test (char *cmd, int tty)
{
int sample_thread = SAMPLE_THREAD;
if (remote_thread_alive (sample_thread))
printf_filtered ("PASS: Thread alive test\n");
else
printf_filtered ("FAIL: Thread alive test\n");
}
void output_threadid (char *title, threadref * ref);
void
output_threadid (char *title, threadref *ref)
{
char hexid[20];
pack_threadid (&hexid[0], ref);
hexid[16] = 0;
printf_filtered ("%s %s\n", title, (&hexid[0]));
}
static void
threadlist_test_cmd (char *cmd, int tty)
{
int startflag = 1;
threadref nextthread;
int done, result_count;
threadref threadlist[3];
printf_filtered ("Remote Threadlist test\n");
if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
&result_count, &threadlist[0]))
printf_filtered ("FAIL: threadlist test\n");
else
{
threadref *scan = threadlist;
threadref *limit = scan + result_count;
while (scan < limit)
output_threadid (" thread ", scan++);
}
}
void
display_thread_info (struct gdb_ext_thread_info *info)
{
output_threadid ("Threadid: ", &info->threadid);
printf_filtered ("Name: %s\n ", info->shortname);
printf_filtered ("State: %s\n", info->display);
printf_filtered ("other: %s\n\n", info->more_display);
}
int
get_and_display_threadinfo (threadref *ref)
{
int result;
int set;
struct gdb_ext_thread_info threadinfo;
set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
| TAG_MOREDISPLAY | TAG_DISPLAY;
if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
display_thread_info (&threadinfo);
return result;
}
static void
threadinfo_test_cmd (char *cmd, int tty)
{
int athread = SAMPLE_THREAD;
threadref thread;
int set;
int_to_threadref (&thread, athread);
printf_filtered ("Remote Threadinfo test\n");
if (!get_and_display_threadinfo (&thread))
printf_filtered ("FAIL cannot get thread info\n");
}
static int
thread_display_step (threadref *ref, void *context)
{
return get_and_display_threadinfo (ref);
}
static void
threadlist_update_test_cmd (char *cmd, int tty)
{
printf_filtered ("Remote Threadlist update test\n");
remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
}
static void
init_remote_threadtests (void)
{
add_com ("tlist", class_obscure, threadlist_test_cmd,
"Fetch and print the remote list of thread identifiers, one pkt only");
add_com ("tinfo", class_obscure, threadinfo_test_cmd,
"Fetch and display info about one thread");
add_com ("tset", class_obscure, threadset_test_cmd,
"Test setting to a different thread");
add_com ("tupd", class_obscure, threadlist_update_test_cmd,
"Iterate through updating all remote thread info");
add_com ("talive", class_obscure, threadalive_test,
" Remote thread alive test ");
}
#endif
static void
init_remote_ops (void)
{
remote_ops.to_shortname = "remote";
remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
remote_ops.to_doc =
"Use a remote computer via a serial line, using a gdb-specific protocol.\n\
Specify the serial device it is connected to\n\
(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
remote_ops.to_open = remote_open;
remote_ops.to_close = remote_close;
remote_ops.to_detach = remote_detach;
remote_ops.to_resume = remote_resume;
remote_ops.to_wait = remote_wait;
remote_ops.to_fetch_registers = remote_fetch_registers;
remote_ops.to_store_registers = remote_store_registers;
remote_ops.to_prepare_to_store = remote_prepare_to_store;
remote_ops.to_xfer_memory = remote_xfer_memory;
remote_ops.to_files_info = remote_files_info;
remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
remote_ops.to_kill = remote_kill;
remote_ops.to_load = generic_load;
remote_ops.to_mourn_inferior = remote_mourn;
remote_ops.to_thread_alive = remote_thread_alive;
remote_ops.to_find_new_threads = remote_threads_info;
remote_ops.to_extra_thread_info = remote_threads_extra_info;
remote_ops.to_stop = remote_stop;
remote_ops.to_query = remote_query;
remote_ops.to_rcmd = remote_rcmd;
remote_ops.to_stratum = process_stratum;
remote_ops.to_has_all_memory = 1;
remote_ops.to_has_memory = 1;
remote_ops.to_has_stack = 1;
remote_ops.to_has_registers = 1;
remote_ops.to_has_execution = 1;
remote_ops.to_has_thread_control = tc_schedlock;
remote_ops.to_magic = OPS_MAGIC;
}
static void
init_extended_remote_ops (void)
{
extended_remote_ops = remote_ops;
extended_remote_ops.to_shortname = "extended-remote";
extended_remote_ops.to_longname =
"Extended remote serial target in gdb-specific protocol";
extended_remote_ops.to_doc =
"Use a remote computer via a serial line, using a gdb-specific protocol.\n\
Specify the serial device it is connected to (e.g. /dev/ttya).",
extended_remote_ops.to_open = extended_remote_open;
extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
}
static void
remote_info_process (char *args, int from_tty)
{
char *buf = alloca (PBUFSIZ);
if (remote_desc == 0)
error ("Command can only be used when connected to the remote target.");
putpkt ("qfProcessInfo");
getpkt (buf, PBUFSIZ, 0);
if (buf[0] == 0)
return;
if (buf[0] == 'E')
error ("info proc: target error.");
while (buf[0] == 'O')
{
remote_console_output (&buf[1]);
putpkt ("qsProcessInfo");
getpkt (buf, PBUFSIZ, 0);
}
}
static void
remote_cisco_open (char *name, int from_tty)
{
if (name == 0)
error (
"To open a remote debug connection, you need to specify what \n\
device is attached to the remote system (e.g. host:port).");
wait_forever_enabled_p = 1;
target_preopen (from_tty);
unpush_target (&remote_cisco_ops);
remote_desc = SERIAL_OPEN (name);
if (!remote_desc)
perror_with_name (name);
baud_rate = (baud_rate > 0) ? baud_rate : 9600;
if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
{
SERIAL_CLOSE (remote_desc);
perror_with_name (name);
}
SERIAL_RAW (remote_desc);
SERIAL_FLUSH_INPUT (remote_desc);
if (from_tty)
{
puts_filtered ("Remote debugging using ");
puts_filtered (name);
puts_filtered ("\n");
}
remote_cisco_mode = 1;
push_target (&remote_cisco_ops);
init_all_packet_configs ();
general_thread = -2;
continue_thread = -2;
use_threadinfo_query = 1;
use_threadextra_query = 1;
inferior_pid = MAGIC_NULL_PID;
if (!catch_errors (remote_start_remote_dummy, (char *) 0,
"Couldn't establish connection to remote target\n",
RETURN_MASK_ALL))
{
pop_target ();
return;
}
}
static void
remote_cisco_close (int quitting)
{
remote_cisco_mode = 0;
remote_close (quitting);
}
static void
remote_cisco_mourn (void)
{
remote_mourn_1 (&remote_cisco_ops);
}
enum
{
READ_MORE,
FATAL_ERROR,
ENTER_DEBUG,
DISCONNECT_TELNET
}
minitelnet_return;
static char *tty_input;
static int escape_count;
static int echo_check;
extern int quit_flag;
static int
readsocket (void)
{
int data;
while ((data = readchar (0)) >= 0)
{
if (data == '|')
{
if (++escape_count == 4)
{
return ENTER_DEBUG;
}
else
{
continue;
}
}
else
{
for (; escape_count > 0; escape_count--)
putchar ('|');
}
if (data == '\r')
continue;
if (echo_check != -1)
{
if (tty_input[echo_check] == data)
{
echo_check++;
continue;
}
else if ((data == '\n') && (tty_input[echo_check] == '\r'))
{
echo_check = -1;
continue;
}
else
{
int j;
for (j = 0; j < echo_check; j++)
putchar (tty_input[j]);
echo_check = -1;
}
}
putchar (data);
}
if (data == SERIAL_TIMEOUT)
return READ_MORE;
else
return FATAL_ERROR;
}
static int
readtty (void)
{
int tty_bytecount;
tty_bytecount = read (fileno (stdin), tty_input, sizeof (tty_input) - 1);
if (tty_bytecount == -1)
{
perror ("readtty: read failed");
return FATAL_ERROR;
}
if (tty_input[tty_bytecount - 1] == '\n' &&
tty_input[tty_bytecount - 2] == '\\')
{
tty_input[--tty_bytecount] = 0;
tty_input[--tty_bytecount] = 0;
}
if (tty_input[tty_bytecount - 1] == '\n')
tty_input[tty_bytecount - 1] = '\r';
if ((tty_input[0] == '~') && (tty_bytecount == 2))
return ENTER_DEBUG;
tty_input[tty_bytecount] = 0;
if (SERIAL_WRITE (remote_desc, tty_input, tty_bytecount))
{
perror_with_name ("readtty: write failed");
return FATAL_ERROR;
}
return READ_MORE;
}
static int
minitelnet (void)
{
fd_set input;
int tablesize;
int status;
int quit_count = 0;
extern int escape_count;
extern int echo_check;
escape_count = 0;
echo_check = -1;
tablesize = 8 * sizeof (input);
for (;;)
{
if ((status = readsocket ()) == FATAL_ERROR)
{
error ("Debugging terminated by communications error");
}
else if (status != READ_MORE)
{
return (status);
}
fflush (stdout);
FD_ZERO (&input);
FD_SET (fileno (stdin), &input);
FD_SET (DEPRECATED_SERIAL_FD (remote_desc), &input);
status = select (tablesize, &input, 0, 0, 0);
if ((status == -1) && (errno != EINTR))
{
error ("Communications error on select %d", errno);
}
if (quit_flag)
{
if ((++quit_count) == 2)
{
if (query ("Interrupt GDB? "))
{
printf_filtered ("Interrupted by user.\n");
return_to_top_level (RETURN_QUIT);
}
quit_count = 0;
}
quit_flag = 0;
if (remote_break)
SERIAL_SEND_BREAK (remote_desc);
else
SERIAL_WRITE (remote_desc, "\003", 1);
continue;
}
if (FD_ISSET (fileno (stdin), &input))
{
quit_count = 0;
echo_check = 0;
status = readtty ();
if (status == READ_MORE)
continue;
return status;
}
}
}
static int
remote_cisco_wait (int pid, struct target_waitstatus *status)
{
if (minitelnet () != ENTER_DEBUG)
{
error ("Debugging session terminated by protocol error");
}
putpkt ("?");
return remote_wait (pid, status);
}
static void
init_remote_cisco_ops (void)
{
remote_cisco_ops.to_shortname = "cisco";
remote_cisco_ops.to_longname = "Remote serial target in cisco-specific protocol";
remote_cisco_ops.to_doc =
"Use a remote machine via TCP, using a cisco-specific protocol.\n\
Specify the serial device it is connected to (e.g. host:2020).";
remote_cisco_ops.to_open = remote_cisco_open;
remote_cisco_ops.to_close = remote_cisco_close;
remote_cisco_ops.to_detach = remote_detach;
remote_cisco_ops.to_resume = remote_resume;
remote_cisco_ops.to_wait = remote_cisco_wait;
remote_cisco_ops.to_fetch_registers = remote_fetch_registers;
remote_cisco_ops.to_store_registers = remote_store_registers;
remote_cisco_ops.to_prepare_to_store = remote_prepare_to_store;
remote_cisco_ops.to_xfer_memory = remote_xfer_memory;
remote_cisco_ops.to_files_info = remote_files_info;
remote_cisco_ops.to_insert_breakpoint = remote_insert_breakpoint;
remote_cisco_ops.to_remove_breakpoint = remote_remove_breakpoint;
remote_cisco_ops.to_kill = remote_kill;
remote_cisco_ops.to_load = generic_load;
remote_cisco_ops.to_mourn_inferior = remote_cisco_mourn;
remote_cisco_ops.to_thread_alive = remote_thread_alive;
remote_cisco_ops.to_find_new_threads = remote_threads_info;
remote_ops.to_extra_thread_info = remote_threads_extra_info;
remote_cisco_ops.to_stratum = process_stratum;
remote_cisco_ops.to_has_all_memory = 1;
remote_cisco_ops.to_has_memory = 1;
remote_cisco_ops.to_has_stack = 1;
remote_cisco_ops.to_has_registers = 1;
remote_cisco_ops.to_has_execution = 1;
remote_cisco_ops.to_magic = OPS_MAGIC;
}
static int
remote_can_async_p (void)
{
return (current_target.to_async_mask_value) && SERIAL_CAN_ASYNC_P (remote_desc);
}
static int
remote_is_async_p (void)
{
return (current_target.to_async_mask_value) && SERIAL_IS_ASYNC_P (remote_desc);
}
static void (*async_client_callback) (enum inferior_event_type event_type, void *context);
static void *async_client_context;
static serial_event_ftype remote_async_serial_handler;
static void
remote_async_serial_handler (serial_t scb, void *context)
{
async_client_callback (INF_REG_EVENT, async_client_context);
}
static void
remote_async (void (*callback) (enum inferior_event_type event_type, void *context), void *context)
{
if (current_target.to_async_mask_value == 0)
internal_error ("Calling remote_async when async is masked");
if (callback != NULL)
{
SERIAL_ASYNC (remote_desc, remote_async_serial_handler, NULL);
async_client_callback = callback;
async_client_context = context;
}
else
SERIAL_ASYNC (remote_desc, NULL, NULL);
}
static void
init_remote_async_ops (void)
{
remote_async_ops.to_shortname = "async";
remote_async_ops.to_longname = "Remote serial target in async version of the gdb-specific protocol";
remote_async_ops.to_doc =
"Use a remote computer via a serial line, using a gdb-specific protocol.\n\
Specify the serial device it is connected to (e.g. /dev/ttya).";
remote_async_ops.to_open = remote_async_open;
remote_async_ops.to_close = remote_close;
remote_async_ops.to_detach = remote_async_detach;
remote_async_ops.to_resume = remote_async_resume;
remote_async_ops.to_wait = remote_async_wait;
remote_async_ops.to_fetch_registers = remote_fetch_registers;
remote_async_ops.to_store_registers = remote_store_registers;
remote_async_ops.to_prepare_to_store = remote_prepare_to_store;
remote_async_ops.to_xfer_memory = remote_xfer_memory;
remote_async_ops.to_files_info = remote_files_info;
remote_async_ops.to_insert_breakpoint = remote_insert_breakpoint;
remote_async_ops.to_remove_breakpoint = remote_remove_breakpoint;
remote_async_ops.to_terminal_inferior = remote_async_terminal_inferior;
remote_async_ops.to_terminal_ours = remote_async_terminal_ours;
remote_async_ops.to_kill = remote_async_kill;
remote_async_ops.to_load = generic_load;
remote_async_ops.to_mourn_inferior = remote_async_mourn;
remote_async_ops.to_thread_alive = remote_thread_alive;
remote_async_ops.to_find_new_threads = remote_threads_info;
remote_ops.to_extra_thread_info = remote_threads_extra_info;
remote_async_ops.to_stop = remote_stop;
remote_async_ops.to_query = remote_query;
remote_async_ops.to_rcmd = remote_rcmd;
remote_async_ops.to_stratum = process_stratum;
remote_async_ops.to_has_all_memory = 1;
remote_async_ops.to_has_memory = 1;
remote_async_ops.to_has_stack = 1;
remote_async_ops.to_has_registers = 1;
remote_async_ops.to_has_execution = 1;
remote_async_ops.to_has_thread_control = tc_schedlock;
remote_async_ops.to_can_async_p = remote_can_async_p;
remote_async_ops.to_is_async_p = remote_is_async_p;
remote_async_ops.to_async = remote_async;
remote_async_ops.to_async_mask_value = 1;
remote_async_ops.to_magic = OPS_MAGIC;
}
static void
init_extended_async_remote_ops (void)
{
extended_async_remote_ops = remote_async_ops;
extended_async_remote_ops.to_shortname = "extended-async";
extended_async_remote_ops.to_longname =
"Extended remote serial target in async gdb-specific protocol";
extended_async_remote_ops.to_doc =
"Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
Specify the serial device it is connected to (e.g. /dev/ttya).",
extended_async_remote_ops.to_open = extended_remote_async_open;
extended_async_remote_ops.to_create_inferior = extended_remote_async_create_inferior;
extended_async_remote_ops.to_mourn_inferior = extended_remote_mourn;
}
static void
set_remote_cmd (char *args, int from_tty)
{
}
static void
show_remote_cmd (char *args, int from_tty)
{
show_remote_protocol_Z_packet_cmd (args, from_tty);
show_remote_protocol_P_packet_cmd (args, from_tty);
show_remote_protocol_binary_download_cmd (args, from_tty);
}
static void
build_remote_gdbarch_data (void)
{
build_remote_packet_sizes ();
tty_input = xmalloc (PBUFSIZ);
remote_address_size = TARGET_PTR_BIT;
}
void
_initialize_remote (void)
{
static struct cmd_list_element *remote_set_cmdlist;
static struct cmd_list_element *remote_show_cmdlist;
struct cmd_list_element *tmpcmd;
build_remote_gdbarch_data ();
register_gdbarch_swap (&tty_input, sizeof (&tty_input), NULL);
register_remote_packet_sizes ();
register_gdbarch_swap (&remote_address_size,
sizeof (&remote_address_size), NULL);
register_gdbarch_swap (NULL, 0, build_remote_gdbarch_data);
init_remote_ops ();
add_target (&remote_ops);
init_extended_remote_ops ();
add_target (&extended_remote_ops);
init_remote_async_ops ();
add_target (&remote_async_ops);
init_extended_async_remote_ops ();
add_target (&extended_async_remote_ops);
init_remote_cisco_ops ();
add_target (&remote_cisco_ops);
#if 0
init_remote_threadtests ();
#endif
add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, "\
Remote protocol specific variables\n\
Configure various remote-protocol specific variables such as\n\
the packets being used",
&remote_set_cmdlist, "set remote ",
0, &setlist);
add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, "\
Remote protocol specific variables\n\
Configure various remote-protocol specific variables such as\n\
the packets being used",
&remote_show_cmdlist, "show remote ",
0, &showlist);
add_cmd ("compare-sections", class_obscure, compare_sections_command,
"Compare section data on target to the exec file.\n\
Argument is a single section name (default: all loaded sections).",
&cmdlist);
add_cmd ("packet", class_maintenance, packet_command,
"Send an arbitrary packet to a remote target.\n\
maintenance packet TEXT\n\
If GDB is talking to an inferior via the GDB serial protocol, then\n\
this command sends the string TEXT to the inferior, and displays the\n\
response packet. GDB supplies the initial `$' character, and the\n\
terminating `#' character and checksum.",
&maintenancelist);
add_show_from_set
(add_set_cmd ("remotebreak", no_class,
var_boolean, (char *) &remote_break,
"Set whether to send break if interrupted.\n",
&setlist),
&showlist);
add_cmd ("remotewritesize", no_class, set_memory_write_packet_size,
"Set the maximum number of bytes per memory write packet (deprecated).\n",
&setlist);
add_cmd ("remotewritesize", no_class, set_memory_write_packet_size,
"Show the maximum number of bytes per memory write packet (deprecated).\n",
&showlist);
add_cmd ("memory-write-packet-size", no_class,
set_memory_write_packet_size,
"Set the maximum number of bytes per memory-write packet.\n"
"Specify the number of bytes in a packet or 0 (zero) for the\n"
"default packet size. The actual limit is further reduced\n"
"dependent on the target. Specify ``fixed'' to disable the\n"
"further restriction and ``limit'' to enable that restriction\n",
&remote_set_cmdlist);
add_cmd ("memory-read-packet-size", no_class,
set_memory_read_packet_size,
"Set the maximum number of bytes per memory-read packet.\n"
"Specify the number of bytes in a packet or 0 (zero) for the\n"
"default packet size. The actual limit is further reduced\n"
"dependent on the target. Specify ``fixed'' to disable the\n"
"further restriction and ``limit'' to enable that restriction\n",
&remote_set_cmdlist);
add_cmd ("memory-write-packet-size", no_class,
show_memory_write_packet_size,
"Show the maximum number of bytes per memory-write packet.\n",
&remote_show_cmdlist);
add_cmd ("memory-read-packet-size", no_class,
show_memory_read_packet_size,
"Show the maximum number of bytes per memory-read packet.\n",
&remote_show_cmdlist);
add_show_from_set
(add_set_cmd ("remoteaddresssize", class_obscure,
var_integer, (char *) &remote_address_size,
"Set the maximum size of the address (in bits) \
in a memory packet.\n",
&setlist),
&showlist);
add_packet_config_cmd (&remote_protocol_binary_download,
"X", "binary-download",
set_remote_protocol_binary_download_cmd,
show_remote_protocol_binary_download_cmd,
&remote_set_cmdlist, &remote_show_cmdlist,
1);
#if 0
add_show_from_set
(add_set_cmd ("remotebinarydownload", no_class,
var_boolean, (char *) &remote_binary_download,
"Set binary downloads.\n", &setlist),
&showlist);
#endif
add_info ("remote-process", remote_info_process,
"Query the remote system for process info.");
add_packet_config_cmd (&remote_protocol_P,
"P", "set-register",
set_remote_protocol_P_packet_cmd,
show_remote_protocol_P_packet_cmd,
&remote_set_cmdlist, &remote_show_cmdlist,
1);
add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP],
"Z0", "software-breakpoint",
set_remote_protocol_Z_software_bp_packet_cmd,
show_remote_protocol_Z_software_bp_packet_cmd,
&remote_set_cmdlist, &remote_show_cmdlist,
0);
add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_HARDWARE_BP],
"Z1", "hardware-breakpoint",
set_remote_protocol_Z_hardware_bp_packet_cmd,
show_remote_protocol_Z_hardware_bp_packet_cmd,
&remote_set_cmdlist, &remote_show_cmdlist,
0);
add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_WRITE_WP],
"Z2", "write-watchpoint",
set_remote_protocol_Z_write_wp_packet_cmd,
show_remote_protocol_Z_write_wp_packet_cmd,
&remote_set_cmdlist, &remote_show_cmdlist,
0);
add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_READ_WP],
"Z3", "read-watchpoint",
set_remote_protocol_Z_read_wp_packet_cmd,
show_remote_protocol_Z_read_wp_packet_cmd,
&remote_set_cmdlist, &remote_show_cmdlist,
0);
add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_ACCESS_WP],
"Z4", "access-watchpoint",
set_remote_protocol_Z_access_wp_packet_cmd,
show_remote_protocol_Z_access_wp_packet_cmd,
&remote_set_cmdlist, &remote_show_cmdlist,
0);
tmpcmd = add_set_auto_boolean_cmd ("Z-packet", class_obscure,
&remote_Z_packet_detect,
"\
Set use of remote protocol `Z' packets", &remote_set_cmdlist);
tmpcmd->function.sfunc = set_remote_protocol_Z_packet_cmd;
add_cmd ("Z-packet", class_obscure, show_remote_protocol_Z_packet_cmd,
"Show use of remote protocol `Z' packets ",
&remote_show_cmdlist);
}