4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 * @file dump_systemstate.c
21 * @brief dump system states.
36 #include "shared/util.h"
37 #include "shared/log.h"
39 #define FILE_PERM (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
40 static struct dump_item {
44 {"==== Binary version " , "/etc/info.ini"},
45 {"==== Tizen version " , "/etc/tizen-release"},
46 {"==== Kernel version " , "/proc/version"},
47 {"==== Boot arguments " , "/proc/cmdline"},
48 {"==== CPU & system architecture " , "/proc/cpuinfo"},
49 {"==== System uptime " , "/proc/uptime"},
50 {"==== System statistics " , "/proc/stat"},
51 {"==== System memory usage " , "/proc/meminfo"},
52 {"==== Device major numbers " , "/proc/devices"},
53 {"==== System disk I/O satistics " , "/proc/diskstats"},
58 fprintf(stderr, "usage: dump_systemstate [-k] [-d] [-f file]\n"
59 " -f: write to file (instead of stdout)\n"
60 " -k: dump kernel messages (only root)\n"
61 " -d: dump dlog messages\n"
62 " -j: dump journal log messages\n"
66 /* get disk used percentage */
67 static int get_disk_used_percent(const char *path)
69 struct statfs lstatfs;
75 if (statfs(path, &lstatfs) < 0)
77 percent = (((lstatfs.f_blocks - lstatfs.f_bfree) * 1000) / (lstatfs.f_blocks)) + 9;
82 int main(int argc, char *argv[])
84 int c, ret, i, is_root, dpercent;
85 const char *arg_file = NULL;
87 bool arg_dlog = false;
88 bool arg_dmesg = false;
89 bool arg_journal = false;
95 while ((c = getopt(argc, argv, "hf:kdj")) != -1) {
118 cur_time = time(NULL);
119 gmtime_r(&cur_time, &gm_tm);
120 localtime_r(&cur_time, &loc_tm);
121 is_root = !(geteuid());
123 /* open output file */
124 if (arg_file == NULL) {
125 out_fd = STDOUT_FILENO;
127 out_fd = open(arg_file, O_WRONLY | O_TRUNC | O_CREAT, FILE_PERM);
129 perror("couldn't open output file");
134 /* print timestamp */
135 strftime(timestr, sizeof(timestr),
136 "%Y-%m-%d %H:%M:%S%z", &loc_tm);
137 fprintf_fd(out_fd, "dump_systemstate: %s\n", timestr);
139 for (i = 0; i < ARRAY_SIZE(dump_item); i++) {
141 fprintf_fd(out_fd, "\n%s(%s)\n",
142 dump_item[i].title, dump_item[i].path);
143 ret = dump_file_write_fd((char *)dump_item[i].path, out_fd);
147 fprintf_fd(out_fd, "\n");
149 fprintf_fd(out_fd, "\n==== System disk space usage (/bin/df -h)\n");
150 ret = run_command_write_fd("/bin/df -h", out_fd);
154 dpercent = get_disk_used_percent("/opt");
156 fprintf_fd(out_fd, "\n==== System disk space usage detail - %d\% (/bin/du -ah /opt)\n", dpercent);
157 ret = run_command_write_fd("/usr/bin/du -ah /opt --exclude=/opt/usr", out_fd);
161 fprintf_fd(out_fd, "\n==== System timezone (ls -al /opt/etc/localtime)\n");
162 ret = run_command_write_fd("ls -al /opt/etc/localtime", out_fd);
166 fprintf_fd(out_fd, "\n==== System summary (/usr/bin/top -bcH -n 1)\n");
167 ret = run_command_write_fd("COLUMNS=200 /usr/bin/top -bcH -n 1", out_fd);
171 fprintf_fd(out_fd, "\n==== Current processes (/bin/ps auxfw)\n");
172 ret = run_command_write_fd("/bin/ps auxfw", out_fd);
176 fprintf_fd(out_fd, "\n==== System memory statistics (/usr/bin/memps -v)\n");
177 ret = run_command_write_fd("/usr/bin/memps -v", out_fd);
182 fprintf_fd(out_fd, "\n==== System configuration (/usr/bin/vconftool get memory, db, file)\n");
183 ret = run_command_write_fd("/usr/bin/vconftool get memory/ -r", out_fd);
187 ret = run_command_write_fd("/usr/bin/vconftool get db/ -r", out_fd);
191 ret = run_command_write_fd("/usr/bin/vconftool get file/ -r", out_fd);
196 if (arg_dmesg && is_root) {
197 fprintf_fd(out_fd, "\n==== Kernel messages (TZ=UTC /bin/dmesg -T)\n");
198 ret = run_command_write_fd("TZ=UTC /bin/dmesg -T", out_fd);
204 fprintf_fd(out_fd, "\n==== Log messages\n");
205 ret = run_command_write_fd("/usr/bin/dlogutil -d -v threadtime -u 16384", out_fd);
211 fprintf_fd(out_fd, "\n==== Journal messages\n");
212 ret = run_command_write_fd("/usr/bin/journalctl -b -n 1024", out_fd);