* utils.c (xmalloc,xcalloc,xstrdup): New fns.

* server.h (ATTR_MALLOC): New macro.
	(xmalloc,xcalloc,xstrdup): Declare.
	* hostio.c: Replace malloc,calloc,strdup with xmalloc,xcalloc,xstrdup.
	* inferiors.c: Ditto.
	* linux-low.c: Ditto.
	* mem-break.c: Ditto.
	* regcache.c: Ditto.
	* remote-utils.c: Ditto.
	* server.c: Ditto.
	* target.c: Ditto.
	* win32-low.c: Ditto.
This commit is contained in:
Doug Evans
2008-12-14 20:51:04 +00:00
parent 023eb17ca2
commit bca929d3a6
12 changed files with 125 additions and 42 deletions

View File

@ -1,3 +1,18 @@
2008-12-13 Doug Evans <dje@google.com>
* utils.c (xmalloc,xcalloc,xstrdup): New fns.
* server.h (ATTR_MALLOC): New macro.
(xmalloc,xcalloc,xstrdup): Declare.
* hostio.c: Replace malloc,calloc,strdup with xmalloc,xcalloc,xstrdup.
* inferiors.c: Ditto.
* linux-low.c: Ditto.
* mem-break.c: Ditto.
* regcache.c: Ditto.
* remote-utils.c: Ditto.
* server.c: Ditto.
* target.c: Ditto.
* win32-low.c: Ditto.
2008-12-12 Doug Evans <dje@google.com> 2008-12-12 Doug Evans <dje@google.com>
* linux-low.c (linux_wait_for_process): Don't clobber current_inferior * linux-low.c (linux_wait_for_process): Don't clobber current_inferior

View File

@ -116,7 +116,7 @@ require_data (char *p, int p_len, char **data, int *data_len)
{ {
int input_index, output_index, escaped; int input_index, output_index, escaped;
*data = malloc (p_len); *data = xmalloc (p_len);
output_index = 0; output_index = 0;
escaped = 0; escaped = 0;
@ -295,7 +295,7 @@ handle_open (char *own_buf)
} }
/* Record the new file descriptor. */ /* Record the new file descriptor. */
new_fd = malloc (sizeof (struct fd_list)); new_fd = xmalloc (sizeof (struct fd_list));
new_fd->fd = fd; new_fd->fd = fd;
new_fd->next = open_fds; new_fd->next = open_fds;
open_fds = new_fd; open_fds = new_fd;
@ -323,7 +323,7 @@ handle_pread (char *own_buf, int *new_packet_len)
return; return;
} }
data = malloc (len); data = xmalloc (len);
#ifdef HAVE_PREAD #ifdef HAVE_PREAD
ret = pread (fd, data, len, offset); ret = pread (fd, data, len, offset);
#else #else

View File

