2008-10-25 16:11:27 +02:00
|
|
|
/*
|
2010-01-12 17:38:36 +01:00
|
|
|
* debug.c
|
2010-01-12 19:09:36 +01:00
|
|
|
* contains utilitary functions for debugging
|
2008-10-25 16:11:27 +02:00
|
|
|
*
|
|
|
|
|
* Copyright (c) 2008 Jonathan Beck All Rights Reserved.
|
2010-01-22 13:45:53 +01:00
|
|
|
* Copyright (c) 2010 Martin S. All Rights Reserved.
|
2008-10-25 16:11:27 +02:00
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2015-01-28 01:27:59 +01:00
|
|
|
*
|
2008-10-25 16:11:27 +02:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
2015-01-28 01:27:59 +01:00
|
|
|
*
|
2008-10-25 16:11:27 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2015-01-28 01:27:59 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2008-10-25 16:11:27 +02:00
|
|
|
*/
|
2010-01-12 19:09:36 +01:00
|
|
|
|
2010-04-03 04:15:26 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
2008-10-25 16:11:27 +02:00
|
|
|
#include <stdarg.h>
|
2010-01-12 19:09:36 +01:00
|
|
|
#define _GNU_SOURCE 1
|
|
|
|
|
#define __USE_GNU 1
|
2008-10-25 16:11:27 +02:00
|
|
|
#include <stdio.h>
|
2009-07-25 01:08:41 +02:00
|
|
|
#include <stdint.h>
|
2010-01-12 19:09:36 +01:00
|
|
|
#include <stdlib.h>
|
2011-09-03 02:10:48 +02:00
|
|
|
#include <time.h>
|
2009-07-25 01:08:41 +02:00
|
|
|
|
2010-01-12 17:38:36 +01:00
|
|
|
#include "debug.h"
|
2010-01-28 22:18:41 +01:00
|
|
|
#include "libimobiledevice/libimobiledevice.h"
|
2014-10-03 16:17:04 +02:00
|
|
|
#include "src/idevice.h"
|
2008-10-25 16:11:27 +02:00
|
|
|
|
2011-09-10 19:49:16 +02:00
|
|
|
#ifndef STRIP_DEBUG_CODE
|
|
|
|
|
#include "asprintf.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-10-27 02:40:14 +01:00
|
|
|
static int debug_level;
|
|
|
|
|
|
|
|
|
|
void internal_set_debug_level(int level)
|
|
|
|
|
{
|
|
|
|
|
debug_level = level;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-22 22:59:47 +02:00
|
|
|
#define MAX_PRINT_LEN 16*1024
|
|
|
|
|
|
2011-03-11 20:38:57 +01:00
|
|
|
#ifndef STRIP_DEBUG_CODE
|
2010-01-12 19:09:36 +01:00
|
|
|
static void debug_print_line(const char *func, const char *file, int line, const char *buffer)
|
|
|
|
|
{
|
|
|
|
|
char *str_time = NULL;
|
|
|
|
|
char *header = NULL;
|
|
|
|
|
time_t the_time;
|
|
|
|
|
|
|
|
|
|
time(&the_time);
|
2011-09-03 02:10:48 +02:00
|
|
|
str_time = (char*)malloc(255);
|
2010-01-12 19:09:36 +01:00
|
|
|
strftime(str_time, 254, "%H:%M:%S", localtime (&the_time));
|
|
|
|
|
|
|
|
|
|
/* generate header text */
|
2010-01-13 01:21:31 +01:00
|
|
|
(void)asprintf(&header, "%s %s:%d %s()", str_time, file, line, func);
|
2010-01-12 19:09:36 +01:00
|
|
|
free (str_time);
|
|
|
|
|
|
2010-01-22 13:45:53 +01:00
|
|
|
/* trim ending newlines */
|
|
|
|
|
|
|
|
|
|
/* print header */
|
2010-01-13 01:21:31 +01:00
|
|
|
printf ("%s: ", header);
|
2010-01-12 19:09:36 +01:00
|
|
|
|
2010-01-22 13:45:53 +01:00
|
|
|
/* print actual debug content */
|
2010-01-12 19:09:36 +01:00
|
|
|
printf ("%s\n", buffer);
|
|
|
|
|
|
|
|
|
|
/* flush this output, as we need to debug */
|
|
|
|
|
fflush (stdout);
|
|
|
|
|
|
|
|
|
|
free (header);
|
|
|
|
|
}
|
2011-03-11 20:38:57 +01:00
|
|
|
#endif
|
2010-01-12 19:09:36 +01:00
|
|
|
|
2013-09-27 12:02:20 +02:00
|
|
|
void debug_info_real(const char *func, const char *file, int line, const char *format, ...)
|
2008-10-25 16:11:27 +02:00
|
|
|
{
|
2008-10-25 17:29:29 +02:00
|
|
|
#ifndef STRIP_DEBUG_CODE
|
2008-10-25 16:11:27 +02:00
|
|
|
va_list args;
|
2010-01-12 19:09:36 +01:00
|
|
|
char *buffer = NULL;
|
|
|
|
|
|
2014-10-27 02:40:14 +01:00
|
|
|
if (!debug_level)
|
2010-01-12 19:09:36 +01:00
|
|
|
return;
|
|
|
|
|
|
2008-10-25 16:11:27 +02:00
|
|
|
/* run the real fprintf */
|
|
|
|
|
va_start(args, format);
|
2010-01-12 19:49:40 +01:00
|
|
|
(void)vasprintf(&buffer, format, args);
|
2008-10-25 16:11:27 +02:00
|
|
|
va_end(args);
|
|
|
|
|
|
2010-01-12 19:09:36 +01:00
|
|
|
debug_print_line(func, file, line, buffer);
|
|
|
|
|
|
|
|
|
|
free(buffer);
|
2008-10-25 16:11:27 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-27 12:02:20 +02:00
|
|
|
void debug_buffer(const char *data, const int length)
|
2008-10-25 16:11:27 +02:00
|
|
|
{
|
2008-10-25 17:29:29 +02:00
|
|
|
#ifndef STRIP_DEBUG_CODE
|
2009-01-08 18:17:21 +01:00
|
|
|
int i;
|
|
|
|
|
int j;
|
|
|
|
|
unsigned char c;
|
2008-10-25 16:11:27 +02:00
|
|
|
|
2014-10-27 02:40:14 +01:00
|
|
|
if (debug_level) {
|
2009-01-08 18:17:21 +01:00
|
|
|
for (i = 0; i < length; i += 16) {
|
|
|
|
|
fprintf(stderr, "%04x: ", i);
|
|
|
|
|
for (j = 0; j < 16; j++) {
|
|
|
|
|
if (i + j >= length) {
|
|
|
|
|
fprintf(stderr, " ");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2013-07-13 16:40:13 +02:00
|
|
|
fprintf(stderr, "%02x ", *(data + i + j) & 0xff);
|
2009-01-08 18:17:21 +01:00
|
|
|
}
|
|
|
|
|
fprintf(stderr, " | ");
|
|
|
|
|
for (j = 0; j < 16; j++) {
|
|
|
|
|
if (i + j >= length)
|
|
|
|
|
break;
|
|
|
|
|
c = *(data + i + j);
|
|
|
|
|
if ((c < 32) || (c > 127)) {
|
|
|
|
|
fprintf(stderr, ".");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
fprintf(stderr, "%c", c);
|
|
|
|
|
}
|
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
}
|
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
}
|
2008-10-25 16:11:27 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-27 12:02:20 +02:00
|
|
|
void debug_buffer_to_file(const char *file, const char *data, const int length)
|
2008-10-25 16:11:27 +02:00
|
|
|
{
|
2008-10-25 17:29:29 +02:00
|
|
|
#ifndef STRIP_DEBUG_CODE
|
2014-10-27 02:40:14 +01:00
|
|
|
if (debug_level) {
|
2011-11-26 15:31:46 +01:00
|
|
|
FILE *f = fopen(file, "wb");
|
2010-01-12 19:09:36 +01:00
|
|
|
fwrite(data, 1, length, f);
|
|
|
|
|
fflush(f);
|
|
|
|
|
fclose(f);
|
2008-10-25 16:11:27 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2010-01-12 19:50:07 +01:00
|
|
|
|
2013-09-27 12:02:20 +02:00
|
|
|
void debug_plist_real(const char *func, const char *file, int line, plist_t plist)
|
2010-01-12 19:50:07 +01:00
|
|
|
{
|
2010-01-13 01:22:50 +01:00
|
|
|
#ifndef STRIP_DEBUG_CODE
|
2010-01-12 19:50:07 +01:00
|
|
|
if (!plist)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
char *buffer = NULL;
|
|
|
|
|
uint32_t length = 0;
|
|
|
|
|
plist_to_xml(plist, &buffer, &length);
|
2010-01-22 13:45:53 +01:00
|
|
|
|
|
|
|
|
/* get rid of ending newline as one is already added in the debug line */
|
|
|
|
|
if (buffer[length-1] == '\n')
|
|
|
|
|
buffer[length-1] = '\0';
|
|
|
|
|
|
2014-10-22 22:59:47 +02:00
|
|
|
if (length <= MAX_PRINT_LEN)
|
|
|
|
|
debug_info_real(func, file, line, "printing %i bytes plist:\n%s", length, buffer);
|
|
|
|
|
else
|
|
|
|
|
debug_info_real(func, file, line, "supress printing %i bytes plist...\n", length);
|
|
|
|
|
|
2010-01-12 19:50:07 +01:00
|
|
|
free(buffer);
|
2010-01-13 01:22:50 +01:00
|
|
|
#endif
|
2010-01-12 19:50:07 +01:00
|
|
|
}
|
|
|
|
|
|