Commit e82095a0 authored by Lubomir Bulej's avatar Lubomir Bulej

disl-agent: fixed compilation issues on Mac OS X and MinGW platforms.

disl-agent: various cleanups to the header files and imports.
parent 60a4a975
#include "common.h"
#include "classparser.h"
......@@ -6,15 +5,25 @@
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <endian.h>
//
#define CLASS_MAGIC 0xCAFEBABE
#ifdef MINGW
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#define __PACKED__ __attribute__ ((__packed__))
#endif
#define member_sizeof(type, member) sizeof(((type *)0)->member)
#define read_be16(value) ntohs(value)
#define read_be32(value) ntohl(value)
//
#define CLASS_MAGIC 0xCAFEBABE
struct java_class {
// Class bytecode.
......@@ -43,25 +52,25 @@ enum cp_tag {
};
struct __PACKED__ cp_info {
struct PACKED cp_info {
uint8_t tag;
union __PACKED__ {
struct __PACKED__ cp_info_utf8 {
union PACKED {
struct PACKED cp_info_utf8 {
uint16_t length;
uint8_t bytes [];
} utf8;
struct __PACKED__ cp_info_class {
struct PACKED cp_info_class {
uint16_t name_index;
} class;
struct __PACKED__ cp_info_method_handle {
struct PACKED cp_info_method_handle {
uint8_t reference_kind;
uint16_t reference_index;
} method_handle;
struct __PACKED__ cp_info_long_double {
struct PACKED cp_info_long_double {
uint32_t high_bytes;
uint32_t low_bytes;
} long_double;
......@@ -70,7 +79,7 @@ struct __PACKED__ cp_info {
//
struct __PACKED__ class_file_start {
struct PACKED class_file_start {
uint32_t magic;
uint16_t minor_version;
uint16_t major_version;
......@@ -78,7 +87,7 @@ struct __PACKED__ class_file_start {
struct cp_info constant_pool []; // [constant_pool_count - 1]
};
struct __PACKED__ class_file_access {
struct PACKED class_file_access {
uint16_t access_flags;
uint16_t this_class;
uint16_t super_class;
......@@ -104,38 +113,38 @@ __class_file_access (class_t java_class) {
static inline bool
__class_bytes_valid (const uint8_t * class_bytes) {
return be32toh (__class_file_start (class_bytes)->magic) == CLASS_MAGIC;
return read_be32 (__class_file_start (class_bytes)->magic) == CLASS_MAGIC;
}
static inline int
__cp_entry_count (const uint8_t * class_bytes) {
return be16toh (__class_file_start (class_bytes)->constant_pool_count);
return read_be16 (__class_file_start (class_bytes)->constant_pool_count);
}
//
static inline int
__class_this_class_index (class_t java_class) {
return be16toh (__class_file_access (java_class)->this_class);
return read_be16 (__class_file_access (java_class)->this_class);
}
static inline int
__class_super_class_index (class_t java_class) {
return be16toh (__class_file_access (java_class)->super_class);
return read_be16 (__class_file_access (java_class)->super_class);
}
static inline int
__class_interface_count (class_t java_class) {
return be16toh (__class_file_access (java_class)->interfaces_count);
return read_be16 (__class_file_access (java_class)->interfaces_count);
}
static inline int
__class_interface_index (class_t java_class, int index) {
return be16toh (__class_file_access (java_class)->interfaces [index]);
return read_be16 (__class_file_access (java_class)->interfaces [index]);
}
//
......@@ -173,7 +182,7 @@ __cp_scan_entries (const uint8_t * class_bytes, size_t * offsets) {
switch (entry->tag) {
case CP_TAG_UTF8: {
uint_fast16_t length = be16toh (entry->utf8.length);
uint_fast16_t length = read_be16 (entry->utf8.length);
offset += sizeof (struct cp_info_utf8) + length;
break;
}
......@@ -203,7 +212,7 @@ __cp_scan_entries (const uint8_t * class_bytes, size_t * offsets) {
}
// Account for the type tag.
offset += member_sizeof (struct cp_info, tag);
offset += sizeof_member (struct cp_info, tag);
}
return offset;
......@@ -215,7 +224,7 @@ __cp_scan_entries (const uint8_t * class_bytes, size_t * offsets) {
static inline struct java_class *
__class_alloc (const uint8_t * class_bytes) {
int const_count = 1 + __cp_entry_count (class_bytes);
int offsets_size = const_count * member_sizeof (struct java_class, cp_offsets[0]);
int offsets_size = const_count * sizeof_member (struct java_class, cp_offsets[0]);
return (struct java_class *) malloc (sizeof (struct java_class) + offsets_size);
}
......@@ -251,15 +260,7 @@ class_alloc (const uint8_t * class_bytes, size_t byte_count) {
//
int
class_cp_entry_count (class_t java_class) {
assert (java_class != NULL);
return __cp_entry_count (java_class->bytes);
}
utf8_t
static utf8_t
class_cp_get_utf8 (class_t java_class, int index, size_t * utf8_length) {
assert (java_class != NULL);
......@@ -267,7 +268,7 @@ class_cp_get_utf8 (class_t java_class, int index, size_t * utf8_length) {
if (entry != NULL) {
if (entry->tag == CP_TAG_UTF8) {
if (utf8_length != NULL) {
*utf8_length = be16toh (entry->utf8.length);
*utf8_length = read_be16 (entry->utf8.length);
}
return &entry->utf8.bytes [0];
......@@ -281,14 +282,14 @@ class_cp_get_utf8 (class_t java_class, int index, size_t * utf8_length) {
}
utf8_t
static utf8_t
class_cp_get_class_name (
class_t java_class, int class_index, size_t * utf8_length
) {
struct cp_info * entry = __class_cp_get_entry (java_class, class_index);
if (entry != NULL) {
if (entry->tag == CP_TAG_CLASS) {
int index = be16toh (entry->class.name_index);
int index = read_be16 (entry->class.name_index);
return class_cp_get_utf8 (java_class, index, utf8_length);
} else {
......@@ -303,7 +304,14 @@ class_cp_get_class_name (
static inline char *
__utf8dup (utf8_t bytes, size_t length) {
return (bytes != NULL) ? strndup ((const char *) bytes, length) : NULL;
size_t len = length + 1;
char * result = (char *) malloc (len);
if (result != NULL) {
strncpy (result, (const char *) bytes, length);
result [length] = '\0';
}
return result;
}
......
......@@ -13,10 +13,6 @@ typedef const uint8_t * utf8_t;
class_t class_alloc (const uint8_t * class_bytes, size_t byte_count);
void class_free (class_t java_class);
int class_cp_entry_count (class_t java_class);
utf8_t class_cp_get_utf8 (class_t java_class, int index, size_t * utf8_length);
utf8_t class_cp_get_class_name (class_t java_class, int class_index, size_t * utf8_length);
char * class_name (class_t java_class);
char * class_super_class_name (class_t java_class);
bool class_has_super_class (class_t java_class);
......
#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "common.h"
//
/**
* Returns the index of the given value in the given array of values.
......
#ifndef _COMMON_H_
#define _COMMON_H_
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
......@@ -15,12 +17,32 @@
/**
* Returns size of an array in array elements.
* Compiler attributes.
*/
#define PACKED __attribute__ ((__packed__))
#ifdef WHOLE
#define VISIBLE __attribute__ ((externally_visible))
#else
#define VISIBLE
#endif
/**
* Returns the size of an array in array elements.
*/
#define sizeof_array(array) \
(sizeof (array) / sizeof ((array) [0]))
/**
* Returns the size of a structure member.
*/
#define sizeof_member(type, member) \
(sizeof (((type *) 0)->member))
/**
* Prints a debug message to stdout, unless NDEBUG is defined.
*
......@@ -77,7 +99,7 @@ void die_with_std_error (const char * message, int errnum);
* Reports a general error and terminates the program if the provided
* error condition is true.
*/
inline static void
static inline void
check_error (bool error, const char * message) {
if (error) {
die_with_error (message);
......@@ -89,13 +111,25 @@ check_error (bool error, const char * message) {
* Reports a standard library error and terminates the program if the provided
* error condition is true.
*/
inline static void
static inline void
check_std_error (bool error, const char * message) {
if (error) {
die_with_std_error (message, errno);
}
}
/**
* Warns about a standard library error if the provided
* error condition is true.
*/
static inline void
warn_std_error (bool error, const char * message) {
if (error) {
warn (message, errno);
}
}
//
#ifdef MINGW
......@@ -107,7 +141,7 @@ void die_with_win_error (const char * message, DWORD errnum);
* Reports a windows error and terminates the program if the provided
* error condition is true.
*/
inline static void
static inline void
check_win_error (bool error, const char * message) {
if (error) {
die_with_win_error (message, GetLastError ());
......
#define _POSIX_C_SOURCE 200908L
#include "common.h"
#include "connection.h"
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
//
#ifdef MINGW
#include <winsock2.h>
#define setsockopt(a,b,c,d,e) setsockopt(a,b,c,(const void*)(d),e)
#else
#include <sys/uio.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <netdb.h>
#endif
#include <sys/types.h>
#include "common.h"
#include "connection.h"
//
static void
__connection_init (struct connection * connection, const int sockfd) {
......@@ -91,7 +86,7 @@ connection_close (struct connection * connection) {
typedef ssize_t (* xfer_fn) (int sockfd, void * buf, size_t len, int flags);
inline static ssize_t
static inline ssize_t
__socket_xfer (xfer_fn xfer, const int sockfd, const void * buf, const ssize_t len) {
unsigned char * buf_tail = (unsigned char *) buf;
size_t remaining = len;
......@@ -157,7 +152,7 @@ connection_recv (struct connection * connection, void * buf, const ssize_t len)
typedef ssize_t (* xfer_iov_fn) (int sockfd, struct iovec * iovs, int iov_count);
inline static ssize_t
static inline ssize_t
__socket_xfer_iov (
xfer_iov_fn xfer_iov, const int sockfd, struct iovec * iovs, const int iov_count
) {
......
#ifndef _CONNECTION_H_
#define _CONNECTION_H_
#include <sys/types.h>
//
#ifdef MINGW
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/uio.h>
#include <sys/socket.h>
#include <netdb.h>
#endif
#include <sys/types.h>
//
#include "list.h"
//
struct connection {
/** File descriptor of the connection socket. */
int sockfd;
......@@ -38,8 +47,12 @@ ssize_t connection_send (struct connection * connection, const void * buf, const
ssize_t connection_recv (struct connection * connection, void * buf, const ssize_t len);
#ifndef MINGW
#include <sys/uio.h>
ssize_t connection_send_iov (struct connection * connection, struct iovec * iovs, int iov_count);
ssize_t connection_recv_iov (struct connection * connection, struct iovec * iovs, int iov_count);
#endif
#endif /* _CONNECTION_H_ */
#define _POSIX_C_SOURCE 200908L
#include <stdio.h>
#include "common.h"
#include "list.h"
#include "connection.h"
#include "connpool.h"
#include <stdio.h>
/**
* Initializes the given connection pool. New connections created by the
......
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jni.h>
#include <jvmti.h>
#include "common.h"
#include "jvmtiutil.h"
#include "dislagent.h"
......@@ -17,6 +10,13 @@
#include "classparser.h"
#include "codeflags.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jni.h>
#include <jvmti.h>
// ****************************************************************************
// AGENT CONFIG
......@@ -546,13 +546,6 @@ __get_jvmti (JavaVM * jvm) {
}
#ifdef WHOLE
#define VISIBLE __attribute__((externally_visible))
#else
#define VISIBLE
#endif
JNIEXPORT jint JNICALL VISIBLE
Agent_OnLoad (JavaVM * jvm, char * options, void * reserved) {
jvmtiEnv * jvmti = __get_jvmti (jvm);
......
#include "common.h"
#include "jvmtiutil.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
......@@ -5,8 +8,7 @@
#include <jvmti.h>
#include "common.h"
#include "jvmtiutil.h"
//
#ifndef ERROR_PREFIX
#error ERROR_PREFIX macro has to be defined
......@@ -16,6 +18,7 @@
#error ERROR_JVMTI macro has to be defined
#endif
//
/**
* Allocates JVM memory for the given buffer and copies the buffer
......
#ifndef _JVMTIUTIL_H_
#define _JVMTIUTIL_H_
#include <jvmti.h>
#include "common.h"
#include <jvmti.h>
//
void die_with_jvmti_error (jvmtiEnv * jvmti, jvmtiError error, const char * message);
......@@ -29,7 +30,7 @@ char * jvmti_get_system_property_string (
* returns an error code, which should be checked to avoid any cascading errors
* down the line.
*/
inline static void
static inline void
check_jvmti_error(jvmtiEnv *jvmti, jvmtiError errnum, const char * message) {
if (errnum != JVMTI_ERROR_NONE) {
die_with_jvmti_error (jvmti, errnum, message);
......@@ -40,7 +41,7 @@ check_jvmti_error(jvmtiEnv *jvmti, jvmtiError errnum, const char * message) {
/**
* Enters a critical section protected by a JVMTI Raw Monitor.
*/
inline static void
static inline void
enter_critical_section (jvmtiEnv *jvmti, jrawMonitorID lock_id) {
jvmtiError error = (*jvmti)->RawMonitorEnter(jvmti, lock_id);
check_jvmti_error (jvmti, error, "failed to enter critical section");
......@@ -50,7 +51,7 @@ enter_critical_section (jvmtiEnv *jvmti, jrawMonitorID lock_id) {
/**
* Leaves a critical section protected by a JVMTI Raw Monitor.
*/
inline static void
static inline void
exit_critical_section (jvmtiEnv *jvmti, jrawMonitorID lock_id) {
jvmtiError error = (*jvmti)->RawMonitorExit(jvmti, lock_id);
check_jvmti_error (jvmti, error, "failed to exit critical section");
......
......@@ -10,7 +10,7 @@
#define _LIST_H_
#include <assert.h>
#include <stdio.h>
#include <stddef.h>
#include <stdbool.h>
/****************************************************************************\
......@@ -56,8 +56,7 @@ typedef int (* list_match_fn) (struct list * item, void * data);
* @param member
* the name of the member within the struct
*/
#define __offset_of(type, member) \
((size_t) &((type *) 0)->member)
#define __offset_of(type, member) offsetof(type, member)
/**
......
#include "common.h"
#include "msgchannel.h"
#include "connection.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
......@@ -8,15 +12,11 @@
#include <jni.h>
#include "common.h"
#include "msgchannel.h"
#include "connection.h"
//
#ifdef MINGW
inline static ssize_t
static inline ssize_t
__send (struct connection * conn, struct message * msg, void * header, const size_t header_size) {
ssize_t sent = connection_send (conn, header, header_size);
sent += connection_send (conn, msg->control, msg->control_size);
......@@ -25,7 +25,7 @@ __send (struct connection * conn, struct message * msg, void * header, const siz
}
inline static void
static inline void
__recv (struct connection * conn, void * control, const size_t control_size, void * classcode, const size_t classcode_size) {
connection_recv (conn, control, control_size);
connection_recv (conn, classcode, classcode_size);
......@@ -33,7 +33,7 @@ __recv (struct connection * conn, void * control, const size_t control_size, voi
#else
inline static ssize_t
static inline ssize_t
__send (struct connection * conn, struct message * msg, void * header, const size_t header_size) {
struct iovec iovs [] = {
{ .iov_base = header, .iov_len = header_size },
......@@ -45,7 +45,7 @@ __send (struct connection * conn, struct message * msg, void * header, const siz
}
inline static void
static inline void
__recv (struct connection * conn, void * control, const size_t control_size, void * classcode, const size_t classcode_size) {
struct iovec iovs [2] = {
{ .iov_base = control, .iov_len = control_size },
......
#include <assert.h>
#include "common.h"
#include "threads.h"
#include "connpool.h"
#include "connection.h"
#include "msgchannel.h"
#include <assert.h>
#include <sys/types.h>
//
#ifdef MINGW
......@@ -20,13 +26,6 @@
#endif /* !MINGW */
#include "common.h"
#include "threads.h"
#include "connpool.h"
#include "connection.h"
#include "msgchannel.h"
/**
* Address info for the DiSL server.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment