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
btsnoop_cb(const uint16_t type,const uint8_t * data,const size_t length)45 static void btsnoop_cb(const uint16_t type, const uint8_t *data, const size_t length) {
46 btsnooz_header_t header;
47
48 // Make room in the ring buffer
49
50 while (ringbuffer_available(buffer) < (length + sizeof(btsnooz_header_t))) {
51 ringbuffer_pop(buffer, (uint8_t *)&header, sizeof(btsnooz_header_t));
52 ringbuffer_delete(buffer, header.length - 1);
53 }
54
55 // Insert data
56
57 const uint64_t now = btif_debug_ts();
58
59 header.type = REDUCE_HCI_TYPE_TO_SIGNIFICANT_BITS(type);
60 header.length = length;
61 header.delta_time_ms = last_timestamp_ms ? now - last_timestamp_ms : 0;
62 last_timestamp_ms = now;
63
64 ringbuffer_insert(buffer, (uint8_t *)&header, sizeof(btsnooz_header_t));
65 ringbuffer_insert(buffer, data, length - 1);
66 }
67
btsnoop_compress(ringbuffer_t * rb_dst,ringbuffer_t * rb_src)68 static bool btsnoop_compress(ringbuffer_t *rb_dst, ringbuffer_t *rb_src) {
69 assert(rb_dst != NULL);
70 assert(rb_src != NULL);
71
72 z_stream zs = {0};
73 if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK)
74 return false;
75
76 bool rc = true;
77 uint8_t block_src[BLOCK_SIZE];
78 uint8_t block_dst[BLOCK_SIZE];
79
80 while (ringbuffer_size(rb_src) > 0) {
81 zs.avail_in = ringbuffer_pop(rb_src, block_src, BLOCK_SIZE);
82 zs.next_in = block_src;
83
84 do {
85 zs.avail_out = BLOCK_SIZE;
86 zs.next_out = block_dst;
87
88 int err = deflate(&zs, ringbuffer_size(rb_src) == 0 ? Z_FINISH : Z_NO_FLUSH);
89 if (err == Z_STREAM_ERROR) {
90 rc = false;
91 break;
92 }
93
94 const size_t length = BLOCK_SIZE - zs.avail_out;
95 ringbuffer_insert(rb_dst, block_dst, length);
96 } while (zs.avail_out == 0);
97 }
98
99 deflateEnd(&zs);
100 return rc;
101 }
102
btif_debug_btsnoop_init(void)103 void btif_debug_btsnoop_init(void) {
104 if (buffer == NULL)
105 buffer = ringbuffer_init(BTSNOOP_MEM_BUFFER_SIZE);
106 btsnoop_mem_set_callback(btsnoop_cb);
107 }
108
btif_debug_btsnoop_dump(int fd)109 void btif_debug_btsnoop_dump(int fd) {
110 dprintf(fd, "\n--- BEGIN:BTSNOOP_LOG_SUMMARY (%zu bytes in) ---\n", ringbuffer_size(buffer));
111
112 ringbuffer_t *ringbuffer = ringbuffer_init(BTSNOOP_MEM_BUFFER_SIZE);
113 if (ringbuffer == NULL) {
114 dprintf(fd, "%s Unable to allocate memory for compression", __func__);
115 return;
116 }
117
118 // Prepend preamble
119
120 btsnooz_preamble_t preamble;
121 preamble.version = BTSNOOZ_CURRENT_VERSION;
122 preamble.last_timestamp_ms = last_timestamp_ms;
123 ringbuffer_insert(ringbuffer, (uint8_t *)&preamble, sizeof(btsnooz_preamble_t));
124
125 // Compress data
126
127 bool rc = btsnoop_compress(ringbuffer, buffer);
128 if (rc == false) {
129 dprintf(fd, "%s Log compression failed", __func__);
130 goto error;
131 }
132
133 // Base64 encode & output
134
135 uint8_t b64_in[3] = {0};
136 char b64_out[5] = {0};
137
138 size_t i = sizeof(btsnooz_preamble_t);
139 while (ringbuffer_size(ringbuffer) > 0) {
140 size_t read = ringbuffer_pop(ringbuffer, b64_in, 3);
141 if (i > 0 && i % MAX_LINE_LENGTH == 0)
142 dprintf(fd, "\n");
143 i += b64_ntop(b64_in, read, b64_out, 5);
144 dprintf(fd, "%s", b64_out);
145 }
146
147 dprintf(fd, "\n--- END:BTSNOOP_LOG_SUMMARY (%zu bytes out) ---\n", i);
148
149 error:
150 ringbuffer_free(ringbuffer);
151 }
152