@ -95,7 +95,7 @@ remove_inferior (struct inferior_list *list,
void void
add_thread (unsigned long thread_id, void *target_data, unsigned int gdb_id) add_thread (unsigned long thread_id, void *target_data, unsigned int gdb_id)
{ {
struct thread_info *new_thread = malloc (sizeof (*new_thread)); struct thread_info *new_thread = xmalloc (sizeof (*new_thread));
memset (new_thread, 0, sizeof (*new_thread)); memset (new_thread, 0, sizeof (*new_thread));
@ -261,12 +261,12 @@ match_dll (struct inferior_list_entry *inf, void *arg)
void void
loaded_dll (const char *name, CORE_ADDR base_addr) loaded_dll (const char *name, CORE_ADDR base_addr)
{ {
struct dll_info *new_dll = malloc (sizeof (*new_dll)); struct dll_info *new_dll = xmalloc (sizeof (*new_dll));
memset (new_dll, 0, sizeof (*new_dll)); memset (new_dll, 0, sizeof (*new_dll));
new_dll->entry.id = -1; new_dll->entry.id = -1;
new_dll->name = strdup (name); new_dll->name = xstrdup (name);
new_dll->base_addr = base_addr; new_dll->base_addr = base_addr;
add_inferior_to_list (&all_dlls, &new_dll->entry); add_inferior_to_list (&all_dlls, &new_dll->entry);
@ -314,7 +314,7 @@ add_pid_to_list (struct inferior_list *list, unsigned long pid)
{ {
struct inferior_list_entry *new_entry; struct inferior_list_entry *new_entry;
new_entry = malloc (sizeof (struct inferior_list_entry)); new_entry = xmalloc (sizeof (struct inferior_list_entry));
new_entry->id = pid; new_entry->id = pid;
add_inferior_to_list (list, new_entry); add_inferior_to_list (list, new_entry);
} }

View File

@ -249,7 +249,7 @@ add_process (unsigned long pid)
{ {
struct process_info *process; struct process_info *process;
process = (struct process_info *) malloc (sizeof (*process)); process = (struct process_info *) xmalloc (sizeof (*process));
memset (process, 0, sizeof (*process)); memset (process, 0, sizeof (*process));
process->head.id = pid; process->head.id = pid;
@ -1120,7 +1120,7 @@ linux_resume_one_process (struct inferior_list_entry *entry,
|| process->bp_reinsert != 0)) || process->bp_reinsert != 0))
{ {
struct pending_signals *p_sig; struct pending_signals *p_sig;
p_sig = malloc (sizeof (*p_sig)); p_sig = xmalloc (sizeof (*p_sig));
p_sig->prev = process->pending_signals; p_sig->prev = process->pending_signals;
p_sig->signal = signal; p_sig->signal = signal;
if (info == NULL) if (info == NULL)
@ -1291,7 +1291,7 @@ linux_queue_one_thread (struct inferior_list_entry *entry)
if (process->resume->sig != 0) if (process->resume->sig != 0)
{ {
struct pending_signals *p_sig; struct pending_signals *p_sig;
p_sig = malloc (sizeof (*p_sig)); p_sig = xmalloc (sizeof (*p_sig));
p_sig->prev = process->pending_signals; p_sig->prev = process->pending_signals;
p_sig->signal = process->resume->sig; p_sig->signal = process->resume->sig;
memset (&p_sig->info, 0, sizeof (siginfo_t)); memset (&p_sig->info, 0, sizeof (siginfo_t));
@ -1527,7 +1527,7 @@ regsets_fetch_inferior_registers ()
continue; continue;
} }
buf = malloc (regset->size); buf = xmalloc (regset->size);
#ifndef __sparc__ #ifndef __sparc__
res = ptrace (regset->get_request, inferior_pid, 0, buf); res = ptrace (regset->get_request, inferior_pid, 0, buf);
#else #else
@ -1580,7 +1580,7 @@ regsets_store_inferior_registers ()
continue; continue;
} }
buf = malloc (regset->size); buf = xmalloc (regset->size);
/* First fill the buffer with the current register set contents, /* First fill the buffer with the current register set contents,
in case there are any items in the kernel's regset that are in case there are any items in the kernel's regset that are
@ -1832,7 +1832,7 @@ linux_test_for_tracefork (void)
{ {
int child_pid, ret, status; int child_pid, ret, status;
long second_pid; long second_pid;
char *stack = malloc (STACK_SIZE * 4); char *stack = xmalloc (STACK_SIZE * 4);
linux_supports_tracefork_flag = 0; linux_supports_tracefork_flag = 0;
@ -2212,6 +2212,6 @@ initialize_low (void)
#ifdef HAVE_LINUX_REGSETS #ifdef HAVE_LINUX_REGSETS
for (num_regsets = 0; target_regsets[num_regsets].size >= 0; num_regsets++) for (num_regsets = 0; target_regsets[num_regsets].size >= 0; num_regsets++)
; ;
disabled_regsets = malloc (num_regsets); disabled_regsets = xmalloc (num_regsets);
#endif #endif
} }

View File

@ -55,7 +55,7 @@ set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR))
if (breakpoint_data == NULL) if (breakpoint_data == NULL)
error ("Target does not support breakpoints."); error ("Target does not support breakpoints.");
bp = malloc (sizeof (struct breakpoint)); bp = xmalloc (sizeof (struct breakpoint));
memset (bp, 0, sizeof (struct breakpoint)); memset (bp, 0, sizeof (struct breakpoint));
(*the_target->read_memory) (where, bp->old_data, (*the_target->read_memory) (where, bp->old_data,

View File

@ -94,12 +94,12 @@ new_register_cache (void)
if (register_bytes == 0) if (register_bytes == 0)
return NULL; /* The architecture hasn't been initialized yet. */ return NULL; /* The architecture hasn't been initialized yet. */
regcache = malloc (sizeof (*regcache)); regcache = xmalloc (sizeof (*regcache));
/* Make sure to zero-initialize the register cache when it is created, /* Make sure to zero-initialize the register cache when it is created,
in case there are registers the target never fetches. This way they'll in case there are registers the target never fetches. This way they'll
read as zero instead of garbage. */ read as zero instead of garbage. */
regcache->registers = calloc (1, register_bytes); regcache->registers = xcalloc (1, register_bytes);
if (regcache->registers == NULL) if (regcache->registers == NULL)
fatal ("Could not allocate register cache."); fatal ("Could not allocate register cache.");

View File

@ -530,7 +530,7 @@ putpkt_binary (char *buf, int cnt)
char buf3[1]; char buf3[1];
char *p; char *p;
buf2 = malloc (PBUFSIZ); buf2 = xmalloc (PBUFSIZ);
/* Copy the packet into buffer BUF2, encapsulating it /* Copy the packet into buffer BUF2, encapsulating it
and giving it a checksum. */ and giving it a checksum. */
@ -1178,7 +1178,7 @@ look_up_one_symbol (const char *name, CORE_ADDR *addrp)
unsigned int mem_len; unsigned int mem_len;
decode_m_packet (&own_buf[1], &mem_addr, &mem_len); decode_m_packet (&own_buf[1], &mem_addr, &mem_len);
mem_buf = malloc (mem_len); mem_buf = xmalloc (mem_len);
if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0) if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
convert_int_to_ascii (mem_buf, own_buf, mem_len); convert_int_to_ascii (mem_buf, own_buf, mem_len);
else else
@ -1209,8 +1209,8 @@ look_up_one_symbol (const char *name, CORE_ADDR *addrp)
decode_address (addrp, p, q - p); decode_address (addrp, p, q - p);
/* Save the symbol in our cache. */ /* Save the symbol in our cache. */
sym = malloc (sizeof (*sym)); sym = xmalloc (sizeof (*sym));
sym->name = strdup (name); sym->name = xstrdup (name);
sym->addr = *addrp; sym->addr = *addrp;
sym->next = symbol_cache; sym->next = symbol_cache;
symbol_cache = sym; symbol_cache = sym;
@ -1221,7 +1221,7 @@ look_up_one_symbol (const char *name, CORE_ADDR *addrp)
void void
monitor_output (const char *msg) monitor_output (const char *msg)
{ {
char *buf = malloc (strlen (msg) * 2 + 2); char *buf = xmalloc (strlen (msg) * 2 + 2);
buf[0] = 'O'; buf[0] = 'O';
hexify (buf + 1, msg, 0); hexify (buf + 1, msg, 0);
@ -1259,7 +1259,7 @@ xml_escape_text (const char *text)
} }
/* Expand the result. */ /* Expand the result. */
result = malloc (i + special + 1); result = xmalloc (i + special + 1);
for (i = 0, special = 0; text[i] != '\0'; i++) for (i = 0, special = 0; text[i] != '\0'; i++)
switch (text[i]) switch (text[i])
{ {

View File

@ -436,7 +436,7 @@ handle_search_memory (char *own_buf, int packet_len)
CORE_ADDR found_addr; CORE_ADDR found_addr;
int cmd_name_len = sizeof ("qSearch:memory:") - 1; int cmd_name_len = sizeof ("qSearch:memory:") - 1;
pattern = malloc (packet_len); pattern = xmalloc (packet_len);
if (pattern == NULL) if (pattern == NULL)
{ {
error ("Unable to allocate memory to perform the search"); error ("Unable to allocate memory to perform the search");
@ -460,7 +460,7 @@ handle_search_memory (char *own_buf, int packet_len)
if (search_space_len < search_buf_size) if (search_space_len < search_buf_size)
search_buf_size = search_space_len; search_buf_size = search_space_len;
search_buf = malloc (search_buf_size); search_buf = xmalloc (search_buf_size);
if (search_buf == NULL) if (search_buf == NULL)
{ {
free (pattern); free (pattern);
@ -575,7 +575,7 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
return; return;
if (len > PBUFSIZ - 2) if (len > PBUFSIZ - 2)
len = PBUFSIZ - 2; len = PBUFSIZ - 2;
spu_buf = malloc (len + 1); spu_buf = xmalloc (len + 1);
if (!spu_buf) if (!spu_buf)
return; return;
@ -604,7 +604,7 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
require_running (own_buf); require_running (own_buf);
strcpy (own_buf, "E00"); strcpy (own_buf, "E00");
spu_buf = malloc (packet_len - 15); spu_buf = xmalloc (packet_len - 15);
if (!spu_buf) if (!spu_buf)
return; return;
if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex, if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex,
@ -648,7 +648,7 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
more. */ more. */
if (len > PBUFSIZ - 2) if (len > PBUFSIZ - 2)
len = PBUFSIZ - 2; len = PBUFSIZ - 2;
data = malloc (len + 1); data = xmalloc (len + 1);
n = (*the_target->read_auxv) (ofs, data, len + 1); n = (*the_target->read_auxv) (ofs, data, len + 1);
if (n < 0) if (n < 0)
write_enn (own_buf); write_enn (own_buf);
@ -726,7 +726,7 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next) for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name); total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name);
document = malloc (total_len); document = xmalloc (total_len);
strcpy (document, "<library-list>\n"); strcpy (document, "<library-list>\n");
p = document + strlen (document); p = document + strlen (document);
@ -782,7 +782,7 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
return; return;
if (len > PBUFSIZ - 2) if (len > PBUFSIZ - 2)
len = PBUFSIZ - 2; len = PBUFSIZ - 2;
workbuf = malloc (len + 1); workbuf = xmalloc (len + 1);
if (!workbuf) if (!workbuf)
return; return;
@ -895,7 +895,7 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
/* Handle "monitor" commands. */ /* Handle "monitor" commands. */
if (strncmp ("qRcmd,", own_buf, 6) == 0) if (strncmp ("qRcmd,", own_buf, 6) == 0)
{ {
char *mon = malloc (PBUFSIZ); char *mon = xmalloc (PBUFSIZ);
int len = strlen (own_buf + 6); int len = strlen (own_buf + 6);
if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2) if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
@ -975,7 +975,7 @@ handle_v_cont (char *own_buf, char *status, int *signal)
/* Allocate room for one extra action, for the default remain-stopped /* Allocate room for one extra action, for the default remain-stopped
behavior; if no default action is in the list, we'll need the extra behavior; if no default action is in the list, we'll need the extra
slot. */ slot. */
resume_info = malloc ((n + 1) * sizeof (resume_info[0])); resume_info = xmalloc ((n + 1) * sizeof (resume_info[0]));
default_action.thread = -1; default_action.thread = -1;
default_action.leave_stopped = 1; default_action.leave_stopped = 1;
@ -1107,7 +1107,7 @@ handle_v_run (char *own_buf, char *status, int *signal)
new_argc++; new_argc++;
} }
new_argv = calloc (new_argc + 2, sizeof (char *)); new_argv = xcalloc (new_argc + 2, sizeof (char *));
i = 0; i = 0;
for (p = own_buf + strlen ("vRun;"); *p; p = next_p) for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
{ {
@ -1119,7 +1119,7 @@ handle_v_run (char *own_buf, char *status, int *signal)
new_argv[i] = NULL; new_argv[i] = NULL;
else else
{ {
new_argv[i] = malloc (1 + (next_p - p) / 2); new_argv[i] = xmalloc (1 + (next_p - p) / 2);
unhexify (new_argv[i], p, (next_p - p) / 2); unhexify (new_argv[i], p, (next_p - p) / 2);
new_argv[i][(next_p - p) / 2] = '\0'; new_argv[i][(next_p - p) / 2] = '\0';
} }
@ -1141,7 +1141,7 @@ handle_v_run (char *own_buf, char *status, int *signal)
return 0; return 0;
} }
new_argv[0] = strdup (program_argv[0]); new_argv[0] = xstrdup (program_argv[0]);
} }
/* Free the old argv. */ /* Free the old argv. */
@ -1443,17 +1443,17 @@ main (int argc, char *argv[])
initialize_async_io (); initialize_async_io ();
initialize_low (); initialize_low ();
own_buf = malloc (PBUFSIZ + 1); own_buf = xmalloc (PBUFSIZ + 1);
mem_buf = malloc (PBUFSIZ); mem_buf = xmalloc (PBUFSIZ);
if (pid == 0 && *next_arg != NULL) if (pid == 0 && *next_arg != NULL)
{ {
int i, n; int i, n;
n = argc - (next_arg - argv); n = argc - (next_arg - argv);
program_argv = malloc (sizeof (char *) * (n + 1)); program_argv = xmalloc (sizeof (char *) * (n + 1));
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
program_argv[i] = strdup (next_arg[i]); program_argv[i] = xstrdup (next_arg[i]);
program_argv[i] = NULL; program_argv[i] = NULL;
/* Wait till we are at first instruction in program. */ /* Wait till we are at first instruction in program. */

View File

@ -70,6 +70,14 @@ extern void *memmem (const void *, size_t , const void *, size_t);
#endif #endif
#endif #endif
#ifndef ATTR_MALLOC
#if defined(__GNUC__) && (__GNUC__ >= 3)
#define ATTR_MALLOC __attribute__ ((__malloc__))
#else
#define ATTR_MALLOC /* nothing */
#endif
#endif
/* A type used for binary buffers. */ /* A type used for binary buffers. */
typedef unsigned char gdb_byte; typedef unsigned char gdb_byte;
@ -265,6 +273,9 @@ char *target_signal_to_name (enum target_signal);
/* Functions from utils.c */ /* Functions from utils.c */
void *xmalloc (size_t) ATTR_MALLOC;
void *xcalloc (size_t, size_t) ATTR_MALLOC;
char *xstrdup (const char *) ATTR_MALLOC;
void perror_with_name (char *string); void perror_with_name (char *string);
void error (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2); void error (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2);
void fatal (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2); void fatal (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2);

View File

@ -77,7 +77,7 @@ write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
if (buffer != NULL) if (buffer != NULL)
free (buffer); free (buffer);
buffer = malloc (len); buffer = xmalloc (len);
memcpy (buffer, myaddr, len); memcpy (buffer, myaddr, len);
check_mem_write (memaddr, buffer, len); check_mem_write (memaddr, buffer, len);
res = (*the_target->write_memory) (memaddr, buffer, len); res = (*the_target->write_memory) (memaddr, buffer, len);
@ -106,6 +106,6 @@ mywait (char *statusp, int connected_wait)
void void
set_target_ops (struct target_ops *target) set_target_ops (struct target_ops *target)
{ {
the_target = (struct target_ops *) malloc (sizeof (*the_target)); the_target = (struct target_ops *) xmalloc (sizeof (*the_target));
memcpy (the_target, target, sizeof (*the_target)); memcpy (the_target, target, sizeof (*the_target));
} }

View File

@ -30,6 +30,63 @@
/* Generally useful subroutines used throughout the program. */ /* Generally useful subroutines used throughout the program. */
static void malloc_failure (size_t size) ATTR_NORETURN;
static void
malloc_failure (size_t size)
{
fprintf (stderr, "gdbserver: ran out of memory while trying to allocate %lu bytes\n",
(unsigned long) size);
exit (1);
}
/* Allocate memory without fail.
If malloc fails, this will print a message to stderr and exit. */
void *
xmalloc (size_t size)
{
void *newmem;
if (size == 0)
size = 1;
newmem = malloc (size);
if (!newmem)
malloc_failure (size);
return newmem;
}
/* Allocate memory without fail and set it to zero.
If malloc fails, this will print a message to stderr and exit. */
void *
xcalloc (size_t nelem, size_t elsize)
{
void *newmem;
if (nelem == 0 || elsize == 0)
nelem = elsize = 1;
newmem = calloc (nelem, elsize);
if (!newmem)
malloc_failure (nelem * elsize);
return newmem;
}
/* Copy a string into a memory buffer.
If malloc fails, this will print a message to stderr and exit. */
char *
xstrdup (const char *s)
{
char *ret = strdup (s);
if (ret == NULL)
malloc_failure (strlen (s) + 1);
return ret;
}
/* Print the system error message for errno, and also mention STRING /* Print the system error message for errno, and also mention STRING
as the file name for which the error was encountered. as the file name for which the error was encountered.
Then return to command level. */ Then return to command level. */

View File

@ -176,7 +176,7 @@ child_add_thread (DWORD tid, HANDLE h)
if ((th = thread_rec (tid, FALSE))) if ((th = thread_rec (tid, FALSE)))
return th; return th;
th = calloc (1, sizeof (*th)); th = xcalloc (1, sizeof (*th));
th->tid = tid; th->tid = tid;
th->h = h; th->h = h;