add 'path' to the 'show' option
authorRoman Marchenko <r.marchenko@samsung.com>
Mon, 8 May 2017 13:31:58 +0000 (16:31 +0300)
committerSooChan Lim <sc1.lim@samsung.com>
Wed, 21 Jun 2017 01:56:13 +0000 (10:56 +0900)
Change-Id: Ie754d958b83e79f4c845ac06886adb88a984ad85
Signed-off-by: Roman Marchenko <r.marchenko@samsung.com>
Signed-off-by: SooChan Lim <sc1.lim@samsung.com>
src/wayland-tbm-client.c
src/wayland-tbm-int.h
src/wayland-tbm-monitor-server.c
src/wayland-tbm-util.c

index 9597c0d..26b6b83 100644 (file)
@@ -223,7 +223,14 @@ static const struct wl_tbm_listener wl_tbm_client_listener = {
 void _waylend_tbm_monitor_client_show(int argc, char *argv[],
                                      char *reply, int *len, struct wayland_tbm_client *tbm_client)
 {
-       tbm_bufmgr_debug_show(tbm_client->bufmgr);
+       struct wayland_tbm_monitor_path path = {0};
+
+       _wayland_tbm_util_show_path_parse(argv[2], 0, &path);
+
+       if (path.dlog)
+               tbm_bufmgr_debug_show(tbm_client->bufmgr);
+
+       /* always answer to the server */
        tbm_bufmgr_debug_tbm_info_get(tbm_client->bufmgr, reply, len);
 }
 
index 67f0909..f6eda58 100644 (file)
@@ -125,7 +125,8 @@ typedef enum {
 } WL_TBM_MONITOR_PROC_STATE;
 
 #define WL_TBM_MONITOR_REPLY_MSG_LEN   8192
-#define WL_TBM_MONITOR_ARGS_MAX        12
+#define WL_TBM_MONITOR_ARGS_MAX                        12
+#define WL_TBM_MONITOR_PATH_LEN                        256
 
 #define WL_TBM_MONITOR_SNPRINTF(p, len, fmt, ARG...)  \
        do { \
@@ -155,13 +156,18 @@ struct wayland_tbm_server {
        int queue_dump;
 };
 
-struct wayland_tbm_monitor_target
-{
+struct wayland_tbm_monitor_target {
        int server;
        int all;
        int pid;
 };
 
+struct wayland_tbm_monitor_path {
+       int console;
+       int dlog;
+       char file[WL_TBM_MONITOR_PATH_LEN];
+};
+
 /* internal functions */
 void
 _wayland_tbm_util_get_appname_brief(char *brief);
@@ -178,6 +184,9 @@ _waylend_tbm_util_proc_state_to_str(WL_TBM_MONITOR_PROC_STATE proc_state);
 int
 _waylend_tbm_util_target_parse(char * target_str, struct wayland_tbm_monitor_target *target);
 
+int
+_wayland_tbm_util_show_path_parse(char * path_str, char * cwd, struct wayland_tbm_monitor_path *path);
+
 WL_TBM_MONITOR_PROC_STATE
 _waylend_tbm_util_proc_state_parse(char * str);
 
index 341d47c..85d4423 100644 (file)
@@ -57,6 +57,7 @@ struct wayland_tbm_monitore_request {
        struct wl_client *wl_client;
        struct wl_resource *wl_resource;
        char app_name[256];
+       char cwd[WL_TBM_MONITOR_PATH_LEN];
 
        char options[256]; /* raw options */
        char arg_mem[256]; /* memory for argv */
@@ -69,6 +70,7 @@ struct wayland_tbm_monitore_request {
        int len;
 
        FILE *console;
+       struct wayland_tbm_monitor_path path;
 
        int wait_count;
 };
@@ -85,10 +87,22 @@ static void _wayland_tbm_monitor_server_usage(struct wayland_tbm_monitore_reques
 static void
 _wayland_tbm_monitor_request_printf(struct wayland_tbm_monitore_request *r, const char *fmt, ...)
 {
+       FILE *f;
        va_list arg;
 
        va_start(arg, fmt);
-       if (r->console)
+
+       if (r->path.file[0] != '\0') {
+               if ((f = fopen(r->path.file, "a+")) != NULL) {
+                       vfprintf(f, fmt, arg);
+                       fclose(f);
+               }
+               else
+                       _wayland_tbm_monitor_request_printf(r, "server: can't write show to file: '%s'\n",
+                                                                                               r->path.file);
+       }
+
+       if (r->path.console && r->console)
                vfprintf(r->console, fmt, arg);
        va_end(arg);
 }
@@ -129,6 +143,10 @@ _send_request_to_client_with_wait(struct wayland_tbm_monitore_request *r,
                wl_resource_for_each(c_res, &r->tbm_srv->tbm_monitor_list) {
                        if (c_res == r->wl_resource)
                                continue;
+#ifdef DEBUG_TRACE
+                       wl_client_get_credentials(wl_resource_get_client(c_res), &c_pid, NULL, NULL);
+                       WL_TBM_TRACE("send request:%d(%d) to client:%d option:'%s'\n", r->id, r->wait_count, c_pid,r->options);
+#endif
                        wl_tbm_monitor_send_request_to_client(c_res, r->options, request_id);
                        if (wait_answer)
                                r->wait_count++;
@@ -148,6 +166,7 @@ _send_request_to_client_with_wait(struct wayland_tbm_monitore_request *r,
 
                /* send option string to c_res */
                if (c_res) {
+                       WL_TBM_TRACE("send request:%d(%d) to client:%d option:'%s'\n", r->id, r->wait_count, c_pid,r->options);
                        wl_tbm_monitor_send_request_to_client(c_res, r->options, request_id);
                        if (wait_answer)
                                r->wait_count++;
@@ -192,15 +211,42 @@ static void
 _waylend_tbm_monitor_server_show(struct wayland_tbm_monitore_request *r)
 {
        struct wayland_tbm_monitor_target target = {0};
+       FILE * f = NULL;
+       char show_str[1024*4] = {0,};
+       int len = sizeof(show_str);
 
        if (!_waylend_tbm_util_target_parse(r->argv[1], &target)) {
                _wayland_tbm_monitor_request_printf(r, "'%s' - unknown target for -show\n", r->argv[1]);
                return;
        }
 
+       if (r->argv[2]) {
+               r->path.console = r->path.dlog = r->path.file[0] = 0;
+               if (!_wayland_tbm_util_show_path_parse(r->argv[2], r->cwd, &r->path)) {
+                       r->path.console = 1;
+                       _wayland_tbm_monitor_request_printf(r, "'%s' - unknown path for -show\n", r->argv[2]);
+                       return;
+               }
+       }
+
        if (target.server) {
-               tbm_bufmgr_debug_tbm_info_get(r->tbm_srv->bufmgr, r->reply, &r->len);
-               tbm_bufmgr_debug_show(r->tbm_srv->bufmgr);
+               tbm_bufmgr_debug_tbm_info_get(r->tbm_srv->bufmgr, show_str, &len);
+
+               if (r->path.console && r->console)
+                       fprintf(r->console, "%s", show_str);
+
+               if (r->path.file[0] != '\0') {
+                       if ((f = fopen(r->path.file, "a+")) != NULL) {
+                               fprintf(f, "%s", show_str);
+                               fclose(f);
+                       }
+                       else
+                               _wayland_tbm_monitor_request_printf(r, "server: can't write show to file: '%s'\n",
+                                                                                                       r->path.file);
+               }
+
+               if (r->path.dlog)
+                       tbm_bufmgr_debug_show(r->tbm_srv->bufmgr);
        }
 
        _send_request_to_client_with_wait(r, &target, 1);
@@ -296,13 +342,14 @@ static struct {
        {
                "show",
                _waylend_tbm_monitor_server_show,
-               "shows all tbm_bo information contained by (all | server | client=<pid>)"
+               "print all tbm_bo information for (all | server | client=<pid>) to path (console | dlog | <filepath>)"
                ,
-               "  client=<pid>        - the client with <pid> (use -list to get pid)\n"
+               "<target> [<path>]\n"
+               "\t\t""    client=<pid>        - the client with <pid> (use -list to get pid)\n"
                "\t\t""    server              - the server\n"
                "\t\t""    all                 - clients and server\n"
                ,
-               "client=<pid>; server; all"
+               "client=<pid> dlog,console; server tmp.txt; all"
        },
        {
                "trace",
@@ -375,6 +422,7 @@ _wayland_tbm_monitor_request_create(struct wl_client *client,
                                            struct wl_resource *resource, const char *options)
 {
        char *end = NULL;
+       char path[256];
        int i;
        int pid;
        static int id_count = 1;
@@ -395,6 +443,7 @@ _wayland_tbm_monitor_request_create(struct wl_client *client,
        r->len = WL_TBM_MONITOR_REPLY_MSG_LEN;
        r->tbm_srv = tbm_srv;
        r->id = id_count++;
+       r->path.console = 1;
        wl_list_init(&r->link);
 
        /* get pid */
@@ -403,14 +452,20 @@ _wayland_tbm_monitor_request_create(struct wl_client *client,
        _wayland_tbm_util_get_appname_brief(r->app_name);
 
        /* get access to the console of a wayland-tbm-monitor application */
-       char fd_name[32];
-       snprintf(fd_name, 32, "/proc/%d/fd/1", pid);
-       r->console = fopen(fd_name, "a");
+       snprintf(path, sizeof(path), "/proc/%d/fd/1", pid);
+       r->console = fopen(path, "a");
        if (!r->console) {
                _wayland_tbm_monitor_request_printf(r, "Can't get access to the console\n");
                return r;
        }
 
+       /* get cwd of the client */
+       snprintf(path, sizeof(path), "/proc/%d/cwd", pid);
+       if (readlink(path, r->cwd, sizeof(r->cwd)) < 0) {
+               _wayland_tbm_monitor_request_printf(r, "Can't get cwd\n");
+               return r;
+       }
+
        /* parse options */
        r->argv[r->argc] = strtok_r(r->arg_mem, " ", &end);
        while (r->argv[r->argc]) {
index f3448a3..cbb471b 100644 (file)
@@ -104,6 +104,37 @@ _waylend_tbm_util_proc_state_parse(char *str)
        return WL_TBM_MONITOR_PROC_STATE_UNKNOWN;
 }
 
+int
+_wayland_tbm_util_show_path_parse(char * path_str, char * cwd, struct wayland_tbm_monitor_path *path)
+{
+       char *end = path_str;
+       char *p = NULL;
+
+       if (!path_str)
+               return 0;
+
+       /* parse options */
+       while ((p = strtok_r(NULL, ",", &end)) != NULL) {
+               if (!strncmp(p, "dlog", 4))
+                       path->dlog = 1;
+               else if (!strncmp(p, "console", 7))
+                       path->console = 1;
+               else {
+                       if (p[0] == '/')
+                               snprintf(path->file, WL_TBM_MONITOR_PATH_LEN, "%s", p);
+                       else {
+                               if (cwd)
+                                       snprintf(path->file, WL_TBM_MONITOR_PATH_LEN, "%s/%s", cwd, p);
+                               else
+                                       snprintf(path->file, WL_TBM_MONITOR_PATH_LEN, "%s", p);
+                       }
+               }
+       }
+
+       return 1;
+}
+
+
 WL_TBM_MONITOR_PROC_STATE
 _waylend_tbm_util_trace(WL_TBM_MONITOR_PROC_STATE cmd, tbm_bufmgr bufmgr)
 {