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 _CommandDLog (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
62 XDBG_REPLY ("DLog level: %d\n", xDbgGetLogEnableDlog() ? 1 : 0);
68 xDbgLogEnableDlog (on);
70 XDBG_REPLY ("Success\n");
74 _CommandSetLogFile (int pid, char *path, char *reply, int *len, XDbgModule *pMod)
76 static int old_stderr = -1;
77 char fd_name[XDBG_PATH_MAX];
80 if (!path || strlen (path) <= 0)
82 XDBG_REPLY ("failed: logpath is invalid\n");
87 old_stderr = dup (STDERR_FILENO);
89 if (!strcmp (path, "console"))
90 snprintf (fd_name, XDBG_PATH_MAX, "/proc/%d/fd/1", pid);
94 snprintf (fd_name, XDBG_PATH_MAX, "%s", path);
98 snprintf (fd_name, XDBG_PATH_MAX, "%s/%s", pMod->cwd, path);
100 snprintf (fd_name, XDBG_PATH_MAX, "%s", path);
104 log_fd = open (fd_name, O_CREAT|O_RDWR|O_APPEND, 0755);
107 XDBG_REPLY ("failed: open file(%s)\n", fd_name);
112 close (STDERR_FILENO);
113 dup2 (log_fd, STDERR_FILENO);
116 if (pMod->real_log_path)
117 free (pMod->real_log_path);
119 pMod->real_log_path = strdup (fd_name);
120 XDBG_RETURN_VAL_IF_FAIL (pMod->real_log_path != NULL, FALSE);
122 XDBG_REPLY ("log path: %s\n", pMod->real_log_path);
129 _CommandLogPath (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
133 XDBG_REPLY ("log path: %s\n", (pMod->real_log_path)?pMod->real_log_path:"stderr");
137 _CommandSetLogFile (pid, argv[2], reply, len, pMod);
141 _CommandSetLogLevel (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
149 xDbgLogEnumModules (MODE_WITH_STATUS, reply, len);
153 module_name = argv[2];
154 level = atoi (argv[3]);
156 if (level < 0 || level >= 5)
158 XDBG_REPLY ("Error : Not valid log level %d.\n", level);
162 ret = xDbgLogSetLevel (xDbgLogGetModule (module_name), level);
165 XDBG_REPLY ("Log level for module %s was successfully set to %d\n",
168 XDBG_REPLY ("Error : An error was occured during log level setting\n");
172 _CommandClientList (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
176 XDBG_REPLY ("Error : too few arguments\n");
180 xDbgModuleCList (pMod, reply, len);
184 _CommandPixmapList (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
188 XDBG_REPLY ("Error : too few arguments\n");
192 xDbgModulePList (pMod, reply, len);
196 _CommandEvlog (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
199 extern Bool xev_trace_on;
203 XDBG_REPLY ("Evlog level: %d\n", xev_trace_on ? 1 : 0);
209 xDbgModuleEvlogPrintEvents (pMod, on, argv[0], reply, len);
211 XDBG_REPLY ("Success\n");
215 _CommandEvlogDetail (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
218 extern int xev_trace_detail_level;
222 XDBG_REPLY ("Detail Level: %d\n", xev_trace_detail_level);
226 level = atoi(argv[2]);
228 xDbgModuleEvlogDetail (pMod, level, reply, len);
229 XDBG_REPLY ("Success\n");
233 _CommandSetEvlogRule (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
237 XDBG_REPLY ("Error : invalid number of arguments.\n");
241 xDbgModuleEvlogInfoSetRule (pMod, argc - 2, (const char**)&(argv[2]), reply, len);
245 _CommandSetEvlogPath (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
249 XDBG_REPLY ("evlog path: %s\n", (pMod->evlog_path)?pMod->evlog_path:"none");
253 if (!argv[2] || strlen (argv[2]) <= 0)
255 XDBG_REPLY ("invalid option\n");
259 if (pMod->evlog_path)
261 free (pMod->evlog_path);
262 pMod->evlog_path=NULL;
265 if (!xDbgModuleEvlogSetEvlogPath (pMod, pid, argv[2], reply, len))
267 XDBG_REPLY ("Error: evlog path(%s)\n", argv[2]);
271 pMod->evlog_path = strdup (argv[2]);
273 if (!strcmp (pMod->evlog_path, "console"))
274 XDBG_REPLY ("/proc/%d/fd/1", pid);
275 else if (pMod->evlog_path[0] == '/')
276 XDBG_REPLY ("evlog path: %s\n", pMod->evlog_path);
278 XDBG_REPLY ("evlog path: %s/%s\n", pMod->cwd, pMod->evlog_path);
282 _CommandDrmEventPending (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
286 XDBG_REPLY ("Error : too few arguments\n");
290 xDbgModuleDrmEventPending (pMod, reply, len);
294 _CommandFpsDebug (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
300 XDBG_REPLY ("Error : too few arguments\n");
306 xDbgModuleFpsDebug (pMod, on, reply, len);
308 XDBG_REPLY ("Success\n");
314 const char *Description;
317 int (*DynamicUsage) (int, char*, int *);
318 const char *DetailedUsage;
320 void (*func) (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod);
324 "dlog", "to enable dlog", "[0-1]",
325 NULL, "[OFF:0/ON:1]",
330 "log_path", "to set log path", "[console/filepath]",
331 NULL, "[console/filepath]",
336 "log", "to set loglevel", "[MODULE] [0-4]",
337 (int(*)(int, char*, int*))xDbgLogEnumModules, "[DEBUG:0/TRACE:1/INFO:2/WARNING:3/ERROR:4]",
342 "clist", "to print clients", "",
348 "plist", "to print pixmap list", "",
354 "evlog", "to print x events", "[0-1]",
355 NULL, "[OFF:0/ON:1]",
360 "evlog_detail", "to set printing detail log level", "[0-2]",
361 NULL, "[Primary logs:0/ More detail logs:1/ Supplementary Reply logs:2]",
366 "evlog_rule", "to set evlog rules", "[add/remove/file/print/help]",
367 NULL, "[add allow/deny rules / remove (index) / file(file_name) / print / help]",
372 "evlog_path", "to set filepath of evlog", "[console/filepath]",
373 NULL, "[console/filepath]",
378 "drmevent_pending", "to print pending drmvents", "",
380 _CommandDrmEventPending
384 "fpsdebug", "to print fps", "[0-1]",
385 NULL, "[OFF:0/ON:1]",
390 static void _CommandPrintUsage (char *reply, int *len, const char * exec)
392 int option_cnt = sizeof (command_proc) / sizeof (command_proc[0]);
395 XDBG_REPLY ("Usage : %s [cmd] [options]\n", exec);
396 XDBG_REPLY (" ex)\n");
398 for (i=0; i<option_cnt; i++)
399 XDBG_REPLY (" %s %s %s\n", exec, command_proc[i].Cmd, command_proc[i].Options);
401 XDBG_REPLY (" options :\n");
403 for (i=0; i<option_cnt; i++)
405 if (command_proc[i].Cmd && command_proc[i].Description)
406 XDBG_REPLY (" %s (%s)\n", command_proc[i].Cmd, command_proc[i].Description);
408 XDBG_REPLY (" Cmd(%p) or Descriptiont(%p).\n", command_proc[i].Cmd, command_proc[i].Description);
410 if (command_proc[i].DynamicUsage)
413 int dynlen = sizeof (dyn);
414 command_proc[i].DynamicUsage (MODE_NAME_ONLY, dyn, &dynlen);
415 XDBG_REPLY (" [MODULE:%s]\n", dyn);
418 if (command_proc[i].DetailedUsage)
419 XDBG_REPLY (" %s\n", command_proc[i].DetailedUsage);
421 XDBG_REPLY (" DetailedUsage(%p).\n", command_proc[i].DetailedUsage);
426 xDbgModuleCommand (void *data, int argc, char **argv, char *reply, int *len)
428 XDbgModule *pMod = (XDbgModule*)data;
429 int nproc = sizeof (command_proc) / sizeof (command_proc[0]);
430 int i, pid, new_argc;
433 pid = atoi (argv[0]);
434 pMod->cwd = strdup (argv[1]);
437 new_argv = (char**)malloc (new_argc * sizeof (char*));
440 XDBG_REPLY ("Error : malloc new_argv\n");
444 for (i = 0; i < new_argc; i++)
445 new_argv[i] = argv[i+2];
449 _CommandPrintUsage (reply, len, new_argv[0]);
454 for (i = 0; i < nproc; i++)
456 if (!strcmp (new_argv[1], command_proc[i].Cmd) ||
457 (new_argv[1][0] == '-' && !strcmp (1 + new_argv[1], command_proc[i].Cmd)))
459 command_proc[i].func (pid, new_argc, new_argv, reply, len, pMod);
465 _CommandPrintUsage (reply, len, new_argv[0]);
471 xDbgModuleCommandInitLogPath (XDbgModule *pMod)
474 int len = sizeof (reply);
476 if (pMod->log_path && strlen (pMod->log_path) > 0)
478 char newname[XDBG_PATH_MAX];
479 char filename[XDBG_PATH_MAX];
481 char *p = NULL, *last = NULL;
484 snprintf (newname, XDBG_PATH_MAX, "%s", pMod->log_path);
486 for (i = 0; i < strlen (newname); i++)
493 snprintf (filename, XDBG_PATH_MAX, "%s", last + 1);
494 snprintf (last, XDBG_PATH_MAX - (last - newname), "/prev.%s", filename);
496 if (!stat (pMod->log_path, &status))
498 if (rename (pMod->log_path, newname))
500 XDBG_ERROR (MXDBG, "Failed: rename %s -> %s\n", pMod->log_path, newname);
507 _CommandSetLogFile (0, pMod->log_path, reply, &len, pMod);
509 _CommandSetLogFile (0, "console", reply, &len, pMod);