diff options
Diffstat (limited to 'common/debug.c')
| -rw-r--r-- | common/debug.c | 174 | 
1 files changed, 174 insertions, 0 deletions
| diff --git a/common/debug.c b/common/debug.c new file mode 100644 index 0000000..7a593fc --- /dev/null +++ b/common/debug.c | |||
| @@ -0,0 +1,174 @@ | |||
| 1 | /* | ||
| 2 | * debug.c | ||
| 3 | * contains utilitary functions for debugging | ||
| 4 | * | ||
| 5 | * Copyright (c) 2008 Jonathan Beck All Rights Reserved. | ||
| 6 | * Copyright (c) 2010 Martin S. All Rights Reserved. | ||
| 7 | * | ||
| 8 | * This library is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * This library is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with this library; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #ifdef HAVE_CONFIG_H | ||
| 24 | #include <config.h> | ||
| 25 | #endif | ||
| 26 | #include <stdarg.h> | ||
| 27 | #define _GNU_SOURCE 1 | ||
| 28 | #define __USE_GNU 1 | ||
| 29 | #include <stdio.h> | ||
| 30 | #include <stdint.h> | ||
| 31 | #include <stdlib.h> | ||
| 32 | #include <time.h> | ||
| 33 | #ifndef _WIN32 | ||
| 34 | #include <sys/time.h> | ||
| 35 | #endif | ||
| 36 | |||
| 37 | #include "src/idevice.h" | ||
| 38 | #include "debug.h" | ||
| 39 | #include "libimobiledevice/libimobiledevice.h" | ||
| 40 | |||
| 41 | #ifndef STRIP_DEBUG_CODE | ||
| 42 | #include "asprintf.h" | ||
| 43 | #endif | ||
| 44 | |||
| 45 | static int debug_level; | ||
| 46 | |||
| 47 | void internal_set_debug_level(int level) | ||
| 48 | { | ||
| 49 | debug_level = level; | ||
| 50 | } | ||
| 51 | |||
| 52 | #define MAX_PRINT_LEN (16*1024) | ||
| 53 | |||
| 54 | #ifndef STRIP_DEBUG_CODE | ||
| 55 | static void debug_print_line(const char *func, const char *file, int line, const char *buffer) | ||
| 56 | { | ||
| 57 | char str_time[24]; | ||
| 58 | #ifdef _WIN32 | ||
| 59 | SYSTEMTIME lt; | ||
| 60 | GetLocalTime(<); | ||
| 61 | snprintf(str_time, 24, "%02d:%02d:%02d.%03d", lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds); | ||
| 62 | #else | ||
| 63 | #ifdef HAVE_GETTIMEOFDAY | ||
| 64 | struct timeval tv; | ||
| 65 | struct tm *tp; | ||
| 66 | gettimeofday(&tv, NULL); | ||
| 67 | #ifdef HAVE_LOCALTIME_R | ||
| 68 | struct tm tp_; | ||
| 69 | tp = localtime_r(&tv.tv_sec, &tp_); | ||
| 70 | #else | ||
| 71 | tp = localtime(&tv.tv_sec); | ||
| 72 | #endif | ||
| 73 | strftime(str_time, 9, "%H:%M:%S", tp); | ||
| 74 | snprintf(str_time+8, 10, ".%03d", (int)tv.tv_usec/1000); | ||
| 75 | #else | ||
| 76 | time_t the_time; | ||
| 77 | time(&the_time); | ||
| 78 | strftime(str_time, 15, "%H:%M:%S", localtime (&the_time)); | ||
| 79 | #endif | ||
| 80 | #endif | ||
| 81 | fprintf(stderr, "%s %s:%d %s(): %s\n", str_time, file, line, func, buffer); | ||
| 82 | } | ||
| 83 | #endif | ||
| 84 | |||
| 85 | void debug_info_real(const char *func, const char *file, int line, const char *format, ...) | ||
| 86 | { | ||
| 87 | #ifndef STRIP_DEBUG_CODE | ||
| 88 | va_list args; | ||
| 89 | char *buffer = NULL; | ||
| 90 | |||
| 91 | if (!debug_level) | ||
| 92 | return; | ||
| 93 | |||
| 94 | /* run the real fprintf */ | ||
| 95 | va_start(args, format); | ||
| 96 | if(vasprintf(&buffer, format, args)<0){} | ||
| 97 | va_end(args); | ||
| 98 | |||
| 99 | debug_print_line(func, file, line, buffer); | ||
| 100 | |||
| 101 | free(buffer); | ||
| 102 | #endif | ||
| 103 | } | ||
| 104 | |||
| 105 | void debug_buffer(const char *data, const int length) | ||
| 106 | { | ||
| 107 | #ifndef STRIP_DEBUG_CODE | ||
| 108 | int i; | ||
| 109 | int j; | ||
| 110 | unsigned char c; | ||
| 111 | |||
| 112 | if (debug_level) { | ||
| 113 | for (i = 0; i < length; i += 16) { | ||
| 114 | fprintf(stderr, "%04x: ", i); | ||
| 115 | for (j = 0; j < 16; j++) { | ||
| 116 | if (i + j >= length) { | ||
| 117 | fprintf(stderr, " "); | ||
| 118 | continue; | ||
| 119 | } | ||
| 120 | fprintf(stderr, "%02x ", *(data + i + j) & 0xff); | ||
| 121 | } | ||
| 122 | fprintf(stderr, " | "); | ||
| 123 | for (j = 0; j < 16; j++) { | ||
| 124 | if (i + j >= length) | ||
| 125 | break; | ||
| 126 | c = *(data + i + j); | ||
| 127 | if ((c < 32) || (c > 127)) { | ||
| 128 | fprintf(stderr, "."); | ||
| 129 | continue; | ||
| 130 | } | ||
| 131 | fprintf(stderr, "%c", c); | ||
| 132 | } | ||
| 133 | fprintf(stderr, "\n"); | ||
| 134 | } | ||
| 135 | fprintf(stderr, "\n"); | ||
| 136 | } | ||
| 137 | #endif | ||
| 138 | } | ||
| 139 | |||
| 140 | void debug_buffer_to_file(const char *file, const char *data, const int length) | ||
| 141 | { | ||
| 142 | #ifndef STRIP_DEBUG_CODE | ||
| 143 | if (debug_level) { | ||
| 144 | FILE *f = fopen(file, "wb"); | ||
| 145 | fwrite(data, 1, length, f); | ||
| 146 | fflush(f); | ||
| 147 | fclose(f); | ||
| 148 | } | ||
| 149 | #endif | ||
| 150 | } | ||
| 151 | |||
| 152 | void debug_plist_real(const char *func, const char *file, int line, plist_t plist) | ||
| 153 | { | ||
| 154 | #ifndef STRIP_DEBUG_CODE | ||
| 155 | if (!plist) | ||
| 156 | return; | ||
| 157 | |||
| 158 | char *buffer = NULL; | ||
| 159 | uint32_t length = 0; | ||
| 160 | plist_to_xml(plist, &buffer, &length); | ||
| 161 | |||
| 162 | /* get rid of ending newline as one is already added in the debug line */ | ||
| 163 | if (buffer[length-1] == '\n') | ||
| 164 | buffer[length-1] = '\0'; | ||
| 165 | |||
| 166 | if (length <= MAX_PRINT_LEN) | ||
| 167 | debug_info_real(func, file, line, "printing %i bytes plist:\n%s", length, buffer); | ||
| 168 | else | ||
| 169 | debug_info_real(func, file, line, "supress printing %i bytes plist...\n", length); | ||
| 170 | |||
| 171 | free(buffer); | ||
| 172 | #endif | ||
| 173 | } | ||
| 174 | |||
