summaryrefslogtreecommitdiff
path: root/src/lib/eina/eina_debug.c
blob: 9dd1a092d52d03bc4182762d7652e88ffea25b78 (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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/* EINA - EFL data type library
 * Copyright (C) 2015 Carsten Haitzler
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

# ifndef _GNU_SOURCE
#  define _GNU_SOURCE 1
# endif

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#ifdef HAVE_SYS_EPOLL_H
# include <sys/epoll.h>
#endif
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_SYS_UN_H
# include <sys/un.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#include <fcntl.h>

#ifdef _WIN32
# include <ws2tcpip.h>
# include <Evil.h>
#endif

#include "eina_alloca.h"
#include "eina_debug.h"
#include "eina_cpu.h"
#include "eina_types.h"
#include "eina_list.h"
#include "eina_mempool.h"
#include "eina_util.h"
#include "eina_evlog.h"
#include "eina_hash.h"
#include "eina_stringshare.h"
#include "eina_debug_private.h"

#ifdef EINA_HAVE_PTHREAD_SETNAME
# ifndef __linux__
# include <pthread_np.h>
# endif
#endif

#if defined(__CYGWIN__) || defined (_WIN32)
# define LIBEXT ".dll"
#else
# define LIBEXT ".so"
#endif

#if __BYTE_ORDER == __LITTLE_ENDIAN
#define SWAP_64(x) x
#define SWAP_32(x) x
#define SWAP_16(x) x
#else
#define SWAP_64(x) eina_swap64(x)
#define SWAP_32(x) eina_swap32(x)
#define SWAP_16(x) eina_swap16(x)
#endif

// yes - a global debug spinlock. i expect contention to be low for now, and
// when needed we can split this up into mroe locks to reduce contention when
// and if that day comes
Eina_Spinlock _eina_debug_lock;

// only init once
static Eina_Bool _inited = EINA_FALSE;

#ifdef __linux__
extern char *__progname;
#endif

extern Eina_Bool eina_module_init(void);
extern Eina_Bool eina_mempool_init(void);
extern Eina_Bool eina_list_init(void);

extern Eina_Spinlock _eina_debug_thread_lock;

static Eina_Bool _debug_disabled = EINA_FALSE;

/* Local session */
/* __thread here to allow debuggers to be master and slave by using two different threads */
static Eina_Debug_Session *_last_local_session = NULL;

typedef struct
{
   const Eina_Debug_Opcode *ops;
   Eina_Debug_Opcode_Status_Cb status_cb;
   void *status_data;
   Eina_Bool sent : 1;
} _opcode_reply_info;

struct _Eina_Debug_Session
{
   Eina_List **cbs; /* Table of callbacks lists indexed by opcode id */
   Eina_List *opcode_reply_infos;
   Eina_Debug_Dispatch_Cb dispatch_cb; /* Session dispatcher */
   void *data; /* User data */
   int cbs_length; /* cbs table size */
   int fd; /* File descriptor */
};

#ifndef _WIN32
static void _opcodes_register_all(Eina_Debug_Session *session);
#endif
static void _thread_start(Eina_Debug_Session *session);

EAPI int
eina_debug_session_send(Eina_Debug_Session *session, int dest, int op, void *data, int size)
{
   Eina_Debug_Packet_Header hdr;

   if (!session) return -1;
   if (op == EINA_DEBUG_OPCODE_INVALID) return -1;
   /* Preparation of the packet header */
   hdr.size = SWAP_32(size + sizeof(Eina_Debug_Packet_Header));
   hdr.opcode = SWAP_32(op);
   hdr.cid = SWAP_32(dest);
   e_debug("socket: %d / opcode %X / bytes to send: %d",
         session->fd, op, size + sizeof(*hdr));
#ifndef _WIN32
   eina_spinlock_take(&_eina_debug_lock);
   /* Sending header */
   if (write(session->fd, &hdr, sizeof(hdr)) != sizeof(hdr)) goto err;
   /* Sending payload */
   if (size)
     {
        if (write(session->fd, data, size) != size) goto err;
     }
   eina_spinlock_release(&_eina_debug_lock);
#else
   (void)data;
#endif
   return size;
#ifndef _WIN32
err:
   eina_spinlock_release(&_eina_debug_lock);
   e_debug("Cannot write to eina debug session");
   return 0;
#endif
}

#ifndef _WIN32
static void
_daemon_greet(Eina_Debug_Session *session)
{
   /* say hello to our debug daemon - tell them our PID and protocol
      version we speak */
   /* Version + Pid + App name */
#ifdef __linux__
   char *app_name = __progname;
#else
   char *app_name = NULL;
#endif
   int size = 8 + (app_name ? strlen(app_name) : 0) + 1;
   unsigned char *buf = alloca(size);
   int version = SWAP_32(1); // version of protocol we speak
   int pid = getpid();
   pid = SWAP_32(pid);
   memcpy(buf + 0, &version, 4);
   memcpy(buf + 4, &pid, 4);
   if (app_name)
      memcpy(buf + 8, app_name, strlen(app_name) + 1);
   else
      buf[8] = '\0';
   eina_debug_session_send(session, 0, EINA_DEBUG_OPCODE_HELLO, buf, size);
}

static int
_packet_receive(Eina_Debug_Session *session, unsigned char **buffer)
{
   unsigned char *packet_buf = NULL;
   int rret = -1;
   unsigned int size = 0;

   if (!session) goto end;

   if ((rret = read(session->fd, &size, 4)) == 4)
     {
        size = SWAP_32(size);
        if (size > EINA_DEBUG_MAX_PACKET_SIZE)
          {
             e_debug("Packet too big: %d. The maximum allowed is %d", size, EINA_DEBUG_MAX_PACKET_SIZE);
             rret = -1;
             goto end;
          }
        e_debug("Begin to receive a packet of %d bytes", size);
        // allocate a buffer for the next bytes to receive
        packet_buf = malloc(size);
        if (packet_buf)
          {
             unsigned int cur_packet_size = 4;
             memcpy(packet_buf, &size, 4);
             /* Receive all the remaining packet bytes */
             while (cur_packet_size < size)
               {
                  rret = read(session->fd, packet_buf + cur_packet_size, size - cur_packet_size);
                  if (rret <= 0)
                    {
                       e_debug("Error on read: %d", rret);
                       perror("Read");
                       goto end;
                    }
                  cur_packet_size += rret;
               }
             *buffer = packet_buf;
             rret = cur_packet_size;
             e_debug("Received a packet of %d bytes", cur_packet_size);
          }
        else
          {
             // we couldn't allocate memory for payload buffer
             // internal memory limit error
             e_debug("Cannot allocate %u bytes for op", size);
             goto end;
          }
     }
   else
     {
        e_debug("Invalid size read %i != %i", rret, size_sz);
        goto end;
     }
end:
   if (rret <= 0 && packet_buf) free(packet_buf);
   return rret;
}
#endif

EAPI void
eina_debug_disable()
{
   _debug_disabled = EINA_TRUE;
}

EAPI void
eina_debug_session_terminate(Eina_Debug_Session *session)
{
   /* Close fd here so the thread terminates its own session by itself */
   if (!session) return;
   close(session->fd);
}

EAPI void
eina_debug_session_dispatch_override(Eina_Debug_Session *session, Eina_Debug_Dispatch_Cb disp_cb)
{
   if (!session) return;
   if (!disp_cb) disp_cb = eina_debug_dispatch;
   session->dispatch_cb = disp_cb;
}

EAPI Eina_Debug_Dispatch_Cb
eina_debug_session_dispatch_get(Eina_Debug_Session *session)
{
   if (session) return session->dispatch_cb;
   else return NULL;
}

#ifndef _WIN32
static void
_static_opcode_register(Eina_Debug_Session *session,
      int op_id, Eina_Debug_Cb cb)
{
   if(session->cbs_length < op_id + 1)
     {
        int i = session->cbs_length;
        session->cbs_length = op_id + 16;
        session->cbs = realloc(session->cbs, session->cbs_length * sizeof(Eina_List *));
        for(; i < session->cbs_length; i++) session->cbs[i] = NULL;
     }
   if (cb)
     {
        session->cbs[op_id] = eina_list_append(session->cbs[op_id], cb);
     }
}

/*
 * Response of the daemon containing the ids of the requested opcodes.
 * PTR64 + (opcode id)*
 */
static Eina_Bool
_callbacks_register_cb(Eina_Debug_Session *session, int src_id EINA_UNUSED, void *buffer, int size)
{
   _opcode_reply_info *info = NULL, *info2;
   Eina_List *itr;
   int *os;
   unsigned int count, i;

   uint64_t info_64;
   memcpy(&info_64, buffer, sizeof(uint64_t));
   // cast to a ptr, so on 32bit we just take the lower 32bits and on 64
   // we take all of it so we're fine
   info = (_opcode_reply_info *)info_64;

   if (!info) return EINA_FALSE;
   EINA_LIST_FOREACH(session->opcode_reply_infos, itr, info2)
     {
        if (info2 == info)
          {
             os = (int *)((unsigned char *)buffer + sizeof(uint64_t));
             count = (size - sizeof(uint64_t)) / sizeof(int);

             for (i = 0; i < count; i++)
               {
                  int op = SWAP_32(os[i]);
                  if (info->ops[i].opcode_id) *(info->ops[i].opcode_id) = op;
                  _static_opcode_register(session, op, info->ops[i].cb);
                  e_debug("Opcode %s -> %d", info->ops[i].opcode_name, op);
               }
             if (info->status_cb) info->status_cb(info->status_data, EINA_TRUE);
             return EINA_TRUE;
          }
     }

   return EINA_FALSE;
}
#endif

static void
_opcodes_registration_send(Eina_Debug_Session *session,
      _opcode_reply_info *info)
{
   unsigned char *buf;

   int count = 0;
   int size = sizeof(uint64_t);
   Eina_Bool already_sent;

   eina_spinlock_take(&_eina_debug_lock);
   already_sent = info->sent;
   info->sent = EINA_TRUE;
   eina_spinlock_release(&_eina_debug_lock);
   if (already_sent) return;

   while (info->ops[count].opcode_name)
     {
        size += strlen(info->ops[count].opcode_name) + 1;
        count++;
     }

   buf = malloc(size);

   // cast to a ptr, so on 32bit we just pad out the upper 32bits with 0
   // and on 64bit we are fine as we use all of it
   uint64_t info_64 = (uint64_t)(uintptr_t)info;
   memcpy(buf, &info_64, sizeof(uint64_t));
   int size_curr = sizeof(uint64_t);

   count = 0;
   while (info->ops[count].opcode_name)
     {
        int len = strlen(info->ops[count].opcode_name) + 1;
        memcpy(buf + size_curr, info->ops[count].opcode_name, len);
        size_curr += len;
        count++;
     }

   eina_debug_session_send(session, 0, EINA_DEBUG_OPCODE_REGISTER, buf, size);
   free(buf);
}

#ifndef _WIN32
static void
_opcodes_register_all(Eina_Debug_Session *session)
{
   Eina_List *l;
   _opcode_reply_info *info = NULL;

   _static_opcode_register(session,
         EINA_DEBUG_OPCODE_REGISTER, _callbacks_register_cb);
   EINA_LIST_FOREACH(session->opcode_reply_infos, l, info)
        _opcodes_registration_send(session, info);;
}

static void
_opcodes_unregister_all(Eina_Debug_Session *session)
{
   Eina_List *l;
   int i;
   _opcode_reply_info *info = NULL;

   if (!session) return;
   for (i = 0; i < session->cbs_length; i++)
      eina_list_free(session->cbs[i]);
   free(session->cbs);
   session->cbs_length = 0;
   session->cbs = NULL;

   EINA_LIST_FOREACH(session->opcode_reply_infos, l, info)
     {
        const Eina_Debug_Opcode *op = info->ops;
        while(!op->opcode_name)
          {
             if (op->opcode_id) *(op->opcode_id) = EINA_DEBUG_OPCODE_INVALID;
             op++;
          }
        if (info->status_cb) info->status_cb(info->status_data, EINA_FALSE);
     }
}

static const char *
_socket_home_get()
{
   // get possible debug daemon socket directory base
   const char *dir = getenv("XDG_RUNTIME_DIR");
   if (!dir) dir = eina_environment_home_get();
   if (!dir) dir = eina_environment_tmp_get();
   return dir;
}

#define LENGTH_OF_SOCKADDR_UN(s) \
   (strlen((s)->sun_path) + (size_t)(((struct sockaddr_un *)NULL)->sun_path))
#endif

static Eina_Debug_Session *
_session_create(int fd)
{
   Eina_Debug_Session *session = calloc(1, sizeof(*session));
   if (!session) return NULL;
   session->dispatch_cb = eina_debug_dispatch;
   session->fd = fd;
   // start the monitor thread
   _thread_start(session);
   return session;
}

EAPI Eina_Debug_Session *
eina_debug_remote_connect(int port)
{
   int fd;
   struct sockaddr_in server;

   //Create socket
   fd = socket(AF_INET, SOCK_STREAM, 0);
   if (fd < 0) goto err;
   // set the socket to close when we exec things so they don't inherit it
   if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) goto err;

   //Prepare the sockaddr_in structure
   server.sin_family = AF_INET;
   if (inet_pton(AF_INET, "127.0.0.1", &server.sin_addr.s_addr) != 1) goto err;
   server.sin_port = htons(port);

   if (connect(fd, (struct sockaddr *)&server, sizeof(server)) < 0)
    {
        perror("connect failed. Error");
        goto err;
    }
   return _session_create(fd);
err:
   // some kind of connection failure here, so close a valid socket and
   // get out of here
   if (fd >= 0) close(fd);
   return NULL;
}

