Coverage Report

Created: 2026-06-30 07:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wireshark/epan/dissectors/packet-logcat-text.c
Line
Count
Source
1
/* packet-logcat-text.c
2
 * Routines for Android Logcat text formats
3
 *
4
 * Copyright 2014, Michal Orynicz for Tieto Corporation
5
 *
6
 * Wireshark - Network traffic analyzer
7
 * By Gerald Combs <gerald@wireshark.org>
8
 * Copyright 1998 Gerald Combs
9
 *
10
 * SPDX-License-Identifier: GPL-2.0-or-later
11
 */
12
13
#include "config.h"
14
15
#include <stdio.h>      /* for sscanf() */
16
17
#include <epan/packet.h>
18
#include <epan/expert.h>
19
#include <epan/exported_pdu.h>
20
#include <epan/tap.h>
21
#include <wiretap/logcat_text.h>
22
23
#include "packet-logcat.h"
24
25
static int proto_logcat_text;
26
27
static int hf_logcat_text_pid;
28
static int hf_logcat_text_tid;
29
static int hf_logcat_text_timestamp;
30
static int hf_logcat_text_priority;
31
static int hf_logcat_text_tag;
32
static int hf_logcat_text_log;
33
34
static int ett_logcat;
35
36
static expert_field ei_malformed_time;
37
static expert_field ei_malformed_token;
38
39
static dissector_handle_t logcat_text_brief_handle;
40
static dissector_handle_t logcat_text_tag_handle;
41
static dissector_handle_t logcat_text_process_handle;
42
static dissector_handle_t logcat_text_time_handle;
43
static dissector_handle_t logcat_text_thread_handle;
44
static dissector_handle_t logcat_text_threadtime_handle;
45
static dissector_handle_t logcat_text_long_handle;
46
47
static int exported_pdu_tap = -1;
48
49
static GRegex *special_regex;
50
static GRegex *brief_regex;
51
static GRegex *tag_regex;
52
static GRegex *time_regex;
53
static GRegex *process_regex;
54
static GRegex *thread_regex;
55
static GRegex *threadtime_regex;
56
static GRegex *long_regex;
57
58
typedef unsigned (*tGETTER) (const char *frame, const char *token, tvbuff_t *tvb,
59
        proto_tree *maintree, unsigned start_offset, packet_info *pinfo);
