Fixed error reporting for Win32.

This commit is contained in:
Daniel Beer 2011-07-27 01:51:06 +12:00
parent a81043a656
commit 07780ff11f
17 changed files with 75 additions and 45 deletions

View File

@ -19,7 +19,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "aliasdb.h"
#include "vector.h"
@ -171,7 +170,7 @@ int cmd_alias(char **arg)
if (vector_push(&alias_list, &na, 1) < 0) {
printc_err("alias: can't allocate memory: %s\n",
strerror(errno));
last_error());
return -1;
}

3
bsl.c
View File

@ -18,7 +18,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <sys/stat.h>
@ -369,7 +368,7 @@ static device_t bsl_open(const struct device_args *args)
dev->serial_fd = sport_open(args->path, B460800, 0);
if (SPORT_ISERR(dev->serial_fd)) {
printc_err("bsl: can't open %s: %s\n",
args->path, strerror(errno));
args->path, last_error());
free(dev);
return NULL;
}

View File

@ -20,9 +20,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "btree.h"
#include "output.h"
#include "util.h"
#define MAX_HEIGHT 16
@ -154,7 +155,7 @@ static struct btree_page *allocate_page(btree_t bt, int height)
p = malloc(size);
if (!p) {
printc_err("btree: couldn't allocate page: %s\n",
strerror(errno));
last_error());
return NULL;
}
@ -436,7 +437,7 @@ btree_t btree_alloc(const struct btree_def *def)
bt = malloc(sizeof(*bt));
if (!bt) {
printc_err("btree: couldn't allocate tree: %s\n",
strerror(errno));
last_error());
return NULL;
}
@ -447,7 +448,7 @@ btree_t btree_alloc(const struct btree_def *def)
bt->root = allocate_page(bt, 0);
if (!bt->root) {
printc_err("btree: couldn't allocate root node: %s\n",
strerror(errno));
last_error());
free(bt);
return NULL;
}

8
coff.c
View File

@ -18,7 +18,7 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "coff.h"
#include "util.h"
#include "output.h"
@ -108,7 +108,7 @@ static int read_block(FILE *in, int offset, int size, void *buf)
if (fseek(in, offset, SEEK_SET) < 0) {
printc_err("coff: can't seek to offset %d: %s\n",
offset, strerror(errno));
offset, last_error());
return -1;
}
@ -116,7 +116,7 @@ static int read_block(FILE *in, int offset, int size, void *buf)
if (len < 0) {
printc_err("coff: can't read %d bytes from "
"offset %d: %s\n",
size, offset, strerror(errno));
size, offset, last_error());
return -1;
}
@ -206,7 +206,7 @@ static int load_section(FILE *in, uint32_t addr, uint32_t offset,
section = malloc(size);
if (!section) {
printc_err("coff: couldn't allocate memory for "
"section at 0x%x: %s\n", offset, strerror(errno));
"section at 0x%x: %s\n", offset, last_error());
return -1;
}

View File

@ -20,8 +20,8 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include "device.h"
#include "binfile.h"
#include "stab.h"
@ -498,7 +498,7 @@ static int do_cmd_prog(char **arg, int prog_flags)
in = fopen(*arg, "r");
if (!in) {
printc_err("prog: %s: %s\n", *arg, strerror(errno));
printc_err("prog: %s: %s\n", *arg, last_error());
return -1;
}

View File

@ -17,11 +17,12 @@
*/
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "elf32.h"
#include "elf_format.h"
#include "output.h"
#include "util.h"
#ifndef EM_MSP430
#define EM_MSP430 0x69
@ -81,7 +82,7 @@ static int read_phdr(struct elf32_info *info, FILE *in)
if (fread(&info->file_phdrs[i],
sizeof(info->file_phdrs[0]), 1, in) == 0) {
printc_err("elf32: can't read phdr %d: %s\n",
i, strerror(errno));
i, last_error());
return -1;
}
}
@ -110,7 +111,7 @@ static int read_shdr(struct elf32_info *info, FILE *in)
if (fread(&info->file_shdrs[i],
sizeof(info->file_shdrs[0]), 1, in) == 0) {
printc_err("elf32: can't read shdr %d: %s\n",
i, strerror(errno));
i, last_error());
return -1;
}
}

