1 /*
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>
22 #else
23 #include <stdio.h>
24 #endif
25 
26 /* IMPORTANT: if you change the following list, don't
27  * forget to update the corresponding 'tags' table in
28  * the adb_trace_init() function implemented in adb.c
29  */
30 enum AdbTrace {
31     TRACE_ADB = 0,   /* 0x001 */
32     TRACE_SOCKETS,
33     TRACE_PACKETS,
34     TRACE_TRANSPORT,
35     TRACE_RWX,       /* 0x010 */
36     TRACE_USB,
37     TRACE_SYNC,
38     TRACE_SYSDEPS,
39     TRACE_JDWP,      /* 0x100 */
40     TRACE_SERVICES,
41     TRACE_AUTH,
42     TRACE_FDEVENT,
43 } ;
44 
45 #if !ADB_HOST
46 /*
47  * When running inside the emulator, guest's adbd can connect to 'adb-debug'
48  * qemud service that can display adb trace messages (on condition that emulator
49  * has been started with '-debug adb' option).
50  */
51 
52 /* Delivers a trace message to the emulator via QEMU pipe. */
53 void adb_qemu_trace(const char* fmt, ...);
54 /* Macro to use to send ADB trace messages to the emulator. */
55 #define DQ(...)    adb_qemu_trace(__VA_ARGS__)
56 #else
57 #define DQ(...) ((void)0)
58 #endif  /* !ADB_HOST */
59 
60 extern int     adb_trace_mask;
61 extern unsigned char    adb_trace_output_count;
62 void    adb_trace_init(void);
63 
64 #  define ADB_TRACING  ((adb_trace_mask & (1 << TRACE_TAG)) != 0)
65 
66 /* you must define TRACE_TAG before using this macro */
67 #if ADB_HOST
68 #  define  D(...)                                      \
69         do {                                           \
70             if (ADB_TRACING) {                         \
71                 int save_errno = errno;                \
72                 adb_mutex_lock(&D_lock);               \
73                 fprintf(stderr, "%16s: %5d:%5lu | ",   \
74                         __FUNCTION__,                  \
75                         getpid(), adb_thread_id());    \
76                 errno = save_errno;                    \
77                 fprintf(stderr, __VA_ARGS__ );         \
78                 fflush(stderr);                        \
79                 adb_mutex_unlock(&D_lock);             \
80                 errno = save_errno;                    \
81            }                                           \
82         } while (0)
83 #  define  DR(...)                                     \
84         do {                                           \
85             if (ADB_TRACING) {                         \
86                 int save_errno = errno;                \
87                 adb_mutex_lock(&D_lock);               \
88                 errno = save_errno;                    \
89                 fprintf(stderr, __VA_ARGS__ );         \
90                 fflush(stderr);                        \
91                 adb_mutex_unlock(&D_lock);             \
92                 errno = save_errno;                    \
93            }                                           \
94         } while (0)
95 #else
96 #  define  D(...)                                      \
97         do {                                           \
98             if (ADB_TRACING) {                         \
99                 __android_log_print(                   \
100                     ANDROID_LOG_INFO,                  \
101                     __FUNCTION__,                      \
102                     __VA_ARGS__ );                     \
103             }                                          \
104         } while (0)
105 #  define  DR(...)                                     \
106         do {                                           \
107             if (ADB_TRACING) {                         \
108                 __android_log_print(                   \
109                     ANDROID_LOG_INFO,                  \
110                     __FUNCTION__,                      \
111                     __VA_ARGS__ );                     \
112             }                                          \
113         } while (0)
114 #endif /* ADB_HOST */
115 
116 #endif /* __ADB_TRACE_H */
117