From 77ebdf1c6fd8eb867c3806c7acd9632387890feb Mon Sep 17 00:00:00 2001 From: Brian Barrett Date: Mon, 29 Aug 2005 23:36:53 +0000 Subject: [PATCH] * Add some debugging output Ralph asked for when an unknown error code is passed to opal_error This commit was SVN r7087. --- opal/runtime/opal_init.c | 2 +- opal/util/error.c | 72 +++++++++++++++++++++++++++------ opal/util/error.h | 4 +- orte/runtime/orte_init_stage1.c | 2 +- test/util/opal_error.c | 6 +++ 5 files changed, 71 insertions(+), 15 deletions(-) diff --git a/opal/runtime/opal_init.c b/opal/runtime/opal_init.c index 86a8f2165f..ae5ab1ffe7 100644 --- a/opal/runtime/opal_init.c +++ b/opal/runtime/opal_init.c @@ -79,7 +79,7 @@ int opal_init(void) opal_mem_free_init(); /* register handler for errnum -> string converstion */ - opal_error_register(opal_err2str); + opal_error_register("OPAL", OPAL_ERR_BASE, OPAL_ERR_MAX, opal_err2str); /* initialize the mca */ mca_base_open(); diff --git a/opal/util/error.c b/opal/util/error.c index 6220212281..a170e814c5 100644 --- a/opal/util/error.c +++ b/opal/util/error.c @@ -24,8 +24,19 @@ #include "opal/include/constants.h" #define MAX_CONVERTERS 5 +#define MAX_CONVERTER_PROJECT_LEN 10 + +struct converter_info_t { + int init; + char project[MAX_CONVERTER_PROJECT_LEN]; + int err_base; + int err_max; + opal_err2str_fn_t converter; +}; +typedef struct converter_info_t converter_info_t; + /* all default to NULL */ -opal_err2str_fn_t converters[MAX_CONVERTERS]; +converter_info_t converters[MAX_CONVERTERS]; static const char * opal_strerror_int(int errnum) @@ -34,8 +45,8 @@ opal_strerror_int(int errnum) const char *ret = NULL; for (i = 0 ; i < MAX_CONVERTERS ; ++i) { - if (NULL != converters[i]) { - ret = converters[i](errnum); + if (0 != converters[i].init) { + ret = converters[i].converter(errnum); if (NULL != ret) break; } } @@ -44,6 +55,31 @@ opal_strerror_int(int errnum) } +/* caller must free string */ +static char* +opal_strerror_unknown(int errnum) +{ + int i; + char *ret; + + for (i = 0 ; i < MAX_CONVERTERS ; ++i) { + if (0 != converters[i].init) { + if (errnum < converters[i].err_base && + errnum > converters[i].err_max) { + asprintf(&ret, "Unknown error: %d (%s error %d)", + errnum, converters[i].project, + errnum - converters[i].err_base); + return ret; + } + } + } + + asprintf(&ret, "Unknown error: %d", errnum); + + return ret; +} + + void opal_perror(int errnum, const char *msg) { @@ -57,7 +93,9 @@ opal_perror(int errnum, const char *msg) if (errnum == OPAL_ERR_IN_ERRNO) { perror(msg); } else { - fprintf(stderr, "Unknown error: %d\n", errnum); + char *ue_msg = opal_strerror_unknown(errnum); + fprintf(stderr, "%s\n", ue_msg); + free(ue_msg); } } else { fprintf(stderr, "%s\n", errmsg); @@ -66,8 +104,9 @@ opal_perror(int errnum, const char *msg) fflush(stderr); } -/* size of "Unknow error: " + 3 digits of errnumber */ -static char unknown_retbuf[20]; +/* big enough to hold long version */ +#define UNKNOWN_RETBUF_LEN 50 +static char unknown_retbuf[UNKNOWN_RETBUF_LEN]; const char * opal_strerror(int errnum) @@ -78,8 +117,10 @@ opal_strerror(int errnum) if (errnum == OPAL_ERR_IN_ERRNO) { return strerror(errno); } else { + char *ue_msg = opal_strerror_unknown(errnum); + snprintf(unknown_retbuf, UNKNOWN_RETBUF_LEN, "%s", ue_msg); + free(ue_msg); errno = EINVAL; - snprintf(unknown_retbuf, 20, "Unknown error: %d", errnum); return (const char*) unknown_retbuf; } } else { @@ -100,12 +141,14 @@ opal_strerror_r(int errnum, char *strerrbuf, size_t buflen) strncpy(strerrbuf, tmp, buflen); return OPAL_SUCCESS; } else { - errno = EINVAL; - ret = snprintf(strerrbuf, buflen, "Unknown error: %d", errnum); + char *ue_msg = opal_strerror_unknown(errnum); + ret = snprintf(strerrbuf, buflen, "%s", ue_msg); + free(ue_msg); if (ret > (int) buflen) { errno = ERANGE; return OPAL_ERR_OUT_OF_RESOURCE; } else { + errno = EINVAL; return OPAL_SUCCESS; } } @@ -122,13 +165,18 @@ opal_strerror_r(int errnum, char *strerrbuf, size_t buflen) int -opal_error_register(opal_err2str_fn_t converter) +opal_error_register(const char *project, int err_base, int err_max, + opal_err2str_fn_t converter) { int i; for (i = 0 ; i < MAX_CONVERTERS ; ++i) { - if (NULL == converters[i]) { - converters[i] = converter; + if (0 == converters[i].init) { + converters[i].init = 1; + strncpy(converters[i].project, project, MAX_CONVERTER_PROJECT_LEN); + converters[i].err_base = err_base; + converters[i].err_max = err_max; + converters[i].converter = converter; return OPAL_SUCCESS; } } diff --git a/opal/util/error.h b/opal/util/error.h index af7ad2604e..7048467547 100644 --- a/opal/util/error.h +++ b/opal/util/error.h @@ -72,7 +72,9 @@ typedef const char * (*opal_err2str_fn_t)(int errnum); * \note A maximum of 5 converters can be registered. The 6th * converter registration attempt will return OPAL_ERR_OUT_OF_RESOURCE */ -OMPI_DECLSPEC int opal_error_register(opal_err2str_fn_t converter); +OMPI_DECLSPEC int opal_error_register(const char *project, + int err_base, int err_max, + opal_err2str_fn_t converter); #if defined(c_plusplus) || defined(__cplusplus) diff --git a/orte/runtime/orte_init_stage1.c b/orte/runtime/orte_init_stage1.c index fd1fadde8e..c81090f2a4 100644 --- a/orte/runtime/orte_init_stage1.c +++ b/orte/runtime/orte_init_stage1.c @@ -69,7 +69,7 @@ int orte_init_stage1(bool infrastructure) orte_cellid_t my_cellid; /* register handler for errnum -> string converstion */ - opal_error_register(orte_err2str); + opal_error_register("ORTE", ORTE_ERR_BASE, ORTE_ERR_MAX, orte_err2str); /* Register all MCA Params */ if (ORTE_SUCCESS != (ret = orte_register_params(infrastructure))) { diff --git a/test/util/opal_error.c b/test/util/opal_error.c index 209fc62eb7..608589c9d1 100644 --- a/test/util/opal_error.c +++ b/test/util/opal_error.c @@ -76,6 +76,12 @@ main(int argc, char *argv[]) printf("--> orte error test\n"); opal_perror(ORTE_ERR_BUFFER, "orte test"); + printf("--> orte unknown error test\n"); + opal_perror(ORTE_ERR_MAX + 10, "orte unknown test"); + + printf("--> unknown error test\n"); + opal_perror(ORTE_ERR_MAX - 200, "unknown error"); + orte_system_finalize(); opal_finalize();