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);
92 z_stream zs = {.zalloc = Z_NULL, .zfree = Z_NULL, .opaque = Z_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 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;
106 zs.avail_out = BLOCK_SIZE;
107 zs.next_out = block_dst;
109 int err = deflate(&zs, (i == num_blocks - 1) ? Z_FINISH : Z_NO_FLUSH);
110 if (err == Z_STREAM_ERROR) {
115 const size_t length = BLOCK_SIZE - zs.avail_out;
116 ringbuffer_insert(rb_dst, block_dst, length);
117 } while (zs.avail_out == 0);
124 void btif_debug_btsnoop_init(void) {
126 buffer = ringbuffer_init(BTSNOOP_MEM_BUFFER_SIZE);
127 btsnoop_mem_set_callback(btsnoop_cb);
130 void btif_debug_btsnoop_dump(int fd) {
131 dprintf(fd, "--- BEGIN:BTSNOOP_LOG_SUMMARY (%zu bytes in) ---\n", ringbuffer_size(buffer));
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__);
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));
148 bool rc = btsnoop_compress(ringbuffer, buffer);
150 dprintf(fd, "%s Log compression failed", __func__);
154 // Base64 encode & output
156 uint8_t b64_in[3] = {0};
157 char b64_out[5] = {0};
159 size_t line_length = 0;
160 while (ringbuffer_size(ringbuffer) > 0) {
161 size_t read = ringbuffer_pop(ringbuffer, b64_in, 3);
162 if (line_length >= MAX_LINE_LENGTH) {
166 line_length += b64_ntop(b64_in, read, b64_out, 5);
167 dprintf(fd, "%s", b64_out);
170 dprintf(fd, "\n--- END:BTSNOOP_LOG_SUMMARY ---\n");
173 ringbuffer_free(ringbuffer);