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] : & / && / and / | / || / or / = / == / != / > / >= / < / <=\n");
89 printf(" [VALUE] : string / number \n");
92 printf(" xevlog_analyze -a \"(type=request) && (major == X11 and (minor = SendEvent or 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 (!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 printf ("failed: open '%s'. (%s)\n", eo->path_name, strerror(errno));
130 snprintf (fd_name, sizeof (fd_name), "/proc/%d/fd/1", eo->pid);
131 cfd = open (fd_name, O_RDWR);
135 printf ("failed: open consol '%s'. (%s)\n", fd_name, strerror(errno));
139 while ((read_len = read (fd, &evlog_len, sizeof (int))) == sizeof (int))
142 int size = sizeof (log);
145 memset (&evinfo, 0, sizeof (EvlogInfo));
150 extern ExtensionInfo Evlog_extensions[];
151 extern int Extensions_size;
153 char* new_argv[3] = {"add", "allow", "all"};
157 xDbgEvlogRuleSet ((const int) new_argc, (const char**) new_argv, reply, len);
159 printf("%s\n", reply);
161 read_len = read (fd, &Extensions_size, sizeof (int));
162 GOTO_IF_FAIL (read_len == sizeof (int), print_done);
165 for (i = 0 ; i < Extensions_size ; i++)
167 read_len = read (fd, &Evlog_extensions[i].opcode, sizeof (int));
168 GOTO_IF_FAIL (read_len == sizeof (int), print_done);
171 read_len = read (fd, &Evlog_extensions[i].evt_base, sizeof (int));
172 GOTO_IF_FAIL (read_len == sizeof (int), print_done);
175 read_len = read (fd, &Evlog_extensions[i].err_base, sizeof (int));
176 GOTO_IF_FAIL (read_len == sizeof (int), print_done);
180 if (!xDbgEvlogGetExtensionEntry ())
182 printf ("failed: get extentions\n");
189 read_len = read (fd, &evinfo.time, sizeof (CARD32));
190 GOTO_IF_FAIL (read_len == sizeof (CARD32), print_done);
193 read_len = read (fd, &evinfo.type, sizeof (EvlogType));
194 GOTO_IF_FAIL (read_len == sizeof (EvlogType), print_done);
195 GOTO_IF_FAIL (evinfo.type >= EVENT && evinfo.type <= ERROR, print_done);
198 read_len = read (fd, &evinfo.mask, sizeof (int));
199 GOTO_IF_FAIL (read_len == sizeof (int), print_done);
202 if (evinfo.mask & EVLOG_MASK_CLIENT)
204 read_len = read (fd, &evinfo.client, sizeof (EvlogClient));
205 GOTO_IF_FAIL (read_len == sizeof (EvlogClient), print_done);
209 if (evinfo.mask & EVLOG_MASK_REQUEST)
211 read_len = read (fd, &evinfo.req, sizeof(EvlogRequest));
212 GOTO_IF_FAIL (read_len == sizeof(EvlogRequest), print_done);
215 evinfo.req.ptr = malloc (evinfo.req.length * 4);
216 GOTO_IF_FAIL (evinfo.req.ptr != NULL, print_done);
218 read_len = read (fd, evinfo.req.ptr, (evinfo.req.length * 4));
219 GOTO_IF_FAIL (read_len == (evinfo.req.length * 4), print_done);
223 if (evinfo.mask & EVLOG_MASK_EVENT)
225 read_len = read (fd, &evinfo.evt, sizeof(EvlogEvent));
226 GOTO_IF_FAIL (read_len == sizeof(EvlogEvent), print_done);
229 evinfo.evt.ptr = malloc (evinfo.evt.size);
230 GOTO_IF_FAIL (evinfo.evt.ptr != NULL, print_done);
232 WARNING_IF_FAIL (evinfo.evt.size > 0);
234 read_len = read (fd, evinfo.evt.ptr, evinfo.evt.size);
235 GOTO_IF_FAIL (read_len == evinfo.evt.size, print_done);
239 if (evinfo.mask & EVLOG_MASK_REPLY)
241 read_len = read (fd, &evinfo.rep, sizeof(EvlogReply));
242 GOTO_IF_FAIL (read_len == sizeof(EvlogReply), print_done);
245 evinfo.rep.ptr = malloc (evinfo.rep.size);
246 GOTO_IF_FAIL (evinfo.rep.ptr != NULL, print_done);
248 WARNING_IF_FAIL (evinfo.rep.size > 0);
250 read_len = read (fd, evinfo.rep.ptr, evinfo.rep.size);
251 GOTO_IF_FAIL (read_len == evinfo.rep.size, print_done);
255 if (evinfo.mask & EVLOG_MASK_ERROR)
257 read_len = read (fd, &evinfo.err, sizeof(EvlogError));
258 GOTO_IF_FAIL (read_len == sizeof(EvlogError), print_done);
262 if (evinfo.mask & EVLOG_MASK_ATOM)
264 EvlogAtomTable *table;
266 read_len = read (fd, &evinfo.evatom.size, sizeof (int));
267 GOTO_IF_FAIL (read_len == sizeof(int), print_done);
270 for (i = 0 ; i < evinfo.evatom.size ; i++)
272 table = malloc (sizeof(EvlogAtomTable));
273 GOTO_IF_FAIL (table != NULL, print_done);
275 if (!evinfo.evatom.init)
277 xorg_list_init(&evinfo.evatom.list);
278 evinfo.evatom.init = 1;
280 read_len = read (fd, table, sizeof (EvlogAtomTable));
281 GOTO_IF_FAIL (read_len == sizeof(EvlogAtomTable), print_done);
284 xorg_list_add(&table->link, &evinfo.evatom.list);
288 if (evinfo.mask & EVLOG_MASK_REGION)
290 EvlogRegionTable *table;
292 read_len = read (fd, &evinfo.evregion.size, sizeof (int));
293 GOTO_IF_FAIL (read_len == sizeof(int), print_done);
296 for (i = 0 ; i < evinfo.evregion.size ; i++)
298 table = malloc (sizeof(EvlogRegionTable));
299 GOTO_IF_FAIL (table != NULL, print_done);
301 if (!evinfo.evregion.init)
303 xorg_list_init(&evinfo.evregion.list);
304 evinfo.evregion.init = 1;
307 read_len = read (fd, table, sizeof (EvlogRegionTable));
308 GOTO_IF_FAIL (read_len == sizeof(EvlogRegionTable), print_done);
311 xorg_list_add(&table->link, &evinfo.evregion.list);
315 GOTO_IF_FAIL (evlog_len == total, print_done);
317 if (xDbgEvlogRuleValidate (&evinfo))
319 if (xDbgEvlogFillLog(&evinfo, eo->detail_level, log, &size))
325 free (evinfo.req.ptr);
326 evinfo.req.ptr = NULL;
331 free (evinfo.evt.ptr);
332 evinfo.evt.ptr = NULL;
335 xDbgDistroyAtomList(&evinfo);
336 xDbgDistroyRegionList(&evinfo);
342 free (evinfo.req.ptr);
345 free (evinfo.evt.ptr);
347 xDbgDistroyAtomList(&evinfo);
348 xDbgDistroyRegionList(&evinfo);
359 _checkOption(int argc, char** argv)
363 char* opt_str = NULL;
364 EvlogOption eo = {0,};
365 char rule_log[LOG_SIZE];
366 int rule_size = sizeof (rule_log);
368 eo.pid = atoi (argv[0]);
371 strncpy(eo.command_name, argv[1], PATH_MAX);
375 _printUsage( eo.command_name );
379 while ((c = getopt(argc, argv, "f:a:n:r:d:h")) != EOF)
386 opt_str_len = strlen(opt_str);
390 strncpy (eo.path_name, opt_str, PATH_MAX);
398 opt_str_len = strlen(opt_str);
402 char* new_argv[3] = {"add", "allow", };
404 new_argv[2] = (char*)malloc (opt_str_len + 1);
407 printf ("failed: malloc new_argv[2]\n");
411 strncpy (new_argv[2], opt_str , opt_str_len);
412 if(!xDbgEvlogRuleSet ((const int) new_argc,
413 (const char**) new_argv,
414 rule_log, &rule_size))
416 printf("%s\n", rule_log);
430 opt_str_len = strlen(opt_str);
435 char* new_argv[3] = {"add", "deny", };
437 new_argv[2] = (char*)malloc (opt_str_len + 1);
440 printf ("failed: malloc new_argv[2]\n");
444 strncpy (new_argv[2], opt_str , opt_str_len);
445 if(!xDbgEvlogRuleSet ((const int) new_argc,
446 (const char**) new_argv,
447 rule_log, &rule_size))
449 printf("%s\n", rule_log);
463 opt_str_len = strlen(opt_str);
472 fd = open (opt_str, O_RDONLY);
475 printf ("failed: open '%s'. (%s)\n", opt_str, strerror(errno));
479 len = read(fd, fs, sizeof(fs));
482 while (pfs - fs < len)
485 char *new_argv[3] = {"add", };
486 char policy[64] = {0, };
487 char rule[1024] = {0, };
490 if (pfs[0] == ' ' || pfs[0] == '\n')
495 for (i = 0 ; pfs[i] != ' ' ; i++)
498 new_argv[1] = policy;
499 pfs += (strlen(new_argv[1]) + 1);
501 memset(rule, 0, sizeof(rule));
502 for (i = 0 ; pfs[i] != '\n' ; i++)
507 pfs += (strlen(new_argv[2]) + 1);
510 if(!xDbgEvlogRuleSet ((const int) new_argc,
511 (const char**) new_argv,
512 rule_log, &rule_size))
514 printf("%s\n", rule_log);
531 opt_str_len = strlen(opt_str);
535 eo.detail_level = (atoi(optarg));
536 printf ("Detail Level: %d\n", eo.detail_level);
544 _printUsage( eo.command_name );
554 _xEvlogAnalyzePrint(&eo, rule_log, &rule_size);
559 int main(int argc, char** argv)
566 new_argv = (char**)malloc (new_argc * sizeof (char*));
569 printf ("failed: malloc new argv\n");
573 snprintf (temp, sizeof(temp), "%d", (int)getpid());
576 for (i = 0; i < argc; i++)
577 new_argv[i+1] = argv[i];
579 _checkOption(new_argc, new_argv);