Debian Codegraph @

runtime/dyninst/io.c

raw
1 /* -*- linux-c -*-
2 * I/O for printing warnings, errors and debug messages
3 * Copyright (C) 2012 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 _STAPDYN_IO_C_
12 #define _STAPDYN_IO_C_
13
14 #include <sys/param.h>
15 #include "transport.h"
16 #include "vsprintf.h"
17 #include "print.h"
18
19 #define WARN_STRING "WARNING: "
20 #define ERR_STRING "ERROR: "
21
22 enum code { INFO=0, WARN, ERROR, DBUG };
23
24 static void _stp_vlog (enum code type, const char *func, int line,
25 const char *fmt, va_list args)
26 __attribute ((format (printf, 4, 0)));
27
28 static void _stp_vlog (enum code type, const char *func, int line,
29 const char *fmt, va_list args)
30 {
31 size_t start = 0;
32 size_t num;
33 char local_buffer[STP_LOG_BUF_LEN];
34 char *buf;
35
36 /* If we're writing a debug message, just use the temporary
37 * buffer. These messages end up going through the normal
38 * transport path, not the OOB data path. */
39 if (type == DBUG) {
40 buf = local_buffer;
41 }
42 else {
43 /* If we can't get a buffer, the transport must be not
44 * up yet. Instead use our temporary buffer. */
45 buf = _stp_dyninst_transport_log_buffer();
46 if (buf == NULL)
47 buf = local_buffer;
48 }
49
50 if (type == DBUG) {
51 start = snprintf(buf, STP_LOG_BUF_LEN, "%s:%d: ", func, line);
52 }
53 else if (type == WARN) {
54 /* This strcpy() is OK, since we know STP_LOG_BUF_LEN
55 * is > sizeof(WARN_STRING). */
56 strcpy(buf, WARN_STRING);
57 start = sizeof(WARN_STRING)- 1;
58 }
59 else if (type == ERROR) {
60 /* This strcpy() is OK, since we know STP_LOG_BUF_LEN
61 * is > sizeof(ERR_STRING) (which is <
62 * sizeof(WARN_STRING). */
63 strcpy(buf, ERR_STRING);
64 start = sizeof(ERR_STRING) - 1;
65 }
66
67 /* Note that if the message is too long it will just get truncated. */
68 num = vsnprintf(buf + start, STP_LOG_BUF_LEN - start - 1, fmt, args);
69 if ((num + start) == 0)
70 return;
71
72 /* Mimic vscnprintf's length computation. */
73 if (num >= STP_LOG_BUF_LEN - start - 1)
74 num = STP_LOG_BUF_LEN - start - 2;
75
76 /* If the last character is not a newline, then add one. */
77 if (buf[num + start - 1] != '\n') {
78 buf[num + start] = '\n';
79 num++;
80 buf[num + start] = '\0';
81 }
82
83 if (buf != local_buffer) {
84 /* If we successfully got log space, send it as OOB now.
85 * NB: don't explicitly send the \0 terminator. */
86 _stp_dyninst_transport_write_oob_data(buf, num + start);
87 }
88 else if (type == DBUG && _stp_runtime_get_context() != NULL) {
89 /* Debug messages go over the normal data path if we have a
90 * context for transport. */
91 _stp_print(buf);
92 _stp_print_flush();
93 }
94 else {
95 /* Fallback to local stderr as a last resort. */
96 fprintf(stderr, "%s", buf);
97 }
98 }
99
100 /** Prints warning.
101 * This function sends a warning message immediately to staprun. It
102 * will also be sent over the bulk transport (relayfs) if it is
103 * being used. If the last character is not a newline, then one
104 * is added.
105 * @param fmt A variable number of args.
106 */
107 static void _stp_warn (const char *fmt, ...)
108 {
109 va_list args;
110 va_start(args, fmt);
111 _stp_vlog (WARN, NULL, 0, fmt, args);
112 va_end(args);
113 }
114
115
116 /** Prints error message and exits.
117 * This function sends an error message immediately to staprun. It
118 * will also be sent over the bulk transport (relayfs) if it is
119 * being used. If the last character is not a newline, then one
120 * is added.
121 *
122 * After the error message is displayed, the module will be unloaded.
123 * @param fmt A variable number of args.
124 * @sa _stp_exit().
125 */
126 static void _stp_error (const char *fmt, ...)
127 {
128 va_list args;
129 va_start(args, fmt);
130 _stp_vlog (ERROR, NULL, 0, fmt, args);
131 va_end(args);
132 // FIXME: need to exit here...
133 // _stp_exit();
134 }
135
136
137 /** Prints error message.
138 * This function sends an error message immediately to staprun. It
139 * will also be sent over the bulk transport (relayfs) if it is
140 * being used. If the last character is not a newline, then one
141 * is added.
142 *
143 * @param fmt A variable number of args.
144 * @sa _stp_error
145 */
146 static void _stp_softerror (const char *fmt, ...)
147 {
148 va_list args;
149 va_start(args, fmt);
150 _stp_vlog (ERROR, NULL, 0, fmt, args);
151 va_end(args);
152 }
153
154
155 static void _stp_dbug (const char *func, int line, const char *fmt, ...)
156 {
157 va_list args;
158 va_start(args, fmt);
159 _stp_vlog (DBUG, func, line, fmt, args);
160 va_end(args);
161 }
162
163 static void _stp_exit (void)
164 {
165 _stp_dyninst_transport_request_exit();
166 }
167
168 #endif /* _STAPDYN_IO_C_ */