debug.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <stdarg.h>
  26. #define _GNU_SOURCE 1
  27. #define __USE_GNU 1
  28. #include <stdio.h>
  29. #include <stdint.h>
  30. #include <stdlib.h>
  31. #include <time.h>
  32. #include "debug.h"
  33. #include "libimobiledevice/libimobiledevice.h"
  34. #include "src/idevice.h"
  35. #ifndef STRIP_DEBUG_CODE
  36. #include "asprintf.h"
  37. #endif
  38. static int debug_level;
  39. void internal_set_debug_level(int level)
  40. {
  41. debug_level = level;
  42. }
  43. #define MAX_PRINT_LEN 16*1024
  44. #ifndef STRIP_DEBUG_CODE
  45. static void debug_print_line(const char *func, const char *file, int line, const char *buffer)
  46. {
  47. char *str_time = NULL;
  48. char *header = NULL;
  49. time_t the_time;
  50. time(&the_time);
  51. str_time = (char*)malloc(255);
  52. strftime(str_time, 254, "%H:%M:%S", localtime (&the_time));
  53. /* generate header text */
  54. (void)asprintf(&header, "%s %s:%d %s()", str_time, file, line, func);
  55. free (str_time);
  56. /* trim ending newlines */
  57. /* print header */
  58. printf ("%s: ", header);
  59. /* print actual debug content */
  60. printf ("%s\n", buffer);
  61. /* flush this output, as we need to debug */
  62. fflush (stdout);
  63. free (header);
  64. }
  65. #endif
  66. void debug_info_real(const char *func, const char *file, int line, const char *format, ...)
  67. {
  68. #ifndef STRIP_DEBUG_CODE
  69. va_list args;
  70. char *buffer = NULL;
  71. if (!debug_level)
  72. return;
  73. /* run the real fprintf */
  74. va_start(args, format);
  75. (void)vasprintf(&buffer, format, args);
  76. va_end(args);
  77. debug_print_line(func, file, line, buffer);
  78. free(buffer);
  79. #endif
  80. }
  81. void debug_buffer(const char *data, const int length)
  82. {
  83. #ifndef STRIP_DEBUG_CODE
  84. int i;
  85. int j;
  86. unsigned char c;
  87. if (debug_level) {
  88. for (i = 0; i < length; i += 16) {
  89. fprintf(stderr, "%04x: ", i);
  90. for (j = 0; j < 16; j++) {
  91. if (i + j >= length) {
  92. fprintf(stderr, " ");
  93. continue;
  94. }
  95. fprintf(stderr, "%02x ", *(data + i + j) & 0xff);
  96. }
  97. fprintf(stderr, " | ");
  98. for (j = 0; j < 16; j++) {
  99. if (i + j >= length)
  100. break;
  101. c = *(data + i + j);
  102. if ((c < 32) || (c > 127)) {
  103. fprintf(stderr, ".");
  104. continue;
  105. }
  106. fprintf(stderr, "%c", c);
  107. }
  108. fprintf(stderr, "\n");
  109. }
  110. fprintf(stderr, "\n");
  111. }
  112. #endif
  113. }
  114. void debug_buffer_to_file(const char *file, const char *data, const int length)
  115. {
  116. #ifndef STRIP_DEBUG_CODE
  117. if (debug_level) {
  118. FILE *f = fopen(file, "wb");
  119. fwrite(data, 1, length, f);
  120. fflush(f);
  121. fclose(f);
  122. }
  123. #endif
  124. }
  125. void debug_plist_real(const char *func, const char *file, int line, plist_t plist)
  126. {
  127. #ifndef STRIP_DEBUG_CODE
  128. if (!plist)
  129. return;
  130. char *buffer = NULL;
  131. uint32_t length = 0;
  132. plist_to_xml(plist, &buffer, &length);
  133. /* get rid of ending newline as one is already added in the debug line */
  134. if (buffer[length-1] == '\n')
  135. buffer[length-1] = '\0';
  136. if (length <= MAX_PRINT_LEN)
  137. debug_info_real(func, file, line, "printing %i bytes plist:\n%s", length, buffer);
  138. else
  139. debug_info_real(func, file, line, "supress printing %i bytes plist...\n", length);
  140. free(buffer);
  141. #endif
  142. }