Git init
[framework/system/sys-assert.git] / lockupinfo / lockupinfo.c
1
2 /*
3  *  LOCKUPINFO
4  *
5  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6  *
7  * Contact: Jeesun Kim <iamjs.kim@samsung.com>
8  * 
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
27
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <time.h>
34
35 #include "_util_log.h"
36
37 #define VERINFO_PATH "/etc/info.ini"
38 #define DEBUG_DIR "/opt/share/hidden_storage/SLP_debug"
39 #define PATH_LEN 256
40 #define BUF_SIZE 256
41 #define PERMS 0755
42 #define FILE_PERMS (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
43
44 /* lockupinfo launch key combination: 
45  * 1. volume side key up
46  * 2. volume side key down
47  * 3. home key
48  * 4. home key
49  * 5. volume side key up
50  * 6. volume side key down
51  * 7. volume side key up
52  * 8. home key
53  */
54
55 /* WARNING : formatted string buffer is limited to 1024 byte */
56 int fprintf_fd(int fd, const char *fmt, ...)
57 {
58         int n;
59         char buff[1024];
60         va_list args;
61         va_start(args, fmt);
62         n = vsnprintf(buff, 1024 - 1, fmt, args);
63         write(fd, buff, n);
64         va_end(args);
65         return n;
66 }
67
68 char *fgets_fd(char *s, int n, int fd)
69 {
70         char c;
71         register char *cs;
72         int num = 0;
73
74         cs = s;
75         while (--n > 0 && (num = read(fd, &c, 1) > 0)) {
76                 if ((*cs++ = c) == '\n')
77                         break;
78         }
79         *cs = '\0';
80         return (num == 0 && cs == s) ? NULL : s;
81 }
82
83 int main()
84 {
85         char lbuf[BUF_SIZE];
86         char name[32] = {0, };
87         char size[32] = {0, };
88         char unit[32] = {0, };
89         int csfd;
90         int tmpfd;
91         int meminfo;
92         int verinfo;
93
94         fprintf(stderr, "[lockupinfo] executed\n");
95
96         /* get time  */
97         time_t cur_time;
98         struct tm ctime;
99         cur_time = time(NULL);
100         localtime_r(&cur_time, &ctime);
101
102         char nbuf[256] = {0, }; /* name buf */
103         char dbuf[256] = {0, }; /* dir buf */
104         char ibuf[256] = {0, }; /* info buf */
105         char cbuf[256] = {0, }; /* cmd buf */
106         char tbuf[256] = {0, }; /* temp buf */
107         int ret;
108
109 //      snprintf(nbuf, sizeof(nbuf), "debuginfo_%02d%02d%02d%02d%02d%02d",
110 //                      ctime->tm_year, ctime->tm_mon, ctime->tm_mday,
111 //                      ctime->tm_hour, ctime->tm_min, ctime->tm_sec);
112
113         strftime(tbuf, sizeof(tbuf), "%Y%m%d%H%M%S", &ctime);
114         snprintf(nbuf, sizeof(nbuf), "debuginfo_%s", tbuf);
115
116         /* make debug directory if absent */
117         ret = access(DEBUG_DIR, F_OK);
118         if (ret < 0) {
119                 if (ret = mkdir(DEBUG_DIR, PERMS) < 0) {
120                         _E("Failed to mkdir %s(errno:%d)\n", DEBUG_DIR, ret);
121                         return -1;
122                 }
123         }
124
125         snprintf(dbuf, sizeof(dbuf), "%s/%s", DEBUG_DIR, nbuf);
126         ret = mkdir(dbuf, PERMS);
127         retvm_if(ret < 0, -1, "Failed to mkdir %s(errno:%d)\n", dbuf, ret);
128
129         snprintf(dbuf, sizeof(dbuf), "%s/%s/%s", DEBUG_DIR, nbuf, "files");
130         ret = mkdir(dbuf, PERMS);
131         retvm_if(ret < 0, -1, "Failed to mkdir %s(errno:%d)\n", dbuf, ret);
132         _D("lockupinfo dir [%s]\n", dbuf);
133
134         snprintf(ibuf, sizeof(ibuf), "%s/%s.info", dbuf, nbuf);
135
136         /* create .info file */
137         csfd = creat(ibuf, FILE_PERMS);
138         retvm_if(csfd < 0, -1, "Failed to creat %s\n", ibuf);
139
140         /* print version info */
141         fprintf_fd(csfd, "******************************\n");
142         fprintf_fd(csfd, "s/w version\n");
143         fprintf_fd(csfd, "******************************\n");
144
145         verinfo = open(VERINFO_PATH, O_RDONLY);
146         if (verinfo < 0) {
147                 _E("Failed to open %s\n", VERINFO_PATH);
148
149         } else {
150                 while (fgets_fd(lbuf, BUF_SIZE, verinfo)) {
151                         if (strncmp("Major=", lbuf, 6) == 0) {
152                                 fprintf_fd(csfd, "%s", lbuf);
153
154                         } else if (strncmp("Minor=", lbuf, 6) == 0) {
155                                 fprintf_fd(csfd, "%s", lbuf);
156
157                         } else if (strncmp("Date=", lbuf, 5) == 0) {
158                                 fprintf_fd(csfd, "%s", lbuf);
159
160                         } else if (strncmp("Time=", lbuf, 5) == 0) {
161                                 fprintf_fd(csfd, "%s", lbuf);
162                                 break;
163                         }
164                 }
165                 close(verinfo);
166
167         }
168
169         /* print mem info */
170         meminfo = open("/proc/meminfo", O_RDONLY);
171         if (meminfo < 0) {
172                 _E("Failed to open %s\n", "/proc/meminfo");
173
174         } else {
175                 fprintf_fd(csfd, "*******************************\n");
176                 fprintf_fd(csfd, "Mem information\n");
177                 fprintf_fd(csfd, "*******************************\n");
178
179                 while (fgets_fd(lbuf, BUF_SIZE, meminfo) != NULL) {
180                         sscanf(lbuf, "%s %s %s", name, size, unit);
181
182                         if (strcmp("MemTotal:", name) == 0) {
183                                 fprintf_fd(csfd, "%s\t%10.0d %s\n", name, atoi(size), unit);
184
185                         } else if (strcmp("MemFree:", name) == 0) {
186                                 fprintf_fd(csfd, "%s\t%10.0d %s\n", name, atoi(size), unit);
187
188                         } else if (strcmp("Buffers:", name) == 0) {
189                                 fprintf_fd(csfd, "%s\t%10.0d %s\n", name, atoi(size), unit);
190
191                         } else if (strcmp("Cached:", name) == 0) {
192                                 fprintf_fd(csfd, "%s\t%10.0d %s\n", name, atoi(size), unit);
193
194                         }
195                 }
196                 close(meminfo);
197         }
198
199         /* ps info */
200         snprintf(cbuf, sizeof(cbuf), "%s > %s",
201                         "ps ax -o pid,tid,ppid,f,stat,pcpu,pmem,wchan,command", "/tmp/ps_tmp.log");
202         system(cbuf);
203
204         tmpfd = open("/tmp/ps_tmp.log", O_RDONLY);
205         if (tmpfd < 0) {
206                 _E("Failed to open %s\n", "/tmp/ps_tmp.log");
207         } else {
208                 fprintf_fd(csfd, "*******************************\n");
209                 fprintf_fd(csfd, "PS information\n");
210                 fprintf_fd(csfd, "*******************************\n");
211
212                 while (fgets_fd(lbuf, BUF_SIZE, tmpfd) != NULL) {
213                         fprintf_fd(csfd, "%s", lbuf);
214                 }
215                 close(tmpfd);
216                 unlink("/tmp/ps_tmp.log");
217         }
218
219         /* ping info */
220         snprintf(cbuf, sizeof(cbuf), "%s 2> %s",
221                         "xinfo -p", "/tmp/ping_tmp.log");
222         system(cbuf);
223
224         if ((tmpfd = open("/tmp/ping_tmp.log", O_RDONLY)) < 0) {
225                 fprintf(stderr, "[lockupinfo]can't open %s\n",
226                         "/tmp/ping_tmp.log");
227         } else {
228                 fprintf_fd(csfd, "*******************************\n");
229                 fprintf_fd(csfd, "ping test for all top level windows\n");
230                 fprintf_fd(csfd, "*******************************\n");
231
232                 while (fgets_fd(lbuf, BUF_SIZE, tmpfd)) {
233                         fprintf_fd(csfd, "%s", lbuf);
234                 }
235                 close(tmpfd);
236                 unlink("/tmp/ping_tmp.log");
237         }
238
239         /* dump topvwins */
240         snprintf(cbuf, sizeof(cbuf), "%s %s",
241                         "/usr/bin/xinfo -xwd_topvwins", dbuf);
242         system(cbuf);
243
244         /* close lockupinfoXXXX.info */
245         close(csfd);
246
247         snprintf(cbuf, sizeof(cbuf), "%s %s/%s.info %s/%s/%s.cs",
248                         "cp", dbuf, nbuf, DEBUG_DIR, nbuf, nbuf);
249         system(cbuf);
250
251
252
253         /* make dlog file */
254         snprintf(tbuf, sizeof(tbuf), "%s/%s", dbuf, "main.log");
255         snprintf(cbuf, sizeof(cbuf), "dlogutil -v time -d -f %s *:v", tbuf);
256         system(cbuf);
257
258         snprintf(tbuf, sizeof(tbuf), "%s/radio.log", dbuf);
259         fprintf(stderr, "radio log path = %s\n", tbuf);
260         snprintf(cbuf, sizeof(cbuf), "dlogutil -v time -b radio -d -f %s *:v", tbuf);
261         system(cbuf);
262
263
264         /* dump window manager info 
265          * this code came from window team
266          */
267         fprintf(stderr, "[lockupinfo]dump window manager info\n");
268         snprintf(cbuf, sizeof(cbuf), "%s %s/%s",
269                         "/usr/bin/e_comp_util -l DUMP_INFO -f", dbuf, "e_comp.log");
270         system(cbuf);
271
272         snprintf(cbuf, sizeof(cbuf), "%s %s/%s",
273                         "/usr/bin/border_win_info -p ALL -f", dbuf, "e_illume2.log");
274         system(cbuf);
275
276         system("/usr/bin/keygrab_status 2");
277         snprintf(cbuf, sizeof(cbuf), "%s %s",
278                         "cp -af /opt/var/log/keygrab_status.txt", cbuf);
279         system(cbuf);
280
281         snprintf(cbuf, sizeof(cbuf), "%s %s/%s",
282                         "/usr/bin/screenshot bmp", cbuf, "slp_screenshot.bmp");
283         system(cbuf);
284
285         snprintf(cbuf, sizeof(cbuf), "%s > %s/%s",
286                         "xinfo -topvwins 2", dbuf, "xinfo_topvwins.txt");
287         system(cbuf);
288
289         /* end from window team */
290
291         /*  copy prev xorg log */
292         snprintf(cbuf, sizeof(cbuf), "%s %s",
293                         "cp /opt/var/log/prev.Xorg.*", dbuf);
294         system(cbuf);
295
296         /*  copy xorg log */
297         snprintf(cbuf, sizeof(cbuf), "%s %s",
298                         "cp /opt/var/log/Xorg.*", dbuf);
299         system(cbuf);
300
301         /* dump a list of current tasks and their information */
302         /* requsted by window team */
303         /*snprintf(cbuf, sizeof(cbuf), "echo t > /proc/sysrq-trigger");
304          *system(cbuf);
305          */
306
307         /* copy syslog messages */
308         snprintf(cbuf, sizeof(cbuf), "%s %s",
309                         "cp /opt/var/log/messages*", dbuf);
310         system(cbuf);
311
312         /* copy nand log */
313         snprintf(cbuf, sizeof(cbuf), "%s %s",
314                         "cp /opt/var/log/nandlog_*", dbuf);
315         system(cbuf);
316
317         /* launch bluescreen */
318         pid_t bs_pid;
319
320         if ((bs_pid = fork()) < 0) {
321                 fprintf(stderr, "[lockupinfo] fork_error\n");
322
323         } else if (bs_pid == 0) {
324                 if (execl
325                     ("/usr/bin/blue-screen", "blue-screen", ibuf,
326                      "LOCKUPINFO", (char *)0) < 0) {
327                         fprintf(stderr, "[lockupinfo] exec_error\n");
328                 }
329                 _exit(1);       /*/ shouldn't get here */
330         }
331
332         fprintf(stderr, "[lockupinfo] exit\n");
333         return 0;
334
335 }
336
337