Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Brian Bruns
3 : * Copyright (C) 2006-2015 Frediano Ziglio
4 : *
5 : * This library is free software; you can redistribute it and/or
6 : * modify it under the terms of the GNU Library General Public
7 : * License as published by the Free Software Foundation; either
8 : * version 2 of the License, or (at your option) any later version.
9 : *
10 : * This library is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Library General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Library General Public
16 : * License along with this library; if not, write to the
17 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 : * Boston, MA 02111-1307, USA.
19 : */
20 :
21 : #include <config.h>
22 :
23 : #include <stdarg.h>
24 :
25 : #include <freetds/time.h>
26 :
27 : #include <assert.h>
28 : #include <ctype.h>
29 : #include <limits.h>
30 : #include <stdio.h>
31 :
32 : #if HAVE_STDLIB_H
33 : #include <stdlib.h>
34 : #endif /* HAVE_STDLIB_H */
35 :
36 : #if HAVE_STRING_H
37 : #include <string.h>
38 : #endif /* HAVE_STRING_H */
39 :
40 : #if HAVE_UNISTD_H
41 : #include <unistd.h>
42 : #endif /* HAVE_UNISTD_H */
43 :
44 : #ifdef _WIN32
45 : # include <process.h>
46 : #endif
47 :
48 : #include <freetds/tds.h>
49 : #include <freetds/checks.h>
50 : #include <freetds/thread.h>
51 : #include <freetds/utils.h>
52 :
53 : /* for now all messages go to the log */
54 : int tds_debug_flags = TDS_DBGFLAG_ALL | TDS_DBGFLAG_SOURCE;
55 : int tds_append_mode = 0;
56 : static tds_dir_char *g_dump_filename = NULL;
57 : /** Tell if TDS debug logging is turned on or off */
58 : bool tds_write_dump = false;
59 : /** List of threads excluded from logging, used to exclude some sensitive data */
60 : static TDSDUMP_OFF_ITEM *off_list;
61 : static FILE *g_dumpfile = NULL; /* file pointer for dump log */
62 : static tds_mutex g_dump_mutex = TDS_MUTEX_INITIALIZER;
63 :
64 : static FILE* tdsdump_append(void);
65 :
66 : #ifdef TDS_ATTRIBUTE_DESTRUCTOR
67 : static void __attribute__((destructor))
68 2214 : tds_util_deinit(void)
69 : {
70 2214 : tdsdump_close();
71 2214 : }
72 : #endif
73 :
74 : /**
75 : * Temporarily turn off logging for current thread.
76 : * @param off_item List item to be used by the function.
77 : * The item will be initialized by the function.
78 : * It's retained till is removed with tdsdump_on so it must be kept alive.
79 : */
80 : void
81 5950 : tdsdump_off(TDSDUMP_OFF_ITEM *off_item)
82 : {
83 : /* if already off don't add current thread to exclude list to make it faster */
84 5950 : if (!tds_write_dump)
85 : return;
86 :
87 3004 : off_item->thread_id = tds_thread_get_current_id();
88 3004 : tds_mutex_lock(&g_dump_mutex);
89 3004 : off_item->next = off_list;
90 3004 : off_list = off_item;
91 3004 : tds_mutex_unlock(&g_dump_mutex);
92 : } /* tdsdump_off() */
93 :
94 :
95 : /**
96 : * Turn logging back on for current thread.
97 : * @param off_item List item to remove from global list.
98 : * Previously used by tdsdump_off().
99 : */
100 : void
101 5950 : tdsdump_on(TDSDUMP_OFF_ITEM *off_item)
102 : {
103 : TDSDUMP_OFF_ITEM **curr;
104 :
105 5950 : tds_mutex_lock(&g_dump_mutex);
106 8021 : for (curr = &off_list; *curr != NULL; curr = &(*curr)->next) {
107 5075 : if (*curr == off_item) {
108 3004 : *curr = (*curr)->next;
109 3004 : break;
110 : }
111 : }
112 5950 : tds_mutex_unlock(&g_dump_mutex);
113 5950 : }
114 :
115 : int
116 8 : tdsdump_isopen(void)
117 : {
118 8 : return g_dumpfile || g_dump_filename;
119 : }
120 :
121 :
122 : /**
123 : * Create and truncate a human readable dump file for the TDS
124 : * traffic. The name of the file is specified by the filename
125 : * parameter. If that is given as NULL or an empty string,
126 : * any existing log file will be closed.
127 : *
128 : * \return true if the file was opened, false if it couldn't be opened.
129 : */
130 : int
131 46 : tdsdump_open(const tds_dir_char *filename)
132 : {
133 : int result; /* really should be a boolean, not an int */
134 :
135 46 : tds_mutex_lock(&g_dump_mutex);
136 :
137 : /* same append file */
138 46 : if (tds_append_mode && filename != NULL && g_dump_filename != NULL && tds_dir_cmp(filename, g_dump_filename) == 0) {
139 0 : tds_mutex_unlock(&g_dump_mutex);
140 0 : return 1;
141 : }
142 :
143 46 : tds_write_dump = false;
144 :
145 : /* free old one */
146 46 : if (g_dumpfile != NULL && g_dumpfile != stdout && g_dumpfile != stderr)
147 0 : fclose(g_dumpfile);
148 46 : g_dumpfile = NULL;
149 46 : if (g_dump_filename)
150 0 : TDS_ZERO_FREE(g_dump_filename);
151 :
152 : /* required to close just log ?? */
153 46 : if (filename == NULL || filename[0] == '\0') {
154 32 : tds_mutex_unlock(&g_dump_mutex);
155 32 : return 1;
156 : }
157 :
158 14 : result = 1;
159 14 : if (tds_append_mode) {
160 0 : g_dump_filename = tds_dir_dup(filename);
161 : /* if mutex are available do not reopen file every time */
162 : #ifdef TDS_HAVE_MUTEX
163 0 : g_dumpfile = tdsdump_append();
164 : #endif
165 14 : } else if (!tds_dir_cmp(filename, TDS_DIR("stdout"))) {
166 2 : g_dumpfile = stdout;
167 12 : } else if (!tds_dir_cmp(filename, TDS_DIR("stderr"))) {
168 0 : g_dumpfile = stderr;
169 12 : } else if (NULL == (g_dumpfile = tds_dir_open(filename, TDS_DIR("w")))) {
170 : result = 0;
171 : }
172 :
173 : if (result)
174 14 : tds_write_dump = true;
175 14 : tds_mutex_unlock(&g_dump_mutex);
176 :
177 14 : if (result) {
178 : char today[64];
179 : struct tm res;
180 : time_t t;
181 :
182 14 : time(&t);
183 14 : today[0] = 0;
184 14 : if (tds_localtime_r(&t, &res))
185 14 : strftime(today, sizeof(today), "%Y-%m-%d %H:%M:%S", &res);
186 :
187 14 : tdsdump_log(TDS_DBG_INFO1, "Starting log file for FreeTDS %s\n"
188 : "\ton %s with debug flags 0x%x.\n", VERSION, today, tds_debug_flags);
189 : }
190 : return result;
191 : } /* tdsdump_open() */
192 :
193 : static FILE*
194 0 : tdsdump_append(void)
195 : {
196 0 : if (!g_dump_filename)
197 : return NULL;
198 :
199 0 : if (!tds_dir_cmp(g_dump_filename, TDS_DIR("stdout"))) {
200 0 : return stdout;
201 0 : } else if (!tds_dir_cmp(g_dump_filename, TDS_DIR("stderr"))) {
202 0 : return stderr;
203 : }
204 0 : return tds_dir_open(g_dump_filename, TDS_DIR("a"));
205 : }
206 :
207 :
208 : /**
209 : * Close the TDS dump log file.
210 : */
211 : void
212 2224 : tdsdump_close(void)
213 : {
214 2224 : tds_mutex_lock(&g_dump_mutex);
215 2224 : tds_write_dump = false;
216 2224 : if (g_dumpfile != NULL && g_dumpfile != stdout && g_dumpfile != stderr)
217 12 : fclose(g_dumpfile);
218 2224 : g_dumpfile = NULL;
219 2224 : if (g_dump_filename)
220 0 : TDS_ZERO_FREE(g_dump_filename);
221 2224 : tds_mutex_unlock(&g_dump_mutex);
222 2224 : } /* tdsdump_close() */
223 :
224 : static void
225 3888 : tdsdump_start(FILE *file, const char *fname, int line)
226 : {
227 : char buf[128], *pbuf;
228 3888 : int started = 0;
229 :
230 : /* write always time before log */
231 3888 : if (tds_debug_flags & TDS_DBGFLAG_TIME) {
232 878 : fputs(tds_timestamp_str(buf, sizeof(buf) - 1), file);
233 878 : started = 1;
234 : }
235 :
236 3888 : pbuf = buf;
237 3888 : if (tds_debug_flags & TDS_DBGFLAG_PID) {
238 0 : if (started)
239 0 : *pbuf++ = ' ';
240 0 : pbuf += sprintf(pbuf, "%d", (int) getpid());
241 0 : started = 1;
242 : }
243 :
244 3888 : if ((tds_debug_flags & TDS_DBGFLAG_SOURCE) && fname && line) {
245 : const char *p;
246 3888 : p = strrchr(fname, '/');
247 3888 : if (p)
248 0 : fname = p + 1;
249 3888 : p = strrchr(fname, '\\');
250 3888 : if (p)
251 0 : fname = p + 1;
252 3888 : if (started)
253 878 : pbuf += sprintf(pbuf, " (%s:%d)", fname, line);
254 : else
255 3010 : pbuf += sprintf(pbuf, "%s:%d", fname, line);
256 : started = 1;
257 : }
258 0 : if (started)
259 3888 : *pbuf++ = ':';
260 3888 : *pbuf = 0;
261 3888 : fputs(buf, file);
262 3888 : }
263 :
264 : /**
265 : * Check if current thread is in the list of excluded threads
266 : * g_dump_mutex must be held.
267 : */
268 : static bool
269 6914 : current_thread_is_excluded(void)
270 : {
271 : TDSDUMP_OFF_ITEM *curr_off;
272 :
273 6914 : tds_mutex_check_owned(&g_dump_mutex);
274 :
275 9830 : for (curr_off = off_list; curr_off; curr_off = curr_off->next)
276 11884 : if (tds_thread_is_current(curr_off->thread_id))
277 : return true;
278 :
279 : return false;
280 : }
281 :
282 : static inline bool
283 : ascii_isprint(int ch)
284 : {
285 9286 : return ch >= ' ' && ch < 127;
286 : }
287 :
288 : #undef tdsdump_dump_buf
289 : /**
290 : * Dump the contents of data into the log file in a human readable format.
291 : * \param file source file name
292 : * \param level_line line and level combined. This and file are automatically computed by
293 : * TDS_DBG_* macros.
294 : * \param msg message to print before dump
295 : * \param buf buffer to dump
296 : * \param length number of bytes in the buffer
297 : */
298 : void
299 62 : tdsdump_dump_buf(const char* file, unsigned int level_line, const char *msg, const void *buf, size_t length)
300 : {
301 : size_t i, j;
302 : #define BYTES_PER_LINE 16
303 62 : const unsigned char *data = (const unsigned char *) buf;
304 62 : const int debug_lvl = level_line & 15;
305 62 : const int line = level_line >> 4;
306 : char line_buf[BYTES_PER_LINE * 8 + 16], *p;
307 : FILE *dumpfile;
308 :
309 62 : if (((tds_debug_flags >> debug_lvl) & 1) == 0 || !tds_write_dump)
310 4 : return;
311 :
312 62 : if (!g_dumpfile && !g_dump_filename)
313 : return;
314 :
315 62 : tds_mutex_lock(&g_dump_mutex);
316 :
317 62 : if (current_thread_is_excluded()) {
318 4 : tds_mutex_unlock(&g_dump_mutex);
319 4 : return;
320 : }
321 :
322 58 : dumpfile = g_dumpfile;
323 : #ifdef TDS_HAVE_MUTEX
324 58 : if (tds_append_mode && dumpfile == NULL)
325 0 : dumpfile = g_dumpfile = tdsdump_append();
326 : #else
327 : if (tds_append_mode)
328 : dumpfile = tdsdump_append();
329 : #endif
330 :
331 58 : if (dumpfile == NULL) {
332 0 : tds_mutex_unlock(&g_dump_mutex);
333 0 : return;
334 : }
335 :
336 58 : tdsdump_start(dumpfile, file, line);
337 :
338 58 : fprintf(dumpfile, "%s\n", msg);
339 :
340 670 : for (i = 0; i < length; i += BYTES_PER_LINE) {
341 612 : p = line_buf;
342 : /*
343 : * print the offset as a 4 digit hex number
344 : */
345 612 : p += sprintf(p, "%04x", ((unsigned int) i) & 0xffffu);
346 :
347 : /*
348 : * print each byte in hex
349 : */
350 10404 : for (j = 0; j < BYTES_PER_LINE; j++) {
351 9792 : if (j == BYTES_PER_LINE / 2)
352 612 : *p++ = '-';
353 : else
354 9180 : *p++ = ' ';
355 9792 : if (j + i >= length)
356 506 : p += sprintf(p, " ");
357 : else
358 9286 : p += sprintf(p, "%02x", data[i + j]);
359 : }
360 :
361 : /*
362 : * skip over to the ascii dump column
363 : */
364 612 : p += sprintf(p, " |");
365 :
366 : /*
367 : * print each byte in ascii
368 : */
369 9898 : for (j = i; j < length && (j - i) < BYTES_PER_LINE; j++) {
370 9286 : if (j - i == BYTES_PER_LINE / 2)
371 576 : *p++ = ' ';
372 18572 : p += sprintf(p, "%c", (ascii_isprint(data[j])) ? data[j] : '.');
373 : }
374 612 : strcpy(p, "|\n");
375 612 : fputs(line_buf, dumpfile);
376 : }
377 58 : fputs("\n", dumpfile);
378 :
379 58 : fflush(dumpfile);
380 :
381 : #ifndef TDS_HAVE_MUTEX
382 : if (tds_append_mode) {
383 : if (dumpfile != stdout && dumpfile != stderr)
384 : fclose(dumpfile);
385 : }
386 : #endif
387 :
388 58 : tds_mutex_unlock(&g_dump_mutex);
389 :
390 : } /* tdsdump_dump_buf() */
391 : #define tdsdump_dump_buf TDSDUMP_BUF_FAST
392 :
393 :
394 : #undef tdsdump_log
395 : /**
396 : * Write a message to the debug log.
397 : * \param file name of the log file
398 : * \param level_line kind of detail to be included
399 : * \param fmt printf-like format string
400 : */
401 : void
402 6852 : tdsdump_log(const char* file, unsigned int level_line, const char *fmt, ...)
403 : {
404 6852 : const int debug_lvl = level_line & 15;
405 6852 : const int line = level_line >> 4;
406 : va_list ap;
407 : FILE *dumpfile;
408 :
409 6852 : if (((tds_debug_flags >> debug_lvl) & 1) == 0 || !tds_write_dump)
410 3022 : return;
411 :
412 6852 : if (!g_dumpfile && !g_dump_filename)
413 : return;
414 :
415 6852 : tds_mutex_lock(&g_dump_mutex);
416 :
417 6852 : if (current_thread_is_excluded()) {
418 3022 : tds_mutex_unlock(&g_dump_mutex);
419 3022 : return;
420 : }
421 :
422 3830 : dumpfile = g_dumpfile;
423 : #ifdef TDS_HAVE_MUTEX
424 3830 : if (tds_append_mode && dumpfile == NULL)
425 0 : dumpfile = g_dumpfile = tdsdump_append();
426 : #else
427 : if (tds_append_mode)
428 : dumpfile = tdsdump_append();
429 : #endif
430 :
431 3830 : if (dumpfile == NULL) {
432 0 : tds_mutex_unlock(&g_dump_mutex);
433 0 : return;
434 : }
435 :
436 3830 : tdsdump_start(dumpfile, file, line);
437 :
438 3830 : va_start(ap, fmt);
439 :
440 3830 : vfprintf(dumpfile, fmt, ap);
441 3830 : va_end(ap);
442 :
443 3830 : fflush(dumpfile);
444 :
445 : #ifndef TDS_HAVE_MUTEX
446 : if (tds_append_mode) {
447 : if (dumpfile != stdout && dumpfile != stderr)
448 : fclose(dumpfile);
449 : }
450 : #endif
451 3830 : tds_mutex_unlock(&g_dump_mutex);
452 : } /* tdsdump_log() */
453 : #define tdsdump_log TDSDUMP_LOG_FAST
454 :
455 :
456 : /**
457 : * Write a column value to the debug log.
458 : * \param col column to dump
459 : */
460 : void
461 0 : tdsdump_col(const TDSCOLUMN *col)
462 : {
463 : const char* type_name;
464 : char* data;
465 : TDS_SMALLINT type;
466 :
467 0 : assert(col);
468 0 : assert(col->column_data);
469 :
470 0 : type_name = tds_prtype(col->column_type);
471 0 : type = tds_get_conversion_type(col->column_type, col->column_size);
472 :
473 0 : switch(type) {
474 0 : case SYBCHAR:
475 : case SYBVARCHAR:
476 0 : if (col->column_cur_size >= 0) {
477 0 : data = tds_new0(char, 1 + col->column_cur_size);
478 0 : if (!data) {
479 0 : tdsdump_log(TDS_DBG_FUNC, "no memory to log data for type %s\n", type_name);
480 : return;
481 : }
482 0 : memcpy(data, col->column_data, col->column_cur_size);
483 0 : tdsdump_log(TDS_DBG_FUNC, "type %s has value \"%s\"\n", type_name, data);
484 0 : free(data);
485 : } else {
486 0 : tdsdump_log(TDS_DBG_FUNC, "type %s has value NULL\n", type_name);
487 : }
488 : break;
489 0 : case SYBINT1:
490 0 : tdsdump_log(TDS_DBG_FUNC, "type %s has value %d\n", type_name, (int)*(TDS_TINYINT*)col->column_data);
491 : break;
492 0 : case SYBINT2:
493 0 : tdsdump_log(TDS_DBG_FUNC, "type %s has value %d\n", type_name, (int)*(TDS_SMALLINT*)col->column_data);
494 : break;
495 0 : case SYBINT4:
496 0 : tdsdump_log(TDS_DBG_FUNC, "type %s has value %d\n", type_name, (int)*(TDS_INT*)col->column_data);
497 : break;
498 0 : case SYBREAL:
499 0 : tdsdump_log(TDS_DBG_FUNC, "type %s has value %f\n", type_name, (double)*(TDS_REAL*)col->column_data);
500 : break;
501 0 : case SYBFLT8:
502 0 : tdsdump_log(TDS_DBG_FUNC, "type %s has value %f\n", type_name, (double)*(TDS_FLOAT*)col->column_data);
503 : break;
504 0 : default:
505 0 : tdsdump_log(TDS_DBG_FUNC, "cannot log data for type %s\n", type_name);
506 : break;
507 : }
508 : }
|