3 * Copyright 2014, Michal Orynicz for Tieto Corporation
4 * Copyright 2014, Michal Labedzki for Tieto Corporation
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "file_wrappers.h"
28 #include "logcat_text.h"
35 /* Returns '?' for invalid priorities */
36 static gchar get_priority(const guint8 priority) {
37 static gchar priorities[] = "??VDIWEFS";
39 if (priority >= (guint8) sizeof(priorities))
42 return priorities[priority];
45 static gint buffered_detect_version(const guint8 *pd)
47 const struct logger_entry *log_entry;
48 const struct logger_entry_v2 *log_entry_v2;
50 const guint8 *msg_payload = NULL;
55 log_entry = (const struct logger_entry *)(const void *) pd;
56 log_entry_v2 = (const struct logger_entry_v2 *)(const void *) pd;
58 /* must contain at least priority and two nulls as separator */
59 if (log_entry->len < 3)
62 /* payload length may not exceed the maximum payload size */
63 if (log_entry->len > LOGGER_ENTRY_MAX_PAYLOAD)
66 /* cannot rely on __pad being 0 for v1, use heuristics to find out what
67 * version is in use. First assume the smallest msg. */
68 for (version = 1; version <= 2; ++version) {
70 msg_payload = (const guint8 *) (log_entry + 1);
71 } else if (version == 2) {
72 /* v2 is 4 bytes longer */
73 msg_payload = (const guint8 *) (log_entry_v2 + 1);
74 if (log_entry_v2->hdr_size != sizeof(*log_entry_v2))
78 /* A v2 msg has a 32-bit userid instead of v1 priority */
79 if (get_priority(msg_payload[0]) == '?')
82 /* Is there a terminating '\0' for the tag? */
83 msg_part = (guint8 *) memchr(msg_payload, '\0', log_entry->len - 1);
87 /* if msg is '\0'-terminated, is it equal to the payload len? */
89 msg_len = (guint16)(log_entry->len - (msg_part - msg_payload));
90 msg_end = (guint8 *) memchr(msg_part, '\0', msg_len);
91 /* is the end of the buffer (-1) equal to the end of msg? */
92 if (msg_end && (msg_payload + log_entry->len - 1 != msg_end))
101 static gchar *logcat_log(const struct dumper_t *dumper, guint32 seconds,
102 gint milliseconds, gint pid, gint tid, gchar priority, const gchar *tag,
105 gchar time_buffer[15];
108 datetime = (time_t) seconds;
110 switch (dumper->type) {
111 case WTAP_ENCAP_LOGCAT_BRIEF:
112 return g_strdup_printf("%c/%-8s(%5i): %s\n",
113 priority, tag, pid, log);
114 case WTAP_ENCAP_LOGCAT_PROCESS:
115 /* NOTE: Last parameter should be "process name", not tag;
116 Unfortunately, we do not have process name */
117 return g_strdup_printf("%c(%5i) %s (%s)\n",
118 priority, pid, log, "");
119 case WTAP_ENCAP_LOGCAT_TAG:
120 return g_strdup_printf("%c/%-8s: %s\n",
122 case WTAP_ENCAP_LOGCAT_THREAD:
123 return g_strdup_printf("%c(%5i:%5i) %s\n",
124 priority, pid, tid, log);
125 case WTAP_ENCAP_LOGCAT_TIME:
126 strftime(time_buffer, sizeof(time_buffer), "%m-%d %H:%M:%S",
128 return g_strdup_printf("%s.%03i %c/%-8s(%5i): %s\n",
129 time_buffer, milliseconds, priority, tag, pid, log);
130 case WTAP_ENCAP_LOGCAT_THREADTIME:
131 strftime(time_buffer, sizeof(time_buffer), "%m-%d %H:%M:%S",
133 return g_strdup_printf("%s.%03i %5i %5i %c %-8s: %s\n",
134 time_buffer, milliseconds, pid, tid, priority, tag, log);
135 case WTAP_ENCAP_LOGCAT_LONG:
136 strftime(time_buffer, sizeof(time_buffer), "%m-%d %H:%M:%S",
138 return g_strdup_printf("[ %s.%03i %5i:%5i %c/%-8s ]\n%s\n\n",
139 time_buffer, milliseconds, pid, tid, priority, tag, log);
146 static void get_time(gchar *string, struct wtap_pkthdr *phdr) {
151 if (6 == sscanf(string, "%d-%d %d:%d:%d.%d", &date.tm_mon, &date.tm_mday, &date.tm_hour,
152 &date.tm_min, &date.tm_sec, &ms)) {
155 seconds = mktime(&date);
156 phdr->ts.secs = (time_t) seconds;
157 phdr->ts.nsecs = (int) (ms * 1e6);
158 phdr->presence_flags = WTAP_HAS_TS;
160 phdr->presence_flags = 0;
161 phdr->ts.secs = (time_t) 0;
162 phdr->ts.nsecs = (int) 0;
166 static gboolean logcat_text_read_packet(FILE_T fh, struct wtap_pkthdr *phdr,
167 Buffer *buf, gint file_type) {
169 gchar cbuff[WTAP_MAX_PACKET_SIZE];
173 ret = file_gets(cbuff, WTAP_MAX_PACKET_SIZE, fh);
174 } while (NULL != ret && 3 > strlen(cbuff) && !file_eof(fh));
176 if (NULL == ret || 3 > strlen(cbuff)) {
180 if (WTAP_FILE_TYPE_SUBTYPE_LOGCAT_LONG == file_type &&
181 !g_regex_match_simple(SPECIAL_STRING, cbuff, (GRegexCompileFlags)((gint) G_REGEX_ANCHORED | (gint) G_REGEX_RAW), G_REGEX_MATCH_NOTEMPTY)) {
183 gchar lbuff[WTAP_MAX_PACKET_SIZE];
187 file_off = file_tell(fh);
188 ret2 = file_gets(lbuff,WTAP_MAX_PACKET_SIZE, fh);
189 while (NULL != ret2 && 2 < strlen(lbuff) && !file_eof(fh)) {
190 g_strlcat(cbuff,lbuff,WTAP_MAX_PACKET_SIZE);
191 file_off = file_tell(fh);
192 ret2 = file_gets(lbuff,WTAP_MAX_PACKET_SIZE, fh);
195 if(NULL == ret2 || 2 < strlen(lbuff)) {
199 file_seek(fh,file_off,SEEK_SET,&err);
202 phdr->rec_type = REC_TYPE_PACKET;
203 phdr->caplen = (guint32)strlen(cbuff);
204 phdr->len = phdr->caplen;
206 ws_buffer_assure_space(buf, phdr->caplen + 1);
207 pd = ws_buffer_start_ptr(buf);
208 if ((WTAP_FILE_TYPE_SUBTYPE_LOGCAT_TIME == file_type
209 || WTAP_FILE_TYPE_SUBTYPE_LOGCAT_THREADTIME == file_type
210 || WTAP_FILE_TYPE_SUBTYPE_LOGCAT_LONG == file_type)
211 && '-' != cbuff[0]) { /* the last part filters out the -- beginning of... lines */
212 if (WTAP_FILE_TYPE_SUBTYPE_LOGCAT_LONG == file_type) {
213 get_time(cbuff+2, phdr);
215 get_time(cbuff, phdr);
218 phdr->presence_flags = 0;
219 phdr->ts.secs = (time_t) 0;
220 phdr->ts.nsecs = (int) 0;
222 memcpy(pd, cbuff, phdr->caplen + 1);
226 static gboolean logcat_text_read(wtap *wth, int *err _U_ , gchar **err_info _U_,
227 gint64 *data_offset) {
228 *data_offset = file_tell(wth->fh);
230 return logcat_text_read_packet(wth->fh, &wth->phdr, wth->frame_buffer,
231 wth->file_type_subtype);
234 static gboolean logcat_text_seek_read(wtap *wth, gint64 seek_off,
235 struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info _U_) {
236 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
239 if (!logcat_text_read_packet(wth->random_fh, phdr, buf,
240 wth->file_type_subtype)) {
242 *err = WTAP_ERR_SHORT_READ;
248 wtap_open_return_val logcat_text_open(wtap *wth, int *err, gchar **err_info _U_) {
249 gchar cbuff[WTAP_MAX_PACKET_SIZE];
252 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
253 return WTAP_OPEN_ERROR;
256 ret = file_gets(cbuff, WTAP_MAX_PACKET_SIZE, wth->fh);
257 } while (NULL != ret && !file_eof(wth->fh)
258 && ((3 > strlen(cbuff))
259 || g_regex_match_simple(SPECIAL_STRING, cbuff, (GRegexCompileFlags)((gint) G_REGEX_ANCHORED | (gint) G_REGEX_RAW),
260 G_REGEX_MATCH_NOTEMPTY)));
262 if (g_regex_match_simple(BRIEF_STRING, cbuff, (GRegexCompileFlags)((gint) G_REGEX_ANCHORED | (gint) G_REGEX_RAW),
263 G_REGEX_MATCH_NOTEMPTY)) {
264 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_LOGCAT_BRIEF;
265 wth->file_encap = WTAP_ENCAP_LOGCAT_BRIEF;
266 } else if (g_regex_match_simple(TAG_STRING, cbuff, (GRegexCompileFlags)((gint) G_REGEX_ANCHORED | (gint) G_REGEX_RAW),
267 G_REGEX_MATCH_NOTEMPTY)) {
268 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_LOGCAT_TAG;
269 wth->file_encap = WTAP_ENCAP_LOGCAT_TAG;
270 } else if (g_regex_match_simple(PROCESS_STRING, cbuff, (GRegexCompileFlags)((gint) G_REGEX_ANCHORED | (gint) G_REGEX_RAW),
271 G_REGEX_MATCH_NOTEMPTY)) {
272 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_LOGCAT_PROCESS;
273 wth->file_encap = WTAP_ENCAP_LOGCAT_PROCESS;
274 } else if (g_regex_match_simple(TIME_STRING, cbuff, (GRegexCompileFlags)((gint) G_REGEX_ANCHORED | (gint) G_REGEX_RAW),
275 G_REGEX_MATCH_NOTEMPTY)) {
276 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_LOGCAT_TIME;
277 wth->file_encap = WTAP_ENCAP_LOGCAT_TIME;
278 } else if (g_regex_match_simple(THREAD_STRING, cbuff, (GRegexCompileFlags)((gint) G_REGEX_ANCHORED | (gint) G_REGEX_RAW),
279 G_REGEX_MATCH_NOTEMPTY)) {
280 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_LOGCAT_THREAD;
281 wth->file_encap = WTAP_ENCAP_LOGCAT_THREAD;
282 } else if (g_regex_match_simple(THREADTIME_STRING, cbuff, (GRegexCompileFlags)((gint) G_REGEX_ANCHORED | (gint) G_REGEX_RAW),
283 G_REGEX_MATCH_NOTEMPTY)) {
284 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_LOGCAT_THREADTIME;
285 wth->file_encap = WTAP_ENCAP_LOGCAT_THREADTIME;
286 } else if (g_regex_match_simple(LONG_STRING, cbuff, (GRegexCompileFlags)((gint) G_REGEX_ANCHORED | (gint) G_REGEX_RAW),
287 G_REGEX_MATCH_NOTEMPTY)) {
288 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_LOGCAT_LONG;
289 wth->file_encap = WTAP_ENCAP_LOGCAT_LONG;
291 return WTAP_OPEN_NOT_MINE;
294 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
295 return WTAP_OPEN_ERROR;
297 wth->snapshot_length = 0;
299 wth->subtype_read = logcat_text_read;
300 wth->subtype_seek_read = logcat_text_seek_read;
301 wth->file_tsprec = WTAP_TSPREC_USEC;
302 return WTAP_OPEN_MINE;
305 int logcat_text_brief_dump_can_write_encap(int encap) {
306 if (encap == WTAP_ENCAP_PER_PACKET)
307 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
310 case WTAP_ENCAP_LOGCAT:
311 case WTAP_ENCAP_LOGCAT_BRIEF:
312 case WTAP_ENCAP_WIRESHARK_UPPER_PDU:
315 return WTAP_ERR_UNWRITABLE_ENCAP;
319 int logcat_text_process_dump_can_write_encap(int encap) {
320 if (encap == WTAP_ENCAP_PER_PACKET)
321 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
324 case WTAP_ENCAP_LOGCAT:
325 case WTAP_ENCAP_LOGCAT_PROCESS:
326 case WTAP_ENCAP_WIRESHARK_UPPER_PDU:
329 return WTAP_ERR_UNWRITABLE_ENCAP;
333 int logcat_text_tag_dump_can_write_encap(int encap) {
334 if (encap == WTAP_ENCAP_PER_PACKET)
335 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
338 case WTAP_ENCAP_LOGCAT:
339 case WTAP_ENCAP_LOGCAT_TAG:
340 case WTAP_ENCAP_WIRESHARK_UPPER_PDU:
343 return WTAP_ERR_UNWRITABLE_ENCAP;
347 int logcat_text_time_dump_can_write_encap(int encap) {
348 if (encap == WTAP_ENCAP_PER_PACKET)
349 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
352 case WTAP_ENCAP_LOGCAT:
353 case WTAP_ENCAP_LOGCAT_TIME:
354 case WTAP_ENCAP_WIRESHARK_UPPER_PDU:
357 return WTAP_ERR_UNWRITABLE_ENCAP;
361 int logcat_text_thread_dump_can_write_encap(int encap) {
362 if (encap == WTAP_ENCAP_PER_PACKET)
363 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
366 case WTAP_ENCAP_LOGCAT:
367 case WTAP_ENCAP_LOGCAT_THREAD:
368 case WTAP_ENCAP_WIRESHARK_UPPER_PDU:
371 return WTAP_ERR_UNWRITABLE_ENCAP;
375 int logcat_text_threadtime_dump_can_write_encap(int encap) {
376 if (encap == WTAP_ENCAP_PER_PACKET)
377 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
380 case WTAP_ENCAP_LOGCAT:
381 case WTAP_ENCAP_LOGCAT_THREADTIME:
382 case WTAP_ENCAP_WIRESHARK_UPPER_PDU:
385 return WTAP_ERR_UNWRITABLE_ENCAP;
389 int logcat_text_long_dump_can_write_encap(int encap) {
390 if (encap == WTAP_ENCAP_PER_PACKET)
391 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
394 case WTAP_ENCAP_LOGCAT:
395 case WTAP_ENCAP_LOGCAT_LONG:
396 case WTAP_ENCAP_WIRESHARK_UPPER_PDU:
399 return WTAP_ERR_UNWRITABLE_ENCAP;
403 static gboolean logcat_text_dump_text(wtap_dumper *wdh,
404 const struct wtap_pkthdr *phdr,
405 const guint8 *pd, int *err, gchar **err_info)
410 const struct logger_entry *log_entry;
411 const struct logger_entry_v2 *log_entry_v2;
418 const guint8 *msg_payload = NULL;
419 const gchar *msg_begin;
425 const struct dumper_t *dumper = (const struct dumper_t *) wdh->priv;
427 /* We can only write packet records. */
428 if (phdr->rec_type != REC_TYPE_PACKET) {
429 *err = WTAP_ERR_UNWRITABLE_REC_TYPE;
433 switch (wdh->encap) {
434 case WTAP_ENCAP_WIRESHARK_UPPER_PDU:
438 skipped_length = logcat_exported_pdu_length(pd);
439 pd += skipped_length;
441 if (!wtap_dump_file_write(wdh, (const gchar*) pd, phdr->caplen - skipped_length, err)) {
446 case WTAP_ENCAP_LOGCAT:
447 /* Skip EXPORTED_PDU*/
448 if (wdh->encap == WTAP_ENCAP_WIRESHARK_UPPER_PDU) {
451 skipped_length = logcat_exported_pdu_length(pd);
452 pd += skipped_length;
454 logcat_version = buffered_detect_version(pd);
456 const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
458 logcat_version = pseudo_header->logcat.version;
461 log_entry = (const struct logger_entry *)(const void *) pd;
462 log_entry_v2 = (const struct logger_entry_v2 *)(const void *) pd;
464 payload_length = GINT32_FROM_LE(log_entry->len);
465 pid = GINT32_FROM_LE(log_entry->pid);
466 tid = GINT32_FROM_LE(log_entry->tid);
467 seconds = GINT32_FROM_LE(log_entry->sec);
468 milliseconds = GINT32_FROM_LE(log_entry->nsec) / 1000000;
470 /* msg: <prio:1><tag:N>\0<msg:N>\0 with N >= 0, last \0 can be missing */
471 if (logcat_version == 1) {
472 msg_payload = (const guint8 *) (log_entry + 1);
474 priority = get_priority(msg_payload[0]);
475 tag = msg_payload + 1;
476 msg_pre_skip = 1 + (gint) strlen(tag) + 1;
477 msg_begin = msg_payload + msg_pre_skip;
478 } else if (logcat_version == 2) {
479 msg_payload = (const guint8 *) (log_entry_v2 + 1);
481 priority = get_priority(msg_payload[0]);
482 tag = msg_payload + 1;
483 msg_pre_skip = 1 + (gint) strlen(tag) + 1;
484 msg_begin = msg_payload + msg_pre_skip;
486 *err = WTAP_ERR_UNWRITABLE_REC_DATA;
487 *err_info = g_strdup_printf("logcat: version %d isn't supported",
492 /* copy the message part. If a nul byte was missing, it will be added. */
493 log = g_strndup(msg_begin, payload_length - msg_pre_skip);
495 /* long format: display one header followed by the whole message (which may
496 * contain new lines). Other formats: include tag, etc. with each line */
500 if (dumper->type == WTAP_ENCAP_LOGCAT_LONG) {
501 /* read until end, there is no next string */
504 /* read until next newline */
505 log_next = strchr(log_part, '\n');
506 if (log_next != NULL) {
509 /* ignore trailing newline */
510 if (*log_next == '\0') {
516 buf = logcat_log(dumper, seconds, milliseconds, pid, tid, priority, tag, log_part);
521 length = (guint32) strlen(buf);
523 if (!wtap_dump_file_write(wdh, buf, length, err)) {
528 wdh->bytes_dumped += length;
529 } while (log_next != NULL );
534 case WTAP_ENCAP_LOGCAT_BRIEF:
535 case WTAP_ENCAP_LOGCAT_TAG:
536 case WTAP_ENCAP_LOGCAT_PROCESS:
537 case WTAP_ENCAP_LOGCAT_TIME:
538 case WTAP_ENCAP_LOGCAT_THREAD:
539 case WTAP_ENCAP_LOGCAT_THREADTIME:
540 case WTAP_ENCAP_LOGCAT_LONG:
541 if (dumper->type == wdh->encap) {
542 if (!wtap_dump_file_write(wdh, (const gchar*) pd, phdr->caplen, err)) {
546 *err = WTAP_ERR_UNWRITABLE_FILE_TYPE;
554 static gboolean logcat_text_dump_open(wtap_dumper *wdh, guint dump_type, int *err _U_) {
555 struct dumper_t *dumper;
557 dumper = (struct dumper_t *) g_malloc(sizeof(struct dumper_t));
558 dumper->type = dump_type;
561 wdh->subtype_write = logcat_text_dump_text;
566 gboolean logcat_text_brief_dump_open(wtap_dumper *wdh, int *err) {
567 return logcat_text_dump_open(wdh, WTAP_ENCAP_LOGCAT_BRIEF, err);
570 gboolean logcat_text_process_dump_open(wtap_dumper *wdh, int *err) {
571 return logcat_text_dump_open(wdh, WTAP_ENCAP_LOGCAT_PROCESS, err);
574 gboolean logcat_text_tag_dump_open(wtap_dumper *wdh, int *err) {
575 return logcat_text_dump_open(wdh, WTAP_ENCAP_LOGCAT_TAG, err);
578 gboolean logcat_text_time_dump_open(wtap_dumper *wdh, int *err) {
579 return logcat_text_dump_open(wdh, WTAP_ENCAP_LOGCAT_TIME, err);
582 gboolean logcat_text_thread_dump_open(wtap_dumper *wdh, int *err) {
583 return logcat_text_dump_open(wdh, WTAP_ENCAP_LOGCAT_THREAD, err);
586 gboolean logcat_text_threadtime_dump_open(wtap_dumper *wdh, int *err) {
587 return logcat_text_dump_open(wdh, WTAP_ENCAP_LOGCAT_THREADTIME, err);
590 gboolean logcat_text_long_dump_open(wtap_dumper *wdh, int *err) {
591 return logcat_text_dump_open(wdh, WTAP_ENCAP_LOGCAT_LONG, err);
595 * Editor modelines - http://www.wireshark.org/tools/modelines.html
600 * indent-tabs-mode: nil
603 * vi: set shiftwidth=4 tabstop=8 expandtab:
604 * :indentSize=4:tabSize=8:noTabs=true: