1 /**************************************************************************
5 Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
7 Contact: Boram Park <boram1288.park@samsung.com>
8 Sangjin LEE <lsj119@samsung.com>
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sub license, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 **************************************************************************/
41 #include <sys/types.h>
43 #include <sys/ioctl.h>
49 #include <X11/Xw32defs.h>
50 #include <xdbg_types.h>
51 #include <xdbg_evlog.h>
55 typedef struct _EvlogOption
58 char command_name[PATH_MAX+1];
59 char path_name[PATH_MAX+1];
66 _printUsage(char* name)
68 printf("Usage: %s [OPTION]...\n", name);
70 printf(" Options:\n");
71 printf(" -f [File_Name] File to save information\n");
73 printf(" -r [Rule_File_Name] Setting Rule of certain policy read in File to find information you want\n");
75 printf(" [How To write Rule file form]\n");
76 printf(" ------------------------\n");
77 printf(" | allow [RULE] |\n");
78 printf(" | deny [RULE] |\n");
79 printf(" ------------------------\n");
80 printf(" * It is possible to write multiple policies.\n");
82 printf(" -a [ALLOW Rule] Setting Rule of 'ALLOW' policy to find information you want\n");
84 printf(" -n [Deny Rule] Setting Rule of 'Deny' policy to find information you want\n");
86 printf(" [RULE] : C Language-style boolean expression syntax. [VARIABLE] [COMPAROTOR] [VALUE]\n");
87 printf(" [VARIABLE] : type / major / minor / command / cmd / pid\n");
88 printf(" [COMPARATOR] : & / && / | / || / = / == / != / > / >= / < / <=\n");
89 printf(" [VALUE] : string / number \n");
92 printf(" xevlog_analyze -a \"(type=request) && (major == X11 && (minor = SendEvent || minor = ReceiveEvent))\"\n");
93 printf(" xevlog_analyze -n cmd!=ls\n");
95 printf(" * WARNING : If you set both -a and -n option, must set -a option first. Otherwise Logs you DO NOT want can be printed.\n");
97 printf(" -d [0-2] To Set printing detail log level\n");
99 printf(" 0: To Print Primary Logs (XID, Root, Atom, Region ...)\n");
100 printf(" 1: To Print More Detail Logs (Time, State, Mask ...\n");
101 printf(" 2: To Print Supplementary Reply Logs (Information Including Each Item)\n");
103 printf(" -h Usage of xevlog_anlayze\n");
107 static void _xEvlogAnalyzePrint (EvlogOption *eo, char* reply, int* len)
109 int fd = -1, cfd = -1;
113 EvlogInfo evinfo={0,};
116 if (!eo || !strlen(eo->path_name))
118 printf ("failed: no evlog path\n");
119 _printUsage(eo->command_name);
123 fd = open (eo->path_name, O_RDONLY);
126 char err_buf[256] = {0,};
129 errp = (char *)strerror_r (errno, err_buf, sizeof(err_buf));
130 printf ("failed: open '%s'. (%s)\n", eo->path_name, errp);
134 snprintf (fd_name, sizeof (fd_name), "/proc/%d/fd/1", eo->pid);
135 cfd = open (fd_name, O_RDWR);
139 char err_buf[256] = {0,};
142 errp = (char *)strerror_r (errno, err_buf, sizeof(err_buf));
143 printf ("failed: open consol '%s'. (%s)\n", fd_name, errp);
147 while ((read_len = read (fd, &evlog_len, sizeof (int))) == sizeof (int))
150 int size = sizeof (log);
153 memset (&evinfo, 0, sizeof (EvlogInfo));
158 extern ExtensionInfo Evlog_extensions[];
159 extern int Extensions_size;
161 char* new_argv[3] = {"add", "allow", "all"};
165 xDbgEvlogRuleSet ((const int) new_argc, (const char**) new_argv, reply, len);
167 printf("%s\n", reply);
169 read_len = read (fd, &Extensions_size, sizeof (int));
170 GOTO_IF_FAIL (read_len == sizeof (int), print_done);
171 GOTO_IF_FAIL (Extensions_size > 0, print_done);
174 for (i = 0 ; i < Extensions_size ; i++)
176 read_len = read (fd, &Evlog_extensions[i].opcode, sizeof (int));
177 GOTO_IF_FAIL (read_len == sizeof (int), print_done);
180 read_len = read (fd, &Evlog_extensions[i].evt_base, sizeof (int));
181 GOTO_IF_FAIL (read_len == sizeof (int), print_done);
184 read_len = read (fd, &Evlog_extensions[i].err_base, sizeof (int));
185 GOTO_IF_FAIL (read_len == sizeof (int), print_done);
189 if (!xDbgEvlogGetExtensionEntry ())
191 printf ("failed: get extentions\n");
198 read_len = read (fd, &evinfo.time, sizeof (CARD32));
199 GOTO_IF_FAIL (read_len == sizeof (CARD32), print_done);
202 read_len = read (fd, &evinfo.type, sizeof (EvlogType));
203 GOTO_IF_FAIL (read_len == sizeof (EvlogType), print_done);
204 GOTO_IF_FAIL (evinfo.type >= EVENT && evinfo.type <= ERROR, print_done);
207 read_len = read (fd, &evinfo.mask, sizeof (int));
208 GOTO_IF_FAIL (read_len == sizeof (int), print_done);
211 if (evinfo.mask & EVLOG_MASK_CLIENT)
213 read_len = read (fd, &evinfo.client, sizeof (EvlogClient));
214 GOTO_IF_FAIL (read_len == sizeof (EvlogClient), print_done);
218 if (evinfo.mask & EVLOG_MASK_REQUEST)
222 read_len = read (fd, &evinfo.req, sizeof(EvlogRequest));
223 GOTO_IF_FAIL (read_len == sizeof(EvlogRequest), print_done);
226 size = (int)(evinfo.req.length * 4);
227 GOTO_IF_FAIL (size > 0, print_done);
229 evinfo.req.ptr = malloc (size);
230 GOTO_IF_FAIL (evinfo.req.ptr != NULL, print_done);
232 read_len = read (fd, evinfo.req.ptr, size);
233 GOTO_IF_FAIL (read_len == size, print_done);
237 if (evinfo.mask & EVLOG_MASK_EVENT)
239 read_len = read (fd, &evinfo.evt, sizeof(EvlogEvent));
240 GOTO_IF_FAIL (read_len == sizeof(EvlogEvent), print_done);
243 evinfo.evt.ptr = malloc (evinfo.evt.size);
244 GOTO_IF_FAIL (evinfo.evt.ptr != NULL, print_done);
246 WARNING_IF_FAIL (evinfo.evt.size > 0);
248 read_len = read (fd, evinfo.evt.ptr, evinfo.evt.size);
249 GOTO_IF_FAIL (read_len == evinfo.evt.size, print_done);
253 if (evinfo.mask & EVLOG_MASK_REPLY)
255 read_len = read (fd, &evinfo.rep, sizeof(EvlogReply));
256 GOTO_IF_FAIL (read_len == sizeof(EvlogReply), print_done);
259 evinfo.rep.ptr = malloc (evinfo.rep.size);
260 GOTO_IF_FAIL (evinfo.rep.ptr != NULL, print_done);
262 WARNING_IF_FAIL (evinfo.rep.size > 0);
264 read_len = read (fd, evinfo.rep.ptr, evinfo.rep.size);
265 GOTO_IF_FAIL (read_len == evinfo.rep.size, print_done);
269 if (evinfo.mask & EVLOG_MASK_ERROR)
271 read_len = read (fd, &evinfo.err, sizeof(EvlogError));
272 GOTO_IF_FAIL (read_len == sizeof(EvlogError), print_done);
276 if (evinfo.mask & EVLOG_MASK_ATOM)
278 EvlogAtomTable *table;
280 read_len = read (fd, &evinfo.evatom.size, sizeof (int));
281 GOTO_IF_FAIL (read_len == sizeof(int), print_done);
284 for (i = 0 ; i < evinfo.evatom.size ; i++)
286 table = malloc (sizeof(EvlogAtomTable));
287 GOTO_IF_FAIL (table != NULL, print_done);
289 if (!evinfo.evatom.init)
291 xorg_list_init(&evinfo.evatom.list);
292 evinfo.evatom.init = 1;
295 read_len = read (fd, table, sizeof (EvlogAtomTable));
296 if (read_len != sizeof(EvlogAtomTable))
298 WARNING_IF_FAIL (read_len == sizeof(EvlogAtomTable));
304 xorg_list_add(&table->link, &evinfo.evatom.list);
308 if (evinfo.mask & EVLOG_MASK_REGION)
310 EvlogRegionTable *table;
312 read_len = read (fd, &evinfo.evregion.size, sizeof (int));
313 GOTO_IF_FAIL (read_len == sizeof(int), print_done);
316 for (i = 0 ; i < evinfo.evregion.size ; i++)
318 table = malloc (sizeof(EvlogRegionTable));
319 GOTO_IF_FAIL (table != NULL, print_done);
321 if (!evinfo.evregion.init)
323 xorg_list_init(&evinfo.evregion.list);
324 evinfo.evregion.init = 1;
327 read_len = read (fd, table, sizeof (EvlogRegionTable));
328 if (read_len != sizeof(EvlogRegionTable))
330 WARNING_IF_FAIL (read_len == sizeof(EvlogRegionTable));
336 xorg_list_add(&table->link, &evinfo.evregion.list);
340 GOTO_IF_FAIL (evlog_len == total, print_done);
342 if (xDbgEvlogRuleValidate (&evinfo))
344 if (xDbgEvlogFillLog(&evinfo, eo->detail_level, log, &size))
350 free (evinfo.req.ptr);
351 evinfo.req.ptr = NULL;
356 free (evinfo.evt.ptr);
357 evinfo.evt.ptr = NULL;
360 xDbgDistroyAtomList(&evinfo);
361 xDbgDistroyRegionList(&evinfo);
367 free (evinfo.req.ptr);
370 free (evinfo.evt.ptr);
372 xDbgDistroyAtomList(&evinfo);
373 xDbgDistroyRegionList(&evinfo);
384 _checkOption(int argc, char** argv)
387 EvlogOption eo = {0,};
388 char rule_log[LOG_SIZE];
389 int rule_size = sizeof (rule_log);
391 eo.pid = atoi (argv[0]);
394 snprintf (eo.command_name, sizeof (eo.command_name), "%s", argv[1]);
398 _printUsage( eo.command_name );
402 while ((c = getopt(argc, argv, "f:a:n:r:d:h")) != EOF)
408 if (!optarg || strlen(optarg) <= 0)
410 snprintf (eo.path_name, sizeof (eo.path_name), "%s", optarg);
417 char* new_argv[3] = {"add", "allow", };
419 if (!optarg || strlen(optarg) <= 0)
422 new_argv[2] = (char*)calloc (1, PATH_MAX);
425 printf ("failed: malloc new_argv[2]\n");
429 snprintf (new_argv[2], PATH_MAX, "%s", optarg);
430 if(!xDbgEvlogRuleSet ((const int) new_argc,
431 (const char**) new_argv,
432 rule_log, &rule_size))
434 printf("%s\n", rule_log);
447 char* new_argv[3] = {"add", "deny", };
449 if (!optarg || strlen(optarg) <= 0)
452 new_argv[2] = (char*)calloc (1, PATH_MAX);
455 printf ("failed: malloc new_argv[2]\n");
459 snprintf (new_argv[2], PATH_MAX, "%s", optarg);
460 if(!xDbgEvlogRuleSet ((const int) new_argc,
461 (const char**) new_argv,
462 rule_log, &rule_size))
464 printf("%s\n", rule_log);
476 if (!optarg || strlen(optarg) <= 0)
479 if(!xDbgEvlogReadRuleFile(optarg, rule_log, &rule_size))
481 printf("%s", rule_log);
490 if (!optarg || strlen(optarg) <= 0 || strlen(optarg) > 2)
493 eo.detail_level = atoi(optarg);
494 printf ("Detail Level: %d\n", eo.detail_level);
501 _printUsage( eo.command_name );
511 _xEvlogAnalyzePrint(&eo, rule_log, &rule_size);
516 int main(int argc, char** argv)
523 new_argv = (char**)malloc (new_argc * sizeof (char*));
526 printf ("failed: malloc new argv\n");
530 snprintf (temp, sizeof(temp), "%d", (int)getpid());
533 for (i = 0; i < argc; i++)
534 new_argv[i+1] = argv[i];
536 _checkOption(new_argc, new_argv);