1 /******************************************************************************
3 * Copyright (C) 2015 Google Inc.
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
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"
29 #define REDUCE_HCI_TYPE_TO_SIGNIFICANT_BITS(type) (type >> 8)
31 // Total btsnoop memory log buffer size
32 #ifndef BTSNOOP_MEM_BUFFER_SIZE
33 static const size_t BTSNOOP_MEM_BUFFER_SIZE = (128 * 1024);
36 // Block size for copying buffers (for compression/encoding etc.)
37 static const size_t BLOCK_SIZE = 16384;
39 // Maximum line length in bugreport (should be multiple of 4 for base64 output)
40 static const uint8_t MAX_LINE_LENGTH = 128;
42 static ringbuffer_t *buffer = NULL;
43 static uint64_t last_timestamp_ms = 0;
45 static void btsnoop_cb(const uint16_t type, const uint8_t *data, const size_t length) {
46 btsnooz_header_t header;
48 // Only log packet content for HCI commands and events (privacy).
49 size_t included_length = 0;
51 case BT_EVT_TO_LM_HCI_CMD:
52 included_length = length;
54 case BT_EVT_TO_BTU_HCI_EVT:
55 included_length = length;
57 case BT_EVT_TO_LM_HCI_ACL:
58 case BT_EVT_TO_BTU_HCI_ACL:
61 case BT_EVT_TO_LM_HCI_SCO:
62 case BT_EVT_TO_BTU_HCI_SCO:
67 // Make room in the ring buffer
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);
76 const uint64_t now = btif_debug_ts();
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;
84 ringbuffer_insert(buffer, (uint8_t *)&header, sizeof(btsnooz_header_t));
85 ringbuffer_insert(buffer, data, included_length);
88 static bool btsnoop_compress(ringbuffer_t *rb_dst, ringbuffer_t *rb_src) {
89 assert(rb_dst != NULL);
90 assert(rb_src != NULL);
93 if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK)
97 uint8_t block_src[BLOCK_SIZE];
98 uint8_t block_dst[BLOCK_SIZE];
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;
105 zs.avail_out = BLOCK_SIZE;
106 zs.next_out = block_dst;
108 int err = deflate(&zs, ringbuffer_size(rb_src) == 0 ? Z_FINISH : Z_NO_FLUSH);
109 if (err == Z_STREAM_ERROR) {
114 const size_t length = BLOCK_SIZE - zs.avail_out;
115 ringbuffer_insert(rb_dst, block_dst, length);
116 } while (zs.avail_out == 0);
123 void btif_debug_btsnoop_init(void) {
125 buffer = ringbuffer_init(BTSNOOP_MEM_BUFFER_SIZE);
126 btsnoop_mem_set_callback(btsnoop_cb);
129 void btif_debug_btsnoop_dump(int fd) {
130 dprintf(fd, "\n--- BEGIN:BTSNOOP_LOG_SUMMARY (%zu bytes in) ---\n", ringbuffer_size(buffer));
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__);
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));
147 bool rc = btsnoop_compress(ringbuffer, buffer);
149 dprintf(fd, "%s Log compression failed", __func__);
153 // Base64 encode & output
155 uint8_t b64_in[3] = {0};
156 char b64_out[5] = {0};
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)
163 i += b64_ntop(b64_in, read, b64_out, 5);
164 dprintf(fd, "%s", b64_out);
167 dprintf(fd, "\n--- END:BTSNOOP_LOG_SUMMARY (%zu bytes out) ---\n", i);
170 ringbuffer_free(ringbuffer);