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 2194 : tds_util_deinit(void)
69 : {
70 2194 : tdsdump_close();
71 2194 : }
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 5942 : 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 5942 : 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 5942 : tdsdump_on(TDSDUMP_OFF_ITEM *off_item)
102 : {
103 : TDSDUMP_OFF_ITEM **curr;
104 :
105 5942 : tds_mutex_lock(&g_dump_mutex);
106 8034 : for (curr = &off_list; *curr != NULL; curr = &(*curr)->next) {
107 5096 : if (*curr == off_item) {
108 3004 : *curr = (*curr)->next;
109 3004 : break;
110 : }
111 : }
112 5942 : tds_mutex_unlock(&g_dump_mutex);
113 5942 : }
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 : #ifdef _WIN32
132 : tdsdump_wopen(const tds_dir_char *filename)
133 : #else
134 46 : tdsdump_open(const tds_dir_char *filename)
135 : #endif
136 : {
137 : int result; /* really should be a boolean, not an int */
138 :
139 46 : tds_mutex_lock(&g_dump_mutex);
140 :
141 : /* same append file */
142 46 : if (tds_append_mode && filename != NULL && g_dump_filename != NULL && tds_dir_cmp(filename, g_dump_filename) == 0) {
143 0 : tds_mutex_unlock(&g_dump_mutex);
144 0 : return 1;
145 : }
146 :
147 46 : tds_write_dump = false;
148 :
149 : /* free old one */
150 46 : if (g_dumpfile != NULL && g_dumpfile != stdout && g_dumpfile != stderr)
151 0 : fclose(g_dumpfile);
152 46 : g_dumpfile = NULL;
153 46 : if (g_dump_filename)
154 0 : TDS_ZERO_FREE(g_dump_filename);
155 :
156 : /* required to close just log ?? */
157 46 : if (filename == NULL || filename[0] == '\0') {
158 32 : tds_mutex_unlock(&g_dump_mutex);
159 32 : return 1;
160 : }
161 :
162 14 : result = 1;
163 14 : if (tds_append_mode) {
164 0 : g_dump_filename = tds_dir_dup(filename);
165 : /* if mutex are available do not reopen file every time */
166 : #ifdef TDS_HAVE_MUTEX
167 0 : g_dumpfile = tdsdump_append();
168 : #endif
169 14 : } else if (!tds_dir_cmp(filename, TDS_DIR("stdout"))) {
170 2 : g_dumpfile = stdout;
171 12 : } else if (!tds_dir_cmp(filename, TDS_DIR("stderr"))) {
172 0 : g_dumpfile = stderr;
173 12 : } else if (NULL == (g_dumpfile = tds_dir_open(filename, TDS_DIR("w")))) {
174 : result = 0;
175 : }
176 :
177 : if (result)
178 14 : tds_write_dump = true;
179 14 : tds_mutex_unlock(&g_dump_mutex);
180 :
181 14 : if (result) {
182 : char today[64];
183 : struct tm res;
184 : time_t t;
185 :
186 14 : time(&t);
187 14 : today[0] = 0;
188 14 : if (tds_localtime_r(&t, &res))
189 14 : strftime(today, sizeof(today), "%Y-%m-%d %H:%M:%S", &res);
190 :
191 14 : tdsdump_log(TDS_DBG_INFO1, "Starting log file for FreeTDS %s\n"
192 : "\ton %s with debug flags 0x%x.\n", VERSION, today, tds_debug_flags);
193 : }
194 : return result;
195 : } /* tdsdump_open() */
196 :
197 : #ifdef _WIN32
198 : int
199 : tdsdump_open(const char *filename)
200 : {
201 : int ret;
202 : tds_dir_char *fn = tds_dir_from_cstr(filename);
203 :
204 : if (!fn)
205 : return 0;
206 :
207 : ret = tdsdump_wopen(fn);
208 : free(fn);
209 : return ret;
210 : }
211 : #endif
212 :
213 : static FILE*
214 0 : tdsdump_append(void)
215 : {
216 0 : if (!g_dump_filename)
217 : return NULL;
218 :
219 0 : if (!tds_dir_cmp(g_dump_filename, TDS_DIR("stdout"))) {
220 0 : return stdout;
221 0 : } else if (!tds_dir_cmp(g_dump_filename, TDS_DIR("stderr"))) {
222 0 : return stderr;
223 : }
224 0 : return tds_dir_open(g_dump_filename, TDS_DIR("a"));
225 : }
226 :
227 :
228 : /**
229 : * Close the TDS dump log file.
230 : */
231 : void
232 2204 : tdsdump_close(void)
233 : {
234 2204 : tds_mutex_lock(&g_dump_mutex);
235 2204 : tds_write_dump = false;
236 2204 : if (g_dumpfile != NULL && g_dumpfile != stdout && g_dumpfile != stderr)
237 12 : fclose(g_dumpfile);
238 2204 : g_dumpfile = NULL;
239 2204 : if (g_dump_filename)
240 0 : TDS_ZERO_FREE(g_dump_filename);
241 2204 : tds_mutex_unlock(&g_dump_mutex);
242 2204 : } /* tdsdump_close() */
243 :
244 : static void
245 3888 : tdsdump_start(FILE *file, const char *fname, int line)
246 : {
247 : char buf[128], *pbuf;
248 3888 : int started = 0;
249 :
250 : /* write always time before log */
251 3888 : if (tds_debug_flags & TDS_DBGFLAG_TIME) {
252 878 : fputs(tds_timestamp_str(buf, 127), file);
253 878 : started = 1;
254 : }
255 :
256 3888 : pbuf = buf;
257 3888 : if (tds_debug_flags & TDS_DBGFLAG_PID) {
258 0 : if (started)
259 0 : *pbuf++ = ' ';
260 0 : pbuf += sprintf(pbuf, "%d", (int) getpid());
261 0 : started = 1;
262 : }
263 :
264 3888 : if ((tds_debug_flags & TDS_DBGFLAG_SOURCE) && fname && line) {
265 : const char *p;
266 3888 : p = strrchr(fname, '/');
267 3888 : if (p)
268 0 : fname = p + 1;
269 3888 : p = strrchr(fname, '\\');
270 3888 : if (p)
271 0 : fname = p + 1;
272 3888 : if (started)
273 878 : pbuf += sprintf(pbuf, " (%s:%d)", fname, line);
274 : else
275 3010 : pbuf += sprintf(pbuf, "%s:%d", fname, line);
276 : started = 1;
277 : }
278 0 : if (started)
279 3888 : *pbuf++ = ':';
280 3888 : *pbuf = 0;
281 3888 : fputs(buf, file);
282 3888 : }
283 :
284 : /**
285 : * Check if current thread is in the list of excluded threads
286 : * g_dump_mutex must be held.
287 : */
288 : static bool
289 6914 : current_thread_is_excluded(void)
290 : {
291 : TDSDUMP_OFF_ITEM *curr_off;
292 :
293 6914 : tds_mutex_check_owned(&g_dump_mutex);
294 :
295 9851 : for (curr_off = off_list; curr_off; curr_off = curr_off->next)
296 11926 : if (tds_thread_is_current(curr_off->thread_id))
297 : return true;
298 :
299 : return false;
300 : }
301 :
302 : #undef tdsdump_dump_buf
303 : /**
304 : * Dump the contents of data into the log file in a human readable format.
305 : * \param file source file name
306 : * \param level_line line and level combined. This and file are automatically computed by
307 : * TDS_DBG_* macros.
308 : * \param msg message to print before dump
309 : * \param buf buffer to dump
310 : * \param length number of bytes in the buffer
311 : */
312 : void
313 62 : tdsdump_dump_buf(const char* file, unsigned int level_line, const char *msg, const void *buf, size_t length)
314 : {
315 : size_t i, j;
316 : #define BYTES_PER_LINE 16
317 62 : const unsigned char *data = (const unsigned char *) buf;
318 62 : const int debug_lvl = level_line & 15;
319 62 : const int line = level_line >> 4;
320 : char line_buf[BYTES_PER_LINE * 8 + 16], *p;
321 : FILE *dumpfile;
322 :
323 62 : if (((tds_debug_flags >> debug_lvl) & 1) == 0 || !tds_write_dump)
324 4 : return;
325 :
326 62 : if (!g_dumpfile && !g_dump_filename)
327 : return;
328 :
329 62 : tds_mutex_lock(&g_dump_mutex);
330 :
331 62 : if (current_thread_is_excluded()) {
332 4 : tds_mutex_unlock(&g_dump_mutex);
333 4 : return;
334 : }
335 :
336 58 : dumpfile = g_dumpfile;
337 : #ifdef TDS_HAVE_MUTEX
338 58 : if (tds_append_mode && dumpfile == NULL)
339 0 : dumpfile = g_dumpfile = tdsdump_append();
340 : #else
341 : if (tds_append_mode)
342 : dumpfile = tdsdump_append();
343 : #endif
344 :
345 58 : if (dumpfile == NULL) {
346 0 : tds_mutex_unlock(&g_dump_mutex);
347 0 : return;
348 : }
349 :
350 58 : tdsdump_start(dumpfile, file, line);
351 :
352 58 : fprintf(dumpfile, "%s\n", msg);
353 :
354 698 : for (i = 0; i < length; i += BYTES_PER_LINE) {
355 640 : p = line_buf;
356 : /*
357 : * print the offset as a 4 digit hex number
358 : */
359 640 : p += sprintf(p, "%04x", ((unsigned int) i) & 0xffffu);
360 :
361 : /*
362 : * print each byte in hex
363 : */
364 10880 : for (j = 0; j < BYTES_PER_LINE; j++) {
365 10240 : if (j == BYTES_PER_LINE / 2)
366 640 : *p++ = '-';
367 : else
368 9600 : *p++ = ' ';
369 10240 : if (j + i >= length)
370 516 : p += sprintf(p, " ");
371 : else
372 9724 : p += sprintf(p, "%02x", data[i + j]);
373 : }
374 :
375 : /*
376 : * skip over to the ascii dump column
377 : */
378 640 : p += sprintf(p, " |");
379 :
380 : /*
381 : * print each byte in ascii
382 : */
383 10364 : for (j = i; j < length && (j - i) < BYTES_PER_LINE; j++) {
384 9724 : if (j - i == BYTES_PER_LINE / 2)
385 604 : *p++ = ' ';
386 9724 : p += sprintf(p, "%c", (isprint(data[j])) ? data[j] : '.');
387 : }
388 640 : strcpy(p, "|\n");
389 640 : fputs(line_buf, dumpfile);
390 : }
391 58 : fputs("\n", dumpfile);
392 :
393 58 : fflush(dumpfile);
394 :
395 : #ifndef TDS_HAVE_MUTEX
396 : if (tds_append_mode) {
397 : if (dumpfile != stdout && dumpfile != stderr)
398 : fclose(dumpfile);
399 : }
400 : #endif
401 :
402 58 : tds_mutex_unlock(&g_dump_mutex);
403 :
404 : } /* tdsdump_dump_buf() */
405 : #define tdsdump_dump_buf TDSDUMP_BUF_FAST
406 :
407 :
408 : #undef tdsdump_log
409 : /**
410 : * Write a message to the debug log.
411 : * \param file name of the log file
412 : * \param level_line kind of detail to be included
413 : * \param fmt printf-like format string
414 : */
415 : void
416 6852 : tdsdump_log(const char* file, unsigned int level_line, const char *fmt, ...)
417 : {
418 6852 : const int debug_lvl = level_line & 15;
419 6852 : const int line = level_line >> 4;
420 : va_list ap;
421 : FILE *dumpfile;
422 :
423 6852 : if (((tds_debug_flags >> debug_lvl) & 1) == 0 || !tds_write_dump)
424 3022 : return;
425 :
426 6852 : if (!g_dumpfile && !g_dump_filename)
427 : return;
428 :
429 6852 : tds_mutex_lock(&g_dump_mutex);
430 :
431 6852 : if (current_thread_is_excluded()) {
432 3022 : tds_mutex_unlock(&g_dump_mutex);
433 3022 : return;
434 : }
435 :
436 3830 : dumpfile = g_dumpfile;
437 : #ifdef TDS_HAVE_MUTEX
438 3830 : if (tds_append_mode && dumpfile == NULL)
439 0 : dumpfile = g_dumpfile = tdsdump_append();
440 : #else
441 : if (tds_append_mode)
442 : dumpfile = tdsdump_append();
443 : #endif
444 :
445 3830 : if (dumpfile == NULL) {
446 0 : tds_mutex_unlock(&g_dump_mutex);
447 0 : return;
448 : }
449 :
450 3830 : tdsdump_start(dumpfile, file, line);
451 :
452 3830 : va_start(ap, fmt);
453 :
454 3830 : vfprintf(dumpfile, fmt, ap);
455 3830 : va_end(ap);
456 :
457 3830 : fflush(dumpfile);
458 :
459 : #ifndef TDS_HAVE_MUTEX
460 : if (tds_append_mode) {
461 : if (dumpfile != stdout && dumpfile != stderr)
462 : fclose(dumpfile);
463 : }
464 : #endif
465 3830 : tds_mutex_unlock(&g_dump_mutex);
466 : } /* tdsdump_log() */
467 : #define tdsdump_log TDSDUMP_LOG_FAST
468 :
469 :
470 : /**
471 : * Write a column value to the debug log.
472 : * \param col column to dump
473 : */
474 : void
475 0 : tdsdump_col(const TDSCOLUMN *col)
476 : {
477 : const char* type_name;
478 : char* data;
479 : TDS_SMALLINT type;
480 :
481 0 : assert(col);
482 0 : assert(col->column_data);
483 :
484 0 : type_name = tds_prtype(col->column_type);
485 0 : type = tds_get_conversion_type(col->column_type, col->column_size);
486 :
487 0 : switch(type) {
488 0 : case SYBCHAR:
489 : case SYBVARCHAR:
490 0 : if (col->column_cur_size >= 0) {
491 0 : data = tds_new0(char, 1 + col->column_cur_size);
492 0 : if (!data) {
493 0 : tdsdump_log(TDS_DBG_FUNC, "no memory to log data for type %s\n", type_name);
494 : return;
495 : }
496 0 : memcpy(data, col->column_data, col->column_cur_size);
497 0 : tdsdump_log(TDS_DBG_FUNC, "type %s has value \"%s\"\n", type_name, data);
498 0 : free(data);
499 : } else {
500 0 : tdsdump_log(TDS_DBG_FUNC, "type %s has value NULL\n", type_name);
501 : }
502 : break;
503 0 : case SYBINT1:
504 0 : tdsdump_log(TDS_DBG_FUNC, "type %s has value %d\n", type_name, (int)*(TDS_TINYINT*)col->column_data);
505 : break;
506 0 : case SYBINT2:
507 0 : tdsdump_log(TDS_DBG_FUNC, "type %s has value %d\n", type_name, (int)*(TDS_SMALLINT*)col->column_data);
508 : break;
509 0 : case SYBINT4:
510 0 : tdsdump_log(TDS_DBG_FUNC, "type %s has value %d\n", type_name, (int)*(TDS_INT*)col->column_data);
511 : break;
512 0 : case SYBREAL:
513 0 : tdsdump_log(TDS_DBG_FUNC, "type %s has value %f\n", type_name, (double)*(TDS_REAL*)col->column_data);
514 : break;
515 0 : case SYBFLT8:
516 0 : tdsdump_log(TDS_DBG_FUNC, "type %s has value %f\n", type_name, (double)*(TDS_FLOAT*)col->column_data);
517 : break;
518 0 : default:
519 0 : tdsdump_log(TDS_DBG_FUNC, "cannot log data for type %s\n", type_name);
520 : break;
521 : }
522 : }
|