EAPI Eina_Debug_Session *
eina_debug_local_connect(Eina_Bool is_master)
{
#ifndef _WIN32
   char buf[4096];
   int fd, socket_unix_len, curstate = 0;
   struct sockaddr_un socket_unix;

   if (is_master) return eina_debug_remote_connect(REMOTE_SERVER_PORT);

   // try this socket file - it will likely be:
   //   ~/.ecore/efl_debug/0
   // or maybe
   //   /var/run/UID/.ecore/efl_debug/0
   snprintf(buf, sizeof(buf), "%s/%s/%s/%i", _socket_home_get(),
         LOCAL_SERVER_PATH, LOCAL_SERVER_NAME, LOCAL_SERVER_PORT);
   // create the socket
   fd = socket(AF_UNIX, SOCK_STREAM, 0);
   if (fd < 0) goto err;
   // set the socket to close when we exec things so they don't inherit it
   if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) goto err;
   // set up some socket options on addr re-use
   if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&curstate,
                  sizeof(curstate)) < 0)
     goto err;
   // sa that it's a unix socket and where the path is
   memset(&socket_unix, 0, sizeof(socket_unix));
   socket_unix.sun_family = AF_UNIX;
   strncpy(socket_unix.sun_path, buf, sizeof(socket_unix.sun_path) - 1);
   socket_unix_len = LENGTH_OF_SOCKADDR_UN(&socket_unix);
   // actually connect to efl_debugd service
   if (connect(fd, (struct sockaddr *)&socket_unix, socket_unix_len) < 0)
      goto err;

   _last_local_session = _session_create(fd);

   return _last_local_session;
