summaryrefslogtreecommitdiff
path: root/src/lib/eolian_debug.c
blob: ce9eda7cd06e2632a9a985b0f16cf87609776423 (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#define _GNU_SOURCE

#include <dlfcn.h> /* dlopen,dlclose,etc */
#include <ffi.h>
#include <Eo.h>
#include <Eina.h>
#include <Eolian.h>
#include "Eolian_Debug.h"

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

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

static int _obj_info_op = EINA_DEBUG_OPCODE_INVALID;

typedef struct
{
   Eolian_Type_Type etype;
   const char *name;
   Eolian_Debug_Basic_Type type;
   const char *print_format;
   void *ffi_type_p;//ffi_type
   const unsigned int size;
} Param_Type_Info;

const Param_Type_Info param_types[] =
{
     {EOLIAN_TYPE_REGULAR, "",  EOLIAN_DEBUG_INVALID_TYPE, "",   &ffi_type_pointer, 0},
     {EOLIAN_TYPE_COMPLEX, "pointer", EOLIAN_DEBUG_POINTER,  "%p", &ffi_type_pointer, 8},
     {EOLIAN_TYPE_COMPLEX, "char", EOLIAN_DEBUG_STRING,  "%s", &ffi_type_pointer, 8},
     {EOLIAN_TYPE_REGULAR, "char", EOLIAN_DEBUG_CHAR,  "%c", &ffi_type_uint, 1},
     {EOLIAN_TYPE_REGULAR, "int",  EOLIAN_DEBUG_INT, "%d", &ffi_type_sint, 4},
     {EOLIAN_TYPE_REGULAR, "short",  EOLIAN_DEBUG_SHORT,    "%d", &ffi_type_sint, 4},
     {EOLIAN_TYPE_REGULAR, "double", EOLIAN_DEBUG_DOUBLE,   "%f", &ffi_type_pointer, 8},
     {EOLIAN_TYPE_REGULAR, "bool", EOLIAN_DEBUG_BOOLEAN,     "%d", &ffi_type_uint, 1},
     {EOLIAN_TYPE_REGULAR, "long", EOLIAN_DEBUG_LONG,     "%f", &ffi_type_pointer, 8},
     {EOLIAN_TYPE_REGULAR, "uint", EOLIAN_DEBUG_UINT,     "%u", &ffi_type_uint, 4},
     {0, NULL, 0, NULL, NULL, 0}
};

static Eolian_Debug_Basic_Type
_eolian_type_resolve(const Eolian_Type *eo_type)
{
   Eolian_Type_Type type = eolian_type_type_get(eo_type);
   Eolian_Type_Type type_base = type;

   if(type == EOLIAN_TYPE_COMPLEX)
     {
         eo_type = eolian_type_base_type_get(eo_type);
         type_base = eolian_type_type_get(eo_type);
     }

   if (type_base == EOLIAN_TYPE_REGULAR)
     {
        const char *full_name = eolian_type_full_name_get(eo_type);
        const Eolian_Typedecl *alias = eolian_typedecl_alias_get_by_name(full_name);
        if (alias)
          {
            eo_type = eolian_typedecl_base_type_get(alias);
            type_base = eolian_type_type_get(eo_type);
            full_name = eolian_type_full_name_get(eo_type);
          }

        if(full_name)
          {
           int i;
           for (i = 0; param_types[i].name; i++)
              if (!strcmp(full_name, param_types[i].name) &&
                      param_types[i].etype == type)  return i;
          }
     }

   if (type == EOLIAN_TYPE_COMPLEX)
      return EOLIAN_DEBUG_POINTER;

#if 0
   printf("Type %s not supported\n",
         type_base != EOLIAN_TYPE_VOID ? eolian_type_full_name_get(eo_type) :
         "void");
#endif
   return EOLIAN_DEBUG_INVALID_TYPE;

}

static const Eolian_Class *
_class_find_by_name(const char *eo_klname)
{
   const Eolian_Class *kl = NULL;
   if (!eo_klname) return NULL;
   char *klname = strdup(eo_klname);

   Eina_Strbuf *buf = eina_strbuf_new();
   eina_strbuf_append(buf, eo_klname);
   eina_strbuf_replace_all(buf, ".", "_");
   eina_strbuf_append(buf, ".eo");
   char *tmp = eina_strbuf_string_steal(buf);
   eina_strbuf_free(buf);
   eina_str_tolower(&tmp);
   eolian_file_parse(tmp);
   free(tmp);

   tmp = klname;
   do
     {
        kl = eolian_class_get_by_name(klname);
        if (kl) goto end;
        while (*tmp && *tmp != '_') tmp++;
        if (*tmp) *tmp = '.';
     }
   while (*tmp);
   printf("Class %s not found.\n", klname);

end:
   free(klname);
   return kl;
}

#if 0
static const Eolian_Function *
_function_find_by_name(const char *func_klname,
      const char *func_name, Eolian_Function_Type ftype)
{
   const Eolian_Function *func = NULL;
   if (func_name)
     {
        const Eolian_Class *func_kl = eolian_class_get_by_name(func_klname);
        func = eolian_class_function_get_by_name(func_kl, func_name, ftype);
     }

   return func;
}
#endif

static int
_function_invoke(Eo *ptr, const Eolian_Function *foo, Eolian_Function_Type foo_type,
      Eolian_Debug_Parameter *params, Eolian_Debug_Return *ret)
{
   /* The params table contains the keys and the values.
    * This function doesn't allocate memory */
   ffi_type *types[EOLIAN_DEBUG_MAXARGS]; /* FFI types */
   void *values[EOLIAN_DEBUG_MAXARGS];    /* FFI Values */
   void *pointers[EOLIAN_DEBUG_MAXARGS];  /* Used as values for out params, as we have to give a pointer to a pointer */
   Eolian_Function_Parameter *eo_param;
   Eina_Iterator *itr;
   Eolian_Debug_Basic_Type ed_type;
   int argc, ffi_argc = 0;

   if (!foo) return -1;
   if (foo_type == EOLIAN_PROPERTY) return -1;

   /* Eo object storage */
   types[ffi_argc] = &ffi_type_pointer;
   values[ffi_argc] = &ptr;
   ffi_argc++;

   itr = eolian_property_keys_get(foo, foo_type);
   argc = 0;
   EINA_ITERATOR_FOREACH(itr, eo_param)
     {
        /* Not verified */
        ed_type = _eolian_type_resolve(eolian_parameter_type_get(eo_param));
        if (!ed_type) goto error;

        types[ffi_argc] = param_types[ed_type].ffi_type_p;
        values[ffi_argc] = &(params[argc].value.value.value);
        ffi_argc++;
        argc++;
     }

   itr = foo_type == EOLIAN_METHOD ? eolian_function_parameters_get(foo) :
      eolian_property_values_get(foo, foo_type);
   EINA_ITERATOR_FOREACH(itr, eo_param)
     {
        ed_type = _eolian_type_resolve(eolian_parameter_type_get(eo_param));
        if (!ed_type) goto error;

        if (foo_type == EOLIAN_PROP_GET ||
              (foo_type == EOLIAN_METHOD && eolian_parameter_direction_get(eo_param) == EOLIAN_OUT_PARAM))
          {
             /* Out parameter */
             params[argc].value.value.value = 0;
             params[argc].value.type = ed_type;
             types[ffi_argc] = &ffi_type_pointer;
             pointers[ffi_argc] = &(params[argc].value.value.value);
             values[ffi_argc] = &pointers[ffi_argc];
          }
        else
          {
             /* In parameter */
             types[ffi_argc] = param_types[ed_type].ffi_type_p;
             values[ffi_argc] = &(params[argc].value.value.value);
          }
        ffi_argc++;
        argc++;
     }

   const Eolian_Type *eo_type = eolian_function_return_type_get(foo, foo_type);
   ffi_type *ffi_ret_type = &ffi_type_void;
   if (eo_type)
     {
        ed_type = _eolian_type_resolve(eo_type);
        if (!ed_type) goto error;

        ffi_ret_type = param_types[ed_type].ffi_type_p;
     }
   else if (argc == 1 && foo_type == EOLIAN_PROP_GET)
     {
        /* If there is no return type but only one value is present, the value will
         * be returned by the function.
         * So we need FFI to not take it into account when invoking the function.
         */
        ffi_ret_type = param_types[params[0].value.type].ffi_type_p;
        ffi_argc--;
     }

   const char *full_func_name = eolian_function_full_c_name_get(foo,  EOLIAN_PROP_GET, EINA_FALSE);
   //printf("dlsym %s\n", full_func_name);
   void *eo_func = dlsym(RTLD_DEFAULT, full_func_name);
   if (!eo_func) goto error;
   ffi_cif cif;
   if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, ffi_argc,
            ffi_ret_type, types) == FFI_OK)
     {
        void *result;
        ffi_call(&cif, eo_func, &result, values);
        if (ret) ret->value.type = EOLIAN_DEBUG_VOID;
        if (eo_type)
          {
             if (ret)
               {
                  ret->value.value.value = (uint64_t) result;
                  ret->value.type = ed_type;
               }
          }
        else if (argc == 1 && foo_type == EOLIAN_PROP_GET)
           params[0].value.value.value = (uint64_t) result;
        goto success;
     }

