summaryrefslogtreecommitdiff
path: root/src/bin/evas/evas_cserve2_debug.c
blob: 7f879ddb79d231f20325135e7d5a5bcd29fc9ec5 (plain) (blame)
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
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <fcntl.h>

#include <Eina.h>

#include "evas_cs2.h"

static int socketfd = -1;
static unsigned int _rid_count = 1;
static int _evas_cserve2_debug_log_dom = -1;

#ifndef UNIX_PATH_MAX
#define UNIX_PATH_MAX sizeof(((struct sockaddr_un *)NULL)->sun_path)
#endif

#ifdef ERR
#undef ERR
#endif
#define ERR(...) EINA_LOG_DOM_ERR(_evas_cserve2_debug_log_dom, __VA_ARGS__)
#ifdef DBG
#undef DBG
#endif
#define DBG(...) EINA_LOG_DOM_DBG(_evas_cserve2_debug_log_dom, __VA_ARGS__)
#ifdef WRN
#undef WRN
#endif
#define WRN(...) EINA_LOG_DOM_WARN(_evas_cserve2_debug_log_dom, __VA_ARGS__)
#ifdef INF
#undef INF
#endif
#define INF(...) EINA_LOG_DOM_INFO(_evas_cserve2_debug_log_dom, __VA_ARGS__)

static void
_socket_path_set(char *path)
{
   char *env;
   char buf[UNIX_PATH_MAX];

   env = getenv("EVAS_CSERVE2_SOCKET");
   if (env && env[0])
     {
        eina_strlcpy(path, env, UNIX_PATH_MAX);
        return;
     }

   snprintf(buf, sizeof(buf), "/tmp/.evas-cserve2-%x.socket", (int)getuid());
   /* FIXME: check we can actually create this socket */
   strcpy(path, buf);
}

static Eina_Bool
_server_connect(void)
{
   int s, len;
   struct sockaddr_un remote;

   if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
     {
        ERR("socket");
        return EINA_FALSE;
     }

   remote.sun_family = AF_UNIX;
   _socket_path_set(remote.sun_path);
   len = strlen(remote.sun_path) + sizeof(remote.sun_family);
   if (connect(s, (struct sockaddr *)&remote, len) == -1)
     {
        close(s);
        ERR("connect");
        return EINA_FALSE;
     }

   if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) ERR("fcntl failed");

   socketfd = s;

   DBG("connected to cserve2 server.");
   return EINA_TRUE;
}

static void
_server_disconnect(void)
{
   close(socketfd);
   socketfd = -1;
}

static Eina_Bool
_server_send(const void *data, int size)
{
   int sent = 0;
   ssize_t ret;
   const char *msg = data;

   while (sent < size)
     {
        ret = send(socketfd, msg + sent, size - sent, MSG_NOSIGNAL);
        if (ret < 0)
          {
             if ((errno == EAGAIN) || (errno == EINTR))
               continue;
             return EINA_FALSE;
          }
        sent += ret;
     }

   return EINA_TRUE;
}

static int sr_size = 0;
static int sr_got = 0;
static char *sr_buf = NULL;

static void *
_server_read(int *size)
{
   int n;
   void *ret;

   if (sr_size)
     goto get_data;

   n = recv(socketfd, &sr_size, sizeof(sr_size), 0);
   if (n < 0)
     return NULL;

   sr_buf = malloc(sr_size);

get_data:
   n = recv(socketfd, sr_buf + sr_got, sr_size - sr_got, 0);
   if (n < 0)
     return NULL;

   sr_got += n;
   if (sr_got < sr_size)
     return NULL;

   *size = sr_size;
   sr_size = 0;
   sr_got = 0;
   ret = sr_buf;
   sr_buf = NULL;

   return ret;
}

static void
_debug_msg_send(void)
{
   Msg_Base msg;
   int size;

   memset(&msg, 0, sizeof(msg));
   msg.type = CSERVE2_FONT_DEBUG;
   msg.rid = _rid_count++;

   size = sizeof(msg);

   if (!_server_send(&size, sizeof(size)))
     {
        ERR("Could not send usage msg size to server.");
        return;
     }
   if (!_server_send(&msg, size))
     {
        ERR("Could not send usage msg body to server.");
        return;
     }
}

typedef struct _Font_Entry Font_Entry;

struct _Font_Entry
{
   const char *file;
   const char *name;
   unsigned int rend_flags;
   unsigned int size;
   unsigned int dpi;
   unsigned int unused;
   char glyph_data_shm[64];
   char glyph_mempool_shm[64];
   Eina_List *glyphs; // Glyph_Data
};

#define READIT(_dst, _src) \
   do { \
      memcpy(&_dst, _src, sizeof(_dst)); \
      _src += sizeof(_dst); \
   } while(0)

