1 /*
2  * Copyright (C) 2015 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 
17 /*
18  * Implements ADB tracing inside the emulator.
19  */
20 
21 #include <stdarg.h>
22 
23 #include "sysdeps.h"
24 #include "qemu_tracing.h"
25 
26 /*
27  * Redefine open and write for qemu_pipe.h that contains inlined references
28  * to those routines. We will redifine them back after qemu_pipe.h inclusion.
29  */
30 
31 #undef open
32 #undef write
33 #define open    adb_open
34 #define write   adb_write
35 #include <hardware/qemu_pipe.h>
36 #undef open
37 #undef write
38 #define open    ___xxx_open
39 #define write   ___xxx_write
40 
41 /* A handle to adb-debug qemud service in the emulator. */
42 int   adb_debug_qemu = -1;
43 
44 /* Initializes connection with the adb-debug qemud service in the emulator. */
adb_qemu_trace_init(void)45 int adb_qemu_trace_init(void)
46 {
47     char con_name[32];
48 
49     if (adb_debug_qemu >= 0) {
50         return 0;
51     }
52 
53     /* adb debugging QEMUD service connection request. */
54     snprintf(con_name, sizeof(con_name), "qemud:adb-debug");
55     adb_debug_qemu = qemu_pipe_open(con_name);
56     return (adb_debug_qemu >= 0) ? 0 : -1;
57 }
58 
adb_qemu_trace(const char * fmt,...)59 void adb_qemu_trace(const char* fmt, ...)
60 {
61     va_list args;
62     va_start(args, fmt);
63     char msg[1024];
64 
65     if (adb_debug_qemu >= 0) {
66         vsnprintf(msg, sizeof(msg), fmt, args);
67         adb_write(adb_debug_qemu, msg, strlen(msg));
68     }
69 }
70