summaryrefslogtreecommitdiff
path: root/src/lib/eo_debug.c
blob: 42edc9ca7692b82be643b07bc3e50644f28b8aed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#define _GNU_SOURCE

#include <Eina.h>
#include <Ecore.h>
#include <Eo.h>
#include <Evas.h>
#include <Elementary.h>
#define ELM_INTERNAL_API_ARGESFSDFEFC
#include <elm_widget.h>

#include <dlfcn.h>

#include "Eo_Debug.h"

#define STORE(_buf, pval, sz) \
{ \
   memcpy(_buf, pval, sz); \
   _buf += sz; \
}

#define EXTRACT(_buf, pval, sz) \
{ \
   memcpy(pval, _buf, sz); \
   _buf += sz; \
}

typedef struct
{
   Eina_Debug_Session *session;
   int srcid;
   void *buffer;
   int size;
} _Main_Loop_Struct;

static int _klids_get_op = EINA_DEBUG_OPCODE_INVALID;
static int _eoids_get_op = EINA_DEBUG_OPCODE_INVALID;

static Eina_Bool
_klids_walk_cb(void *data, Efl_Class *kl)
{
   char **buf = data;
   const char *name = efl_class_name_get(kl);
   int len = strlen(name) + 1;
   uint64_t u64 = (uint64_t)kl;

   memcpy(*buf, &u64, sizeof(u64));
   *buf += sizeof(u64);
   memcpy(*buf, name, len);
   *buf += len;
   return EINA_TRUE;
}

static void
_klids_cb(void *data)
{
   static Eina_Bool (*foo)(Eo_Debug_Class_Iterator_Cb, void *) = NULL;
   _Main_Loop_Struct *s = data;
   char *buf = alloca(10000), *current = buf;

   if (!foo) foo = dlsym(RTLD_DEFAULT, "eo_debug_classes_iterate");
   foo(_klids_walk_cb, &current);
   eina_debug_session_send(s->session, s->srcid, _klids_get_op, buf, current - buf);

   free(s);
}

typedef struct
{
   char *buf;
   uint64_t *kls;
   int nb_kls;
} _EoIds_Walk_Data;

static Eina_Bool
_eoids_walk_cb(void *_data, Eo *obj)
{
   _EoIds_Walk_Data *data = _data;
   Eo *parent = NULL;
   int i;
   Eina_Bool klass_ok = EINA_FALSE;

   for (i = 0; i < data->nb_kls && !klass_ok; i++)
     {
        if (efl_isa(obj, (Efl_Class *)data->kls[i])) klass_ok = EINA_TRUE;
     }
   if (!klass_ok && data->nb_kls) return EINA_TRUE;

   uint64_t u64 = (uint64_t)obj;
   STORE(data->buf, &u64, sizeof(u64));
   u64 = (uint64_t)efl_class_get(obj);
   STORE(data->buf, &u64, sizeof(u64));

   if (efl_isa(obj, ELM_WIDGET_CLASS))
      parent = elm_widget_parent_get(obj);
   if (!parent && efl_isa(obj, EFL_CANVAS_OBJECT_CLASS))
     {
        parent = evas_object_data_get(obj, "elm-parent");
        if (!parent) parent = evas_object_smart_parent_get(obj);
     }
   if (!parent) parent = efl_parent_get(obj);
   u64 = (uint64_t)parent;
   STORE(data->buf, &u64, sizeof(u64));

   return EINA_TRUE;
}

static Eina_Debug_Error
_eoids_get(Eina_Debug_Session *session, int srcid, void *buffer, int size)
{
   static Eina_Bool (*foo)(Eo_Debug_Object_Iterator_Cb, void *) = NULL;
   int thread_id = eina_debug_thread_id_get();
   char *buf = alloca(sizeof(int) + 3 * 10000 * sizeof(uint64_t));
   _EoIds_Walk_Data data;

   data.buf = buf;
   data.kls = buffer;
   data.nb_kls = size / sizeof(uint64_t);
   STORE(data.buf, &thread_id, sizeof(int));
   if (!foo) foo = dlsym(RTLD_DEFAULT, "eo_debug_objects_iterate");
   foo(_eoids_walk_cb, &data);
   eina_debug_session_send(session, srcid, _eoids_get_op, buf, data.buf - buf);
   return EINA_DEBUG_OK;
}

static Eina_Debug_Error
_klids_get(Eina_Debug_Session *session, int srcid, void *buffer, int size)
{
   _Main_Loop_Struct *s = malloc(sizeof(*s));
   s->session = session;
   s->srcid = srcid;
   s->buffer = buffer;
   s->size = size;
   ecore_main_loop_thread_safe_call_async(_klids_cb, s);
   return EINA_DEBUG_OK;
}

static const Eina_Debug_Opcode _debug_ops[] =
{
     {"Eo/objects_ids_get", &_eoids_get_op, &_eoids_get},
     {"Eo/classes_ids_get", &_klids_get_op, &_klids_get},
     {NULL, NULL, NULL}
};

EAPI Eina_Bool
eo_debug_init(void)
{
   eina_init();
   efl_object_init();

   eina_debug_opcodes_register(NULL, _debug_ops, NULL);

   printf("%s - In\n", __FUNCTION__);
   return EINA_TRUE;
}

EAPI Eina_Bool
eo_debug_shutdown(void)
{
   efl_object_shutdown();
   eina_shutdown();

   return EINA_TRUE;
}

EAPI void *
eo_debug_eoids_request_prepare(int *size, ...)
{
   va_list list;
   uint64_t kl;
   int nb_kls = 0, max_kls = 0;
   char *buf = NULL;
   va_start(list, size);
   kl = va_arg(list, uint64_t);
   while (kl)
     {
        nb_kls++;
        if (max_kls < nb_kls)
          {
             max_kls += 10;
             buf = realloc(buf, max_kls * sizeof(uint64_t));
          }
        char *tmp = buf + (nb_kls-1) * sizeof(uint64_t);
        STORE(tmp, &kl, sizeof(uint64_t));
        kl = va_arg(list, uint64_t);
     }
   *size = nb_kls * sizeof(uint64_t);
   return buf;
}

EAPI void
eo_debug_eoids_extract(void *buffer, int size, Eo_Debug_Object_Extract_Cb cb, void *data, int *thread_id)
{
   if (!buffer || !size || !cb) return;
   char *buf = buffer;
   EXTRACT(buf, thread_id, sizeof(int));
   size -= sizeof(int);

   while (size > 0)
     {
        uint64_t obj, kl, parent;
        EXTRACT(buf, &obj, sizeof(uint64_t));
        EXTRACT(buf, &kl, sizeof(uint64_t));
        EXTRACT(buf, &parent, sizeof(uint64_t));
        cb(data, obj, kl, parent);
        size -= (3 * sizeof(uint64_t));
     }
}

EAPI void
eo_debug_klids_extract(void *buffer, int size, Eo_Debug_Class_Extract_Cb cb, void *data)
{
   if (!buffer || !size || !cb) return;
   char *buf = buffer;
   while (size > 0)
     {
        uint64_t kl;
        char *name;
        EXTRACT(buf, &kl, sizeof(uint64_t));
        name = buf;
        cb(data, kl, buf);
        buf += (strlen(name) + 1);
        size -= (strlen(name) + 1 + sizeof(uint64_t));
     }
}