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(" -a [Add Rule] Setting Rule of 'Add' policy to find information you want\n");
75 printf(" -n [Deny Rule] Setting Rule of 'Deny' policy to find information you want\n");
77 printf(" [RULE] : C Language-style boolean expression syntax. [VARIABLE] [COMPAROTOR] [VALUE]\n");
78 printf(" [VARIABLE] : type / major / minor / command / cmd / pid\n");
79 printf(" [COMPARATOR] : & / && / and / | / || / or / = / == / != / > / >= / < / <=\n");
80 printf(" [VALUE] : string / number \n");
83 printf(" xevlog_analyze -a \"(type=request) && (major == X11 and (minor = SendEvent or minor = ReceiveEvent))\"\n");
84 printf(" xevlog_analyze -n cmd!=ls\n");
86 printf(" -d [OFF:0/ON:1] To Print detail log of extensions\n");
88 printf(" -h Usage of xevlog_anlayze\n");
93 static void _xEvlogAnalyzePrint (EvlogOption *eo, char* reply, int* len)
95 int fd = -1, cfd = -1;
99 EvlogInfo evinfo={0,};
102 if (!strlen(eo->path_name))
104 printf ("failed: no evlog path\n");
108 fd = open (eo->path_name, O_RDONLY);
111 printf ("failed: open '%s'. (%s)\n", eo->path_name, strerror(errno));
115 snprintf (fd_name, sizeof (fd_name), "/proc/%d/fd/1", eo->pid);
116 cfd = open (fd_name, O_RDWR);
120 printf ("failed: open consol '%s'. (%s)\n", fd_name, strerror(errno));
124 while ((read_len = read (fd, &evlog_len, sizeof (int))) == sizeof (int))
127 int size = sizeof (log);
130 memset (&evinfo, 0, sizeof (EvlogInfo));
135 extern ExtensionInfo Evlog_extensions[];
136 extern int Extensions_size;
138 char* new_argv[3] = {"add", "allow", "all"};
142 xDbgEvlogRuleSet ((const int) new_argc, (const char**) new_argv, reply, len);
144 printf("%s\n", reply);
146 read_len = read (fd, &Extensions_size, sizeof (int));
147 GOTO_IF_FAIL (read_len == sizeof (int), print_done);
150 for (i = 0 ; i < Extensions_size ; i++)
152 read_len = read (fd, &Evlog_extensions[i].opcode, sizeof (int));
153 GOTO_IF_FAIL (read_len == sizeof (int), print_done);
156 read_len = read (fd, &Evlog_extensions[i].evt_base, sizeof (int));
157 GOTO_IF_FAIL (read_len == sizeof (int), print_done);
160 read_len = read (fd, &Evlog_extensions[i].err_base, sizeof (int));
161 GOTO_IF_FAIL (read_len == sizeof (int), print_done);
165 if (!xDbgEvlogGetExtensionEntry ())
167 printf ("failed: get extentions\n");
174 read_len = read (fd, &evinfo.time, sizeof (CARD32));
175 GOTO_IF_FAIL (read_len == sizeof (CARD32), print_done);
178 read_len = read (fd, &evinfo.type, sizeof (EvlogType));
179 GOTO_IF_FAIL (read_len == sizeof (EvlogType), print_done);
180 GOTO_IF_FAIL (evinfo.type >= EVENT && evinfo.type <= FLUSH, print_done);
183 read_len = read (fd, &evinfo.mask, sizeof (int));
184 GOTO_IF_FAIL (read_len == sizeof (int), print_done);
187 if (evinfo.mask & EVLOG_MASK_CLIENT)
189 read_len = read (fd, &evinfo.client, sizeof (EvlogClient));
190 GOTO_IF_FAIL (read_len == sizeof (EvlogClient), print_done);
194 if (evinfo.mask & EVLOG_MASK_REQUEST)
196 read_len = read (fd, &evinfo.req, sizeof(EvlogRequest));
197 GOTO_IF_FAIL (read_len == sizeof(EvlogRequest), print_done);
200 evinfo.req.ptr = malloc (evinfo.req.length * 4);
201 GOTO_IF_FAIL (evinfo.req.ptr != NULL, print_done);
203 read_len = read (fd, evinfo.req.ptr, (evinfo.req.length * 4));
204 GOTO_IF_FAIL (read_len == (evinfo.req.length * 4), print_done);
208 if (evinfo.mask & EVLOG_MASK_EVENT)
210 read_len = read (fd, &evinfo.evt, sizeof(EvlogEvent));
211 GOTO_IF_FAIL (read_len == sizeof(EvlogEvent), print_done);
214 evinfo.evt.ptr = malloc (evinfo.evt.size);
215 GOTO_IF_FAIL (evinfo.evt.ptr != NULL, print_done);
217 WARNING_IF_FAIL (evinfo.evt.size > 0);
219 read_len = read (fd, evinfo.evt.ptr, evinfo.evt.size);
220 GOTO_IF_FAIL (read_len == evinfo.evt.size, print_done);
224 if (evinfo.mask & EVLOG_MASK_ATOM)
226 EvlogAtomTable *table;
228 read_len = read (fd, &evinfo.evatom.size, sizeof (int));
229 GOTO_IF_FAIL (read_len == sizeof(int), print_done);
232 for (i = 0 ; i < evinfo.evatom.size ; i++)
234 table = malloc (sizeof(EvlogAtomTable));
235 GOTO_IF_FAIL (table != NULL, print_done);
237 if (!evinfo.evatom.init)
239 xorg_list_init(&evinfo.evatom.list);
240 evinfo.evatom.init = 1;
242 read_len = read (fd, table, sizeof (EvlogAtomTable));
243 GOTO_IF_FAIL (read_len == sizeof(EvlogAtomTable), print_done);
246 xorg_list_add(&table->link, &evinfo.evatom.list);
250 if (evinfo.mask & EVLOG_MASK_REGION)
252 EvlogRegionTable *table;
254 read_len = read (fd, &evinfo.evregion.size, sizeof (int));
255 GOTO_IF_FAIL (read_len == sizeof(int), print_done);
258 for (i = 0 ; i < evinfo.evregion.size ; i++)
260 table = malloc (sizeof(EvlogRegionTable));
261 GOTO_IF_FAIL (table != NULL, print_done);
263 if (!evinfo.evregion.init)
265 xorg_list_init(&evinfo.evregion.list);
266 evinfo.evregion.init = 1;
269 read_len = read (fd, table, sizeof (EvlogRegionTable));
270 GOTO_IF_FAIL (read_len == sizeof(EvlogRegionTable), print_done);
273 xorg_list_add(&table->link, &evinfo.evregion.list);
277 GOTO_IF_FAIL (evlog_len == total, print_done);
279 if (xDbgEvlogRuleValidate (&evinfo))
281 xDbgEvlogFillLog(&evinfo, eo->detail, log, &size);
287 free (evinfo.req.ptr);
288 evinfo.req.ptr = NULL;
293 free (evinfo.evt.ptr);
294 evinfo.evt.ptr = NULL;
297 xDbgDistroyAtomList(&evinfo);
298 xDbgDistroyRegionList(&evinfo);
304 free (evinfo.req.ptr);
307 free (evinfo.evt.ptr);
309 xDbgDistroyAtomList(&evinfo);
310 xDbgDistroyRegionList(&evinfo);
321 _checkOption(int argc, char** argv)
325 char* opt_str = NULL;
326 EvlogOption eo = {0,};
327 char rule_log[LOG_SIZE];
328 int rule_size = sizeof (rule_log);
330 eo.pid = atoi (argv[0]);
333 strncpy(eo.command_name, argv[1], PATH_MAX);
337 _printUsage( eo.command_name );
341 while ((c = getopt(argc, argv, "f:a:n:d:h")) != EOF)
348 opt_str_len = strlen(opt_str);
352 strncpy (eo.path_name, opt_str, PATH_MAX);
360 opt_str_len = strlen(opt_str);
364 char* new_argv[3] = {"add", "allow", };
366 new_argv[2] = (char*)malloc (opt_str_len + 1);
369 printf ("failed: malloc new_argv[2]\n");
373 strncpy (new_argv[2], opt_str , opt_str_len);
374 if(!xDbgEvlogRuleSet ((const int) new_argc,
375 (const char**) new_argv,
376 rule_log, &rule_size))
378 printf("%s\n", rule_log);
392 opt_str_len = strlen(opt_str);
397 char* new_argv[3] = {"add", "deny", };
399 new_argv[2] = (char*)malloc (opt_str_len + 1);
402 printf ("failed: malloc new_argv[2]\n");
406 strncpy (new_argv[2], opt_str , opt_str_len);
407 if(!xDbgEvlogRuleSet ((const int) new_argc,
408 (const char**) new_argv,
409 rule_log, &rule_size))
411 printf("%s\n", rule_log);
425 opt_str_len = strlen(opt_str);
429 eo.detail = (atoi(optarg))?TRUE:FALSE;
430 printf ("detail %s\n", (eo.detail)?"ON":"OFF");
438 _printUsage( eo.command_name );
448 _xEvlogAnalyzePrint(&eo, rule_log, &rule_size);
453 int main(int argc, char** argv)
460 new_argv = (char**)malloc (new_argc * sizeof (char*));
463 printf ("failed: malloc new argv\n");
467 snprintf (temp, sizeof(temp), "%d", (int)getpid());
470 for (i = 0; i < argc; i++)
471 new_argv[i+1] = argv[i];
473 _checkOption(new_argc, new_argv);