60
61
typedef struct {
62
    GRegex **regex;
63
    const tGETTER *getters;
64
    unsigned no_of_getters;
65
} dissect_info_t;
66
67
void proto_register_logcat_text(void);
68
void proto_reg_handoff_logcat_text(void);
69
70
static unsigned get_priority(const char *frame, const char *token, tvbuff_t *tvb,
71
0
        proto_tree *maintree, unsigned start_offset, packet_info *pinfo) {
72
0
    unsigned prio;
73
0
    char *p = g_strstr_len(frame + start_offset, -1, token);
74
0
    unsigned offset = (unsigned)(p - frame);
75
76
0
    if (!p) {
77
0
        proto_tree_add_expert_remaining(maintree, pinfo, &ei_malformed_token, tvb, start_offset);
78
0
        return (start_offset + 1);
79
0
    }
80
81
0
    switch (*p) {
82
0
    case 'I':
83
0
        prio = 4;
84
0
        break;
85
0
    case 'V':
86
0
        prio = 2;
87
0
        break;
88
0
    case 'D':
89
0
        prio = 3;
90
0
        break;
91
0
    case 'W':
92
0
        prio = 5;
93
0
        break;
94
0
    case 'E':
95
0
        prio = 6;
96
0
        break;
97
0
    case 'F':
98
0
        prio = 7;
99
0
        break;
100
0
    default:
101
0
        prio = 0;
102
0
    }
103
104
0
    proto_tree_add_uint(maintree, hf_logcat_text_priority, tvb, offset, 1, prio);
105
0
    return offset + 1;
106
0
}
107
108
static unsigned get_tag(const char *frame, const char *token, tvbuff_t *tvb,
109
0
        proto_tree *maintree, unsigned start_offset, packet_info *pinfo) {
110
0
    char *p = g_strstr_len(frame + start_offset, -1, token);
111
0
    unsigned offset = (unsigned)(p - frame);
112
0
    uint8_t *src_addr = (uint8_t*)wmem_strdup(pinfo->pool, token);
113
0
    unsigned tok_len = (unsigned)strlen(token);
114
115
0
    proto_tree_add_string(maintree, hf_logcat_text_tag, tvb, offset, tok_len,
116
0
            token);
117
0
    set_address(&pinfo->src, AT_STRINGZ, tok_len + 1, src_addr);
118
0
    set_address(&pinfo->dst, AT_STRINGZ, sizeof("Logcat Text"), "Logcat Text");
119
0
    return offset + tok_len;
120
0
}
121
122
static unsigned get_ptid(const char *frame, const char *token, tvbuff_t *tvb,
123
0
        proto_tree *maintree, int header_field, unsigned start_offset) {
124
0
    char *p = g_strstr_len(frame + start_offset, -1, token);
125
0
    unsigned offset = (unsigned)(p - frame);
126
127
0
    proto_tree_add_uint(maintree, header_field, tvb, offset, (int)strlen(token),
128
0
            (uint32_t)g_ascii_strtoull(token, NULL, 10));
129
0
    return offset + (unsigned)strlen(token);
130
0
}
131
132
static unsigned get_pid(const char *frame, const char *token, tvbuff_t *tvb,
133
0
        proto_tree *maintree, unsigned start_offset, packet_info *pinfo _U_) {
134
0
    return get_ptid(frame, token, tvb, maintree, hf_logcat_text_pid, start_offset);
135
0
}
136
137
static unsigned get_tid(const char *frame, const char *token, tvbuff_t *tvb,
138
0
        proto_tree *maintree, unsigned start_offset, packet_info *pinfo _U_) {
139
0
    return get_ptid(frame, token, tvb, maintree, hf_logcat_text_tid, start_offset);
140
0
}
141
142
static unsigned get_log(const char *frame, const char *token, tvbuff_t *tvb,
143
0
        proto_tree *maintree, unsigned start_offset, packet_info *pinfo) {
144
0
    char *p = g_strstr_len(frame + start_offset, -1, token);
145
0
    unsigned offset = (unsigned)(p - frame);
146
147
0
    proto_tree_add_string(maintree, hf_logcat_text_log, tvb, offset, (unsigned)strlen(token), token);
148
0
    col_add_str(pinfo->cinfo, COL_INFO, token);
149
0
    return offset + (unsigned)strlen(token);
150
0
}
151
152
static unsigned get_time(const char *frame, const char *token, tvbuff_t *tvb,
153
0
        proto_tree *maintree, unsigned start_offset, packet_info *pinfo) {
154
0
    unsigned offset;
155
0
    char *p;
156
0
    unsigned ms;
157
0
    struct tm date;
158
0
    time_t seconds;
159
0
    nstime_t ts;
160
161
0
    p = g_strstr_len(frame + start_offset, -1, token);
162
0
    offset = (unsigned)(p - frame);
163
164
0
    if (6 == sscanf(token, "%d-%d %d:%d:%d.%d", &date.tm_mon, &date.tm_mday,
165
0
                    &date.tm_hour, &date.tm_min, &date.tm_sec, &ms)) {
166
0
        date.tm_year = 70;
167
0
        date.tm_mon -= 1;
168
0
        date.tm_isdst = -1;
169
0
        seconds = mktime(&date);
170
0
        ts.secs = seconds;
171
0
        ts.nsecs = (int) (ms * 1e6);
172
0
        proto_tree_add_time(maintree, hf_logcat_text_timestamp, tvb, offset,
173
0
            (unsigned)strlen(token), &ts);
174
0
    } else {
175
0
        proto_tree_add_expert_remaining(maintree, pinfo, &ei_malformed_time, tvb, offset);
176
0
    }
177
0
    return offset + (unsigned)strlen(token);
178
0
}
179
180
static unsigned dissect_logcat_text(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo,
181
0
        const dissect_info_t *dinfo) {
182
0
    char **tokens;
183
0
    unsigned i;
184
0
    char *frame = (char*)tvb_get_string_enc(pinfo->pool, tvb, 0, tvb_captured_length(tvb),
185
0
            ENC_UTF_8);
186
0
    proto_item *mainitem = proto_tree_add_item(tree, proto_logcat_text, tvb, 0, -1, ENC_NA);
187
0
    proto_tree *maintree = proto_item_add_subtree(mainitem, ett_logcat);
188
0
    unsigned offset = 0;
189
190
0
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "Logcat Text");
191
192
0
    if (!g_regex_match(special_regex, frame, G_REGEX_MATCH_NOTEMPTY, NULL)) {
193
194
0
        tokens = g_regex_split(*dinfo->regex, frame, G_REGEX_MATCH_NOTEMPTY);
195
0
        if (NULL == tokens) return 0;
196
0
        if (g_strv_length(tokens) != dinfo->no_of_getters + 2) {
197
0
            proto_tree_add_expert_remaining(maintree, pinfo, &ei_malformed_token, tvb, offset);
198
0
            g_strfreev(tokens);
199
0
            return 0;
200
0
        }
201
202
0
        for (i = 0; i < dinfo->no_of_getters; ++i) {
203
0
            offset = ((*dinfo->getters[i])(frame, tokens[i + 1], tvb, maintree, offset, pinfo));
204
0
        }
205
0
    } else {
206
0
        tokens = g_regex_split(special_regex, frame, G_REGEX_MATCH_NOTEMPTY);
207
0
        if (NULL == tokens) return 0;
208
0
        offset = get_log(frame, tokens[1], tvb, maintree, 0, pinfo);
209
0
    }
