blob: ef5dc2409a8ffe435978397f42ed896356c111f0 [file] [log] [blame]
leozwangd3fc15f2014-07-29 12:50:02 -07001/*
2 * Copyright (C) 2014 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#ifndef __ADB_TRACE_H
18#define __ADB_TRACE_H
19
20#if !ADB_HOST
21#include <android/log.h>
Dan Albertcc731cc2015-02-24 21:26:58 -080022#else
23#include <stdio.h>
leozwangd3fc15f2014-07-29 12:50:02 -070024#endif
25
Dan Albert818fb4b2015-02-18 00:18:25 -080026#ifdef __cplusplus
27extern "C" {
28#endif
29
leozwangd3fc15f2014-07-29 12:50:02 -070030/* define ADB_TRACE to 1 to enable tracing support, or 0 to disable it */
31#define ADB_TRACE 1
32
33/* IMPORTANT: if you change the following list, don't
34 * forget to update the corresponding 'tags' table in
35 * the adb_trace_init() function implemented in adb.c
36 */
37typedef enum {
38 TRACE_ADB = 0, /* 0x001 */
39 TRACE_SOCKETS,
40 TRACE_PACKETS,
41 TRACE_TRANSPORT,
42 TRACE_RWX, /* 0x010 */
43 TRACE_USB,
44 TRACE_SYNC,
45 TRACE_SYSDEPS,
46 TRACE_JDWP, /* 0x100 */
47 TRACE_SERVICES,
48 TRACE_AUTH,
49 TRACE_FDEVENT,
50} AdbTrace;
51
52#if ADB_TRACE
53
54#if !ADB_HOST
55/*
56 * When running inside the emulator, guest's adbd can connect to 'adb-debug'
57 * qemud service that can display adb trace messages (on condition that emulator
58 * has been started with '-debug adb' option).
59 */
60
61/* Delivers a trace message to the emulator via QEMU pipe. */
62void adb_qemu_trace(const char* fmt, ...);
63/* Macro to use to send ADB trace messages to the emulator. */
64#define DQ(...) adb_qemu_trace(__VA_ARGS__)
65#else
66#define DQ(...) ((void)0)
67#endif /* !ADB_HOST */
68
69extern int adb_trace_mask;
70extern unsigned char adb_trace_output_count;
71void adb_trace_init(void);
72
73# define ADB_TRACING ((adb_trace_mask & (1 << TRACE_TAG)) != 0)
74
75/* you must define TRACE_TAG before using this macro */
76#if ADB_HOST
77# define D(...) \
78 do { \
79 if (ADB_TRACING) { \
80 int save_errno = errno; \
81 adb_mutex_lock(&D_lock); \
leozwangcbf02672014-08-15 09:51:27 -070082 fprintf(stderr, "%16s: %5d:%5lu | ", \
83 __FUNCTION__, \
84 getpid(), adb_thread_id()); \
leozwangd3fc15f2014-07-29 12:50:02 -070085 errno = save_errno; \
86 fprintf(stderr, __VA_ARGS__ ); \
87 fflush(stderr); \
88 adb_mutex_unlock(&D_lock); \
89 errno = save_errno; \
90 } \
91 } while (0)
92# define DR(...) \
93 do { \
94 if (ADB_TRACING) { \
95 int save_errno = errno; \
96 adb_mutex_lock(&D_lock); \
97 errno = save_errno; \
98 fprintf(stderr, __VA_ARGS__ ); \
99 fflush(stderr); \
100 adb_mutex_unlock(&D_lock); \
101 errno = save_errno; \
102 } \
103 } while (0)
104# define DD(...) \
105 do { \
leozwangcbf02672014-08-15 09:51:27 -0700106 int save_errno = errno; \
107 adb_mutex_lock(&D_lock); \
108 fprintf(stderr, "%16s: %5d:%5lu | ", \
109 __FUNCTION__, \
110 getpid(), adb_thread_id()); \
111 errno = save_errno; \
112 fprintf(stderr, __VA_ARGS__ ); \
113 fflush(stderr); \
114 adb_mutex_unlock(&D_lock); \
115 errno = save_errno; \
leozwangd3fc15f2014-07-29 12:50:02 -0700116 } while (0)
117#else
118# define D(...) \
119 do { \
120 if (ADB_TRACING) { \
121 __android_log_print( \
122 ANDROID_LOG_INFO, \
123 __FUNCTION__, \
124 __VA_ARGS__ ); \
125 } \
126 } while (0)
127# define DR(...) \
128 do { \
129 if (ADB_TRACING) { \
130 __android_log_print( \
131 ANDROID_LOG_INFO, \
132 __FUNCTION__, \
133 __VA_ARGS__ ); \
134 } \
135 } while (0)
136# define DD(...) \
137 do { \
138 __android_log_print( \
139 ANDROID_LOG_INFO, \
140 __FUNCTION__, \
141 __VA_ARGS__ ); \
142 } while (0)
143#endif /* ADB_HOST */
144#else
145# define D(...) ((void)0)
146# define DR(...) ((void)0)
147# define DD(...) ((void)0)
148# define ADB_TRACING 0
149#endif /* ADB_TRACE */
150
Dan Albert818fb4b2015-02-18 00:18:25 -0800151#ifdef __cplusplus
152}
153#endif
154
leozwangd3fc15f2014-07-29 12:50:02 -0700155#endif /* __ADB_TRACE_H */