| Petr Machata | e99af27 | 2012-10-26 00:29:52 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * This file is part of ltrace. |
| 3 | * Copyright (C) 2003,2008,2009 Juan Cespedes |
| 4 | * Copyright (C) 2006 Ian Wienand |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2 of the |
| 9 | * License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 19 | * 02110-1301 USA |
| 20 | */ |
| 21 | |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdarg.h> |
| 24 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 25 | #include "common.h" |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 26 | |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 27 | void |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 28 | debug_(int level, const char *file, int line, const char *fmt, ...) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 29 | char buf[1024]; |
| 30 | va_list args; |
| 31 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 32 | if (!(options.debug & level)) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 33 | return; |
| 34 | } |
| 35 | va_start(args, fmt); |
| 36 | vsnprintf(buf, 1024, fmt, args); |
| 37 | va_end(args); |
| 38 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 39 | output_line(NULL, "DEBUG: %s:%d: %s", file, line, buf); |
| Petr Machata | 5d93a41 | 2012-04-13 18:44:26 +0200 | [diff] [blame] | 40 | fflush(options.output); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 41 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 42 | |
| Juan Cespedes | 5c68204 | 2009-05-21 15:59:56 +0200 | [diff] [blame] | 43 | /* |
| 44 | * The following section provides a way to print things, like hex dumps, |
| 45 | * with out using buffered output. This was written by Steve Munroe of IBM. |
| 46 | */ |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 47 | |
| 48 | #include <stdio.h> |
| 49 | #include <errno.h> |
| 50 | #include <unistd.h> |
| 51 | #include <stdlib.h> |
| 52 | #include <sys/ptrace.h> |
| 53 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 54 | static int |
| 55 | xwritehexl(long i) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 56 | int rc = 0; |
| 57 | char text[17]; |
| 58 | int j; |
| 59 | unsigned long temp = (unsigned long)i; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 60 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 61 | for (j = 15; j >= 0; j--) { |
| 62 | char c; |
| 63 | c = (char)((temp & 0x0f) + '0'); |
| 64 | if (c > '9') { |
| 65 | c = (char)(c + ('a' - '9' - 1)); |
| 66 | } |
| 67 | text[j] = c; |
| 68 | temp = temp >> 4; |
| 69 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 70 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 71 | rc = write(1, text, 16); |
| 72 | return rc; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 75 | static int |
| 76 | xwritec(char c) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 77 | char temp = c; |
| 78 | char *text = &temp; |
| 79 | int rc = 0; |
| 80 | rc = write(1, text, 1); |
| 81 | return rc; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 84 | static int |
| 85 | xwritecr(void) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 86 | return xwritec('\n'); |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 89 | static int |
| 90 | xwritedump(void *ptr, long addr, int len) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 91 | int rc = 0; |
| 92 | long *tprt = (long *)ptr; |
| 93 | int i; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 94 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 95 | for (i = 0; i < len; i += 8) { |
| 96 | xwritehexl(addr); |
| 97 | xwritec('-'); |
| 98 | xwritec('>'); |
| 99 | xwritehexl(*tprt++); |
| 100 | xwritecr(); |
| 101 | addr += sizeof(long); |
| 102 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 103 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 104 | return rc; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 107 | int |
| 108 | xinfdump(long pid, void *ptr, int len) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 109 | int rc; |
| 110 | int i; |
| Juan Cespedes | e672ad1 | 2009-02-11 18:49:18 +0100 | [diff] [blame] | 111 | long wrdcnt; |
| 112 | long *infwords; |
| 113 | long addr; |
| 114 | |
| 115 | wrdcnt = len / sizeof(long) + 1; |
| 116 | infwords = malloc(wrdcnt * sizeof(long)); |
| 117 | if (!infwords) { |
| 118 | perror("ltrace: malloc"); |
| 119 | exit(1); |
| 120 | } |
| 121 | addr = (long)ptr; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 122 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 123 | addr = ((addr + sizeof(long) - 1) / sizeof(long)) * sizeof(long); |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 124 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 125 | for (i = 0; i < wrdcnt; ++i) { |
| Petr Machata | f4c56d4 | 2012-05-05 01:42:14 +0200 | [diff] [blame] | 126 | infwords[i] = ptrace(PTRACE_PEEKTEXT, pid, (void *)addr, NULL); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 127 | addr += sizeof(long); |
| 128 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 129 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 130 | rc = xwritedump(infwords, (long)ptr, len); |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 131 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 132 | free(infwords); |
| 133 | return rc; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 134 | } |