210
0
    g_strfreev(tokens);
211
0
    return offset;
212
0
}
213
214
0
static void add_exported_pdu(tvbuff_t *tvb, packet_info *pinfo, const char * subdissector_name){
215
0
    if (have_tap_listener(exported_pdu_tap)) {
216
0
        exp_pdu_data_t *exp_pdu_data;
217
218
0
        exp_pdu_data = export_pdu_create_tags(pinfo, subdissector_name, EXP_PDU_TAG_DISSECTOR_NAME, NULL);
219
220
0
        exp_pdu_data->tvb_captured_length = tvb_captured_length(tvb);
221
0
        exp_pdu_data->tvb_reported_length = tvb_reported_length(tvb);
222
0
        exp_pdu_data->pdu_tvb = tvb;
223
0
        tap_queue_packet(exported_pdu_tap, pinfo, exp_pdu_data);
224
0
    }
225
0
}
226
227
static int dissect_logcat_text_brief(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
228
0
        void *data _U_) {
229
0
    static const tGETTER getters[] = { get_priority, get_tag, get_pid, get_log };
230
0
    dissect_info_t dinfo = { &brief_regex, getters, array_length(getters) };
231
232
0
    add_exported_pdu(tvb,pinfo,"logcat_text_brief");
233
0
    return dissect_logcat_text(tvb, tree, pinfo, &dinfo);
234
0
}
235
236
static int dissect_logcat_text_tag(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
237
0
        void *data _U_) {
238
0
    static const tGETTER getters[] = { get_priority, get_tag, get_log };
239
0
    dissect_info_t dinfo = { &tag_regex, getters, array_length(getters) };
240
241
0
    add_exported_pdu(tvb,pinfo,"logcat_text_tag");
242
0
    return dissect_logcat_text(tvb, tree, pinfo, &dinfo);
243
0
}
244
245
static int dissect_logcat_text_process(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
246
0
        void *data _U_) {
247
0
    static const tGETTER getters[] = { get_priority, get_pid, get_log };
248
0
    dissect_info_t dinfo = { &process_regex, getters, array_length(getters) };
249
250
0
    add_exported_pdu(tvb,pinfo,"logcat_text_process");
251
0
    set_address(&pinfo->dst, AT_STRINGZ, 1, "");
252
0
    set_address(&pinfo->src, AT_STRINGZ, 1, "");
253
254
0
    return dissect_logcat_text(tvb, tree, pinfo, &dinfo);
255
0
}
256
257
static int dissect_logcat_text_time(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
258
0
        void *data _U_) {
259
0
    static const tGETTER getters[] = { get_time, get_priority, get_tag, get_pid, get_log };
260
0
    dissect_info_t dinfo = { &time_regex, getters, array_length(getters) };
261
262
0
    add_exported_pdu(tvb,pinfo,"logcat_text_time");
263
0
    return dissect_logcat_text(tvb, tree, pinfo, &dinfo);
264
0
}
265
266
static int dissect_logcat_text_thread(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
267
0
        void *data _U_) {
268
0
    static const tGETTER getters[] = { get_priority, get_pid, get_tid, get_log };
269
0
    dissect_info_t dinfo = { &thread_regex, getters, array_length(getters) };
270
271
0
    add_exported_pdu(tvb,pinfo,"logcat_text_brief");
272
0
    set_address(&pinfo->dst, AT_STRINGZ, 1, "");
273
0
    set_address(&pinfo->src, AT_STRINGZ, 1, "");
274
275
0
    return dissect_logcat_text(tvb, tree, pinfo, &dinfo);
276
0
}
277
278
static int dissect_logcat_text_threadtime(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
279
0
        void *data _U_) {
280
0
    static const tGETTER getters[] = { get_time, get_pid, get_tid, get_priority, get_tag, get_log };
281
0
    dissect_info_t dinfo = { &threadtime_regex, getters, array_length(getters) };
282
283
0
    add_exported_pdu(tvb,pinfo,"logcat_text_threadtime");
284
0
    return dissect_logcat_text(tvb, tree, pinfo, &dinfo);
285
0
}
286
287
static int dissect_logcat_text_long(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
288
0
        void *data _U_) {
289
0
    static const tGETTER getters[] = { get_time, get_pid, get_tid, get_priority, get_tag, get_log };
290
0
    dissect_info_t dinfo = { &long_regex, getters, array_length(getters) };
291
292
0
    add_exported_pdu(tvb,pinfo,"logcat_text_long");
293
0
    return dissect_logcat_text(tvb, tree, pinfo, &dinfo);
294
0
}
295
296
static void logcat_text_create_regex(void)
297
14
{
298
14
    special_regex =    g_regex_new(SPECIAL_STRING,    (GRegexCompileFlags)(G_REGEX_ANCHORED | G_REGEX_OPTIMIZE | G_REGEX_RAW),  G_REGEX_MATCH_NOTEMPTY, NULL);
299
14
    brief_regex =      g_regex_new(BRIEF_STRING,      (GRegexCompileFlags)(G_REGEX_ANCHORED | G_REGEX_OPTIMIZE | G_REGEX_RAW),  G_REGEX_MATCH_NOTEMPTY, NULL);
300
14
    tag_regex =        g_regex_new(TAG_STRING,        (GRegexCompileFlags)(G_REGEX_ANCHORED | G_REGEX_OPTIMIZE | G_REGEX_RAW),  G_REGEX_MATCH_NOTEMPTY, NULL);
301
14
    time_regex =       g_regex_new(TIME_STRING,       (GRegexCompileFlags)(G_REGEX_ANCHORED | G_REGEX_OPTIMIZE | G_REGEX_RAW),  G_REGEX_MATCH_NOTEMPTY, NULL);
302
14
    thread_regex =     g_regex_new(THREAD_STRING,     (GRegexCompileFlags)(G_REGEX_ANCHORED | G_REGEX_OPTIMIZE | G_REGEX_RAW),  G_REGEX_MATCH_NOTEMPTY, NULL);
303
14
    threadtime_regex = g_regex_new(THREADTIME_STRING, (GRegexCompileFlags)(G_REGEX_ANCHORED | G_REGEX_OPTIMIZE | G_REGEX_RAW),  G_REGEX_MATCH_NOTEMPTY, NULL);
304
14
    process_regex =    g_regex_new(PROCESS_STRING,    (GRegexCompileFlags)(G_REGEX_ANCHORED | G_REGEX_OPTIMIZE | G_REGEX_RAW),  G_REGEX_MATCH_NOTEMPTY, NULL);
305
14
    long_regex =       g_regex_new(LONG_STRING,       (GRegexCompileFlags)(G_REGEX_MULTILINE | G_REGEX_OPTIMIZE | G_REGEX_RAW), G_REGEX_MATCH_NOTEMPTY, NULL);
306
14
}
307
308
static void logcat_text_shutdown(void)
309
0
{
310
0
    g_regex_unref(special_regex);
311
0
    g_regex_unref(brief_regex);
312
0
    g_regex_unref(tag_regex);
313
0
    g_regex_unref(time_regex);
314
0
    g_regex_unref(thread_regex);
315
0
    g_regex_unref(threadtime_regex);
316
0
    g_regex_unref(process_regex);
317
0
    g_regex_unref(long_regex);
318
0
}
319
320
14
void proto_register_logcat_text(void) {
321
14
    expert_module_t  *expert_module;
322
14
    static hf_register_info hf[] = {
323
14
            { &hf_logcat_text_timestamp,
324
14
                { "Timestamp", "logcat_text.timestamp",
325
14
                FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0x00, NULL, HFILL
326
14
                }
327
14
            },
328
14
            { &hf_logcat_text_tag,
329
14
                { "Tag",       "logcat_text.tag",
330
14
                FT_STRING, BASE_NONE, NULL, 0x00, NULL, HFILL
331
14
                }
332
14
            },
333
14
            { &hf_logcat_text_log,
334
14
                { "Log",       "logcat_text.log",
335
14
                FT_STRING, BASE_NONE, NULL, 0x00, NULL, HFILL
336
14
                }
337
14
            },
338
14
            { &hf_logcat_text_priority,
339
14
                { "Priority",  "logcat_text.priority",
340
14
                FT_UINT8, BASE_DEC, VALS(logcat_priority_vals), 0x00, NULL, HFILL
341
14
                }
342
14
            },
343
14
            { &hf_logcat_text_pid,
344
14
                { "PID",       "logcat_text.pid",
345
14
                FT_UINT32, BASE_DEC, NULL, 0x00, "Process ID", HFILL
346
14
                }
347
14
            },
348
14
            { &hf_logcat_text_tid,
349
14
                { "TID",       "logcat_text.tid",
350
14
                FT_UINT32, BASE_DEC, NULL, 0x00, "Thread ID", HFILL
351
14
                }
352
14
            }
353
14
    };
354
355
14
    static ei_register_info ei[] = {
356
14
            { &ei_malformed_time,  { "logcat_text.malformed_time", PI_PROTOCOL, PI_ERROR, "Malformed time data", EXPFILL }},
357
14
            { &ei_malformed_token, { "logcat_text.malformed_token", PI_PROTOCOL, PI_ERROR, "Failed to decode one or more tokens", EXPFILL }},
358
14
    };
359
360
14
    static int *ett[] = { &ett_logcat};
361
362
14
    proto_logcat_text = proto_register_protocol("Android Logcat Text", "Logcat Text", "logcat_text");
363
14
    proto_register_field_array(proto_logcat_text, hf, array_length(hf));
364
14
    proto_register_subtree_array(ett, array_length(ett));
365
366
14
    logcat_text_brief_handle =      register_dissector("logcat_text_brief",
367
14
            dissect_logcat_text_brief, proto_logcat_text);
368
14
    logcat_text_tag_handle =        register_dissector("logcat_text_tag",
369
14
            dissect_logcat_text_tag, proto_logcat_text);
370
14
    logcat_text_time_handle =       register_dissector("logcat_text_time",
371
14
            dissect_logcat_text_time, proto_logcat_text);
372
14
    logcat_text_process_handle =    register_dissector("logcat_text_process",
373
14
            dissect_logcat_text_process, proto_logcat_text);
374
14
    logcat_text_thread_handle =     register_dissector("logcat_text_thread",
375
14
            dissect_logcat_text_thread, proto_logcat_text);
376
14
    logcat_text_threadtime_handle = register_dissector("logcat_text_threadtime",
377
14
            dissect_logcat_text_threadtime, proto_logcat_text);
378
14
    logcat_text_long_handle =       register_dissector("logcat_text_long",
379
14
            dissect_logcat_text_long, proto_logcat_text);
380
381
14
    logcat_text_create_regex();
382
14
    register_shutdown_routine(logcat_text_shutdown);
383
384
14
    expert_module = expert_register_protocol(proto_logcat_text);
385
14
    expert_register_field_array(expert_module, ei, array_length(ei));
386
387
14
    exported_pdu_tap = register_export_pdu_tap("Logcat Text");
388
14
}
389
390
14
void proto_reg_handoff_logcat_text(void) {
391
14
    dissector_add_uint("wtap_encap", WTAP_ENCAP_LOGCAT_BRIEF,
392
14
            logcat_text_brief_handle);
393
14
    dissector_add_uint("wtap_encap", WTAP_ENCAP_LOGCAT_TAG,
394
14
            logcat_text_tag_handle);
395
14
    dissector_add_uint("wtap_encap", WTAP_ENCAP_LOGCAT_TIME,
396
14
            logcat_text_time_handle);
397
14
    dissector_add_uint("wtap_encap", WTAP_ENCAP_LOGCAT_THREAD,
398
14
            logcat_text_thread_handle);
399
14
    dissector_add_uint("wtap_encap", WTAP_ENCAP_LOGCAT_THREADTIME,
400
14
            logcat_text_threadtime_handle);
401
14
    dissector_add_uint("wtap_encap", WTAP_ENCAP_LOGCAT_PROCESS,
402
14
            logcat_text_process_handle);
403
14
    dissector_add_uint("wtap_encap", WTAP_ENCAP_LOGCAT_LONG,
404
14
            logcat_text_long_handle);
405
14
}
406
407
/*
408
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
409
 *
410
 * Local variables:
411
 * c-basic-offset: 4
412
 * tab-width: 8
413
 * indent-tabs-mode: nil
414
 * End:
415
 *
416
 * vi: set shiftwidth=4 tabstop=8 expandtab:
417
 * :indentSize=4:tabSize=8:noTabs=true:
418
 */