OSDN Git Service

Don't clear the btsnoop log ringbuffer on compress.
[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 = {.zalloc = Z_NULL, .zfree = Z_NULL, .opaque = Z_NULL};
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   const size_t num_blocks = (ringbuffer_size(rb_src) + BLOCK_SIZE - 1) / BLOCK_SIZE;
101   for (size_t i = 0; i < num_blocks; ++i) {
102     zs.avail_in = ringbuffer_peek(rb_src, i * BLOCK_SIZE, block_src, BLOCK_SIZE);
103     zs.next_in = block_src;
104
105     do {
106       zs.avail_out = BLOCK_SIZE;
107       zs.next_out = block_dst;
108
109       int err = deflate(&zs, (i == num_blocks - 1) ? Z_FINISH : Z_NO_FLUSH);
110       if (err == Z_STREAM_ERROR) {
111         rc = false;
112         break;
113       }
114
115       const size_t length = BLOCK_SIZE - zs.avail_out;
116       ringbuffer_insert(rb_dst, block_dst, length);
117     } while (zs.avail_out == 0);
118   }
119
120   deflateEnd(&zs);
121   return rc;
122 }
123
124 void btif_debug_btsnoop_init(void) {
125   if (buffer == NULL)
126     buffer = ringbuffer_init(BTSNOOP_MEM_BUFFER_SIZE);
127   btsnoop_mem_set_callback(btsnoop_cb);
128 }
129
130 void btif_debug_btsnoop_dump(int fd) {
131   dprintf(fd, "--- BEGIN:BTSNOOP_LOG_SUMMARY (%zu bytes in) ---\n", ringbuffer_size(buffer));
132
133   ringbuffer_t *ringbuffer = ringbuffer_init(BTSNOOP_MEM_BUFFER_SIZE);
134   if (ringbuffer == NULL) {
135     dprintf(fd, "%s Unable to allocate memory for compression", __func__);
136     return;
137   }
138
139   // Prepend preamble
140
141   btsnooz_preamble_t preamble;
142   preamble.version = BTSNOOZ_CURRENT_VERSION;
143   preamble.last_timestamp_ms = last_timestamp_ms;
144   ringbuffer_insert(ringbuffer, (uint8_t *)&preamble, sizeof(btsnooz_preamble_t));
145
146   // Compress data
147
148   bool rc = btsnoop_compress(ringbuffer, buffer);
149   if (rc == false) {
150     dprintf(fd, "%s Log compression failed", __func__);
151     goto error;
152   }
153
154   // Base64 encode & output
155
156   uint8_t b64_in[3] = {0};
157   char b64_out[5] = {0};
158
159   size_t i = sizeof(btsnooz_preamble_t);
160   while (ringbuffer_size(ringbuffer) > 0) {
161     size_t read = ringbuffer_pop(ringbuffer, b64_in, 3);
162     if (i > 0 && i % MAX_LINE_LENGTH == 0)
163       dprintf(fd, "\n");
164     i += b64_ntop(b64_in, read, b64_out, 5);
165     dprintf(fd, "%s", b64_out);
166   }
167
168   dprintf(fd, "\n--- END:BTSNOOP_LOG_SUMMARY (%zu bytes out) ---\n", i);
169
170 error:
171   ringbuffer_free(ringbuffer);
172 }