error:
   argc = -1;
success:
   eina_iterator_free(itr);
   return argc;
}

static Eina_Bool
_eolian_function_is_implemented(
      const Eolian_Function *function_id, Eolian_Function_Type func_type,
      const Eolian_Class *klass)
{
   Eina_Iterator *impl_itr = NULL;
   Eolian_Function_Type found_type = EOLIAN_UNRESOLVED;
   Eina_Bool found = EINA_TRUE;
   if (!function_id || !klass) return EINA_FALSE;
   Eina_List *list = eina_list_append(NULL, klass), *list2, *itr;
   EINA_LIST_FOREACH(list, itr, klass)
     {
        const char *inherit_name;
        const Eolian_Implement *impl;
        if (eolian_class_type_get(klass) == EOLIAN_CLASS_INTERFACE) continue;
        impl_itr = eolian_class_implements_get(klass);
        EINA_ITERATOR_FOREACH(impl_itr, impl)
          {
             if (eolian_implement_is_pure_virtual(impl, EOLIAN_PROP_SET)) continue;
             Eolian_Function_Type impl_type = EOLIAN_UNRESOLVED;
             const Eolian_Function *impl_func = eolian_implement_function_get(impl, &impl_type);
             if (impl_func == function_id)
               {
                  /* The type matches the requested or is not important for the caller */
                  if (func_type == EOLIAN_UNRESOLVED || impl_type == func_type) goto end;
                  if (impl_type == EOLIAN_METHOD) continue;
                  /* In case we search for a property type */
                  if (impl_type == EOLIAN_PROPERTY &&
                        (func_type == EOLIAN_PROP_GET || func_type == EOLIAN_PROP_SET))
                     goto end;
                  /* Property may be splitted on multiple implements */
                  if (func_type == EOLIAN_PROPERTY)
                    {
                       if (found_type == EOLIAN_UNRESOLVED) found_type = impl_type;
                       if ((found_type == EOLIAN_PROP_SET && impl_type == EOLIAN_PROP_GET) ||
                             (found_type == EOLIAN_PROP_GET && impl_type == EOLIAN_PROP_SET))
                          goto end;
                    }
               }
          }
        eina_iterator_free(impl_itr);
        impl_itr = NULL;

        Eina_Iterator *inherits_itr = eolian_class_inherits_get(klass);
        EINA_ITERATOR_FOREACH(inherits_itr, inherit_name)
          {
             const Eolian_Class *inherit = eolian_class_get_by_name(inherit_name);
             /* Avoid duplicates. */
             if (!eina_list_data_find(list, inherit))
               {
                  list2 = eina_list_append(list, inherit);
               }
          }
        eina_iterator_free(inherits_itr);
     }
   (void) list2;
   found = EINA_FALSE;
end:
   if (impl_itr) eina_iterator_free(impl_itr);
   eina_list_free(list);
   return found;
}

