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 **************************************************************************/
36 #include <sys/types.h>
42 #include <windowstr.h>
45 #include "xdbg_types.h"
47 #include "xdbg_module.h"
48 #include "xdbg_module_clist.h"
49 #include "xdbg_module_plist.h"
50 #include "xdbg_module_evlog.h"
51 #include "xdbg_module_drmevent.h"
52 #include "xdbg_module_fpsdebug.h"
53 #include "xdbg_module_command.h"
56 _CommandSetLogFile (int pid, char *path, char *reply, int *len, XDbgModule *pMod)
58 static int old_stderr = -1;
59 char fd_name[XDBG_PATH_MAX];
62 if (!path || strlen (path) <= 0)
64 XDBG_REPLY ("failed: logpath is invalid\n");
69 old_stderr = dup (STDERR_FILENO);
71 if (!strcmp (path, "console"))
72 snprintf (fd_name, XDBG_PATH_MAX, "/proc/%d/fd/1", pid);
75 char *temp[3] = {"/", "./", "../"};
79 for (i = 0; i < sizeof (temp) / sizeof (char*); i++)
80 if (path == strstr (path, temp[i]))
89 snprintf (fd_name, XDBG_PATH_MAX, "%s", path);
93 if (getcwd (cwd, sizeof (cwd)))
94 snprintf (fd_name, XDBG_PATH_MAX, "%s/%s", cwd, path);
96 snprintf (fd_name, XDBG_PATH_MAX, "%s", path);
101 XDBG_REPLY ("failed: invalid option(%s)\n", path);
106 log_fd = open (fd_name, O_CREAT|O_RDWR|O_APPEND, 0755);
109 XDBG_REPLY ("failed: open file(%s)\n", fd_name);
114 close (STDERR_FILENO);
115 dup2 (log_fd, STDERR_FILENO);
118 if (pMod->real_log_path)
119 free (pMod->real_log_path);
121 pMod->real_log_path = strdup (fd_name);
122 XDBG_RETURN_VAL_IF_FAIL (pMod->real_log_path != NULL, FALSE);
124 XDBG_REPLY ("log path: %s\n", pMod->real_log_path);
131 _CommandLogPath (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
135 XDBG_REPLY ("log path: %s\n", (pMod->real_log_path)?pMod->real_log_path:"stderr");
139 _CommandSetLogFile (pid, argv[2], reply, len, pMod);
143 _CommandSetLogLevel (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
151 xDbgLogEnumModules (MODE_WITH_STATUS, reply, len);
155 module_name = argv[2];
156 level = atoi (argv[3]);
158 if (level < 0 || level >= 5)
160 XDBG_REPLY ("Error : Not valid log level %d.\n", level);
164 ret = xDbgLogSetLevel (xDbgLogGetModule (module_name), level);
167 XDBG_REPLY ("Log level for module %s was successfully set to %d\n",
170 XDBG_REPLY ("Error : An error was occured during log level setting\n");
174 _CommandClientList (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
178 XDBG_REPLY ("Error : too few arguments\n");
182 xDbgModuleCList (pMod, reply, len);
186 _CommandPixmapList (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
190 XDBG_REPLY ("Error : too few arguments\n");
194 xDbgModulePList (pMod, reply, len);
198 _CommandEvlog (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
204 XDBG_REPLY ("Error : too few arguments\n");
210 xDbgModuleEvlogPrintEvents (pMod, on, argv[0], reply, len);
212 XDBG_REPLY ("Success\n");
216 _CommandSetEvlogRule (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
220 XDBG_REPLY ("Error : invalid number of arguments.\n");
224 xDbgModuleEvlogInfoSetRule (pMod, argc - 2, (const char**)&(argv[2]), reply, len);
228 _CommandSetEvlogPath (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
232 XDBG_REPLY ("evlog path: %s\n", (pMod->evlog_path)?pMod->evlog_path:"none");
236 if (!argv[2] || strlen (argv[2]) <= 0)
238 XDBG_REPLY ("invalid option\n");
242 if (pMod->evlog_path)
244 free (pMod->evlog_path);
245 pMod->evlog_path=NULL;
248 if (!xDbgModuleEvlogSetEvlogPath (pMod, pid, argv[2], reply, len))
250 XDBG_REPLY ("Error: evlog path(%s)\n", argv[2]);
254 pMod->evlog_path = strdup (argv[2]);
256 XDBG_REPLY ("evlog path: %s\n", pMod->evlog_path);
260 _CommandDrmEventPending (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
264 XDBG_REPLY ("Error : too few arguments\n");
268 xDbgModuleDrmEventPending (pMod, reply, len);
272 _CommandFpsDebug (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
278 XDBG_REPLY ("Error : too few arguments\n");
284 xDbgModuleFpsDebug (pMod, on, reply, len);
286 XDBG_REPLY ("Success\n");
292 const char *Description;
295 int (*DynamicUsage) (int, char*, int *);
296 const char *DetailedUsage;
298 void (*func) (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod);
302 "log_path", "to set log path", "[console/filepath]",
303 NULL, "[console/filepath]",
308 "log", "to set loglevel", "[MODULE] [0-4]",
309 (int(*)(int, char*, int*))xDbgLogEnumModules, "[DEBUG:0/TRACE:1/INFO:2/WARNING:3/ERROR:4]",
314 "clist", "to print clients", "",
320 "plist", "to print pixmap list", "",
326 "evlog", "to print x events", "[0-1]",
327 NULL, "[OFF:0/ON:1]",
332 "evlog_rule", "to set evlog rules", "[add/remove/print]",
333 NULL, "[add allow/deny rules / remove (index) / print]",
338 "evlog_path", "to set filepath of evlog", "[console/filepath]",
339 NULL, "[console/filepath]",
344 "drmevent_pending", "to print pending drmvents", "",
346 _CommandDrmEventPending
350 "fpsdebug", "to print fps", "[0-1]",
351 NULL, "[OFF:0/ON:1]",
356 static void _CommandPrintUsage (char *reply, int *len, const char * exec)
358 int option_cnt = sizeof (command_proc) / sizeof (command_proc[0]);
361 XDBG_REPLY ("Usage : %s [cmd] [options]\n", exec);
362 XDBG_REPLY (" ex)\n");
364 for (i=0; i<option_cnt; i++)
365 XDBG_REPLY (" %s %s %s\n", exec, command_proc[i].Cmd, command_proc[i].Options);
367 XDBG_REPLY (" options :\n");
369 for (i=0; i<option_cnt; i++)
371 if (command_proc[i].Cmd && command_proc[i].Description)
372 XDBG_REPLY (" %s (%s)\n", command_proc[i].Cmd, command_proc[i].Description);
374 XDBG_REPLY (" Cmd(%p) or Descriptiont(%p).\n", command_proc[i].Cmd, command_proc[i].Description);
376 if (command_proc[i].DynamicUsage)
379 int dynlen = sizeof (dyn);
380 command_proc[i].DynamicUsage (MODE_NAME_ONLY, dyn, &dynlen);
381 XDBG_REPLY (" [MODULE:%s]\n", dyn);
384 if (command_proc[i].DetailedUsage)
385 XDBG_REPLY (" %s\n", command_proc[i].DetailedUsage);
387 XDBG_REPLY (" DetailedUsage(%p).\n", command_proc[i].DetailedUsage);
392 xDbgModuleCommand (void *data, int argc, char **argv, char *reply, int *len)
394 XDbgModule *pMod = (XDbgModule*)data;
395 int nproc = sizeof (command_proc) / sizeof (command_proc[0]);
396 int i, pid, new_argc;
399 pid = atoi (argv[0]);
402 new_argv = (char**)malloc (new_argc * sizeof (char*));
405 XDBG_REPLY ("Error : malloc new_argv\n");
409 for (i = 0; i < new_argc; i++)
410 new_argv[i] = argv[i+1];
414 _CommandPrintUsage (reply, len, new_argv[0]);
419 for (i = 0; i < nproc; i++)
421 if (!strcmp (new_argv[1], command_proc[i].Cmd) ||
422 (new_argv[1][0] == '-' && !strcmp (1 + new_argv[1], command_proc[i].Cmd)))
424 command_proc[i].func (pid, new_argc, new_argv, reply, len, pMod);
430 _CommandPrintUsage (reply, len, new_argv[0]);
436 xDbgModuleCommandInitLogPath (XDbgModule *pMod)
439 int len = sizeof (reply);
441 if (pMod->log_path && strlen (pMod->log_path) > 0)
443 char newname[XDBG_PATH_MAX];
444 char filename[XDBG_PATH_MAX];
446 char *p = NULL, *last = NULL;
449 snprintf (newname, XDBG_PATH_MAX, "%s", pMod->log_path);
451 for (i = 0; i < strlen (newname); i++)
458 snprintf (filename, XDBG_PATH_MAX, "%s", last + 1);
459 snprintf (last, XDBG_PATH_MAX - (last - newname), "/prev.%s", filename);
461 if (!stat (pMod->log_path, &status))
463 if (rename (pMod->log_path, newname))
465 XDBG_ERROR (MXDBG, "Failed: rename %s -> %s\n", pMod->log_path, newname);
472 _CommandSetLogFile (0, pMod->log_path, reply, &len, pMod);
474 _CommandSetLogFile (0, "console", reply, &len, pMod);