1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/*
* Copyright (c) 2013 Qualcomm Atheros, Inc.
* All Rights Reserved.
* Qualcomm Atheros Confidential and Proprietary.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <getopt.h>
#include <limits.h>
#include <asm/types.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <athdefs.h>
#include <a_types.h>
#include "dbglog.h"
#include "dbglog_host.h"
#define LOGFILE_FLAG 0x01
#define CONSOLE_FLAG 0x02
#define QXDM_FLAG 0x04
const char options[] =
"Options:\n\
-f, --logfile=<Output log file> [Mandotory]\n\
-r, --reclimit=<Maximum number of records before the log rolls over> [Optional]\n\
-c, --console (prints the logs in the console)\n\
-q, --qxdm (prints the logs in the qxdm)\n\
The options can also be given in the abbreviated form --option=x or -o x. The options can be given in any order";
struct sockaddr_nl src_addr, dest_addr;
struct nlmsghdr *nlh = NULL;
struct iovec iov;
int sock_fd;
struct msghdr msg;
static FILE *fwlog_res;
static FILE *log_out;
const char *fwlog_res_file;
int max_records;
int record;
const char *progname;
char dbglogoutfile[PATH_MAX];
int optionflag;
int rec_limit = 1000000; /* Million records is a good default */
static void
usage(void)
{
fprintf(stderr, "Usage:\n%s options\n", progname);
fprintf(stderr, "%s\n", options);
exit(-1);
}
extern int parser_init();
extern int
dbglog_parse_debug_logs(u_int8_t *datap, u_int16_t len);
static unsigned int get_le32(const unsigned char *pos)
{
return pos[0] | (pos[1] << 8) | (pos[2] << 16) | (pos[3] << 24);
}
static size_t reorder(FILE *log_in, FILE *log_out)
{
unsigned char buf[RECLEN];
size_t res;
unsigned int timestamp, min_timestamp = -1;
int pos = 0, min_pos = 0;
pos = 0;
while ((res = fread(buf, RECLEN, 1, log_in)) == 1) {
timestamp = get_le32(buf);
if (timestamp < min_timestamp) {
min_timestamp = timestamp;
min_pos = pos;
}
pos++;
}
printf("First record at position %d\n", min_pos);
fseek(log_in, min_pos * RECLEN, SEEK_SET);
while ((res = fread(buf, RECLEN, 1, log_in)) == 1) {
printf("Read record timestamp=%u length=%u\n",
get_le32(buf), get_le32(&buf[4]));
if (fwrite(buf, RECLEN, res, log_out) != res)
perror("fwrite");
}
fseek(log_in, 0, SEEK_SET);
pos = min_pos;
while (pos > 0 && (res = fread(buf, RECLEN, 1, log_out)) == 1) {
pos--;
printf("Read record timestamp=%u length=%u\n",
get_le32(buf), get_le32(&buf[4]));
if (fwrite(buf, RECLEN, res, log_out) != res)
perror("fwrite");
}
return 0;
}
static void cleanup(void) {
close(sock_fd);
fwlog_res = fopen(fwlog_res_file, "w");
if (fwlog_res == NULL) {
perror("Failed to open reorder fwlog file");
goto out;
}
reorder(log_out, fwlog_res);
out:
fclose(fwlog_res);
fclose(log_out);
}
static void stop(int signum)
{
if(optionflag & LOGFILE_FLAG){
printf("Recording stopped\n");
cleanup();
}
exit(0);
}
int main(int argc, char *argv[])
{
int res =0;
unsigned char *buf;
int c;
char *mesg="Hello";
progname = argv[0];
int option_index = 0;
static struct option long_options[] = {
{"logfile", 1, NULL, 'f'},
{"reclimit", 1, NULL, 'r'},
{"console", 0, NULL, 'c'},
{"qxdm", 0, NULL, 'q'},
{ 0, 0, 0, 0}
};
while (1) {
c = getopt_long (argc, argv, "f:cq:r:", long_options, &option_index);
if (c == -1) break;
switch (c) {
case 'f':
memset(dbglogoutfile, 0, PATH_MAX);
memcpy(dbglogoutfile, optarg, strlen(optarg));
optionflag |= LOGFILE_FLAG;
break;
case 'c':
optionflag |= CONSOLE_FLAG;
break;
case 'q':
printf("Do it for QXDM \n");
optionflag |= QXDM_FLAG;
break;
case 'r':
rec_limit = strtoul(optarg, NULL, 0);
break;
default:
usage();
}
}
if (!(optionflag & (LOGFILE_FLAG | CONSOLE_FLAG | QXDM_FLAG))) {
usage();
return -1;
}
sock_fd = socket(PF_NETLINK, SOCK_RAW, CLD_NETLINK_USER);
if (sock_fd < 0)
return -1;
memset(&src_addr, 0, sizeof(src_addr));
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid(); /* self pid */
bind(sock_fd, (struct sockaddr *)&src_addr, sizeof(src_addr));
memset(&dest_addr, 0, sizeof(dest_addr));
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.nl_family = AF_NETLINK;
dest_addr.nl_pid = 0; /* For Linux Kernel */
dest_addr.nl_groups = 0; /* unicast */
nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(RECLEN));
memset(nlh, 0, NLMSG_SPACE(RECLEN));
nlh->nlmsg_len = NLMSG_SPACE(RECLEN);
nlh->nlmsg_pid = getpid();
nlh->nlmsg_flags = 0;
memcpy(NLMSG_DATA(nlh), mesg, strlen(mesg));
iov.iov_base = (void *)nlh;
iov.iov_len = nlh->nlmsg_len;
msg.msg_name = (void *)&dest_addr;
msg.msg_namelen = sizeof(dest_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
sendmsg(sock_fd, &msg, 0);
signal(SIGINT, stop);
signal(SIGTERM, stop);
if (optionflag & LOGFILE_FLAG) {
if (rec_limit < RECLEN) {
fprintf(stderr, "Too small maximum length (has to be >= %d)\n",
RECLEN);
close(sock_fd);
return -1;
}
max_records = rec_limit / RECLEN;
printf("Storing last %d records\n", max_records);
log_out = fopen(dbglogoutfile, "w");
if (log_out == NULL) {
perror("Failed to create output file");
close(sock_fd);
return -1;
}
fwlog_res_file = "./reorder";
/* Read message from kernel */
while ((res = recvmsg(sock_fd, &msg, 0)) > 0) {
buf = (unsigned char *)NLMSG_DATA(nlh);
printf("Read record timestamp=%u length=%u \n",
get_le32(&buf[0]), get_le32(&buf[4]));
fseek(log_out, record * RECLEN, SEEK_SET);
if ((res = fwrite(buf, RECLEN, 1, log_out)) != 1){
perror("fwrite");
break;
}
record++;
if (record == max_records)
record = 0;
}
printf("Incomplete read: %d bytes\n", (int) res);
cleanup();
}
if (optionflag & CONSOLE_FLAG) {
parser_init();
while ((res = recvmsg(sock_fd, &msg, 0)) > 0) {
buf = (unsigned char *)NLMSG_DATA(nlh);
dbglog_parse_debug_logs(&buf[8], get_le32(&buf[4]));
}
close(sock_fd);
}
return 0;
}
|