err:
   // some kind of connection failure here, so close a valid socket and
   // get out of here
   if (fd >= 0) close(fd);
#else
   (void) is_master;
#endif
   return NULL;
}

// this is a DEDICATED debug thread to monitor the application so it works
// even if the mainloop is blocked or the app otherwise deadlocked in some
// way. this is an alternative to using external debuggers so we can get
// users or developers to get useful information about an app at all times

#ifndef _WIN32
static void *
_monitor(void *_data)
{
   Eina_Debug_Session *session = _data;

   _daemon_greet(session);
   _opcodes_register_all(session);

   // set a name for this thread for system debugging
#ifdef EINA_HAVE_PTHREAD_SETNAME
# ifndef __linux__
   pthread_set_name_np
# else
   pthread_setname_np
# endif
     (pthread_self(), "Edbg-mon");
#endif

   // sit forever processing commands or timeouts in the debug monitor
   // thread - this is separate to the rest of the app so it shouldn't
   // impact the application specifically
   for (;session;)
     {
        unsigned char *buffer = NULL;
        int size;

        size = _packet_receive(session, &buffer);
        // if not negative - we have a real message
        if (size > 0)
          {
             Eina_Debug_Packet_Header *hdr = (Eina_Debug_Packet_Header *)buffer;
             hdr->cid = SWAP_32(hdr->cid);
             hdr->opcode = SWAP_32(hdr->opcode);
             if (EINA_TRUE != session->dispatch_cb(session, buffer))
               {
                  // something we don't understand
                  e_debug("EINA DEBUG ERROR: Unknown command");
               }
             /* Free the buffer only if the default dispatcher is used */
             if (session->dispatch_cb == eina_debug_dispatch)
                free(buffer);
          }
        else
          {
             close(session->fd);
             _opcodes_unregister_all(session);
             free(session);
             session = NULL;
          }
     }
   return NULL;
}
#endif

