Support endianness

This commit is contained in:
Daniel Zaoui 2017-06-03 19:33:58 +03:00
parent 758bb0557c
commit ea50cbd2b3
4 changed files with 108 additions and 43 deletions

View File

@ -23,6 +23,16 @@
# include "config.h"
# 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
#define EXTRACT(_buf, pval, sz) \
{ \
memcpy(pval, _buf, sz); \
@ -65,7 +75,7 @@ _evlog_get_cb(Eina_Debug_Session *session EINA_UNUSED, int src EINA_UNUSED, void
unsigned int header[3];
header[0] = 0xffee211;
header[1] = blocksize;
header[1] = SWAP_32(blocksize);
header[2] = *overflow;
if (fwrite(header, 1, 12, _evlog_file) < 12 ||
fwrite(p, 1, blocksize, _evlog_file) < blocksize)
@ -113,7 +123,7 @@ _cid_get_cb(Eina_Debug_Session *session EINA_UNUSED, int cid EINA_UNUSED, void *
if ((!strcmp(op_str, "pon")) && (3 <= (my_argc - 1)))
{
int freq = atoi(my_argv[3]);
int freq = SWAP_32(atoi(my_argv[3]));
eina_debug_session_send(_session, _cid, _prof_on_opcode, &freq, sizeof(int));
}
else if (!strcmp(op_str, "poff"))
@ -155,6 +165,8 @@ _clients_info_added_cb(Eina_Debug_Session *session EINA_UNUSED, int src EINA_UNU
int cid, pid, len;
EXTRACT(buf, &cid, sizeof(int));
EXTRACT(buf, &pid, sizeof(int));
cid = SWAP_32(cid);
pid = SWAP_32(pid);
/* We dont need client notifications on evlog */
if(!_evlog_fetch_timer)
printf("Added: CID: %d - PID: %d - Name: %s\n", cid, pid, buf);
@ -173,6 +185,7 @@ _clients_info_deleted_cb(Eina_Debug_Session *session EINA_UNUSED, int src EINA_U
{
int cid;
EXTRACT(buf, &cid, sizeof(int));
cid = SWAP_32(cid);
size -= sizeof(int);
/* If client deleted dont send anymore evlog requests */

View File

@ -29,6 +29,16 @@
#include <Eina.h>
#include <Ecore.h>
#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
#define STORE(_buf, pval, sz) \
{ \
memcpy(_buf, pval, sz); \
@ -121,9 +131,9 @@ _send(Client *dest, int opcode, void *payload, int payload_size)
int size = sizeof(Eina_Debug_Packet_Header) + payload_size;
char *buf = alloca(size);
Eina_Debug_Packet_Header *hdr = (Eina_Debug_Packet_Header *)buf;
hdr->size = size;
hdr->size = SWAP_32(size);
hdr->cid = 0;
hdr->opcode = opcode;
hdr->opcode = SWAP_32(opcode);
memcpy(buf + sizeof(Eina_Debug_Packet_Header), payload, payload_size);
printf("Send packet (size = %d, opcode %s) to %s\n", size,
_opcodes[hdr->opcode]->opcode_string,
@ -145,7 +155,8 @@ _client_del(Client *c)
EINA_LIST_FOREACH(_clients, itr, c2)
{
if (c2->cl_stat_obs) _send(c2, _slave_deleted_opcode, &c->cid, sizeof(int));
int cid = SWAP_32(c->cid);
if (c2->cl_stat_obs) _send(c2, _slave_deleted_opcode, &cid, sizeof(int));
}
free(c);
}
@ -162,7 +173,7 @@ _dispatch(Client *src, void *buffer, unsigned int size)
{
if (dest->is_master != src->is_master)
{
hdr->cid = src->cid;
hdr->cid = SWAP_32(src->cid);
if (send(dest->fd, buffer, size, 0) != size) perror("send");
printf("Transfer of %d bytes from %s(%d) to %s(%d): operation %s\n",
hdr->size,
@ -222,12 +233,16 @@ _hello_cb(Client *c, void *buffer, int size)
{
Eina_List *itr;
char *buf = (char *)buffer, *tmp;
int version, pid, cid;
EXTRACT(buf, &c->version, 4);
EXTRACT(buf, &c->pid, 4);
EXTRACT(buf, &version, 4);
EXTRACT(buf, &pid, 4);
c->version = SWAP_32(version);
c->pid = SWAP_32(pid);
size -= 8;
c->cid = _free_cid++;
cid = SWAP_32(c->cid);
if (size > 1)
{
c->app_name = eina_stringshare_add_length(buf, size);
@ -242,8 +257,8 @@ _hello_cb(Client *c, void *buffer, int size)
size = 2 * sizeof(int) + (c->app_name ? strlen(c->app_name) : 0) + 1; /* cid + pid + name + \0 */
buf = alloca(size);
tmp = buf;
STORE(tmp, &c->cid, sizeof(int));
STORE(tmp, &c->pid, sizeof(int));
STORE(tmp, &cid, sizeof(int));
STORE(tmp, &pid, sizeof(int));
if (c->app_name)
{
STORE(tmp, c->app_name, strlen(c->app_name) + 1);
@ -263,9 +278,9 @@ _hello_cb(Client *c, void *buffer, int size)
static Eina_Bool
_cid_get_cb(Client *src, void *buffer, int size EINA_UNUSED)
{
int pid = *(int *)buffer;
int pid = SWAP_32(*(int *)buffer);
Client *c = _client_find_by_pid(pid);
int cid = c ? c->cid : 0;
int cid = c ? SWAP_32(c->cid) : 0;
_send(src, _cid_from_pid_opcode, &cid, sizeof(int));
return EINA_TRUE;
}
@ -292,12 +307,15 @@ _cl_stat_obs_register_cb(Client *src, void *buffer, int size)
EINA_LIST_FOREACH(_clients, itr, c)
{
char *tmp;
int cid, pid;
if (c->is_master) continue;
size = 2 * sizeof(int) + (c->app_name ? strlen(c->app_name) : 0) + 1;
buffer = alloca(size);
tmp = buffer;
STORE(tmp, &c->cid, sizeof(int));
STORE(tmp, &c->pid, sizeof(int));
cid = SWAP_32(c->cid);
pid = SWAP_32(c->pid);
STORE(tmp, &cid, sizeof(int));
STORE(tmp, &pid, sizeof(int));
if (c->app_name)
{
STORE(tmp, c->app_name, strlen(c->app_name) + 1);
@ -327,7 +345,7 @@ _opcode_register_cb(Client *src, void *buffer, int size)
while (ops_size > 0)
{
int len = strlen(ops_buf) + 1;
*opcodes++ = _opcode_register(ops_buf, EINA_DEBUG_OPCODE_INVALID, NULL);
*opcodes++ = SWAP_32(_opcode_register(ops_buf, EINA_DEBUG_OPCODE_INVALID, NULL));
ops_buf += len;
ops_size -= len;
}
@ -350,7 +368,9 @@ _data_receive(Client *c, unsigned char *buffer)
if (rret == -1 || !size) goto error;
if (rret == sizeof(int))
{
Eina_Debug_Packet_Header *hdr;
unsigned int cur_packet_size = 0;
size = SWAP_32(size);
if (size > EINA_DEBUG_MAX_PACKET_SIZE) goto error;
while (cur_packet_size < size)
{
@ -358,6 +378,10 @@ _data_receive(Client *c, unsigned char *buffer)
if (rret <= 0) goto error;
cur_packet_size += rret;
}
hdr = (Eina_Debug_Packet_Header *) buffer;
hdr->size = SWAP_32(hdr->size);
hdr->opcode = SWAP_32(hdr->opcode);
hdr->cid = SWAP_32(hdr->cid);
}
//printf("%d bytes received from client %s fd %d\n", size, c->app_name, c->fd);
return size;
@ -536,6 +560,7 @@ end:
free(socket_path);
return fd;
}
#endif
static int
_listening_tcp_socket_create()
@ -569,17 +594,19 @@ err:
if (fd >= 0) close(fd);
return -1;
}
#endif
static Eina_Bool
_server_launch()
{
#ifndef _WIN32
struct epoll_event event = {0};
_epfd = epoll_create (MAX_EVENTS);
#ifndef _WIN32
_listening_unix_fd = _listening_unix_socket_create();
#else
_listening_unix_fd = -1;
#endif
if (_listening_unix_fd <= 0) goto err;
event.data.fd = _listening_unix_fd;
event.events = EPOLLIN;
@ -596,7 +623,6 @@ err:
_listening_unix_fd = -1;
if (_listening_tcp_fd >= 0) close(_listening_tcp_fd);
_listening_tcp_fd = -1;
#endif
return EINA_FALSE;
}

View File

@ -56,6 +56,16 @@
# 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
@ -108,11 +118,11 @@ eina_debug_session_send(Eina_Debug_Session *session, int dest, int op, void *dat
if (!session) return -1;
if (op == EINA_DEBUG_OPCODE_INVALID) return -1;
/* Preparation of the packet header */
hdr.size = size + sizeof(Eina_Debug_Packet_Header);
hdr.opcode = op;
hdr.cid = dest;
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, hdr.size);
session->fd, op, size + sizeof(*hdr));
#ifndef _WIN32
eina_spinlock_take(&_eina_debug_lock);
/* Sending header */
@ -121,7 +131,7 @@ eina_debug_session_send(Eina_Debug_Session *session, int dest, int op, void *dat
if (size) write(session->fd, data, size);
eina_spinlock_release(&_eina_debug_lock);
#endif
return hdr.size;
return size;
}
static void
@ -137,8 +147,9 @@ _daemon_greet(Eina_Debug_Session *session)
#endif
int size = 8 + (app_name ? strlen(app_name) : 0) + 1;
unsigned char *buf = alloca(size);
int version = 1; // version of protocol we speak
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)
@ -160,6 +171,7 @@ _packet_receive(Eina_Debug_Session *session, unsigned char **buffer)
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);
@ -268,6 +280,7 @@ _callbacks_register_cb(Eina_Debug_Session *session, int src_id EINA_UNUSED, void
uint64_t info_64;
memcpy(&info_64, buffer, sizeof(uint64_t));
info_64 = SWAP_64(info_64);
info = (_opcode_reply_info *)info_64;
if (!info) return EINA_FALSE;
@ -280,9 +293,10 @@ _callbacks_register_cb(Eina_Debug_Session *session, int src_id EINA_UNUSED, void
for (i = 0; i < count; i++)
{
if (info->ops[i].opcode_id) *(info->ops[i].opcode_id) = os[i];
_static_opcode_register(session, os[i], info->ops[i].cb);
e_debug("Opcode %s -> %d", info->ops[i].opcode_name, os[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;
@ -310,6 +324,7 @@ _opcodes_registration_send(Eina_Debug_Session *session,
buf = malloc(size);
uint64_t info_64 = (uint64_t)info;
info_64 = SWAP_64(info_64);
memcpy(buf, &info_64, sizeof(uint64_t));
int size_curr = sizeof(uint64_t);
@ -393,7 +408,6 @@ _session_create(int fd)
EAPI Eina_Debug_Session *
eina_debug_remote_connect(int port)
{
#ifndef _WIN32
int fd;
struct sockaddr_in server;
@ -418,9 +432,6 @@ err:
// some kind of connection failure here, so close a valid socket and
// get out of here
if (fd >= 0) close(fd);
#else
(void) port;
#endif
return NULL;
}
@ -477,7 +488,6 @@ err:
static void *
_monitor(void *_data)
{
#ifndef _WIN32
Eina_Debug_Session *session = _data;
_daemon_greet(session);
@ -505,6 +515,9 @@ _monitor(void *_data)
// 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
@ -522,7 +535,6 @@ _monitor(void *_data)
session = NULL;
}
}
#endif
return NULL;
}
@ -588,7 +600,7 @@ eina_debug_opcodes_register(Eina_Debug_Session *session, const Eina_Debug_Opcode
EAPI Eina_Bool
eina_debug_dispatch(Eina_Debug_Session *session, void *buffer)
{
Eina_Debug_Packet_Header *hdr = buffer;
Eina_Debug_Packet_Header *hdr = buffer;
Eina_List *itr;
int opcode = hdr->opcode;
Eina_Debug_Cb cb = NULL;

View File

@ -39,6 +39,18 @@
# include <sys/mman.h>
# endif
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define SWAP_64(x) x
#define SWAP_32(x) x
#define SWAP_16(x) x
#define SWAP_DBL(x) x
#else
#define SWAP_64(x) eina_swap64(x)
#define SWAP_32(x) eina_swap32(x)
#define SWAP_16(x) eina_swap16(x)
#define SWAP_DBL(x) SWAP_64(x)
#endif
# define EVLOG_BUF_SIZE (4 * (1024 * 1024))
static Eina_Spinlock _evlog_lock;
@ -149,13 +161,13 @@ eina_evlog(const char *event, void *obj, double srctime, const char *detail)
eina_spinlock_take(&_evlog_lock);
strings = push_buf(buf, size);
item = (Eina_Evlog_Item *)strings;
item->tim = now;
item->srctim = srctime;
item->thread = (unsigned long long)(uintptr_t)pthread_self();
item->obj = (unsigned long long)(uintptr_t)obj;
item->event_offset = sizeof(Eina_Evlog_Item);
item->detail_offset = detail_offset;
item->event_next = size;
item->tim = SWAP_DBL(now);
item->srctim = SWAP_DBL(srctime);
item->thread = SWAP_64((unsigned long long)(uintptr_t)pthread_self());
item->obj = SWAP_64((unsigned long long)(uintptr_t)obj);
item->event_offset = SWAP_16(sizeof(Eina_Evlog_Item));
item->detail_offset = SWAP_16(detail_offset);
item->event_next = SWAP_16(size);
strcpy(strings + sizeof(Eina_Evlog_Item), event);
if (detail_offset > 0) strcpy(strings + detail_offset, detail);
eina_spinlock_release(&_evlog_lock);
@ -222,13 +234,15 @@ _get_cb(Eina_Debug_Session *session EINA_UNUSED, int cid EINA_UNUSED, void *buff
if ((evlog) && (evlog->buf))
{
int ovf = SWAP_32(evlog->overflow);
resp_size = evlog->top + sizeof(evlog->overflow);
resp_buf = alloca(resp_size);
memcpy(resp_buf, &(evlog->overflow), sizeof(evlog->overflow));
resp_buf = malloc(resp_size);
memcpy(resp_buf, &ovf, sizeof(ovf));
memcpy(resp_buf + sizeof(evlog->overflow), evlog->buf, evlog->top);
}
printf("send evlog size %d\n", resp_size);
eina_debug_session_send(session, cid, _evlog_get_opcode, resp_buf, resp_size);
free(resp_buf);
return EINA_TRUE;
}