add supplementary log massage of each major extension
[platform/adaptation/xf86-module-xdbg.git] / bin / xevlog_analyze / xevlog_analyze.c
1 /**************************************************************************
2
3 xevlog-analyze
4
5 Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
6
7 Contact: Boram Park <boram1288.park@samsung.com>
8          Sangjin LEE <lsj119@samsung.com>
9
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:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
20 of the Software.
21
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.
29
30 **************************************************************************/
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <time.h>
38 #include <sys/ipc.h>
39 #include <sys/shm.h>
40
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/ioctl.h>
44 #include <fcntl.h>
45 #include <unistd.h>
46 #include <sys/mman.h>
47
48 #include <X11/Xlib.h>
49 #include <X11/Xw32defs.h>
50 #include <xdbg_types.h>
51 #include <xdbg_evlog.h>
52
53 typedef struct _EvlogOption
54 {
55     int pid;
56     Bool help_opt;
57     char command_name[PATH_MAX+1];
58     char path_name[PATH_MAX+1];
59 } EvlogOption;
60
61 #define BUF_SIZE 256
62
63
64 static void
65 _printUsage(char* name)
66 {
67     fprintf(stderr, "Usage: %s [OPTION]...\n", name);
68     fprintf(stderr, "\n");
69     fprintf(stderr, " Options:\n");
70     fprintf(stderr, "       -f [File_Name]  File to save information\n");
71     fprintf(stderr, "\n");
72     fprintf(stderr, "       -h              Command Usage information\n");
73     fprintf(stderr, "\n");
74 }
75
76 static void _xEvlogAnalyzePrint (EvlogOption *eo)
77 {
78     int fd = -1, cfd = -1;
79     int total, read_len;
80     int evlog_len;
81     char fd_name[256];
82     EvlogInfo evinfo={0,};
83
84     if (eo->help_opt)
85     {
86         _printUsage(eo->command_name);
87         return;
88     }
89
90     if (strlen (eo->path_name) <= 0)
91     {
92         printf ("failed: no evlog path\n");
93         return;
94     }
95
96     fd = open (eo->path_name, O_RDONLY);
97     if (fd < 0)
98     {
99         printf ("failed: open '%s'. (%s)\n", eo->path_name, strerror(errno));
100         return;
101     }
102
103     snprintf (fd_name, sizeof (fd_name), "/proc/%d/fd/1", eo->pid);
104     cfd = open (fd_name, O_RDWR);
105
106     if (cfd < 0)
107     {
108         printf ("failed: open consol '%s'. (%s)\n", fd_name, strerror(errno));
109         goto print_done;
110     }
111
112     while ((read_len = read (fd, &evlog_len, sizeof (int))) == sizeof (int))
113     {
114         char log[1024];
115         int size = sizeof (log);
116
117         memset (&evinfo, 0, sizeof (EvlogInfo));
118
119         total = read_len;
120
121         read_len = read (fd, &evinfo.time, sizeof (CARD32));
122         GOTO_IF_FAIL (read_len == sizeof (CARD32), print_done);
123         total += read_len;
124
125         read_len = read (fd, &evinfo.type, sizeof (EvlogType));
126         GOTO_IF_FAIL (read_len == sizeof (EvlogType), print_done);
127         GOTO_IF_FAIL (evinfo.type >= EVENT && evinfo.type <= FLUSH, print_done);
128         total += read_len;
129
130         read_len = read (fd, &evinfo.mask, sizeof (int));
131         GOTO_IF_FAIL (read_len == sizeof (int), print_done);
132         total += read_len;
133
134         if (evinfo.mask & EVLOG_MASK_CLIENT)
135         {
136             read_len = read (fd, &evinfo.client, sizeof (EvlogClient));
137             GOTO_IF_FAIL (read_len == sizeof (EvlogClient), print_done);
138             total += read_len;
139         }
140
141         if (evinfo.mask & EVLOG_MASK_REQUEST)
142         {
143             read_len = read (fd, &evinfo.req, sizeof(EvlogRequest));
144             GOTO_IF_FAIL (read_len == sizeof(EvlogRequest), print_done);
145             total += read_len;
146
147             evinfo.req.ptr = malloc (evinfo.req.length * 4);
148             GOTO_IF_FAIL (evinfo.req.ptr != NULL, print_done);
149
150             read_len = read (fd, evinfo.req.ptr, (evinfo.req.length * 4));
151             GOTO_IF_FAIL (read_len == (evinfo.req.length * 4), print_done);
152             total += read_len;
153         }
154
155         if (evinfo.mask & EVLOG_MASK_EVENT)
156         {
157             read_len = read (fd, &evinfo.evt, sizeof(EvlogEvent));
158             GOTO_IF_FAIL (read_len == sizeof(EvlogEvent), print_done);
159             total += read_len;
160
161             evinfo.evt.ptr = malloc (sizeof(xEvent));
162             GOTO_IF_FAIL (evinfo.evt.ptr != NULL, print_done);
163
164             read_len = read (fd, evinfo.evt.ptr, sizeof (xEvent));
165             GOTO_IF_FAIL (read_len == sizeof(xEvent), print_done);
166             total += read_len;
167         }
168         GOTO_IF_FAIL (evlog_len == total, print_done);
169
170         xDbgEvlogFillLog(&evinfo, log, &size);
171         printf ("%s", log);
172
173         if (evinfo.req.ptr)
174         {
175             free (evinfo.req.ptr);
176             evinfo.req.ptr = NULL;
177         }
178
179         if (evinfo.evt.ptr)
180         {
181             free (evinfo.evt.ptr);
182             evinfo.evt.ptr = NULL;
183         }
184     }
185
186
187 print_done:
188     if (evinfo.req.ptr)
189         free (evinfo.req.ptr);
190
191     if (evinfo.evt.ptr)
192         free (evinfo.evt.ptr);
193
194     if (cfd >= 0)
195         close (cfd);
196
197     if (fd >= 0)
198         close (fd);
199 }
200
201 static void
202 _checkOption(int argc, char** argv)
203 {
204     int c;
205     int opt_str_len = 0;
206     char* opt_str = NULL;
207     EvlogOption eo = {0,};
208
209     eo.pid = atoi (argv[0]);
210     eo.help_opt = TRUE;
211     strncpy(eo.command_name, argv[1], PATH_MAX);
212
213     if (argc <= 2)
214     {
215         _printUsage( eo.command_name );
216         return;
217     }
218
219     while ((c = getopt(argc, argv, "f:h")) != EOF)
220     {
221         switch (c)
222         {
223
224             case 'f':
225                 opt_str = optarg;
226                 opt_str_len = strlen(opt_str);
227
228                 if(opt_str_len > 0)
229                 {
230                     eo.help_opt = FALSE;
231                     strncpy (eo.path_name, opt_str, opt_str_len);
232                 }
233                 break;
234
235             case 'h':
236                 _printUsage( eo.command_name );
237                 return;
238                 break;
239
240             default:
241                 break;
242         }
243     }
244
245     _xEvlogAnalyzePrint(&eo);
246 }
247
248
249 int main(int argc, char** argv)
250 {
251
252     Display *dpy;
253     char **new_argv;
254     int new_argc, i;
255     char temp[128];
256
257     dpy = XOpenDisplay (NULL);
258     if (!dpy)
259     {
260         fprintf (stderr, "failed: open display\n");
261         exit (-1);
262     }
263
264     new_argc = argc + 1;
265     new_argv = (char**)malloc (new_argc * sizeof (char*));
266     if (!new_argv)
267     {
268         fprintf (stderr, "failed: malloc new argv\n");
269         exit (-1);
270     }
271
272     snprintf (temp, sizeof(temp), "%d", (int)getpid());
273     new_argv[0] = temp;
274
275     for (i = 0; i < argc; i++)
276         new_argv[i+1] = argv[i];
277
278     _checkOption(new_argc, new_argv);
279
280     free (new_argv);
281
282     XCloseDisplay (dpy);
283
284     return 0;
285 }