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_dump_module.h"
48 #include "xdbg_module.h"
49 #include "xdbg_module_clist.h"
50 #include "xdbg_module_plist.h"
51 #include "xdbg_module_evlog.h"
52 #include "xdbg_module_drmevent.h"
53 #include "xdbg_module_fpsdebug.h"
54 #include "xdbg_module_command.h"
57 _CommandDLog (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
63 XDBG_REPLY ("DLog level: %d\n", xDbgGetLogEnableDlog() ? 1 : 0);
69 xDbgLogEnableDlog (on);
71 XDBG_REPLY ("Success\n");
75 _CommandSetLogFile (int pid, char *path, char *reply, int *len, XDbgModule *pMod)
77 static int old_stderr = -1;
78 char fd_name[XDBG_PATH_MAX];
82 if (!path || strlen (path) <= 0)
84 XDBG_REPLY ("failed: logpath is invalid\n");
89 old_stderr = dup (STDERR_FILENO);
91 if (!strcmp (path, "console"))
92 snprintf (fd_name, XDBG_PATH_MAX, "/proc/%d/fd/1", pid);
96 snprintf (fd_name, XDBG_PATH_MAX, "%s", path);
100 snprintf (fd_name, XDBG_PATH_MAX, "%s/%s", pMod->cwd, path);
102 snprintf (fd_name, XDBG_PATH_MAX, "%s", path);
106 log_fl = fopen (fd_name, "a");
109 XDBG_REPLY ("failed: open file(%s)\n", fd_name);
114 close (STDERR_FILENO);
116 setvbuf (log_fl, NULL, _IOLBF, 512);
117 log_fd = fileno (log_fl);
119 dup2 (log_fd, STDERR_FILENO);
123 free (pMod->log_path);
125 pMod->log_path = strdup (fd_name);
126 XDBG_RETURN_VAL_IF_FAIL (pMod->log_path != NULL, FALSE);
128 XDBG_REPLY ("log path: %s\n", pMod->log_path);
135 _CommandLogPath (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
139 XDBG_REPLY ("log path: %s\n", (pMod->log_path)?pMod->log_path:"stderr");
143 _CommandSetLogFile (pid, argv[2], reply, len, pMod);
147 _CommandSetLogLevel (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
155 xDbgLogEnumModules (MODE_WITH_STATUS, reply, len);
159 module_name = argv[2];
160 level = atoi (argv[3]);
162 if (level < 0 || level >= 5)
164 XDBG_REPLY ("Error : Not valid log level %d.\n", level);
168 ret = xDbgLogSetLevel (xDbgLogGetModule (module_name), level);
171 XDBG_REPLY ("Log level for module %s was successfully set to %d\n",
174 XDBG_REPLY ("Error : An error was occured during log level setting\n");
178 _CommandClientList (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
182 XDBG_REPLY ("Error : too few arguments\n");
186 xDbgModuleCList (pMod, reply, len);
190 _CommandPixmapList (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
194 XDBG_REPLY ("Error : too few arguments\n");
198 xDbgModulePList (pMod, reply, len);
202 _CommandEvlog (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
205 extern Bool xev_trace_on;
209 XDBG_REPLY ("Evlog level: %d\n", xev_trace_on ? 1 : 0);
215 xDbgModuleEvlogPrintEvents (pMod, on, argv[0], reply, len);
217 XDBG_REPLY ("Success\n");
221 _CommandEvlogDetail (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
224 extern int xev_trace_detail_level;
228 XDBG_REPLY ("Detail Level: %d\n", xev_trace_detail_level);
232 level = atoi(argv[2]);
234 xDbgModuleEvlogDetail (pMod, level, reply, len);
235 XDBG_REPLY ("Success\n");
239 _CommandSetEvlogRule (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
243 XDBG_REPLY ("Error : invalid number of arguments.\n");
247 if (argc > 2 && argv[2] && !strcmp (argv[2], "file"))
249 if (argc < 4 || !argv[3])
251 XDBG_REPLY ("Error : no evlog rule file.\n");
255 if (pMod->evlog_rule_path)
256 free (pMod->evlog_rule_path);
258 pMod->evlog_rule_path = strdup (argv[3]);
261 xDbgModuleEvlogInfoSetRule (pMod, argc - 2, (const char**)&(argv[2]), reply, len);
265 _CommandSetEvlogPath (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
269 XDBG_REPLY ("evlog path: %s\n", (pMod->evlog_path)?pMod->evlog_path:"none");
273 if (!argv[2] || strlen (argv[2]) <= 0)
275 XDBG_REPLY ("invalid option\n");
279 if (pMod->evlog_path)
281 free (pMod->evlog_path);
282 pMod->evlog_path=NULL;
285 if (!xDbgModuleEvlogSetEvlogPath (pMod, pid, argv[2], reply, len))
287 XDBG_REPLY ("Error: evlog path(%s)\n", argv[2]);
291 pMod->evlog_path = strdup (argv[2]);
292 if (!pMod->evlog_path) {
293 XDBG_REPLY ("Failed to allocate memory to evlog_path\n");
297 if (!strcmp (pMod->evlog_path, "console"))
298 XDBG_REPLY ("/proc/%d/fd/1", pid);
299 else if (pMod->evlog_path[0] == '/')
300 XDBG_REPLY ("evlog path: %s\n", pMod->evlog_path);
302 XDBG_REPLY ("evlog path: %s/%s\n", pMod->cwd, pMod->evlog_path);
306 _CommandDrmEventPending (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
310 XDBG_REPLY ("Error : too few arguments\n");
314 xDbgModuleDrmEventPending (pMod, reply, len);
318 _CommandFpsDebug (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
324 XDBG_REPLY ("Error : too few arguments\n");
330 xDbgModuleFpsDebug (pMod, on, reply, len);
332 XDBG_REPLY ("Success\n");
336 _printDumpOption (char *reply, int *len)
338 if (xDbgDumpGetType ())
339 XDBG_REPLY ("type:%s ", xDbgDumpGetType ());
340 if (xDbgDumpGetCount ())
341 XDBG_REPLY ("count:%s ", xDbgDumpGetCount ());
342 if (xDbgDumpGetFile ())
343 XDBG_REPLY ("file:%s ", xDbgDumpGetFile ());
344 if (xDbgDumpGetCrop ())
345 XDBG_REPLY ("crop:%s ", xDbgDumpGetCrop ());
351 _CommandDump (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod)
358 reply = _printDumpOption (reply, len);
363 for (i = 0; i < argc; i++)
369 if (!strcmp (c, "-type"))
372 if (!xDbgDumpSetType (c))
374 XDBG_REPLY ("fail: set '%s' (already running)\n", c);
378 else if (!strcmp (c, "-count"))
381 if (!xDbgDumpSetCount (c))
383 XDBG_REPLY ("fail: set '%s' (already running)\n", c);
387 else if (!strcmp (c, "-file"))
390 if (!xDbgDumpSetFile (c))
392 XDBG_REPLY ("fail: set '%s' (already running)\n", c);
396 else if (!strcmp (c, "-crop"))
399 if (!xDbgDumpSetCrop (c))
401 XDBG_REPLY ("fail: set '%s' (already running)\n", c);
408 if (!strcmp (c, "on"))
411 XDBG_REPLY ("'%s'", c);
412 reply = _printDumpOption (reply, len);
414 else if (!strcmp (c, "off"))
418 XDBG_REPLY ("'%s' ", c);
420 else if (!strcmp (c, "clear"))
423 XDBG_REPLY ("'%s' ", c);
427 XDBG_REPLY ("unknown option '%s'\n", c);
431 XDBG_REPLY ("success\n");
437 const char *Description;
440 int (*DynamicUsage) (int, char*, int *);
441 const char *DetailedUsage;
443 void (*func) (int pid, int argc, char **argv, char *reply, int *len, XDbgModule *pMod);
447 "dlog", "to enable dlog", "[0-1]",
448 NULL, "[OFF:0/ON:1]",
453 "log_path", "to set log path", "[console/filepath]",
454 NULL, "[console/filepath]",
459 "log", "to set loglevel", "[MODULE] [0-4]",
460 (int(*)(int, char*, int*))xDbgLogEnumModules, "[DEBUG:0/TRACE:1/INFO:2/WARNING:3/ERROR:4]",
465 "clist", "to print clients", "",
471 "plist", "to print pixmap list", "",
477 "evlog", "to print x events", "[0-1]",
478 NULL, "[OFF:0/ON:1]",
483 "evlog_detail", "to set printing detail log level", "[0-2]",
484 NULL, "[Primary logs:0/ More detail logs:1/ Supplementary Reply logs:2]",
489 "evlog_rule", "to set evlog rules", "[add/remove/file/print/help]",
490 NULL, "[add allow/deny rules / remove (index) / file(file_name) / print / help]",
495 "evlog_path", "to set filepath of evlog", "[console/filepath]",
496 NULL, "[console/filepath]",
501 "drmevent_pending", "to print pending drmvents", "",
503 _CommandDrmEventPending
507 "fpsdebug", "to print fps", "[0-1]",
508 NULL, "[OFF:0/ON:1]",
513 "dump", "to dump buffers", "[on,off,clear]",
514 NULL, "[on,off,clear] -type [ui,drawable,fb,video] -count [n] -file [bmp,raw]",
519 static void _CommandPrintUsage (char *reply, int *len, const char * exec)
521 int option_cnt = sizeof (command_proc) / sizeof (command_proc[0]);
524 XDBG_REPLY ("Usage : %s [cmd] [options]\n", exec);
525 XDBG_REPLY (" ex)\n");
527 for (i=0; i<option_cnt; i++)
528 XDBG_REPLY (" %s %s %s\n", exec, command_proc[i].Cmd, command_proc[i].Options);
530 XDBG_REPLY (" options :\n");
532 for (i=0; i<option_cnt; i++)
534 if (command_proc[i].Cmd && command_proc[i].Description)
535 XDBG_REPLY (" %s (%s)\n", command_proc[i].Cmd, command_proc[i].Description);
537 XDBG_REPLY (" Cmd(%p) or Descriptiont(%p).\n", command_proc[i].Cmd, command_proc[i].Description);
539 if (command_proc[i].DynamicUsage)
542 int dynlen = sizeof (dyn);
543 command_proc[i].DynamicUsage (MODE_NAME_ONLY, dyn, &dynlen);
544 XDBG_REPLY (" [MODULE:%s]\n", dyn);
547 if (command_proc[i].DetailedUsage)
548 XDBG_REPLY (" %s\n", command_proc[i].DetailedUsage);
550 XDBG_REPLY (" DetailedUsage(%p).\n", command_proc[i].DetailedUsage);
555 xDbgModuleCommand (void *data, int argc, char **argv, char *reply, int *len)
557 XDbgModule *pMod = (XDbgModule*)data;
558 int nproc = sizeof (command_proc) / sizeof (command_proc[0]);
559 int i, pid, new_argc;
562 pid = atoi (argv[0]);
563 pMod->cwd = strdup (argv[1]);
566 new_argv = (char**)malloc (new_argc * sizeof (char*));
569 XDBG_REPLY ("Error : malloc new_argv\n");
573 for (i = 0; i < new_argc; i++)
574 new_argv[i] = argv[i+2];
578 _CommandPrintUsage (reply, len, new_argv[0]);
583 for (i = 0; i < nproc; i++)
585 if (!strcmp (new_argv[1], command_proc[i].Cmd) ||
586 (new_argv[1][0] == '-' && !strcmp (1 + new_argv[1], command_proc[i].Cmd)))
588 command_proc[i].func (pid, new_argc, new_argv, reply, len, pMod);
594 _CommandPrintUsage (reply, len, new_argv[0]);
600 xDbgModuleCommandInitLogPath (XDbgModule *pMod, char *log_path)
603 int len = sizeof (reply);
605 if (log_path && strlen (log_path) > 0)
607 char newname[XDBG_PATH_MAX];
608 char filename[XDBG_PATH_MAX];
610 char *p = NULL, *last = NULL;
613 snprintf (newname, XDBG_PATH_MAX, "%s", log_path);
615 for (i = 0; i < strlen (newname); i++)
622 snprintf (filename, XDBG_PATH_MAX, "%s", last + 1);
623 snprintf (last, XDBG_PATH_MAX - (last - newname), "/prev.%s", filename);
625 if (!stat (log_path, &status))
627 if (rename (log_path, newname))
629 XDBG_ERROR (MXDBG, "Failed: rename %s -> %s\n", log_path, newname);
634 _CommandSetLogFile (0, log_path, reply, &len, pMod);
637 _CommandSetLogFile (0, "console", reply, &len, pMod);
643 xDbgModuleCommandInitEvlogRulePath (XDbgModule *pMod, char *evlog_rule_path)
646 int len = sizeof (reply);
650 if (!evlog_rule_path || strlen (evlog_rule_path) <= 0)
654 argv[1] = "evlog_rule";
656 argv[3] = evlog_rule_path;
658 _CommandSetEvlogRule (0, argc, argv, reply, &len, pMod);
660 xDbgModuleEvlogPrintEvents (pMod, TRUE, "", reply, &len);