static unsigned int
_class_buffer_fill(Eo *obj, const Eolian_Class *ekl, char *buf)
{
   unsigned int size = 0;
   Eina_Iterator *funcs = eolian_class_functions_get(ekl, EOLIAN_PROPERTY);
   const Eolian_Function *func;
   EINA_ITERATOR_FOREACH(funcs, func)
     {
        if (eolian_function_type_get(func) == EOLIAN_PROP_SET ||
              !_eolian_function_is_implemented(func, EOLIAN_PROP_GET, ekl)) continue;
        Eina_Iterator *keys_itr = eolian_property_keys_get(func, EOLIAN_PROP_GET);
        eina_iterator_free(keys_itr);
        /* We dont support functions with key parameters */
        if (keys_itr) continue;

        Eolian_Debug_Parameter params[EOLIAN_DEBUG_MAXARGS];
        Eolian_Debug_Return ret;
        int argnum = _function_invoke(obj, func, EOLIAN_PROP_GET, params, &ret);

        if (argnum == -1) continue;

        int len, i;
        if (!size) // only if its the first func to succeed
          {
             const char *class_name = eolian_class_full_name_get(ekl);
             len = strlen(class_name) + 1;
             memcpy(buf, class_name, len);
             size += len;
          }
        else
          {
             //write zero length string to mark that it's still the same class
             buf[size] = '\0';
             size++;
          }
        const char *func_name = eolian_function_name_get(func);
        len = strlen(func_name) + 1;
        memcpy(buf + size, func_name, len);
        size += len;
        for (i = 0; i < argnum; i++) //print params
          {
             // if its a string we wont copy the pointer but the value
             if (params[i].value.type == EOLIAN_DEBUG_STRING)
               {
                  if((char *)params[i].value.value.value == NULL)
                     params[i].value.value.value = (uint64_t)"";
                  len = strlen((char *)params[i].value.value.value) + 1;
                  memcpy(buf + size, (char *)params[i].value.value.value, len);
                  size += len;
               }
             else
               {
                  memcpy(buf + size,  &(params[i].value.value.value),
                        param_types[params[i].value.type].size);
                  size += param_types[params[i].value.type].size;
               }
          }
        /*
           For Eolian funcs that dont define "pram type" for their
           return value we dont have part of the info that we have on
           normal Eolian param (such as name). Also the return value wont
           be in the function params iterator - hence argnum = 0. For that
           case we use Eolian_Debug_Return struct
           */
        if (ret.value.type != EOLIAN_DEBUG_VOID)
          {
             //if its a string we wont copy the pointer but the values
             if (ret.value.type == EOLIAN_DEBUG_STRING)
               {
                  if((char *)ret.value.value.value == NULL)
                     ret.value.value.value =  (uint64_t)"";
                  len = strlen((char *)ret.value.value.value) + 1;
                  memcpy(buf + size, (char *)ret.value.value.value, len);
                  size += len;
               }
             else
               {
                  memcpy(buf + size,  &(ret.value.value.value),
                        param_types[ret.value.type].size);
                  size += param_types[ret.value.type].size;
               }
          }

     }
   eina_iterator_free(funcs);
   return size;
}

