| 1 |
/* -*- linux-c -*- |
| 2 |
* I/O for printing warnings, errors and debug messages |
| 3 |
* Copyright (C) 2005-2009 Red Hat Inc. |
| 4 |
* |
| 5 |
* This file is part of systemtap, and is free software. You can |
| 6 |
* redistribute it and/or modify it under the terms of the GNU General |
| 7 |
* Public License (GPL); either version 2, or (at your option) any |
| 8 |
* later version. |
| 9 |
*/ |
| 10 |
|
| 11 |
#ifndef _STAPLINUX_IO_C_ |
| 12 |
#define _STAPLINUX_IO_C_ |
| 13 |
|
| 14 |
/** @file io.c |
| 15 |
* @brief I/O for printing warnings, errors and debug messages. |
| 16 |
*/ |
| 17 |
/** @addtogroup io I/O |
| 18 |
* @{ |
| 19 |
*/ |
| 20 |
|
| 21 |
#define WARN_STRING "WARNING: " |
| 22 |
#define ERR_STRING "ERROR: " |
| 23 |
|
| 24 |
enum code { WARN=1, ERROR, DBUG }; |
| 25 |
|
| 26 |
static void _stp_vlog (enum code type, const char *func, int line, const char *fmt, va_list args) |
| 27 |
__attribute ((format (printf, 4, 0))); |
| 28 |
|
| 29 |
static void _stp_vlog (enum code type, const char *func, int line, const char *fmt, va_list args) |
| 30 |
{ |
| 31 |
int prefix_len, msg_len; |
| 32 |
struct _stp_log *log; |
| 33 |
unsigned long flags; |
| 34 |
va_list args_copy; |
| 35 |
char *buf; |
| 36 |
|
| 37 |
/* Warnings and errors are printed to the control channel */ |
| 38 |
switch (type) { |
| 39 |
case WARN: |
| 40 |
_stp_ctl_log_werr(WARN_STRING, sizeof(WARN_STRING) - 1, |
| 41 |
fmt, args); |
| 42 |
return; |
| 43 |
case ERROR: |
| 44 |
_stp_ctl_log_werr(ERR_STRING, sizeof(ERR_STRING) - 1, |
| 45 |
fmt, args); |
| 46 |
return; |
| 47 |
case DBUG: |
| 48 |
/* Debug messages are handled below */ |
| 49 |
break; |
| 50 |
} |
| 51 |
|
| 52 |
/* |
| 53 |
* Calculate the total possible length of the debug message. It's the |
| 54 |
* length of the prefix plus the length of the message plus one in case |
| 55 |
* a newline character needs to be appended. Note that we need a copy of |
| 56 |
* the va_list arguments because the vsnprintf() call will erase them. |
| 57 |
*/ |
| 58 |
prefix_len = snprintf(NULL, 0, "%s:%d: ", func, line); |
| 59 |
va_copy(args_copy, args); |
| 60 |
msg_len = vsnprintf(NULL, 0, fmt, args_copy); |
| 61 |
va_end(args_copy); |
| 62 |
|
| 63 |
if (!_stp_print_trylock_irqsave(&flags)) |
| 64 |
return; |
| 65 |
|
| 66 |
buf = _stp_reserve_bytes(prefix_len + msg_len + 1); |
| 67 |
if (!buf) |
| 68 |
goto err_unlock; |
| 69 |
|
| 70 |
/* |
| 71 |
* We can use raw *sprintf() here because the sizes have already been |
| 72 |
* validated. Additionally, the reserved size accomodates for an extra |
| 73 |
* byte so there won't be an overflow when the NUL termination is added |
| 74 |
* by *sprintf(). The NUL termination isn't desired but there isn't any |
| 75 |
* way to prevent it from being added. |
| 76 |
*/ |
| 77 |
sprintf(buf, "%s:%d: ", func, line); |
| 78 |
vsprintf(buf + prefix_len, fmt, args); |
| 79 |
|
| 80 |
/* |
| 81 |
* Make sure the last character is a newline. If it already is, then |
| 82 |
* discard the extra byte that was reserved. |
| 83 |
*/ |
| 84 |
if (buf[prefix_len + msg_len - 1] != '\n') |
| 85 |
buf[prefix_len + msg_len] = '\n'; |
| 86 |
else |
| 87 |
_stp_unreserve_bytes(1); |
| 88 |
|
| 89 |
/* Flush the log now so userspace is quickly notified of the message */ |
| 90 |
log = per_cpu_ptr(_stp_log_pcpu, raw_smp_processor_id()); |
| 91 |
__stp_print_flush(log); |
| 92 |
err_unlock: |
| 93 |
_stp_print_unlock_irqrestore(&flags); |
| 94 |
} |
| 95 |
|
| 96 |
/** Prints warning. |
| 97 |
* This function sends a warning message immediately to staprun. It |
| 98 |
* will also be sent over the bulk transport (relayfs) if it is |
| 99 |
* being used. If the last character is not a newline, then one |
| 100 |
* is added. |
| 101 |
* @param fmt A variable number of args. |
| 102 |
*/ |
| 103 |
static void _stp_warn (const char *fmt, ...) |
| 104 |
{ |
| 105 |
va_list args; |
| 106 |
va_start(args, fmt); |
| 107 |
_stp_vlog (WARN, NULL, 0, fmt, args); |
| 108 |
va_end(args); |
| 109 |
} |
| 110 |
|
| 111 |
/** Exits and unloads the module. |
| 112 |
* This function sends a signal to staprun to tell it to |
| 113 |
* unload the module and exit. The module will not be |
| 114 |
* unloaded until after the current probe returns. |
| 115 |
* @note Be careful to not treat this like the Linux exit() |
| 116 |
* call. You should probably call return immediately after |
| 117 |
* calling _stp_exit(). |
| 118 |
*/ |
| 119 |
static void _stp_exit (void) |
| 120 |
{ |
| 121 |
/* Just set the flag since this is possibly called from |
| 122 |
kprobe context. A timer will come along and call |
| 123 |
_stp_request_exit() for us. */ |
| 124 |
_stp_exit_flag = 1; |
| 125 |
} |
| 126 |
|
| 127 |
/** Prints error message and exits. |
| 128 |
* This function sends an error message immediately to staprun. It |
| 129 |
* will also be sent over the bulk transport (relayfs) if it is |
| 130 |
* being used. If the last character is not a newline, then one |
| 131 |
* is added. |
| 132 |
* |
| 133 |
* After the error message is displayed, the module will transition |
| 134 |
* to exiting-state (as if ^C was pressed) and will eventually unload. |
| 135 |
* @param fmt A variable number of args. |
| 136 |
* @sa _stp_exit(). |
| 137 |
* |
| 138 |
* NB: this function should not be used from script-accessible tapset |
| 139 |
* functions. Those should simply set CONTEXT->last_error, so that |
| 140 |
* script-level try/catch blocks can handle them. This is for random |
| 141 |
* runtime internal matters that a script didn't invoke and can't |
| 142 |
* expect to handle. |
| 143 |
*/ |
| 144 |
static void _stp_error (const char *fmt, ...) |
| 145 |
{ |
| 146 |
va_list args; |
| 147 |
va_start(args, fmt); |
| 148 |
_stp_vlog (ERROR, NULL, 0, fmt, args); |
| 149 |
va_end(args); |
| 150 |
_stp_exit(); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
/** Prints error message. |
| 155 |
* This function sends an error message immediately to staprun. It |
| 156 |
* will also be sent over the bulk transport (relayfs) if it is |
| 157 |
* being used. If the last character is not a newline, then one |
| 158 |
* is added. |
| 159 |
* |
| 160 |
* @param fmt A variable number of args. |
| 161 |
* @sa _stp_error |
| 162 |
*/ |
| 163 |
static void _stp_softerror (const char *fmt, ...) |
| 164 |
{ |
| 165 |
va_list args; |
| 166 |
va_start(args, fmt); |
| 167 |
_stp_vlog (ERROR, NULL, 0, fmt, args); |
| 168 |
va_end(args); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
static void _stp_dbug (const char *func, int line, const char *fmt, ...) |
| 173 |
{ |
| 174 |
va_list args; |
| 175 |
va_start(args, fmt); |
| 176 |
_stp_vlog (DBUG, func, line, fmt, args); |
| 177 |
va_end(args); |
| 178 |
} |
| 179 |
|
| 180 |
/** @} */ |
| 181 |
#endif /* _STAPLINUX_IO_C_ */ |