View File

@ -19,7 +19,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <sys/stat.h>
@ -124,13 +123,13 @@ static int flash_bsl_send(struct flash_bsl_device *dev,
if (sport_write_all(dev->serial_fd, cmd_buf, len + 5) < 0) {
printc_err("flash_bsl: serial write failed: %s\n",
strerror(errno));
last_error());
return -1;
}
if (sport_read_all(dev->serial_fd, &response, 1) < 0) {
printc_err("flash_bsl: serial read failed: %s\n",
strerror(errno));
last_error());
return -1;
}
@ -177,7 +176,7 @@ static int flash_bsl_recv(struct flash_bsl_device *dev,
if (sport_read_all(dev->serial_fd, header, 3) < 0) {
printc_err("flash_bsl: read response failed: %s\n",
strerror(errno));
last_error());
return -1;
}
@ -635,7 +634,7 @@ static device_t flash_bsl_open(const struct device_args *args)
dev->serial_fd = sport_open(args->path, B9600, SPORT_EVEN_PARITY);
if (SPORT_ISERR(dev->serial_fd)) {
printc_err("flash_bsl: can't open %s: %s\n",
args->path, strerror(errno));
args->path, last_error());
free(dev);
return NULL;
}

3
gdb.c
View File

@ -19,7 +19,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <ctype.h>
#include <stdarg.h>
@ -466,7 +465,7 @@ static int gdb_server(int port)
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
printc_err("gdb: can't bind to port %d: %s\n",
port, strerror(errno));
port, last_error());
closesocket(sock);
return -1;
}

12
gdbc.c
View File

@ -18,14 +18,15 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <errno.h>
#include "sockets.h"
#include "output.h"
#include "gdbc.h"
#include "gdb_proto.h"
#include "opdb.h"
#include "util.h"
struct gdb_client {
struct device base;
@ -378,7 +379,8 @@ static int connect_to(const char *spec)
ent = gethostbyname(hostname);
if (!ent) {
#ifdef WIN32
printc_err("No such host: %s: %s\n", hostname);
printc_err("No such host: %s: %s\n", hostname,
last_error());
#else
printc_err("No such host: %s: %s\n", hostname,
hstrerror(h_errno));
@ -388,7 +390,7 @@ static int connect_to(const char *spec)
sock = socket(PF_INET, SOCK_STREAM, 0);
if (!sock) {
printc_err("socket: %s\n", strerror(errno));
printc_err("socket: %s\n", last_error());
return -1;
}
@ -399,7 +401,7 @@ static int connect_to(const char *spec)
inet_ntoa(addr.sin_addr), port);
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
printc_err("connect: %s\n", strerror(errno));
printc_err("connect: %s\n", last_error());
closesocket(sock);
return -1;
}
@ -418,7 +420,7 @@ static device_t gdbc_open(const struct device_args *args)
dev = malloc(sizeof(struct gdb_client));
if (!dev) {
printc_err("gdbc: can't allocate memory: %s\n",
strerror(errno));
last_error());
return NULL;
}

View File

@ -20,9 +20,10 @@
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include "opdb.h"
#include "output.h"
#include "util.h"
struct outbuf {
char buf[4096];
@ -109,7 +110,7 @@ int printc_err(const char *fmt, ...)
void pr_error(const char *prefix)
{
printc_err("%s: %s\n", prefix, strerror(errno));
printc_err("%s: %s\n", prefix, last_error());
}
void capture_start(capture_func_t func, void *data)

View File

@ -21,7 +21,6 @@
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#ifdef USE_READLINE
#include <readline/readline.h>
@ -229,7 +228,7 @@ int process_file(const char *filename, int show)
in = fopen(filename, "r");
if (!in) {
printc_err("read: can't open %s: %s\n",
filename, strerror(errno));
filename, last_error());
return -1;
}

View File

@ -19,7 +19,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "util.h"
@ -349,7 +348,7 @@ static int do_isearch(address_t addr, address_t len,
mbuf = malloc(len);
if (!mbuf) {
printc_err("isearch: couldn't allocate memory: %s\n",
strerror(errno));
last_error());
return -1;
}
@ -917,7 +916,7 @@ int cmd_cgraph(char **arg)
memory = malloc(len);
if (!memory) {
printc_err("cgraph: couldn't allocate memory: %s\n",
strerror(errno));
last_error());
return -1;
}

View File

@ -170,7 +170,7 @@ int sport_read(sport_t s, uint8_t *data, int len)
return -1;
if (!result) {
errno = EAGAIN;
SetLastError(WAIT_TIMEOUT);
return -1;
}
@ -185,7 +185,7 @@ int sport_write(sport_t s, const uint8_t *data, int len)
return -1;
if (!result) {
errno = EAGAIN;
SetLastError(WAIT_TIMEOUT);
return -1;
}

8
sym.c
View File

@ -22,7 +22,7 @@
#include <ctype.h>
#include <stdint.h>
#include <regex.h>
#include <errno.h>
#include "stab.h"
#include "expr.h"
#include "binfile.h"
@ -58,7 +58,7 @@ static int cmd_sym_load_add(int clear, char **arg)
in = fopen(*arg, "r");
if (!in) {
printc_err("sym: %s: %s\n", *arg, strerror(errno));
printc_err("sym: %s: %s\n", *arg, last_error());
return -1;
}
@ -103,7 +103,7 @@ static int cmd_sym_savemap(char **arg)
savemap_out = fopen(fname, "w");
if (!savemap_out) {
printc_err("sym: couldn't write to %s: %s\n", fname,
strerror(errno));
last_error());
return -1;
}
@ -113,7 +113,7 @@ static int cmd_sym_savemap(char **arg)
}
if (fclose(savemap_out) < 0) {
printc_err("sym: error on close: %s\n", strerror(errno));
printc_err("sym: error on close: %s\n", last_error());
return -1;
}

5
uif.c
View File

@ -19,7 +19,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <stdint.h>
#include <unistd.h>
@ -101,7 +100,7 @@ static int open_olimex_iso(const char *device)
if (ioctl(fd, TIOCSSERIAL, &serial_info) < 0) {
printc_err("open_olimex_iso: can't do "
"ioctl TIOCSSERIAL: %s\n",
strerror(errno));
last_error());
return -1;
}
@ -149,7 +148,7 @@ transport_t uif_open(const char *device, uif_type_t type)
if (SPORT_ISERR(tr->serial_fd)) {
printc_err("uif: can't open serial device: %s: %s\n",
device, strerror(errno));
device, last_error());
free(tr);
return NULL;
}

29
util.c
View File

@ -26,6 +26,10 @@
#include <signal.h>
#include <assert.h>
#ifdef WIN32
#include <windows.h>
#endif
#include "util.h"
#include "output.h"
@ -222,3 +226,28 @@ char *strsep(char **strp, const char *delim)
return start;
}
#endif
#ifdef WIN32
const char *last_error(void)
{
DWORD err = GetLastError();
static char msg_buf[128];
int len;
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0,
msg_buf, sizeof(msg_buf), NULL);
/* Trim trailing newline characters */
len = strlen(msg_buf);
while (len > 0 && isspace(msg_buf[len - 1]))
len--;
msg_buf[len] = 0;
return msg_buf;
}
#else
const char *last_error(void)
{
return strerror(errno);
}
#endif

3
util.h
View File

@ -31,6 +31,9 @@
/* This type fits an MSP430X register value */
typedef uint32_t address_t;
/* Retrive a string describing the last system error */
const char *last_error(void);
/* Check and catch ^C from the user */
void ctrlc_init(void);
void ctrlc_reset(void);