Commit 7d511204 authored by Lubomir Bulej's avatar Lubomir Bulej

Allow error checking functions to take format and arguments

parent e14d1fda
......@@ -2,6 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include <assert.h>
......@@ -31,8 +32,11 @@ find_value_index (const char * strval, const char * values [], int nvals) {
* an error code indicating a generic error.
*/
void
die_with_error (const char * message) {
fprintf (stderr, "%s%s\n", ERROR_PREFIX, message);
die_with_error (const char * restrict format, va_list args) {
fprintf (stderr, AGENT_NAME ": error: ");
vfprintf (stderr, format, args);
fprintf (stderr, "\n");
exit (ERROR_GENERIC);
}
......@@ -44,15 +48,22 @@ die_with_error (const char * message) {
* exits with an error code indicating failure in standard library call.
*/
void
die_with_std_error (const char * message, int errnum) {
die_with_std_error (int errnum, const char * restrict format, va_list args) {
char * cause = strerror (errnum);
fprintf (stderr, "%s%s\ncause: %s\n", ERROR_PREFIX, message, cause);
fprintf (stderr, AGENT_NAME ": std-error: %s\n", cause);
fprintf (stderr, AGENT_NAME ": ");
vfprintf (stderr, format, args);
fprintf (stderr, "\n");
exit (ERROR_STD);
}
// ****************************************************************************
// STRING UTILS SECTION
// ****************************************************************************
size_t
substr_count(const char * string, const char * restrict substr) {
size_t count = 0;
......@@ -117,13 +128,17 @@ __get_error_message (const DWORD msg_id) {
* exits with an error code indicating failure in standard library call.
*/
void
die_with_win_error (const char * message, DWORD errnum) {
char * cause = __get_error_message (errnum);
fprintf (stderr, "%s%s\ncause: %s", ERROR_PREFIX, message, cause);
die_with_win_error (DWORD errnum, const char * restrict format, va_list args) {
const char * restrict cause = __get_error_message (errnum);
fprintf (stderr, AGENT_NAME ": std-error: %s\n", cause);
if (cause != NULL) {
free (cause);
}
fprintf (stderr, AGENT_NAME ": ");
vfprintf (stderr, format, args);
fprintf (stderr, "\n");
exit (ERROR_STD);
}
......
......@@ -4,9 +4,9 @@
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <errno.h>
#include <sys/types.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
......@@ -45,6 +45,7 @@
/**
* Prints a warning message to stderr.
*
* @format format string for printf
* @args arguments associated with the format string
......@@ -66,12 +67,12 @@ int find_value_index (const char * strval, const char * values [], int nvals);
#define ERROR_STD 10002
#define ERROR_JVMTI 10003
#define ERROR_PREFIX "disl-agent: error: "
#define AGENT_NAME "disl-agent"
//
void die_with_error (const char * message);
void die_with_std_error (const char * message, int errnum);
void die_with_error (const char * format, va_list args);
void die_with_std_error (int errnum, const char * format, va_list args);
// ****************************************************************************
// STRING UTILS SECTION
......@@ -103,9 +104,14 @@ split_string(char * string, const char * restrict separator, size_t * tokens);
* error condition is true.
*/
static inline void
check_error (bool error, const char * message) {
check_error (bool error, const char * format, ...) {
if (error) {
die_with_error (message);
va_list args;
va_start (args, format);
die_with_error (format, args);
va_end (args);
}
}
......@@ -115,9 +121,14 @@ check_error (bool error, const char * message) {
* error condition is true.
*/
static inline void
check_std_error (bool error, const char * message) {
check_std_error (bool error, const char * format, ...) {
if (error) {
die_with_std_error (message, errno);
va_list args;
va_start (args, format);
die_with_std_error (errno, format, args);
va_end (args);
}
}
......@@ -137,7 +148,7 @@ warn_std_error (bool error, const char * message) {
#ifdef MINGW
void die_with_win_error (const char * message, DWORD errnum);
void die_with_win_error (DWORD errnum, const char * message, va_list args);
/**
......@@ -145,13 +156,17 @@ void die_with_win_error (const char * message, DWORD errnum);
* error condition is true.
*/
static inline void
check_win_error (bool error, const char * message) {
check_win_error (bool error, const char * format, ...) {
if (error) {
die_with_win_error (message, GetLastError ());
va_list args;
va_start (args, format);
die_with_win_error (GetLastError (), format, args);
va_end (args);
}
}
#endif /* MINGW */
#endif /* _COMMON_H_ */
......@@ -210,8 +210,8 @@ __instrument_class (
//
if (response->result == INSTRUMENT_CLASS_RESPONSE__INSTRUMENT_CLASS_RESULT__ERROR) {
fprintf (
stderr, "%sinstrumentation server error:\n%s\n",
ERROR_PREFIX, response->errormessage
stderr, AGENT_NAME ": instrumentation server error:\n%s\n",
response->errormessage
);
exit (ERROR_SERVER);
......@@ -709,11 +709,11 @@ __get_jvmti (JavaVM * jvm) {
// JVMTI interface. This is a fatal error for the agent.
//
fprintf (
stderr,
"%sFailed to obtain JVMTI interface Version 1 (0x%x)\n"
stderr, AGENT_NAME "error: "
"Failed to obtain JVMTI interface Version 1 (0x%x)\n"
"JVM GetEnv() returned %ld - is your Java runtime "
"version 1.5 or newer?\n",
ERROR_PREFIX, JVMTI_VERSION_1, (long) result
JVMTI_VERSION_1, (long) result
);
exit (ERROR_JVMTI);
......
......@@ -10,8 +10,8 @@
//
#ifndef ERROR_PREFIX
#error ERROR_PREFIX macro has to be defined
#ifndef AGENT_NAME
#error AGENT_NAME macro has to be defined
#endif
#ifndef ERROR_JVMTI
......@@ -175,16 +175,25 @@ jvmti_get_system_property_string (
* interface.
*/
void
die_with_jvmti_error (jvmtiEnv *jvmti, jvmtiError errnum, const char *str) {
char * errnum_str = NULL;
(void) (*jvmti)->GetErrorName (jvmti, errnum, &errnum_str);
die_with_jvmti_error (
jvmtiEnv * jvmti, jvmtiError error,
const char * restrict format, va_list args
) {
// JVMTI error
char * cause = NULL;
(void) (*jvmti)->GetErrorName (jvmti, error, &cause);
fprintf (
stderr, "%sJVMTI: %d (%s): %s\n",
ERROR_PREFIX, errnum,
(errnum_str == NULL ? "Unknown" : errnum_str),
(str == NULL ? "" : str)
stderr, AGENT_NAME ": jvmti-error: %s (%d)\n",
(cause != NULL) ? cause : "unknown", error
);
(*jvmti)->Deallocate (jvmti, (unsigned char *) cause);
// agent error
fprintf (stderr, AGENT_NAME ": ");
vfprintf (stderr, format, args);
fprintf (stderr, "\n");
exit (ERROR_JVMTI);
}
......@@ -3,12 +3,14 @@
#include "common.h"
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <jvmti.h>
//
void die_with_jvmti_error (jvmtiEnv * jvmti, jvmtiError error, const char * message);
unsigned char * jvmti_alloc_copy (jvmtiEnv * jvmti, const void * src, size_t size);
bool jvmti_redefine_class (
......@@ -24,6 +26,12 @@ char * jvmti_get_system_property_string (
jvmtiEnv * jvmti, const char * name, const char * dflval
);
//
void die_with_jvmti_error (
jvmtiEnv * jvmti, jvmtiError error, const char * format, va_list args
);
/**
* Checks whether a JVMTI invocation returned an error. Every JVMTI interface
......@@ -31,9 +39,17 @@ char * jvmti_get_system_property_string (
* down the line.
*/
static inline void
check_jvmti_error(jvmtiEnv *jvmti, jvmtiError errnum, const char * message) {
check_jvmti_error (
jvmtiEnv * restrict jvmti, jvmtiError errnum,
const char * restrict format, ...
) {
if (errnum != JVMTI_ERROR_NONE) {
die_with_jvmti_error (jvmti, errnum, message);
va_list args;
va_start (args, format);
die_with_jvmti_error (jvmti, errnum, format, args);
va_end (args);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment