packaging: update to 0.7.91
[platform/upstream/ltrace.git] / debug.c
1 /*
2  * This file is part of ltrace.
3  * Copyright (C) 2012 Petr Machata, Red Hat Inc.
4  * Copyright (C) 2003,2008,2009 Juan Cespedes
5  * Copyright (C) 2006 Ian Wienand
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  */
22
23 #include <stdio.h>
24 #include <stdarg.h>
25
26 #include "common.h"
27 #include "backend.h"
28
29 void
30 debug_(int level, const char *file, int line, const char *fmt, ...) {
31         char buf[1024];
32         va_list args;
33
34         if (!(options.debug & level)) {
35                 return;
36         }
37         va_start(args, fmt);
38         vsnprintf(buf, 1024, fmt, args);
39         va_end(args);
40
41         output_line(NULL, "DEBUG: %s:%d: %s", file, line, buf);
42         fflush(options.output);
43 }
44
45 static int
46 xwritedump(long *ptr, arch_addr_t addr, size_t count)
47 {
48         size_t i;
49         for (i = 0; i < count; ++i) {
50                 if (fprintf(stderr, "%p->%0*lx\n",
51                             addr, 2 * (int)sizeof(long), ptr[i]) < 0)
52                         return -1;
53                 addr += sizeof(long);
54         }
55
56         return 0;
57 }
58
59 int
60 xinfdump(struct process *proc, arch_addr_t addr, size_t length)
61 {
62         unsigned char buf[length];
63         size_t got = umovebytes(proc, addr, buf, length);
64         if (got == (size_t)-1)
65                 return -1;
66         return xwritedump((long *)buf, addr, got / sizeof(long));
67 }