The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <errno.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <unistd.h> |
| 8 | |
| 9 | // This is the pathname to the sysfs file that enables and disables |
| 10 | // tracing on the qemu emulator. |
| 11 | #define SYS_QEMU_TRACE_STATE "/sys/qemu_trace/state" |
| 12 | |
| 13 | |
| 14 | // This is the pathname to the sysfs file that adds new (address, symbol) |
| 15 | // pairs to the trace. |
| 16 | #define SYS_QEMU_TRACE_SYMBOL "/sys/qemu_trace/symbol" |
| 17 | |
| 18 | // The maximum length of a symbol name |
| 19 | #define MAX_SYMBOL_NAME_LENGTH (4 * 1024) |
| 20 | |
| 21 | // Allow space in the buffer for the address plus whitespace. |
| 22 | #define MAX_BUF_SIZE (MAX_SYMBOL_NAME_LENGTH + 20) |
| 23 | |
| 24 | // return 0 on success, or an error if the qemu driver cannot be opened |
| 25 | int qemu_start_tracing() |
| 26 | { |
| 27 | int fd = open(SYS_QEMU_TRACE_STATE, O_WRONLY); |
| 28 | if (fd < 0) |
| 29 | return fd; |
| 30 | write(fd, "1\n", 2); |
| 31 | close(fd); |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | int qemu_stop_tracing() |
| 36 | { |
| 37 | int fd = open(SYS_QEMU_TRACE_STATE, O_WRONLY); |
| 38 | if (fd < 0) |
| 39 | return fd; |
| 40 | write(fd, "0\n", 2); |
| 41 | close(fd); |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | int qemu_add_mapping(unsigned int addr, const char *name) |
| 46 | { |
| 47 | char buf[MAX_BUF_SIZE]; |
| 48 | |
| 49 | if (strlen(name) > MAX_SYMBOL_NAME_LENGTH) |
| 50 | return EINVAL; |
| 51 | int fd = open(SYS_QEMU_TRACE_SYMBOL, O_WRONLY); |
| 52 | if (fd < 0) |
| 53 | return fd; |
| 54 | sprintf(buf, "%x %s\n", addr, name); |
| 55 | write(fd, buf, strlen(buf)); |
| 56 | close(fd); |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | int qemu_remove_mapping(unsigned int addr) |
| 61 | { |
| 62 | char buf[MAX_BUF_SIZE]; |
| 63 | |
| 64 | int fd = open(SYS_QEMU_TRACE_SYMBOL, O_WRONLY); |
| 65 | if (fd < 0) |
| 66 | return fd; |
| 67 | sprintf(buf, "%x\n", addr); |
| 68 | write(fd, buf, strlen(buf)); |
| 69 | close(fd); |
| 70 | return 0; |
| 71 | } |