OSDN Git Service

Merge "service: Solve locking issues inside GATT interface"
[android-x86/system-bt.git] / btif / src / btif_debug_btsnoop.c
1 /******************************************************************************
2  *
3  *  Copyright (C) 2015 Google Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18
19 #include <assert.h>
20 #include <resolv.h>
21 #include <zlib.h>
22
23 #include "btif/include/btif_debug.h"
24 #include "btif/include/btif_debug_btsnoop.h"
25 #include "hci/include/btsnoop_mem.h"
26 #include "include/bt_target.h"
27 #include "osi/include/ringbuffer.h"
28
29 #define REDUCE_HCI_TYPE_TO_SIGNIFICANT_BITS(type) (type >> 8)
30
31 // Total btsnoop memory log buffer size
32 #ifndef BTSNOOP_MEM_BUFFER_SIZE
33 static const size_t BTSNOOP_MEM_BUFFER_SIZE = (128 * 1024);
34 #endif
35
36 // Block size for copying buffers (for compression/encoding etc.)
37 static const size_t BLOCK_SIZE = 16384;
38
39 // Maximum line length in bugreport (should be multiple of 4 for base64 output)
40 static const uint8_t MAX_LINE_LENGTH = 128;
41
42 static ringbuffer_t *buffer = NULL;
43 static uint64_t last_timestamp_ms = 0;
44
45 static void btsnoop_cb(const uint16_t type, const uint8_t *data, const size_t length) {
46   btsnooz_header_t header;
47
48   // Only log packet content for HCI commands and events (privacy).
49   size_t included_length = 0;
50   switch (type) {
51     case BT_EVT_TO_LM_HCI_CMD:
52       included_length = length;
53       break;
54     case BT_EVT_TO_BTU_HCI_EVT:
55       included_length = length;
56       break;
57     case BT_EVT_TO_LM_HCI_ACL:
58     case BT_EVT_TO_BTU_HCI_ACL:
59       included_length = 4;
60       break;
61     case BT_EVT_TO_LM_HCI_SCO:
62     case BT_EVT_TO_BTU_HCI_SCO:
63       included_length = 2;
64       break;
65   }
66
67   // Make room in the ring buffer
68
69   while (ringbuffer_available(buffer) < (included_length + sizeof(btsnooz_header_t))) {
70     ringbuffer_pop(buffer, (uint8_t *)&header, sizeof(btsnooz_header_t));
71     ringbuffer_delete(buffer, header.length - 1);
72   }
73
74   // Insert data
75
76   const uint64_t now = btif_debug_ts();
77
78   header.type = REDUCE_HCI_TYPE_TO_SIGNIFICANT_BITS(type);
79   header.length = included_length + 1;  // +1 for type byte
80   header.packet_length = length + 1;  // +1 for type byte.
81   header.delta_time_ms = last_timestamp_ms ? now - last_timestamp_ms : 0;
82   last_timestamp_ms = now;
83
84   ringbuffer_insert(buffer, (uint8_t *)&header, sizeof(btsnooz_header_t));
85   ringbuffer_insert(buffer, data, included_length);
86 }
87
88 static bool btsnoop_compress(ringbuffer_t *rb_dst, ringbuffer_t *rb_src) {
89   assert(rb_dst != NULL);
90   assert(rb_src != NULL);
91
92   z_stream zs = {0};
93   if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK)
94     return false;
95
96   bool rc = true;
97   uint8_t block_src[BLOCK_SIZE];
98   uint8_t block_dst[BLOCK_SIZE];
99
100   while (ringbuffer_size(rb_src) > 0) {
101     zs.avail_in = ringbuffer_pop(rb_src, block_src, BLOCK_SIZE);
102     zs.next_in = block_src;
103
104     do {
105       zs.avail_out = BLOCK_SIZE;
106       zs.next_out = block_dst;
107
108       int err = deflate(&zs, ringbuffer_size(rb_src) == 0 ? Z_FINISH : Z_NO_FLUSH);
109       if (err == Z_STREAM_ERROR) {
110         rc = false;
111         break;
112       }
113
114       const size_t length = BLOCK_SIZE - zs.avail_out;
115       ringbuffer_insert(rb_dst, block_dst, length);
116     } while (zs.avail_out == 0);
117   }
118
119   deflateEnd(&zs);
120   return rc;
121 }
122
123 void btif_debug_btsnoop_init(void) {
124   if (buffer == NULL)
125     buffer = ringbuffer_init(BTSNOOP_MEM_BUFFER_SIZE);
126   btsnoop_mem_set_callback(btsnoop_cb);
127 }
128
129 void btif_debug_btsnoop_dump(int fd) {
130   dprintf(fd, "\n--- BEGIN:BTSNOOP_LOG_SUMMARY (%zu bytes in) ---\n", ringbuffer_size(buffer));
131
132   ringbuffer_t *ringbuffer = ringbuffer_init(BTSNOOP_MEM_BUFFER_SIZE);
133   if (ringbuffer == NULL) {
134     dprintf(fd, "%s Unable to allocate memory for compression", __func__);
135     return;
136   }
137
138   // Prepend preamble
139
140   btsnooz_preamble_t preamble;
141   preamble.version = BTSNOOZ_CURRENT_VERSION;
142   preamble.last_timestamp_ms = last_timestamp_ms;
143   ringbuffer_insert(ringbuffer, (uint8_t *)&preamble, sizeof(btsnooz_preamble_t));
144
145   // Compress data
146
147   bool rc = btsnoop_compress(ringbuffer, buffer);
148   if (rc == false) {
149     dprintf(fd, "%s Log compression failed", __func__);
150     goto error;
151   }
152
153   // Base64 encode & output
154
155   uint8_t b64_in[3] = {0};
156   char b64_out[5] = {0};
157
158   size_t i = sizeof(btsnooz_preamble_t);
159   while (ringbuffer_size(ringbuffer) > 0) {
160     size_t read = ringbuffer_pop(ringbuffer, b64_in, 3);
161     if (i > 0 && i % MAX_LINE_LENGTH == 0)
162       dprintf(fd, "\n");
163     i += b64_ntop(b64_in, read, b64_out, 5);
164     dprintf(fd, "%s", b64_out);
165   }
166
167   dprintf(fd, "\n--- END:BTSNOOP_LOG_SUMMARY (%zu bytes out) ---\n", i);
168
169 error:
170   ringbuffer_free(ringbuffer);
171 }