blob: 64b25d137840a5c31c9f03b0b6a21bf90f2ad731 [file] [log] [blame]
David Ng11394472012-10-25 17:32:04 -07001/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <unistd.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <getopt.h>
33#include <errno.h>
34#include <sys/types.h>
35#include <sys/stat.h>
36#include <fcntl.h>
37
38#include <linux/coresight-stm.h>
39#include "stm-log.h"
40
41#define FILE_READ_MAX 8192 /* simplify handling by restricting to
42 maximum of 8K file read as this is only
43 a reference/test app for STM logging
44 */
45static char buf[FILE_READ_MAX];
46static int buf_count = 0;
47
48int main(int argc, char **argv)
49{
50 int opt, fd = -1;
51 int ent = 0, prot = 0, Ots= 1, Og = 0, Oopts = 0;
52 char *string = NULL;
53 int isString = 0, isFile = 0;
54
55 while ((opt = getopt(argc, argv, "e:p:t:g:s:f:")) != -1) {
56 switch (opt) {
57 case 'e':
58 ent = atoi(optarg);
59 if (ent < 0)
60 ent = 0;
61 if (ent > 255)
62 ent = 255;
63 break;
64 case 'p':
65 prot = atoi(optarg);
66 if (prot < 0)
67 prot = 0;
68 if (prot > 255)
69 prot = 255;
70 break;
71 case 't':
72 Ots = (atoi(optarg) != 0);
73 break;
74 case 'g':
75 Og = (atoi(optarg) != 0);
76 break;
77 case 's':
78 if (string)
79 free(string);
80 string = strdup(optarg);
81 isString = 1;
82 isFile = 0;
83 break;
84 case 'f':
85 if (string)
86 free(string);
87 string = strdup(optarg);
88 isString = 0;
89 isFile = 1;
90 break;
91 default:
92 printf("\n");
93 printf("%s [-e <entity>] [-p <protocol>] [-t <0|1>] [-g <0|1>] -s <string>\n", argv[0]);
94 printf("%s [-e <entity>] [-p <protocol>] [-t <0|1>] [-g <0|1>] -f <file>\n", argv[0]);
95 return 1;
96 }
97 }
98
99 if (!isString && !isFile) {
100 printf("Must either specify string or file\n");
101 return 1;
102 }
103 if (!string) {
104 printf("Internal error\n");
105 return 1;
106 }
107
108 printf("Entity=%d Protocol=%d Opt-TS=%d Opt-G=%d: %s='%s'\n",
109 ent, prot, Ots, Og,
110 (isString) ? "string" : "file",
111 string);
112
113 Oopts = (Ots ? STM_OPTION_TIMESTAMPED : 0)
114 | (Og ? STM_OPTION_GUARANTEED : 0);
115
116 do {
117 if (isString) {
118 stm_log_ex(ent, prot, Oopts, string);
119 }
120 else {
121 fd = open(string, O_RDONLY);
122 if (fd == -1) {
123 printf("Failed to open file (%s)\n", strerror(errno));
124 break;
125 }
126 buf_count = read(fd, buf, FILE_READ_MAX);
127 if (buf_count >= 0) {
128 stm_logbin_ex(ent, prot, Oopts, buf_count, buf);
129 }
130 }
131 } while (0);
132
133 free(string);
134 if (fd != -1)
135 close(fd);
136 return 0;
137}