// start up the debug monitor if we haven't already
static void
_thread_start(Eina_Debug_Session *session)
{
#ifndef _WIN32
   pthread_t monitor_thread;
   int err;
   sigset_t oldset, newset;

   sigemptyset(&newset);
   sigaddset(&newset, SIGPIPE);
   sigaddset(&newset, SIGALRM);
   sigaddset(&newset, SIGCHLD);
   sigaddset(&newset, SIGUSR1);
   sigaddset(&newset, SIGUSR2);
   sigaddset(&newset, SIGHUP);
   sigaddset(&newset, SIGQUIT);
   sigaddset(&newset, SIGINT);
   sigaddset(&newset, SIGTERM);
#ifdef SIGPWR
   sigaddset(&newset, SIGPWR);
#endif
   pthread_sigmask(SIG_BLOCK, &newset, &oldset);

   err = pthread_create(&monitor_thread, NULL, _monitor, session);

   pthread_sigmask(SIG_SETMASK, &oldset, NULL);
   if (err != 0)
     {
        e_debug("EINA DEBUG ERROR: Can't create monitor debug thread!");
        abort();
     }
#else
   (void)session;
#endif
}

/*
 * Sends to daemon:
 * - Pointer to ops: returned in the response to determine which opcodes have been added
 * - List of opcode names separated by \0
 */
EAPI void
eina_debug_opcodes_register(Eina_Debug_Session *session, const Eina_Debug_Opcode ops[],
      Eina_Debug_Opcode_Status_Cb status_cb, void *data)
{
   if (!session) session = _last_local_session;
   if (!session) return;

   _opcode_reply_info *info = malloc(sizeof(*info));
   info->ops = ops;
   info->status_cb = status_cb;
   info->status_data = data;
   info->sent = EINA_FALSE;

   session->opcode_reply_infos = eina_list_append(
         session->opcode_reply_infos, info);

   /* Send only if session's fd connected.
    * Otherwise, it will be sent when connected */
   if(session && session->fd!= -1)
      _opcodes_registration_send(session, info);
}

EAPI Eina_Bool
eina_debug_dispatch(Eina_Debug_Session *session, void *buffer)
{
   Eina_Debug_Packet_Header *hdr = buffer;
   Eina_List *itr;
   int opcode = hdr->opcode;
   Eina_Debug_Cb cb = NULL;

   if (opcode >= session->cbs_length)
     {
        e_debug("Invalid opcode %d", opcode);
        return EINA_FALSE;
     }

   EINA_LIST_FOREACH(session->cbs[opcode], itr, cb)
     {
        if (!cb) continue;
        Eina_Bool ret = cb(session, hdr->cid,
              (unsigned char *)buffer + sizeof(*hdr),
              hdr->size - sizeof(*hdr));
        if (ret == EINA_FALSE) return ret;
     }
   return EINA_TRUE;
}

EAPI void
eina_debug_session_data_set(Eina_Debug_Session *session, void *data)
{
   if (session) session->data = data;
}

EAPI void *
eina_debug_session_data_get(Eina_Debug_Session *session)
{
   if (session) return session->data;
   else return NULL;
}

Eina_Bool
eina_debug_init(void)
{
   pthread_t self;

   // if already inbitted simply release our lock that we may have locked on
   // shutdown if we are re-initted again in the same process
   if (_inited)
     {
        eina_spinlock_release(&_eina_debug_thread_lock);
        return EINA_TRUE;
     }
   // mark as initted
   _inited = EINA_TRUE;
   eina_module_init();
   eina_mempool_init();
   eina_list_init();
   // For Windows support GetModuleFileName can be used
   // set up thread things
   eina_spinlock_new(&_eina_debug_lock);
   eina_spinlock_new(&_eina_debug_thread_lock);
   self = pthread_self();
   _eina_debug_thread_mainloop_set(&self);
   _eina_debug_thread_add(&self);
#if defined(HAVE_GETUID) && defined(HAVE_GETEUID)
   // if we are setuid - don't debug!
   if (getuid() != geteuid()) return EINA_TRUE;
#endif
   // if someone uses the EFL_NODEBUG env var or disabled debug - do not do
   // debugging. handy for when this debug code is buggy itself

   if (!getenv("EFL_NODEBUG") && !_debug_disabled)
     {
        eina_debug_local_connect(EINA_FALSE);
     }
   _eina_debug_cpu_init();
   _eina_debug_bt_init();
   _eina_debug_timer_init();
   return EINA_TRUE;
}

Eina_Bool
eina_debug_shutdown(void)
{
   _eina_debug_timer_shutdown();
   _eina_debug_bt_shutdown();
   _eina_debug_cpu_shutdown();
   eina_spinlock_take(&_eina_debug_thread_lock);
   // yes - we never free on shutdown - this is because the monitor thread
   // never exits. this is not a leak - we intend to never free up any
   // resources here because they are allocated once only ever.
   return EINA_TRUE;
}