- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / sfc_netback / ci / tools / log.h
1 /****************************************************************************
2  * Copyright 2002-2005: Level 5 Networks Inc.
3  * Copyright 2005-2008: Solarflare Communications Inc,
4  *                      9501 Jeronimo Road, Suite 250,
5  *                      Irvine, CA 92618, USA
6  *
7  * Maintained by Solarflare Communications
8  *  <linux-xen-drivers@solarflare.com>
9  *  <onload-dev@solarflare.com>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU General Public License version 2 as published
13  * by the Free Software Foundation, incorporated herein by reference.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23  ****************************************************************************
24  */
25
26 /*
27  * \author  djr
28  *  \brief  Functions for logging and pretty-printing.
29  *   \date  2002/08/07
30  */
31
32 /*! \cidoxg_include_ci_tools */
33
34 #ifndef __CI_TOOLS_LOG_H__
35 #define __CI_TOOLS_LOG_H__
36
37 #include <stdarg.h>
38
39
40 /**********************************************************************
41  * Logging.
42  */
43
44 /* size of internal log buffer */ 
45 #define  CI_LOG_MAX_LINE        512
46 /* uses of ci_log must ensure that all trace messages are shorter than this */ 
47 #define  CI_LOG_MAX_MSG_LENGTH        (CI_LOG_MAX_LINE-50)
48
49 extern void ci_vlog(const char* fmt, va_list args)  CI_HF;
50 extern void ci_log(const char* fmt, ...) CI_PRINTF_LIKE(1,2) CI_HF;
51
52   /*! Set the prefix for log messages.
53   **
54   ** Uses the storage pointed to by \em prefix.  Therefore \em prefix must
55   ** be allocated on the heap, or statically.
56   */
57 extern void ci_set_log_prefix(const char* prefix)  CI_HF;
58
59 typedef void (*ci_log_fn_t)(const char* msg);
60 extern ci_log_fn_t  ci_log_fn  CI_HV;
61
62 /* Log functions. */
63 extern void ci_log_null(const char* msg) CI_HF;
64 extern void ci_log_stderr(const char* msg) CI_HF;
65 extern void ci_log_stdout(const char* msg) CI_HF;
66 extern void ci_log_syslog(const char* msg) CI_HF;
67
68 /*! Call the following to install special logging behaviours. */
69 extern void ci_log_buffer_till_fail(void) CI_HF;
70 extern void ci_log_buffer_till_exit(void) CI_HF;
71
72 extern void __ci_log_unique(const char* msg) CI_HF;
73 extern ci_log_fn_t __ci_log_unique_fn CI_HV;
74 ci_inline void ci_log_uniquify(void) {
75   if( ci_log_fn != __ci_log_unique ) {
76     __ci_log_unique_fn = ci_log_fn;
77     ci_log_fn = __ci_log_unique;
78   }
79 }
80
81 extern void ci_log_file(const char* msg) CI_HF;
82 extern int  ci_log_file_fd CI_HV;
83
84 extern void __ci_log_nth(const char* msg) CI_HF;
85 extern ci_log_fn_t __ci_log_nth_fn CI_HV;
86 extern int  ci_log_nth_n CI_HV;  /* default 100 */
87 ci_inline void ci_log_nth(void) {
88   if( ci_log_fn != __ci_log_nth ) {
89     __ci_log_nth_fn = ci_log_fn;
90     ci_log_fn = __ci_log_nth;
91   }
92 }
93
94 extern int  ci_log_level  CI_HV;
95
96 extern int  ci_log_options  CI_HV;
97 #define CI_LOG_PID              0x1
98 #define CI_LOG_TID              0x2
99 #define CI_LOG_TIME             0x4
100 #define CI_LOG_DELTA            0x8
101
102 /**********************************************************************
103  * Used to define which mode we are in
104  */
105 #if (defined(_WIN32) && !defined(__KERNEL__))
106 typedef enum {
107   ci_log_md_NULL=0,
108     ci_log_md_ioctl,
109     ci_log_md_stderr,
110     ci_log_md_stdout,
111     ci_log_md_file,
112     ci_log_md_serial,
113     ci_log_md_syslog,
114     ci_log_md_pidfile
115 } ci_log_mode_t;
116 extern ci_log_mode_t ci_log_mode;
117 #endif
118
119 /**********************************************************************
120  * Pretty-printing.
121  */
122
123 extern char ci_printable_char(char c) CI_HF;
124
125 extern void (*ci_hex_dump_formatter)(char* buf, const ci_octet* s,
126                                      int i, int off, int len) CI_HV;
127 extern void ci_hex_dump_format_octets(char*,const ci_octet*,int,int,int) CI_HF;
128 extern void ci_hex_dump_format_dwords(char*,const ci_octet*,int,int,int) CI_HF;
129
130 extern void ci_hex_dump_row(char* buf, volatile const void* s, int len,
131                             ci_ptr_arith_t address) CI_HF;
132   /*!< A row contains up to 16 bytes.  Row starts at [address & 15u], so
133   ** therefore [len + (address & 15u)] must be <= 16.
134   */
135
136 extern void ci_hex_dump(ci_log_fn_t, volatile const void*,
137                         int len, ci_ptr_arith_t address) CI_HF;
138
139 extern int  ci_hex_dump_to_raw(const char* src_hex, void* buf,
140                                unsigned* addr_out_opt, int* skip)  CI_HF;
141   /*!< Recovers raw data from a single line of a hex dump.  [buf] must be at
142   ** least 16 bytes long.  Returns the number of bytes written to [buf] (in
143   ** range 1 -> 16), or -1 if [src_hex] doesn't contain hex data.  Does not
144   ** cope with missing bytes at the start of a line.
145   */
146
147 extern int ci_format_eth_addr(char* buf, const void* eth_mac_addr,
148                               char sep)  CI_HF;
149   /*!< This will write 18 characters to <buf> including terminating null.
150   ** Returns number of bytes written excluding null.  If [sep] is zero, ':'
151   ** is used.
152   */
153
154 extern int ci_parse_eth_addr(void* eth_mac_addr,
155                              const char* str, char sep) CI_HF;
156   /*!< If [sep] is zero, absolutely any separator is accepted (even
157   ** inconsistent separators).  Returns 0 on success, -1 on error.
158   */
159
160 extern int ci_format_ip4_addr(char* buf, unsigned addr_be32) CI_HF;
161   /*!< Formats the IP address (in network endian) in dotted-quad.  Returns
162   ** the number of bytes written (up to 15), excluding the null.  [buf]
163   ** must be at least 16 bytes long.
164   */
165
166 #if defined(__unix__) && ! defined(__KERNEL__)
167 extern int ci_format_select_set(char* s, int len_s, int nfds, const fd_set*);
168 extern int ci_format_select(char* s, int len_s,
169                             int nfds, const fd_set* rds, const fd_set* wrs,
170                             const fd_set* exs, struct timeval* timeout);
171 #endif
172
173
174 /**********************************************************************
175  * Error checking.
176  */
177
178 extern void (*ci_fail_stop_fn)(void) CI_HV;
179
180 extern void ci_fail_stop(void) CI_HF;
181 extern void ci_fail_hang(void) CI_HF;
182 extern void ci_fail_bomb(void) CI_HF;
183 extern void ci_backtrace(void) CI_HF;
184
185 #if defined __linux__ && !defined __KERNEL__
186 extern void ci_fail_abort (void) CI_HF;
187 #endif
188
189 #ifdef __GNUC__
190 extern void
191 __ci_fail(const char*, ...) CI_PRINTF_LIKE(1,2) CI_HF;
192 #else
193 # if _PREFAST_
194   extern void _declspec(noreturn) __ci_fail(const char* fmt, ...);
195 # else 
196   extern void __ci_fail(const char* fmt, ...);
197 # endif
198
199 #endif
200
201 #define ci_warn(x)                                                         \
202   do{ ci_log("WARN at %s:%d", __FILE__, __LINE__); }while(0)
203
204 #define ci_fail(x)                                                         \
205   do{ ci_log("FAIL at %s:%d", __FILE__, __LINE__);  __ci_fail x; }while(0)
206
207 extern void __ci_sys_fail(const char* fn, int rc,
208                           const char* file, int line) CI_HF;
209 #define ci_sys_fail(fn, rc)  __ci_sys_fail(fn, rc, __FILE__, __LINE__)
210
211 /**********************************************************************
212  * Logging to buffer (src/citools/log_buffer.c)
213  */
214
215 /*! Divert ci_log() messages to the log buffer
216  *  normally they go to the  system console */
217 extern void ci_log_buffer_till_fail(void) CI_HF;
218
219 /*! Dump the contents of the log buffer to the system console */
220 extern void ci_log_buffer_dump(void) CI_HF;
221
222
223 /**********************************************************************
224  * Some useful pretty-printing.
225  */
226
227 #ifdef  __linux__
228 # define CI_SOCKCALL_FLAGS_FMT  "%s%s%s%s%s%s%s%s%s%s%s"
229
230 # define CI_SOCKCALL_FLAGS_PRI_ARG(x)           \
231   (((x) & MSG_OOB         ) ? "OOB "         :""),      \
232   (((x) & MSG_PEEK        ) ? "PEEK "        :""),      \
233   (((x) & MSG_DONTROUTE   ) ? "DONTROUTE "   :""),      \
234   (((x) & MSG_EOR         ) ? "EOR "         :""),      \
235   (((x) & MSG_CTRUNC      ) ? "CTRUNC "      :""),      \
236   (((x) & MSG_TRUNC       ) ? "TRUNC "       :""),      \
237   (((x) & MSG_WAITALL     ) ? "WAITALL "     :""),      \
238   (((x) & MSG_DONTWAIT    ) ? "DONTWAIT "    :""),      \
239   (((x) & MSG_NOSIGNAL    ) ? "NOSIGNAL "    :""),      \
240   (((x) & MSG_ERRQUEUE    ) ? "ERRQUEUE "    :""),      \
241   (((x) & MSG_CONFIRM     ) ? "CONFIRM "     :"")
242 #endif
243
244 #ifdef  _WIN32
245 # define CI_SOCKCALL_FLAGS_FMT  "%s%s%s"
246
247 # define CI_SOCKCALL_FLAGS_PRI_ARG(x)           \
248   (((x) & MSG_OOB         ) ? "OOB "         :""),      \
249   (((x) & MSG_PEEK        ) ? "PEEK "        :""),      \
250   (((x) & MSG_DONTROUTE   ) ? "DONTROUTE "   :"")
251 #endif
252
253 #ifdef  __sun__
254 # define CI_SOCKCALL_FLAGS_FMT  "%s%s%s%s%s%s%s%s%s"
255
256 # define CI_SOCKCALL_FLAGS_PRI_ARG(x)           \
257   (((x) & MSG_OOB         ) ? "OOB "         :""),      \
258   (((x) & MSG_PEEK        ) ? "PEEK "        :""),      \
259   (((x) & MSG_DONTROUTE   ) ? "DONTROUTE "   :""),      \
260   (((x) & MSG_EOR         ) ? "EOR "         :""),      \
261   (((x) & MSG_CTRUNC      ) ? "CTRUNC "      :""),      \
262   (((x) & MSG_TRUNC       ) ? "TRUNC "       :""),      \
263   (((x) & MSG_WAITALL     ) ? "WAITALL "     :""),      \
264   (((x) & MSG_DONTWAIT    ) ? "DONTWAIT "    :""),      \
265   (((x) & MSG_NOTIFICATION) ? "NOTIFICATION" :"")
266 #endif
267
268 #endif  /* __CI_TOOLS_LOG_H__ */
269 /*! \cidoxg_end */