aboutsummaryrefslogtreecommitdiff
path: root/hw/misc/android_adb_dbg.c
blob: a3b13849c52e451bf1666bd85573041e6c21883f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * ARM Android emulator adb backend
 *
 * Copyright (c) 2014 Linaro Limited
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2 or later, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Handle connections to the qemud:adb pipe from Android guests and route
 * traffic over this pipe to the host adb server connecting to the qemu
 * process on a tcp socket.
 */

#include "hw/misc/android_pipe.h"


// #define DEBUG_ADB_DEBUG

#ifdef DEBUG_ADB_DEBUG
#define DPRINTF(fmt, ...) \
do { fprintf(stderr, "adb_debug: " fmt , ## __VA_ARGS__); } while (0)
#else
#define DPRINTF(fmt, ...) do {} while(0)
#endif

static bool _pipe_backend_initialized = false;

typedef struct {
    void*     hwpipe;
} adb_dbg_pipe;

static void* adb_dbg_pipe_init(void *hwpipe,void *opaque, const char *args)
{
    adb_dbg_pipe *apipe;

    DPRINTF("%s: hwpipe=%p", __FUNCTION__, hwpipe);
    apipe = g_malloc0(sizeof(adb_dbg_pipe));
    apipe->hwpipe = hwpipe;
    return apipe;
}

static void adb_dbg_pipe_close(void *opaque)
{
    adb_dbg_pipe *apipe = opaque;

    DPRINTF("%s: hwpipe=%p", __FUNCTION__, apipe->hwpipe);
    g_free(apipe);
}

static unsigned pipe_buffers_len(const AndroidPipeBuffer *buffers, int cnt)
{
    unsigned len = 0;
    while (cnt > 0) {
        len += buffers[0].size;
        cnt --;
        buffers++;
    }
    return len;
}

/* Guest is sending us some ADB traces, print it on stderr */
static int adb_dbg_pipe_send(void *opaque, const AndroidPipeBuffer *buffers,
                             int cnt)
{
    int ret = 0;
    char *data;
    unsigned total_len = pipe_buffers_len(buffers, cnt);

    DPRINTF("%s: something is coming over the wire....\n", __func__);

    if (total_len > 4 * 4096) {
        return total_len;  /* ignore outrageous requests */
    }

    data = g_malloc(total_len);

    while (cnt > 0) {
        memcpy(data, buffers[0].data, buffers[0].size);
        data += buffers[0].size;
        ret += buffers[0].size;

        cnt--;
        buffers++;
    }

    fprintf(stderr, "ADB: %s", data);

    return ret;
}

/* Guest is reading data: Act as the zero pipe and just return a bunch of
 * zeros.
 */
static int adb_dbg_pipe_recv(void *opaque, AndroidPipeBuffer *buffers,
                             int cnt)
{
    int ret = 0;

    while (cnt > 0) {
        ret += buffers[0].size;
        memset(buffers[0].data, 0, buffers[0].size);
        buffers++;
        cnt--;
    }
    return ret;
}

static unsigned adb_dbg_pipe_poll(void *opaque)
{
    return PIPE_POLL_IN | PIPE_POLL_OUT;
}

static void
adb_dbg_pipe_wake_on(void *opaque, int flags)
{
    /* nothing to do here, we never block */
}

static const AndroidPipeFuncs adb_dbg_pipe_funcs = {
    adb_dbg_pipe_init,
    adb_dbg_pipe_close,
    adb_dbg_pipe_send,
    adb_dbg_pipe_recv,
    adb_dbg_pipe_poll,
    adb_dbg_pipe_wake_on,
};

void android_adb_dbg_backend_init(void)
{
    if (_pipe_backend_initialized) {
        return;
    }

    android_pipe_add_type("qemud:adb-debug", NULL, &adb_dbg_pipe_funcs);
    _pipe_backend_initialized = true;
}