blob: 32b6ae4691e0887bf2f1b160f8dc90ddaa6b94ad [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
26/* define ADB_TRACE to 1 to enable tracing support, or 0 to disable it */
27#define ADB_TRACE 1
28
29/* IMPORTANT: if you change the following list, don't
30 * forget to update the corresponding 'tags' table in
31 * the adb_trace_init() function implemented in adb.c
32 */
Elliott Hughes2d4121c2015-04-17 09:47:42 -070033enum AdbTrace {
leozwangd3fc15f2014-07-29 12:50:02 -070034 TRACE_ADB = 0, /* 0x001 */
35 TRACE_SOCKETS,
36 TRACE_PACKETS,
37 TRACE_TRANSPORT,
38 TRACE_RWX, /* 0x010 */
39 TRACE_USB,
40 TRACE_SYNC,
41 TRACE_SYSDEPS,
42 TRACE_JDWP, /* 0x100 */
43 TRACE_SERVICES,
44 TRACE_AUTH,
45 TRACE_FDEVENT,
Elliott Hughes2d4121c2015-04-17 09:47:42 -070046} ;
leozwangd3fc15f2014-07-29 12:50:02 -070047
48#if ADB_TRACE
49
50#if !ADB_HOST
51/*
52 * When running inside the emulator, guest's adbd can connect to 'adb-debug'
53 * qemud service that can display adb trace messages (on condition that emulator
54 * has been started with '-debug adb' option).
55 */
56
57/* Delivers a trace message to the emulator via QEMU pipe. */
58void adb_qemu_trace(const char* fmt, ...);
59/* Macro to use to send ADB trace messages to the emulator. */
60#define DQ(...) adb_qemu_trace(__VA_ARGS__)
61#else
62#define DQ(...) ((void)0)
63#endif /* !ADB_HOST */
64
65extern int adb_trace_mask;
66extern unsigned char adb_trace_output_count;
67void adb_trace_init(void);
68
69# define ADB_TRACING ((adb_trace_mask & (1 << TRACE_TAG)) != 0)
70
71/* you must define TRACE_TAG before using this macro */
72#if ADB_HOST
73# define D(...) \
74 do { \
75 if (ADB_TRACING) { \
76 int save_errno = errno; \
77 adb_mutex_lock(&D_lock); \
leozwangcbf02672014-08-15 09:51:27 -070078 fprintf(stderr, "%16s: %5d:%5lu | ", \
79 __FUNCTION__, \
80 getpid(), adb_thread_id()); \
leozwangd3fc15f2014-07-29 12:50:02 -070081 errno = save_errno; \
82 fprintf(stderr, __VA_ARGS__ ); \
83 fflush(stderr); \
84 adb_mutex_unlock(&D_lock); \
85 errno = save_errno; \
86 } \
87 } while (0)
88# define DR(...) \
89 do { \
90 if (ADB_TRACING) { \
91 int save_errno = errno; \
92 adb_mutex_lock(&D_lock); \
93 errno = save_errno; \
94 fprintf(stderr, __VA_ARGS__ ); \
95 fflush(stderr); \
96 adb_mutex_unlock(&D_lock); \
97 errno = save_errno; \
98 } \
99 } while (0)
100# define DD(...) \
101 do { \
leozwangcbf02672014-08-15 09:51:27 -0700102 int save_errno = errno; \
103 adb_mutex_lock(&D_lock); \
104 fprintf(stderr, "%16s: %5d:%5lu | ", \
105 __FUNCTION__, \
106 getpid(), adb_thread_id()); \
107 errno = save_errno; \
108 fprintf(stderr, __VA_ARGS__ ); \
109 fflush(stderr); \
110 adb_mutex_unlock(&D_lock); \
111 errno = save_errno; \
leozwangd3fc15f2014-07-29 12:50:02 -0700112 } while (0)
113#else
114# define D(...) \
115 do { \
116 if (ADB_TRACING) { \
117 __android_log_print( \
118 ANDROID_LOG_INFO, \
119 __FUNCTION__, \
120 __VA_ARGS__ ); \
121 } \
122 } while (0)
123# define DR(...) \
124 do { \
125 if (ADB_TRACING) { \
126 __android_log_print( \
127 ANDROID_LOG_INFO, \
128 __FUNCTION__, \
129 __VA_ARGS__ ); \
130 } \
131 } while (0)
132# define DD(...) \
133 do { \
134 __android_log_print( \
135 ANDROID_LOG_INFO, \
136 __FUNCTION__, \
137 __VA_ARGS__ ); \
138 } while (0)
139#endif /* ADB_HOST */
140#else
141# define D(...) ((void)0)
142# define DR(...) ((void)0)
143# define DD(...) ((void)0)
144# define ADB_TRACING 0
145#endif /* ADB_TRACE */
146
leozwangd3fc15f2014-07-29 12:50:02 -0700147#endif /* __ADB_TRACE_H */