static Font_Entry *
_parse_font_entry(char **msg)
{
   Font_Entry *fe;
   char *buf = *msg;
   unsigned int n;

   fe = calloc(1, sizeof(*fe));

   READIT(n, buf);
   if (n)
     fe->file = eina_stringshare_add_length(buf, n);
   buf += n;
   READIT(n, buf);
   if (n)
     fe->name = eina_stringshare_add_length(buf, n);
   buf += n;

   READIT(fe->rend_flags, buf);
   READIT(fe->size, buf);
   READIT(fe->dpi, buf);
   READIT(fe->unused, buf);

   eina_strlcpy(fe->glyph_data_shm, buf, 64);
   buf += 64;
   eina_strlcpy(fe->glyph_mempool_shm, buf, 64);
   buf += 64;

   READIT(n, buf);
   while (n--)
     {
        Glyph_Data *gd = calloc(1, sizeof(Glyph_Data));
        memcpy(gd, buf, sizeof(Glyph_Data));
        buf += sizeof(Glyph_Data);
        fe->glyphs = eina_list_append(fe->glyphs, gd);
     }

   *msg = buf;

   return fe;
}

static Eina_List *
_debug_msg_read(void)
{
   Msg_Font_Debug *msg = NULL;
   char *buf;
   int size;
   unsigned int nfonts;
   Eina_List *fonts = NULL;

   printf("Requesting server debug info.\n\n");
   while (!msg)
     msg = _server_read(&size);

   if (msg->base.type != CSERVE2_FONT_DEBUG)
     {
        ERR("Invalid message received from server. "
            "Something went badly wrong.");
        return NULL;
     }

   buf = (char *)msg + sizeof(*msg);

   nfonts = msg->nfonts;
   while (nfonts--)
     {
        Font_Entry *fe;
        fe = _parse_font_entry(&buf);
        fonts = eina_list_append(fonts, fe);
     }

   printf("Font index table: %s\n", msg->fonts_index_path);
   printf("Contains %u fonts\n\n", msg->nfonts);
   return fonts;
}

static void
_font_entry_free(Font_Entry *fe)
{
   Glyph_Data *gd;

   EINA_LIST_FREE(fe->glyphs, gd)
     free(gd);

   eina_stringshare_del(fe->name);
   eina_stringshare_del(fe->file);
   free(fe);
}

static void
_glyph_data_print(Glyph_Data *gd)
{
   const char *pxmode[] = {
        "FT_PIXEL_MODE_NONE",
        "FT_PIXEL_MODE_MONO",
        "FT_PIXEL_MODE_GRAY",
        "FT_PIXEL_MODE_GRAY2",
        "FT_PIXEL_MODE_GRAY4",
        "FT_PIXEL_MODE_LCD",
        "FT_PIXEL_MODE_LCD_V"
   };
   printf("  GLYPH id: %-4u refcount %-2u: index: %-6u offset: %-6u size: %-3u "
          "%2ux%-3u pitch: %-2u grays: %-3u pixel mode: %s hint: %d\n",
          gd->id, gd->refcount, gd->index, gd->offset, gd->size,
          gd->width, gd->rows, gd->pitch,
          gd->num_grays, pxmode[gd->pixel_mode], gd->hint);
}

static void
_font_entry_print(Font_Entry *fe)
{
   Eina_List *l;
   Glyph_Data *gd;

   printf("FONT %s:%s size: %u dpi: %u %s%s%s %s\n",
          fe->file, fe->name, fe->size, fe->dpi,
          fe->rend_flags == 0 ? "REGULAR " : "",
          fe->rend_flags & 1 ? "SLANT " : "",
          fe->rend_flags & 2 ? "WEIGHT" : "",
          fe->unused ? "(unused)" : "");
   printf("  Index:       %s\n"
          "  Mempool:     %s\n"
          "  Glyph count: %u\n",
          fe->glyph_data_shm, fe->glyph_mempool_shm,
          eina_list_count(fe->glyphs));

   EINA_LIST_FOREACH(fe->glyphs, l, gd)
     _glyph_data_print(gd);

   putchar('\n');
}

static void
_shared_index_print(Msg_Index_List *msg, size_t size)
{
   if (size < sizeof(*msg) || msg->base.type != CSERVE2_INDEX_LIST)
     {
        ERR("Invalid message received from server. "
            "Something went wrong.");
        return;
     }

   printf("Printing shared indexes status.\n");
   printf("===============================\n\n");
   printf("Generation ID:        %-4d\n", msg->generation_id);
   printf("Strings entries path: %s\n", msg->strings_entries_path);
   printf("Strings index path:   %s\n", msg->strings_index_path);
   printf("Files index path:     %s\n", msg->files_index_path);
   printf("Images index path:    %s\n", msg->images_index_path);
   printf("Fonts index path:     %s\n", msg->fonts_index_path);
   printf("\n\n\n");
}

int
main(void)
{
   Eina_List *fonts;
   Font_Entry *fe;
   Msg_Index_List *msg = NULL;
   int size;

   eina_init();
   _evas_cserve2_debug_log_dom = eina_log_domain_register
      ("evas_cserve2_debug", EINA_COLOR_BLUE);
   if (!_server_connect())
     {
        ERR("Could not connect to server.");
        return -1;
     }

   while (!msg)
     msg = _server_read(&size);
   _shared_index_print(msg, size);

   _debug_msg_send();
   fonts = _debug_msg_read();
   EINA_LIST_FREE(fonts, fe)
     {
        _font_entry_print(fe);
        _font_entry_free(fe);
     }
   _server_disconnect();
   eina_shutdown();
   return 0;
}