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);
76 snprintf (fd_name, XDBG_PATH_MAX, "%s", path);
80 snprintf (fd_name, XDBG_PATH_MAX, "%s/%s", pMod->cwd, path);
82 snprintf (fd_name, XDBG_PATH_MAX, "%s", path);
86 log_fd = open (fd_name, O_CREAT|O_RDWR|O_APPEND, 0755);
89 XDBG_REPLY ("failed: open file(%s)\n", fd_name);
94 close (STDERR_FILENO);
95 dup2 (log_fd, STDERR_FILENO);
98 if (pMod->real_log_path)
99 free (pMod->real_log_path);
101 pMod->real_log_path = strdup (fd_name);
102 XDBG_RETURN_VAL_IF_FAIL (pMod->real_log_path != NULL, FALSE);
104 XDBG_REPLY ("log path: %s\n", pMod->real_log_path);
111 _CommandLogPath (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
115 XDBG_REPLY ("log path: %s\n", (pMod->real_log_path)?pMod->real_log_path:"stderr");
119 _CommandSetLogFile (pid, argv[2], reply, len, pMod);
123 _CommandSetLogLevel (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
131 xDbgLogEnumModules (MODE_WITH_STATUS, reply, len);
135 module_name = argv[2];
136 level = atoi (argv[3]);
138 if (level < 0 || level >= 5)
140 XDBG_REPLY ("Error : Not valid log level %d.\n", level);
144 ret = xDbgLogSetLevel (xDbgLogGetModule (module_name), level);
147 XDBG_REPLY ("Log level for module %s was successfully set to %d\n",
150 XDBG_REPLY ("Error : An error was occured during log level setting\n");
154 _CommandClientList (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
158 XDBG_REPLY ("Error : too few arguments\n");
162 xDbgModuleCList (pMod, reply, len);
166 _CommandPixmapList (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
170 XDBG_REPLY ("Error : too few arguments\n");
174 xDbgModulePList (pMod, reply, len);
178 _CommandEvlog (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
184 XDBG_REPLY ("Error : too few arguments\n");
190 xDbgModuleEvlogPrintEvents (pMod, on, argv[0], reply, len);
192 XDBG_REPLY ("Success\n");
196 _CommandSetEvlogRule (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
200 XDBG_REPLY ("Error : invalid number of arguments.\n");
204 xDbgModuleEvlogInfoSetRule (pMod, argc - 2, (const char**)&(argv[2]), reply, len);
208 _CommandSetEvlogPath (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
212 XDBG_REPLY ("evlog path: %s\n", (pMod->evlog_path)?pMod->evlog_path:"none");
216 if (!argv[2] || strlen (argv[2]) <= 0)
218 XDBG_REPLY ("invalid option\n");
222 if (pMod->evlog_path)
224 free (pMod->evlog_path);
225 pMod->evlog_path=NULL;
228 if (!xDbgModuleEvlogSetEvlogPath (pMod, pid, argv[2], reply, len))
230 XDBG_REPLY ("Error: evlog path(%s)\n", argv[2]);
234 pMod->evlog_path = strdup (argv[2]);
236 if (pMod->evlog_path[0] == '/')
237 XDBG_REPLY ("evlog path: %s\n", pMod->evlog_path);
239 XDBG_REPLY ("evlog path: %s/%s\n", pMod->cwd, pMod->evlog_path);
243 _CommandDrmEventPending (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
247 XDBG_REPLY ("Error : too few arguments\n");
251 xDbgModuleDrmEventPending (pMod, reply, len);
255 _CommandFpsDebug (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
261 XDBG_REPLY ("Error : too few arguments\n");
267 xDbgModuleFpsDebug (pMod, on, reply, len);
269 XDBG_REPLY ("Success\n");
275 const char *Description;
278 int (*DynamicUsage) (int, char*, int *);
279 const char *DetailedUsage;
281 void (*func) (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod);
285 "log_path", "to set log path", "[console/filepath]",
286 NULL, "[console/filepath]",
291 "log", "to set loglevel", "[MODULE] [0-4]",
292 (int(*)(int, char*, int*))xDbgLogEnumModules, "[DEBUG:0/TRACE:1/INFO:2/WARNING:3/ERROR:4]",
297 "clist", "to print clients", "",
303 "plist", "to print pixmap list", "",
309 "evlog", "to print x events", "[0-1]",
310 NULL, "[OFF:0/ON:1]",
315 "evlog_rule", "to set evlog rules", "[add/remove/print]",
316 NULL, "[add allow/deny rules / remove (index) / print]",
321 "evlog_path", "to set filepath of evlog", "[console/filepath]",
322 NULL, "[console/filepath]",
327 "drmevent_pending", "to print pending drmvents", "",
329 _CommandDrmEventPending
333 "fpsdebug", "to print fps", "[0-1]",
334 NULL, "[OFF:0/ON:1]",
339 static void _CommandPrintUsage (char *reply, int *len, const char * exec)
341 int option_cnt = sizeof (command_proc) / sizeof (command_proc[0]);
344 XDBG_REPLY ("Usage : %s [cmd] [options]\n", exec);
345 XDBG_REPLY (" ex)\n");
347 for (i=0; i<option_cnt; i++)
348 XDBG_REPLY (" %s %s %s\n", exec, command_proc[i].Cmd, command_proc[i].Options);
350 XDBG_REPLY (" options :\n");
352 for (i=0; i<option_cnt; i++)
354 if (command_proc[i].Cmd && command_proc[i].Description)
355 XDBG_REPLY (" %s (%s)\n", command_proc[i].Cmd, command_proc[i].Description);
357 XDBG_REPLY (" Cmd(%p) or Descriptiont(%p).\n", command_proc[i].Cmd, command_proc[i].Description);
359 if (command_proc[i].DynamicUsage)
362 int dynlen = sizeof (dyn);
363 command_proc[i].DynamicUsage (MODE_NAME_ONLY, dyn, &dynlen);
364 XDBG_REPLY (" [MODULE:%s]\n", dyn);
367 if (command_proc[i].DetailedUsage)
368 XDBG_REPLY (" %s\n", command_proc[i].DetailedUsage);
370 XDBG_REPLY (" DetailedUsage(%p).\n", command_proc[i].DetailedUsage);
375 xDbgModuleCommand (void *data, int argc, char **argv, char *reply, int *len)
377 XDbgModule *pMod = (XDbgModule*)data;
378 int nproc = sizeof (command_proc) / sizeof (command_proc[0]);
379 int i, pid, new_argc;
382 pid = atoi (argv[0]);
383 pMod->cwd = strdup (argv[1]);
386 new_argv = (char**)malloc (new_argc * sizeof (char*));
389 XDBG_REPLY ("Error : malloc new_argv\n");
393 for (i = 0; i < new_argc; i++)
394 new_argv[i] = argv[i+2];
398 _CommandPrintUsage (reply, len, new_argv[0]);
403 for (i = 0; i < nproc; i++)
405 if (!strcmp (new_argv[1], command_proc[i].Cmd) ||
406 (new_argv[1][0] == '-' && !strcmp (1 + new_argv[1], command_proc[i].Cmd)))
408 command_proc[i].func (pid, new_argc, new_argv, reply, len, pMod);
414 _CommandPrintUsage (reply, len, new_argv[0]);
420 xDbgModuleCommandInitLogPath (XDbgModule *pMod)
423 int len = sizeof (reply);
425 if (pMod->log_path && strlen (pMod->log_path) > 0)
427 char newname[XDBG_PATH_MAX];
428 char filename[XDBG_PATH_MAX];
430 char *p = NULL, *last = NULL;
433 snprintf (newname, XDBG_PATH_MAX, "%s", pMod->log_path);
435 for (i = 0; i < strlen (newname); i++)
442 snprintf (filename, XDBG_PATH_MAX, "%s", last + 1);
443 snprintf (last, XDBG_PATH_MAX - (last - newname), "/prev.%s", filename);
445 if (!stat (pMod->log_path, &status))
447 if (rename (pMod->log_path, newname))
449 XDBG_ERROR (MXDBG, "Failed: rename %s -> %s\n", pMod->log_path, newname);
456 _CommandSetLogFile (0, pMod->log_path, reply, &len, pMod);
458 _CommandSetLogFile (0, "console", reply, &len, pMod);