1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include <stdio.h>
17#include <string.h>
18#include <errno.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <fcntl.h>
22#include <unistd.h>
23
24// This is the pathname to the sysfs file that enables and disables
25// tracing on the qemu emulator.
26#define SYS_QEMU_TRACE_STATE "/sys/qemu_trace/state"
27
28
29// This is the pathname to the sysfs file that adds new (address, symbol)
30// pairs to the trace.
31#define SYS_QEMU_TRACE_SYMBOL "/sys/qemu_trace/symbol"
32
33// The maximum length of a symbol name
34#define MAX_SYMBOL_NAME_LENGTH (4 * 1024)
35
36// Allow space in the buffer for the address plus whitespace.
37#define MAX_BUF_SIZE (MAX_SYMBOL_NAME_LENGTH + 20)
38
39// return 0 on success, or an error if the qemu driver cannot be opened
40int qemu_start_tracing()
41{
42    int fd = open(SYS_QEMU_TRACE_STATE, O_WRONLY);
43    if (fd < 0)
44        return fd;
45    write(fd, "1\n", 2);
46    close(fd);
47    return 0;
48}
49
50int qemu_stop_tracing()
51{
52    int fd = open(SYS_QEMU_TRACE_STATE, O_WRONLY);
53    if (fd < 0)
54        return fd;
55    write(fd, "0\n", 2);
56    close(fd);
57    return 0;
58}
59
60int qemu_add_mapping(unsigned int addr, const char *name)
61{
62    char buf[MAX_BUF_SIZE];
63
64    if (strlen(name) > MAX_SYMBOL_NAME_LENGTH)
65        return EINVAL;
66    int fd = open(SYS_QEMU_TRACE_SYMBOL, O_WRONLY);
67    if (fd < 0)
68        return fd;
69    sprintf(buf, "%x %s\n", addr, name);
70    write(fd, buf, strlen(buf));
71    close(fd);
72    return 0;
73}
74
75int qemu_remove_mapping(unsigned int addr)
76{
77    char buf[MAX_BUF_SIZE];
78
79    int fd = open(SYS_QEMU_TRACE_SYMBOL, O_WRONLY);
80    if (fd < 0)
81        return fd;
82    sprintf(buf, "%x\n", addr);
83    write(fd, buf, strlen(buf));
84    close(fd);
85    return 0;
86}
87