tizen 2.4 release
[adaptation/xorg/driver/xserver-xorg-module-xdbg.git] / module / xdbg_module_clist.c
1 /**************************************************************************
2
3 xdbg
4
5 Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
6
7 Contact: Boram Park <boram1288.park@samsung.com>
8          Sangjin LEE <lsj119@samsung.com>
9
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:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
20 of the Software.
21
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.
29
30 **************************************************************************/
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39
40 #include "xdbg.h"
41 #include "xdbg_types.h"
42 #include "xdbg_module.h"
43 #include "xdbg_module_clist.h"
44
45 static ModuleTraceClientOption*
46 _findTraceClientOption (XDbgModule *pMod, char *client_name)
47 {
48     int i = 0;
49
50     for (i = 0 ; i < pMod->trace_cnt ; i++)
51     {
52         ModuleTraceClientOption *option = &pMod->trace_options[i];
53         if (!strcmp(client_name, option->name))
54         {
55             return option;
56         }
57     }
58     return NULL;
59 }
60
61 void
62 xDbgModuleCList (XDbgModule *pMod, char *reply, int *len)
63 {
64     int i;
65
66     XDBG_REPLY ("%6s   %6s   %s   %s\n", "INDEX", "PID", "BLOCKED", "NAME");
67
68     for (i = 1; i < currentMaxClients && (0 < *len); i++)
69     {
70         ClientPtr pClient = clients[i];
71         ModuleClientInfo *info;
72
73         if (!pClient)
74             continue;
75
76         info = GetClientInfo (pClient);
77         if (!info)
78             continue;
79
80         XDBG_REPLY ("%6d   %6d   %4d      %9s\n",
81                         info->index, info->pid, pClient->ignoreCount, info->command);
82     }
83 }
84
85
86 void
87 xDbgModuleClistTraceInit (XDbgModule *pMod)
88 {
89     int i = 0;
90
91     for (i = 1 ; i < currentMaxClients ; i++)
92     {
93         ClientPtr pClient = clients[i];
94         ModuleClientInfo *info;
95
96         if (!pClient)
97             continue;
98
99         info = GetClientInfo (pClient);
100         xDbgModuleClistTraceAdd (pMod, info);
101     }
102 }
103
104 void
105 xDbgModuleClistTraceAdd (XDbgModule *pMod, ModuleClientInfo *info)
106 {
107     char *client_name = strrchr (info->command, '/');
108     ModuleTraceClient *trace_client = NULL;
109     ModuleTraceClient *client = NULL, *tmp = NULL;
110     ModuleTraceClientOption *option = NULL;
111
112     XDBG_DEBUG (MXDBG, "[TRACE_CLIENT] AddClist Trace command %s\n", info->command);
113
114     if (client_name)
115         client_name = client_name+1;
116     else
117         client_name = info->command;
118
119     if (!(option = _findTraceClientOption (pMod, client_name)))
120         return;
121
122     xorg_list_for_each_entry_safe(client, tmp, &pMod->trace_list, link) {
123         if (client->pid == info->pid)
124         {
125             XDBG_DEBUG (MXDBG, "[TRACE_CLIENT] Find Trace list:%s conn_cnt:%d+1\n", client_name, client->conn_cnt);
126             client->conn_cnt++;
127             return;
128         }
129     }
130
131     trace_client = (ModuleTraceClient *) calloc (1, sizeof(ModuleTraceClient));
132     XDBG_RETURN_IF_FAIL (trace_client != NULL);
133
134     trace_client->pid = info->pid;
135     trace_client->conn_cnt = 1;
136     strncpy (trace_client->name, client_name, strlen(client_name)+1);
137
138     XDBG_DEBUG (MXDBG, "[TRACE_CLIENT] Add Trace list:%s conn_cnt:%d\n", client_name, trace_client->conn_cnt);
139     xorg_list_add (&trace_client->link, &pMod->trace_list);
140
141     return;
142 }
143
144 void
145 xDbgModuleClistTraceRemove (XDbgModule *pMod, ModuleClientInfo *info)
146 {
147     char *client_name = strrchr (info->command, '/');
148     ModuleTraceClient *client = NULL, *tmp = NULL;
149     ModuleTraceClientOption *option = NULL;
150
151     XDBG_DEBUG (MXDBG, "[TRACE_CLIENT] RevmoeClist Trace command %s\n", info->command);
152
153     if (client_name)
154         client_name = client_name+1;
155     else
156         client_name = info->command;
157
158     if (!(option = _findTraceClientOption (pMod, client_name)))
159         return;
160
161     xorg_list_for_each_entry_safe(client, tmp, &pMod->trace_list, link) {
162         if (client->pid == info->pid)
163         {
164             if (client->conn_cnt == 1)
165             {
166                 if (!strcmp(option->action, "cmd"))
167                 {
168                     int ret = 0;
169                     ErrorF ("[TRACE_CLIENT] Disconnect Trace client:%s pid:%d cmd:%s", client->name, client->pid, option->cmd);
170                     if (strstr(option->cmd, "reboot"))
171                         sleep (1);
172
173                     ret = system (option->cmd);
174                     ErrorF (" ret:%d\n", ret);
175                 }
176                 else if (!strcmp(option->action, "error"))
177                 {
178
179                     FatalError ("[TRACE_CLIENT] %s\n", option->cmd);
180                     ErrorF ("[TRACE_CLIENT] Disconnect Trace client:%s pid:%d msg:%s\n", client->name, client->pid, option->cmd);
181
182                 }
183                 else if (!strcmp(option->action, "log"))
184                 {
185                     ErrorF ("[TRACE_CLIENT] Disconnect Trace client:%s pid:%d msg:%s\n", client->name, client->pid, option->cmd);
186                 }
187                 else
188                 {
189                     XDBG_DEBUG (MXDBG, "[TRACE_CLIENT] Unkown action Trace client:%s pid:%d action:%s cmd:%s\n",
190                                 client->name, client->pid, option->action, option->cmd);
191                 }
192
193                 xorg_list_del (&client->link);
194             }
195             else
196             {
197                 XDBG_DEBUG (MXDBG, "[TRACE_CLIENT] Unref client:%s conn_cnt:%d-1 \n", client_name, client->conn_cnt);
198                 client->conn_cnt--;
199             }
200             return;
201         }
202     }
203     return;
204 }