summaryrefslogtreecommitdiff
path: root/src/bin
diff options
context:
space:
mode:
authorCarsten Haitzler (Rasterman) <raster@rasterman.com>2015-05-08 19:48:26 +0900
committerCarsten Haitzler (Rasterman) <raster@rasterman.com>2015-05-08 19:48:26 +0900
commitafd2c1c0be2ba0de3e289912f1f081f430ebfa47 (patch)
treed6a74b5b5ef8433c2b0fe5eadc62933121f67a66 /src/bin
parent0ba6ddc44a3cb55dcdbd34675afb9025b07ca387 (diff)
efl debug - clean up debugd and debug cli tool code proto handling
this clenas up protocol handling to share common code and have more compact and easier to maintain code on both sides here.
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/efl/efl_debug.c109
-rw-r--r--src/bin/efl/efl_debug_common.c94
-rw-r--r--src/bin/efl/efl_debug_common.h30
-rw-r--r--src/bin/efl/efl_debugd.c183
4 files changed, 220 insertions, 196 deletions
diff --git a/src/bin/efl/efl_debug.c b/src/bin/efl/efl_debug.c
index b9800988ce..c8df269bf9 100644
--- a/src/bin/efl/efl_debug.c
+++ b/src/bin/efl/efl_debug.c
@@ -10,21 +10,19 @@ static const char *expect = NULL;
10static Ecore_Con_Server *svr; 10static Ecore_Con_Server *svr;
11 11
12static void 12static void
13_proto(unsigned char *d, unsigned int size) 13_do(char *op, unsigned char *d, int size)
14{ 14{
15 if (size >= 4) 15 if (!strcmp(op, "CLST"))
16 { 16 {
17 char *cmd = (char *)d; 17 int i, n;
18 18
19 if (!strncmp(cmd, "CLST", 4)) 19 n = (size) / sizeof(int);
20 if (n < 10000)
20 { 21 {
21 int i, n; 22 int *pids = malloc(n * sizeof(int));
22 23 if (pids)
23 n = (size - 4) / sizeof(int);
24 if (n < 10000)
25 { 24 {
26 int *pids = malloc(n * sizeof(int)); 25 memcpy(pids, d, n * sizeof(int));
27 memcpy(pids, d + 4, n * sizeof(int));
28 for (i = 0; i < n; i++) 26 for (i = 0; i < n; i++)
29 { 27 {
30 if (pids[i] > 0) printf("%i\n", pids[i]); 28 if (pids[i] > 0) printf("%i\n", pids[i]);
@@ -32,78 +30,42 @@ _proto(unsigned char *d, unsigned int size)
32 free(pids); 30 free(pids);
33 } 31 }
34 } 32 }
35 if ((expect) && (!strncmp(cmd, expect, 4)))
36 ecore_main_loop_quit();
37 }
38}
39
40
41static Eina_Bool
42_server_proto(void)
43{
44 unsigned int size, newsize;
45 unsigned char *b;
46 if (!buf) return EINA_FALSE;
47 if (buf_size < 4) return EINA_FALSE;
48 memcpy(&size, buf, 4);
49 if (buf_size < (size + 4)) return EINA_FALSE;
50 _proto(buf + 4, size);
51 newsize = buf_size - (size + 4);
52 if (buf_size == newsize)
53 {
54 free(buf);
55 buf = NULL;
56 buf_size = 0;
57 }
58 else
59 {
60 b = malloc(newsize);
61 memcpy(b, buf + size + 4, newsize);
62 free(buf);
63 buf = b;
64 buf_size = newsize;
65 } 33 }
66 return EINA_TRUE; 34 if ((expect) && (!strcmp(op, expect))) ecore_main_loop_quit();
67} 35}
68 36
69Eina_Bool 37Eina_Bool
70_server_add(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Server_Add *ev) 38_server_add(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Server_Add *ev EINA_UNUSED)
71{ 39{
72 int i; 40 int i;
73 for (i = 1; i < my_argc; i++) 41 for (i = 1; i < my_argc; i++)
74 { 42 {
75 if (!strcmp(my_argv[i], "list")) 43 if (!strcmp(my_argv[i], "list"))
76 { 44 {
77 unsigned int size = 4; 45 send_svr(svr, "LIST", NULL, 0);
78 char *head = "LIST";
79 expect = "CLST"; 46 expect = "CLST";
80 ecore_con_server_send(svr, &size, 4); 47 printf("send... expect %s\n", expect);
81 ecore_con_server_send(svr, head, 4);
82 } 48 }
83 else if ((!strcmp(my_argv[i], "pon")) && 49 else if ((!strcmp(my_argv[i], "pon")) &&
84 (i < (my_argc - 2))) 50 (i < (my_argc - 2)))
85 { 51 {
86 unsigned int size = 12; 52 unsigned char buf[8];
87 char *head = "PLON";
88 int pid = atoi(my_argv[i + 1]); 53 int pid = atoi(my_argv[i + 1]);
89 unsigned int freq = atoi(my_argv[i + 2]); 54 unsigned int freq = atoi(my_argv[i + 2]);
90 i++; 55 i += 2;
91 ecore_con_server_send(svr, &size, 4); 56 store_val(buf, 0, pid);
92 ecore_con_server_send(svr, head, 4); 57 store_val(buf, 4, freq);
93 ecore_con_server_send(svr, &pid, 4); 58 send_svr(svr, "PLON", buf, sizeof(buf));
94 ecore_con_server_send(svr, &freq, 4);
95 ecore_main_loop_quit(); 59 ecore_main_loop_quit();
96 } 60 }
97 else if ((!strcmp(my_argv[i], "poff")) && 61 else if ((!strcmp(my_argv[i], "poff")) &&
98 (i < (my_argc - 1))) 62 (i < (my_argc - 1)))
99 { 63 {
100 unsigned int size = 8; 64 unsigned char buf[4];
101 char *head = "PLOFF";
102 int pid = atoi(my_argv[i + 1]); 65 int pid = atoi(my_argv[i + 1]);
103 i++; 66 i++;
104 ecore_con_server_send(svr, &size, 4); 67 store_val(buf, 0, pid);
105 ecore_con_server_send(svr, head, 4); 68 send_svr(svr, "PLOF", buf, sizeof(buf));
106 ecore_con_server_send(svr, &pid, 4);
107 ecore_main_loop_quit(); 69 ecore_main_loop_quit();
108 } 70 }
109 } 71 }
@@ -111,7 +73,7 @@ _server_add(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Server
111} 73}
112 74
113Eina_Bool 75Eina_Bool
114_server_del(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Server_Del *ev) 76_server_del(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Server_Del *ev EINA_UNUSED)
115{ 77{
116 ecore_main_loop_quit(); 78 ecore_main_loop_quit();
117 return ECORE_CALLBACK_RENEW; 79 return ECORE_CALLBACK_RENEW;
@@ -120,26 +82,17 @@ _server_del(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Server
120static Eina_Bool 82static Eina_Bool
121_server_data(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Server_Data *ev) 83_server_data(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Server_Data *ev)
122{ 84{
123 if (!buf) 85 char op[5];
124 { 86 unsigned char *d = NULL;
125 buf = malloc(ev->size); 87 int size;
126 if (buf) 88
127 { 89 _protocol_collect(&(buf), &(buf_size), ev->data, ev->size);
128 buf_size = ev->size; 90 while ((size = _proto_read(&(buf), &(buf_size), op, &d)) >= 0)
129 memcpy(buf, ev->data, ev->size);
130 }
131 }
132 else
133 { 91 {
134 unsigned char *b = realloc(buf, buf_size + ev->size); 92 _do(op, d, size);
135 if (b) 93 free(d);
136 { 94 d = NULL;
137 buf = b;
138 memcpy(buf + buf_size, ev->data, ev->size);
139 buf_size += ev->size;
140 }
141 } 95 }
142 while (_server_proto());
143 return ECORE_CALLBACK_RENEW; 96 return ECORE_CALLBACK_RENEW;
144} 97}
145 98
@@ -156,7 +109,7 @@ main(int argc, char **argv)
156 svr = ecore_con_server_connect(ECORE_CON_LOCAL_USER, "efl_debug", 0, NULL); 109 svr = ecore_con_server_connect(ECORE_CON_LOCAL_USER, "efl_debug", 0, NULL);
157 if (!svr) 110 if (!svr)
158 { 111 {
159 fprintf(stderr, "ERROR: Cannot connetc to debug daemon.\n"); 112 fprintf(stderr, "ERROR: Cannot connect to debug daemon.\n");
160 return -1; 113 return -1;
161 } 114 }
162 115
diff --git a/src/bin/efl/efl_debug_common.c b/src/bin/efl/efl_debug_common.c
index 5b8c6f8f50..82587d2a55 100644
--- a/src/bin/efl/efl_debug_common.c
+++ b/src/bin/efl/efl_debug_common.c
@@ -1 +1,95 @@
1#include "efl_debug_common.h" 1#include "efl_debug_common.h"
2
3void
4_protocol_collect(unsigned char **buf, unsigned int *buf_size,
5 void *data, int size)
6{
7 // no buffer yet - duplicate it as out only data
8 if (!*buf)
9 {
10 *buf = malloc(size);
11 if (*buf)
12 {
13 *buf_size = size;
14 memcpy(*buf, data, size);
15 }
16 }
17 // we have data - append to the buffer and reallocate it as needed
18 else
19 {
20 unsigned char *b = realloc(*buf, *buf_size + size);
21 if (b)
22 {
23 *buf = b;
24 memcpy(*buf + *buf_size, data, size);
25 *buf_size += size;
26 }
27 }
28}
29
30int
31_proto_read(unsigned char **buf, unsigned int *buf_size,
32 char *op, unsigned char **data)
33{
34 unsigned int size, new_buf_size;
35 unsigned char *b;
36
37 // we have no data yet, or not enough - minimum 8 bytes
38 if (!*buf) return -1;
39 if (*buf_size < 8) return -1;
40 // get size of total message
41 memcpy(&size, *buf, 4);
42 // if size is invalid < 4 bytes - no message there
43 if (size < 4) return -1;
44 // if our total message buffer size is not big enough yet - no message
45 if (*buf_size < (size + 4)) return -1;
46
47 // copy out 4 byte opcode and nul byet terminate it
48 memcpy(op, *buf + 4, 4);
49 op[4] = 0;
50
51 // take off opcode header of 4 bytes
52 size -= 4;
53 // the new buffer size once we remove header+payload is...
54 new_buf_size = *buf_size - (size + 8);
55 if (size == 0)
56 {
57 *data = NULL;
58 size = 0;
59 }
60 else
61 {
62 // allocate new space for payload
63 *data = malloc(size);
64 if (!*data)
65 {
66 // allocation faild - no message
67 return -1;
68 }
69 memcpy(*data, *buf + 8, size);
70 }
71 // if new shrunk buffer size is empty -= just simply free buffer
72 if (new_buf_size == 0)
73 {
74 free(*buf);
75 *buf = NULL;
76 }
77 else
78 {
79 // allocate newly shrunk buffer
80 b = malloc(new_buf_size);
81 if (!b)
82 {
83 // alloc failure - bad. fail proto read then
84 free(*data);
85 return -1;
86 }
87 // copy data to new smaller buffer and free old, storing new buffer
88 memcpy(b, *buf + size + 8, new_buf_size);
89 free(*buf);
90 *buf = b;
91 }
92 // store new buffer size
93 *buf_size = new_buf_size;
94 return (int)size;
95}
diff --git a/src/bin/efl/efl_debug_common.h b/src/bin/efl/efl_debug_common.h
index 435ddc0923..46f859c651 100644
--- a/src/bin/efl/efl_debug_common.h
+++ b/src/bin/efl/efl_debug_common.h
@@ -9,4 +9,34 @@
9#include <unistd.h> 9#include <unistd.h>
10#include <string.h> 10#include <string.h>
11 11
12void _protocol_collect(unsigned char **buf, unsigned int *buf_size,
13 void *data, int size);
14int _proto_read(unsigned char **buf, unsigned int *buf_size,
15 char *op, unsigned char **data);
16
17#define fetch_val(dst, buf, off) \
18 memcpy(&dst, ((unsigned char *)buf) + off, sizeof(dst))
19#define store_val(buf, off, src) \
20 memcpy(buf + off, &src, sizeof(src))
21#define send_svr(svr, op, data, size) \
22 do { \
23 unsigned char head[8]; \
24 char *op2 = op; \
25 int size2 = size + 4; \
26 memcpy(head + 0, &size2, 4); \
27 memcpy(head + 4, op2, 4); \
28 ecore_con_server_send(svr, head, 8); \
29 if (size > 0) ecore_con_server_send(svr, data, size); \
30 } while (0)
31#define send_cli(cli, op, data, size) \
32 do { \
33 unsigned char head[8]; \
34 char *op2 = op; \
35 int size2 = size + 4; \
36 memcpy(head + 0, &size2, 4); \
37 memcpy(head + 4, op2, 4); \
38 ecore_con_client_send(cli, head, 8); \
39 if (size > 0) ecore_con_client_send(cli, data, size); \
40 } while (0)
41
12#endif 42#endif
diff --git a/src/bin/efl/efl_debugd.c b/src/bin/efl/efl_debugd.c
index c102ed4a9b..b64747f63d 100644
--- a/src/bin/efl/efl_debugd.c
+++ b/src/bin/efl/efl_debugd.c
@@ -5,129 +5,85 @@ typedef struct _Client Client;
5struct _Client 5struct _Client
6{ 6{
7 Ecore_Con_Client *client; 7 Ecore_Con_Client *client;
8 int version; 8 unsigned char *buf;
9 pid_t pid; 9 unsigned int buf_size;
10 unsigned char *buf; 10
11 unsigned int buf_size; 11 int version;
12 pid_t pid;
12}; 13};
13 14
14static Ecore_Con_Server *svr = NULL; 15static Ecore_Con_Server *svr = NULL;
15static Eina_List *clients = NULL; 16static Eina_List *clients = NULL;
16 17
18static Client *
19_client_pid_find(int pid)
20{
21 Client *c;
22 Eina_List *l;
23
24 if (pid <= 0) return NULL;
25 EINA_LIST_FOREACH(clients, l, c)
26 {
27 if (c->pid == pid) return c;
28 }
29 return NULL;
30}
31
17static void 32static void
18_proto(Client *c, unsigned char *d, unsigned int size) 33_do(Client *c, char *op, unsigned char *d, int size)
19{ 34{
20 if (size >= 4) 35 Client *c2;
36 Eina_List *l;
37
38 if ((!strcmp(op, "HELO")) && (size >= 8))
21 { 39 {
22 char *cmd = (char *)d; 40 int version;
41 int pid;
23 42
24 if (!strncmp(cmd, "HELO", 4)) 43 fetch_val(version, d, 0);
44 fetch_val(pid, d, 4);
45 c->version = version;
46 c->pid = pid;
47 }
48 else if (!strcmp(op, "LIST"))
49 {
50 int n = eina_list_count(clients);
51 unsigned int *pids = malloc(n * sizeof(int));
52 if (pids)
25 { 53 {
26 int version; 54 int i = 0;
27 int pid;
28 55
29 memcpy(&version, d + 4, 4);
30 memcpy(&pid, d + 8, 4);
31 c->version = version;
32 c->pid = pid;
33 }
34 else if (!strncmp(cmd, "LIST", 4))
35 {
36 int n = eina_list_count(clients), i;
37 unsigned int *pids, size2;
38 Client *c2;
39 Eina_List *l;
40 char *head = "CLST";
41
42 pids = malloc(n * sizeof(int));
43 i = 0;
44 size2 = 4 + (n * sizeof(int));
45 EINA_LIST_FOREACH(clients, l, c2) 56 EINA_LIST_FOREACH(clients, l, c2)
46 { 57 {
47 pids[i] = c2->pid; 58 pids[i] = c2->pid;
48 i++; 59 i++;
49 } 60 }
50 ecore_con_client_send(c->client, &size2, 4); 61 send_cli(c->client, "CLST", pids, n * sizeof(int));
51 ecore_con_client_send(c->client, head, 4);
52 ecore_con_client_send(c->client, pids, n * sizeof(int));
53 free(pids); 62 free(pids);
54 } 63 }
55 else if (!strncmp(cmd, "PLON", 4))
56 {
57 int pid;
58 unsigned int freq = 1000;
59 Client *c2;
60 Eina_List *l;
61
62 memcpy(&pid, d + 4, 4);
63 memcpy(&freq, d + 8, 4);
64 if (pid > 0)
65 {
66 EINA_LIST_FOREACH(clients, l, c2)
67 {
68 if (c2->pid == pid)
69 {
70 unsigned int size2 = 8;
71
72 ecore_con_client_send(c2->client, &size2, 4);
73 ecore_con_client_send(c2->client, d, 4);
74 ecore_con_client_send(c2->client, &freq, 4);
75 break;
76 }
77 }
78 }
79 }
80 else if (!strncmp(cmd, "PLOF", 4))
81 {
82 int pid;
83 Client *c2;
84 Eina_List *l;
85
86 memcpy(&pid, d + 4, 4);
87 if (pid > 0)
88 {
89 EINA_LIST_FOREACH(clients, l, c2)
90 {
91 if (c2->pid == pid)
92 {
93 unsigned int size2 = 4;
94
95 ecore_con_client_send(c2->client, &size2, 4);
96 ecore_con_client_send(c2->client, d, 4);
97 break;
98 }
99 }
100 }
101 }
102 } 64 }
103} 65 else if ((!strcmp(op, "PLON")) && (size >= 8))
104
105static Eina_Bool
106_client_proto(Client *c)
107{
108 unsigned int size, newsize;
109 unsigned char *b;
110 if (!c->buf) return EINA_FALSE;
111 if (c->buf_size < 4) return EINA_FALSE;
112 memcpy(&size, c->buf, 4);
113 if (c->buf_size < (size + 4)) return EINA_FALSE;
114 _proto(c, c->buf + 4, size);
115 newsize = c->buf_size - (size + 4);
116 if (c->buf_size == newsize)
117 { 66 {
118 free(c->buf); 67 int pid;
119 c->buf = NULL; 68 unsigned int freq = 1000;
120 c->buf_size = 0; 69 fetch_val(pid, d, 0);
70 fetch_val(freq, d, 4);
71 if ((c2 = _client_pid_find(pid)))
72 {
73 unsigned char buf[4];
74 store_val(buf, 0, freq);
75 send_cli(c2->client, "PLON", buf, sizeof(buf));
76 }
121 } 77 }
122 else 78 else if (!strcmp(op, "PLOF"))
123 { 79 {
124 b = malloc(newsize); 80 int pid;
125 memcpy(b, c->buf + size + 4, newsize); 81 fetch_val(pid, d, 0);
126 free(c->buf); 82 if ((c2 = _client_pid_find(pid)))
127 c->buf = b; 83 {
128 c->buf_size = newsize; 84 send_cli(c2->client, "PLOF", NULL, 0);
85 }
129 } 86 }
130 return EINA_TRUE;
131} 87}
132 88
133static Eina_Bool 89static Eina_Bool
@@ -161,26 +117,17 @@ _client_data(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Clien
161 Client *c = ecore_con_client_data_get(ev->client); 117 Client *c = ecore_con_client_data_get(ev->client);
162 if (c) 118 if (c)
163 { 119 {
164 if (!c->buf) 120 char op[5];
165 { 121 unsigned char *d = NULL;
166 c->buf = malloc(ev->size); 122 int size;
167 if (c->buf) 123
168 { 124 _protocol_collect(&(c->buf), &(c->buf_size), ev->data, ev->size);
169 c->buf_size = ev->size; 125 while ((size = _proto_read(&(c->buf), &(c->buf_size), op, &d)) >= 0)
170 memcpy(c->buf, ev->data, ev->size);
171 }
172 }
173 else
174 { 126 {
175 unsigned char *b = realloc(c->buf, c->buf_size + ev->size); 127 _do(c, op, d, size);
176 if (b) 128 free(d);
177 { 129 d = NULL;
178 c->buf = b;
179 memcpy(c->buf + c->buf_size, ev->data, ev->size);
180 c->buf_size += ev->size;
181 }
182 } 130 }
183 while (_client_proto(c));
184 } 131 }
185 return ECORE_CALLBACK_RENEW; 132 return ECORE_CALLBACK_RENEW;
186} 133}