static Eina_Debug_Error
_obj_info_req_cb(Eina_Debug_Session *session, int srcid, void *buffer, int size EINA_UNUSED)
{
   uint64_t ptr64;
   memcpy(&ptr64, buffer, sizeof(ptr64));
   Eo *obj = (Eo *)ptr64;

   const char *class_name = efl_class_name_get(obj);
   const Eolian_Class *kl = _class_find_by_name(class_name);
   char *buf = alloca(100000);
   unsigned int size_curr = 0;
   if (!kl)
     {
        printf("Class %s not found.\n", class_name);
        goto end;
     }
   memcpy(buf, &ptr64, sizeof(uint64_t));
   size_curr = sizeof(uint64_t);

   Eina_List *itr, *list2;
   Eina_List *list = eina_list_append(NULL, kl);
   EINA_LIST_FOREACH(list, itr, kl)
     {
        const char *inherit_name;
        Eina_Iterator *inherits_itr = eolian_class_inherits_get(kl);

        size_curr += _class_buffer_fill(obj, kl, buf + size_curr);
        EINA_ITERATOR_FOREACH(inherits_itr, inherit_name)
          {
             const Eolian_Class *inherit = eolian_class_get_by_name(inherit_name);
             if (!inherit) printf("class not found for name: \"%s\"", inherit_name);
             /* Avoid duplicates in MRO list. */
             if (!eina_list_data_find(list, inherit))
                list2 = eina_list_append(list, inherit);
          }
        eina_iterator_free(inherits_itr);
     }
   (void) list2;

   eina_debug_session_send(session, srcid, _obj_info_op, buf, size_curr);

end:
   return EINA_DEBUG_OK;
}

EAPI void
eolian_debug_object_information_free(Eolian_Debug_Object_Information *main)
{
   Eolian_Debug_Class *kl;
   if (!main) return;
   EINA_LIST_FREE(main->classes, kl)
     {
        Eolian_Debug_Function *func;
        EINA_LIST_FREE(kl->functions, func)
          {
             Eolian_Debug_Parameter *param;
             EINA_LIST_FREE(func->params, param)
               {
                  if (param->value.type == EOLIAN_DEBUG_STRING)
                     eina_stringshare_del((char *)param->value.value.value);
                  free(param);
               }
             free(func);
          }
        free(kl);
     }
   free(main);
}

/*
 * receive buffer of the following format:
 * Eo *pointer (uint64_t)
 * followed by list of
 * class_name, function. if class_name='\0' this function belongs to last correct class_name
 */
EAPI Eolian_Debug_Object_Information *
eolian_debug_object_information_decode(char *buffer, unsigned int size)
{
   if (size < sizeof(uint64_t)) return NULL;
   Eolian_Debug_Object_Information *ret = calloc(1, sizeof(*ret));
   Eolian_Debug_Class *kl = NULL;

   //get the Eo* pointer
   EXTRACT(buffer, &(ret->obj), sizeof(uint64_t));
   size -= sizeof(uint64_t);

   while (size)
     {
        int len = strlen(buffer) + 1;
        if (len > 1) // if class_name is not NULL, we begin a new class
          {
             kl = calloc(1, sizeof(*kl));
             kl->ekl = _class_find_by_name(buffer);
             ret->classes = eina_list_append(ret->classes, kl);
          }
        buffer += len;
        size -= len;

        Eolian_Debug_Function *func = calloc(1, sizeof(*func));
        printf("Class name = %s function = %s\n", eolian_class_name_get(kl->ekl), buffer);
        kl->functions = eina_list_append(kl->functions, func);
        func->efunc = eolian_class_function_get_by_name(kl->ekl, buffer, EOLIAN_PROP_GET);
        if(!func->efunc)
          {
             printf("Function %s not found!\n", buffer);
             goto error;
          }
        len = strlen(buffer) + 1;
        buffer += len;
        size -= len;

        //go over function params using eolian
        Eolian_Function_Parameter *eo_param;
        Eina_Iterator *itr = eolian_property_values_get(func->efunc, EOLIAN_PROP_GET);
        EINA_ITERATOR_FOREACH(itr, eo_param)
          {
             Eolian_Debug_Basic_Type type = _eolian_type_resolve(
                   eolian_parameter_type_get(eo_param));

             if (type)
               {
                  Eolian_Debug_Parameter *p = calloc(1, sizeof(*p));
                  p->eparam = eo_param;
                  p->value.type = type;
                  if (type == EOLIAN_DEBUG_STRING)
                    {
                       len = strlen(buffer) + 1;
                       p->value.value.value = (uint64_t) eina_stringshare_add(buffer);
                       buffer += len;
                       size -= len;
                    }
                  else
                    {
                       EXTRACT(buffer, &(p->value.value), param_types[type].size);
                       size -= param_types[type].size;
                    }
                  func->params = eina_list_append(func->params, p);
               }
             else goto error;
          }
        func->ret.etype = eolian_function_return_type_get(
              func->efunc, EOLIAN_PROP_GET);
        if(func->ret.etype)
          {
             Eolian_Debug_Basic_Type type = _eolian_type_resolve(func->ret.etype);
             if (type)
               {
                  func->ret.value.type = type;
                  if (type == EOLIAN_DEBUG_STRING)
                    {
                       len = strlen(buffer) + 1;
                       func->ret.value.value.value = (uint64_t) eina_stringshare_add(buffer);
                       buffer += len;
                       size -= len;
                    }
                  else
                    {
                       EXTRACT(buffer, &(func->ret.value.value), param_types[type].size);
                       size -= param_types[type].size;
                    }
               }
             else goto error;
          }
        eina_iterator_free(itr);
     }
   return ret;

error:
   eolian_debug_object_information_free(ret);
   return NULL;
}

static const Eina_Debug_Opcode _debug_ops[] =
{
     {"Eolian/object/info_get", &_obj_info_op, &_obj_info_req_cb},
     {NULL, NULL, NULL}
};


EAPI Eina_Bool
eolian_debug_init(void)
{
   eina_init();
   eolian_init();

   eolian_system_directory_scan();

   eina_debug_opcodes_register(NULL, _debug_ops, NULL, NULL);

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

EAPI Eina_Bool
eolian_debug_shutdown(void)
{
   eolian_shutdown();
   eina_shutdown();

   return EINA_TRUE;
}