[FEATURE] internal external call instrumentation 67/24467/4
authorVitaliy Cherepanov <v.cherepanov@samsung.com>
Tue, 15 Jul 2014 05:55:16 +0000 (09:55 +0400)
committerDmitry Kovalenko <d.kovalenko@samsung.com>
Wed, 16 Jul 2014 12:00:30 +0000 (05:00 -0700)
if the library is instrumented all ld probes for this lib will be packed
if regular feture is enabled but always feature is not.

Change-Id: I27225519ec5aaa222d021835eab516d4d72910e7
Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
daemon/da_inst.c
daemon/da_inst.h
daemon/da_protocol.c
daemon/da_protocol.h
daemon/daemon.c
daemon/daemon.h
daemon/debug.h
daemon/threads.c

index a058cdf..dddea5a 100644 (file)
 
 #include <stdint.h>
 #include <sys/types.h>
+#include <linux/limits.h>
 
 #include "da_inst.h"
 #include "da_protocol.h"
 #include "da_protocol_inst.h"
 #include "debug.h"
-
+#include "daemon.h"
 
 struct lib_list_t *new_lib_inst_list = NULL;
 
@@ -41,6 +42,10 @@ uint32_t app_count = 0;
 char *packed_app_list = NULL;
 char *packed_lib_list = NULL;
 
+static struct _msg_target_t *lib_maps_message = NULL;
+static pthread_mutex_t lib_inst_list_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t lib_maps_message_mutex = PTHREAD_MUTEX_INITIALIZER;
+
 uint32_t libs_count;
 
 //----------------------------------- lists ----------------------------------
@@ -541,6 +546,105 @@ static int generate_msg(struct msg_t **msg, struct lib_list_t *lib_list, struct
        return 1;
 }
 
+static void lock_lib_maps_message()
+{
+       pthread_mutex_lock(&lib_inst_list_mutex);
+}
+
+static void unlock_lib_maps_message()
+{
+       pthread_mutex_unlock(&lib_inst_list_mutex);
+}
+
+static void lock_lib_list()
+{
+       pthread_mutex_lock(&lib_maps_message_mutex);
+}
+
+static void unlock_lib_list()
+{
+       pthread_mutex_unlock(&lib_maps_message_mutex);
+}
+
+static void generate_maps_inst_msg(struct user_space_inst_t *us_inst)
+{
+       lock_lib_maps_message();
+
+       struct lib_list_t *lib = us_inst->lib_inst_list;
+       struct app_list_t *app = us_inst->app_inst_list;
+       char *p, *resolved;
+       uint32_t total_maps_count = 0;
+       uint32_t total_len = sizeof(total_maps_count) + sizeof(*lib_maps_message);
+       char real_path_buf[PATH_MAX];
+
+       /* total message len calculate */
+       while (lib != NULL) {
+               p = lib->lib->bin_path;
+               total_len += strlen(p) + 1;
+               total_maps_count++;
+               LOGI("lib #%u <%s>\n", total_maps_count, p);
+               lib = (struct lib_list_t *)lib->next;
+       }
+
+       while (app != NULL) {
+               p = app->app->exe_path;
+               resolved = realpath((const char *)p, real_path_buf);
+               if (resolved != NULL) {
+                       total_len += strlen(p) + 1;
+                       total_maps_count++;
+                       LOGI("app #%u <%s>\n", total_maps_count, resolved);
+               } else {
+                       LOGE("cannot resolve bin path <%s>", p);
+               }
+
+               app = (struct app_list_t *)app->next;
+       }
+
+       if (lib_maps_message != NULL)
+               free(lib_maps_message);
+
+       lib_maps_message = malloc(total_len);
+       lib_maps_message->type = MSG_MAPS_INST_LIST;
+       lib_maps_message->length = total_len;
+
+       /* pack data */
+       p = lib_maps_message->data;
+       pack_int32(p, total_maps_count);
+
+       lib = us_inst->lib_inst_list;
+       while (lib != NULL) {
+               pack_str(p, lib->lib->bin_path);
+               lib = (struct lib_list_t *)lib->next;
+       }
+
+       app = us_inst->app_inst_list;
+       while (app != NULL) {
+               resolved = realpath(app->app->exe_path, real_path_buf);
+               if (resolved != NULL)
+                       pack_str(p, real_path_buf);
+               app = (struct app_list_t *)app->next;
+       }
+
+       LOGI("total_len = %u\n", total_len);
+       print_buf((char *)lib_maps_message, total_len, "lib_maps_message");
+
+       unlock_lib_maps_message();
+}
+
+void send_maps_inst_msg_to(int sock)
+{
+       lock_lib_maps_message();
+       send_msg_to_target(sock, (struct msg_target_t *)lib_maps_message);
+       unlock_lib_maps_message();
+}
+
+static void send_maps_inst_msg_to_all_targets()
+{
+       lock_lib_maps_message();
+       send_msg_to_all_targets(lib_maps_message);
+       unlock_lib_maps_message();
+}
+
 //-----------------------------------------------------------------------------
 struct app_info_t *app_info_get_first(struct app_list_t **app_list)
 {
@@ -567,12 +671,18 @@ struct app_info_t *app_info_get_next(struct app_list_t **app_list)
 int msg_start(struct msg_buf_t *data, struct user_space_inst_t *us_inst,
              struct msg_t **msg, enum ErrorCode *err)
 {
+       int res = 0;
        char *p = NULL;
        *msg = NULL;
+
+       /* lock list access */
+       lock_lib_list();
+
        if (!parse_app_inst_list(data, &us_inst->app_num, &us_inst->app_inst_list)) {
                *err = ERR_WRONG_MESSAGE_FORMAT;
                LOGE("parse app inst\n");
-               return 1;
+               res = 1;
+               goto msg_start_exit;
        }
 
        generate_msg(msg, us_inst->lib_inst_list, us_inst->app_inst_list);
@@ -582,28 +692,42 @@ int msg_start(struct msg_buf_t *data, struct user_space_inst_t *us_inst,
                pack_int32(p, NMSG_START);
        } else {
                *err = ERR_CANNOT_START_PROFILING;
-               return 1;
+               res = 1;
+               goto msg_start_exit;
        }
-       return 0;
+
+       generate_maps_inst_msg(us_inst);
+
+msg_start_exit:
+       /* unlock list access */
+       unlock_lib_list();
+
+       return res;
 }
 
 int msg_swap_inst_add(struct msg_buf_t *data, struct user_space_inst_t *us_inst,
                      struct msg_t **msg, enum ErrorCode *err)
 {
+       int res = 0;
        uint32_t lib_num = 0;
        char *p = NULL;
 
        *err = ERR_UNKNOWN;
 
+       /* lock list access */
+       lock_lib_list();
+
        if (!parse_lib_inst_list(data, &lib_num, &us_inst->lib_inst_list)) {
                *err = ERR_WRONG_MESSAGE_FORMAT;
                LOGE("parse lib inst list fail\n");
-               return 1;
+               res = 1;
+               goto msg_swap_inst_add_exit;
        }
        // rm probes from new if its presents in cur
        if (!resolve_collisions_for_add_msg(&us_inst->lib_inst_list, &new_lib_inst_list)) {
                LOGE("resolve collision\n");
-               return 1;
+               res = 1;
+               goto msg_swap_inst_add_exit;
        };
 
        // generate msg to send
@@ -619,38 +743,54 @@ int msg_swap_inst_add(struct msg_buf_t *data, struct user_space_inst_t *us_inst,
                cmp_libs))
        {
                LOGE("data move\n");
-               return 1;
+               res = 1;
+               goto msg_swap_inst_add_exit;
        };
 
        // free new_list
        free_data_list((struct data_list_t **)&new_lib_inst_list);
        new_lib_inst_list = NULL;
        *err = ERR_NO;
-       return 0;
+
+       generate_maps_inst_msg(us_inst);
+       send_maps_inst_msg_to_all_targets();
+
+msg_swap_inst_add_exit:
+       /* unlock list access */
+       unlock_lib_list();
+
+       return res;
 }
 
 int msg_swap_inst_remove(struct msg_buf_t *data, struct user_space_inst_t *us_inst,
                         struct msg_t **msg, enum ErrorCode *err)
 {
+       int res = 0;
        uint32_t lib_num = 0;
        char *p = NULL;
        *err = ERR_UNKNOWN;
 
+       /* lock list access */
+       lock_lib_list();
+
        if (!parse_lib_inst_list(data, &lib_num, &new_lib_inst_list)) {
                *err = ERR_WRONG_MESSAGE_FORMAT;
                LOGE("parse lib inst\n");
-               return 1;
+               res = 1;
+               goto msg_swap_inst_remove_exit;
        }
 
        if (!resolve_collisions_for_rm_msg(&us_inst->lib_inst_list, &new_lib_inst_list)) {
                LOGE("resolve collisions\n");
-               return 1;
+               res = 1;
+               goto msg_swap_inst_remove_exit;
        }
 
        if (us_inst->app_inst_list != NULL) {
                if (!generate_msg(msg, new_lib_inst_list, us_inst->app_inst_list)) {
                        LOGE("generate msg\n");
-                       return 1;
+                       res = 1;
+                       goto msg_swap_inst_remove_exit;
                }
                p = (char *)*msg;
                pack_int32(p, NMSG_SWAP_INST_ADD);
@@ -659,7 +799,15 @@ int msg_swap_inst_remove(struct msg_buf_t *data, struct user_space_inst_t *us_in
        free_data_list((struct data_list_t **)&new_lib_inst_list);
        new_lib_inst_list = NULL;
        *err = ERR_NO;
-       return 0;
+
+       generate_maps_inst_msg(us_inst);
+       send_maps_inst_msg_to_all_targets();
+
+msg_swap_inst_remove_exit:
+       /* unlock list access */
+       unlock_lib_list();
+
+       return res;
 }
 
 void msg_swap_free_all_data(struct user_space_inst_t *us_inst)
index 81d4e61..9368f64 100644 (file)
@@ -89,4 +89,7 @@ void free_data_list(struct data_list_t **data);
 
 struct app_info_t *app_info_get_first(struct app_list_t **app_list);
 struct app_info_t *app_info_get_next(struct app_list_t **app_list);
+
+void send_maps_inst_msg_to(int sock);
+
 #endif /* __DA_INST_H__*/
index 7ca357a..c6e5658 100644 (file)
@@ -938,12 +938,38 @@ send_ack:
        return -(err_code != ERR_NO);
 }
 
-static int process_msg_get_screenshot(struct msg_buf_t *msg_control)
+int send_msg_to_target(int sock, struct msg_target_t *msg)
+{
+       if (sock != -1) {
+               if (send(sock, msg, sizeof(struct _msg_target_t) + msg->length, MSG_NOSIGNAL) == -1)
+                       LOGE("fail to send data to target socket(%d)\n", sock);
+               else
+                       return 1;
+       }
+       return 0;
+}
+
+int send_msg_to_all_targets(struct msg_target_t *msg)
 {
        int target_index;
        int sock;
+       for (target_index = 0; target_index < MAX_TARGET_COUNT; target_index++) {
+               sock = manager.target[target_index].socket;
+               if (sock != -1) {
+                       if (send(sock, msg, sizeof(struct _msg_target_t) + msg->length, MSG_NOSIGNAL) == -1)
+                               LOGE("fail to send data to target index(%d)\n",
+                                    target_index);
+                       else
+                               return 1;
+               }
+       }
+       return 0;
+}
+
+static int process_msg_get_screenshot(struct msg_buf_t *msg_control)
+{
        uint32_t log_len;
-       msg_target_t sendlog;
+       struct msg_target_t sendlog;
        enum ErrorCode err_code = ERR_UNKNOWN;
 
        // send config message to target process
@@ -951,18 +977,8 @@ static int process_msg_get_screenshot(struct msg_buf_t *msg_control)
        sendlog.length = 0;
        log_len = sizeof(sendlog.type) + sizeof(sendlog.length) + sendlog.length;
 
-       for (target_index = 0; target_index < MAX_TARGET_COUNT; target_index++) {
-               sock = manager.target[target_index].socket;
-               if (sock != -1) {
-                       if (send(sock, &sendlog, log_len, MSG_NOSIGNAL) == -1) {
-                               LOGE("fail to send data to target index(%d)\n",
-                                    target_index);
-                       } else {
-                               err_code = ERR_NO;
-                               break;
-                       }
-               }
-       }
+       if (send_msg_to_all_targets(&sendlog) == 1)
+               err_code = ERR_NO;
 
        return -(err_code != ERR_NO);
 }
@@ -976,7 +992,7 @@ int host_message_handler(struct msg_t *msg)
        enum ErrorCode error_code = ERR_NO;
 
        int target_index;
-       msg_target_t sendlog;
+       struct msg_target_t sendlog;
 
        LOGI("MY HANDLE %s (%X)\n", msg_ID_str(msg->id), msg->id);
        init_parse_control(&msg_control, msg);
index 77ee743..3173b39 100644 (file)
@@ -166,6 +166,20 @@ static const char *supported_devices_strings[] = {
 #define array_size(x) (sizeof(x)/sizeof((x)[0]))
 enum { supported_devices_count = array_size(supported_devices_strings) };
 
+#define DA_MSG_MAX                                     4096
+#define RECV_BUF_MAX                           4104    // = sizeof(msg_t)
+struct _msg_target_t {
+       unsigned int type;
+       unsigned int length;
+       char data[0];
+};
+
+struct msg_target_t {
+       unsigned int type;
+       unsigned int length;
+       char data[DA_MSG_MAX];
+};
+
 #define MAX_FILENAME 128
 
 #define MSG_DATA_HDR_LEN 20
@@ -422,6 +436,9 @@ int check_running_status(const struct prof_session_t *prof_session);
 
 extern struct prof_session_t prof_session;
 
+extern int send_msg_to_target(int sock, struct msg_target_t *msg);
+extern int send_msg_to_all_targets(struct msg_target_t *msg);
+
 //debugs
 void print_replay_event(struct replay_event_t *ev, uint32_t num, char *tab);
 
index e31e0c4..b43f0eb 100644 (file)
@@ -277,7 +277,7 @@ static void terminate_all_target()
 {
        int i;
        ssize_t sendlen;
-       msg_target_t sendlog;
+       struct msg_target_t sendlog;
 
        sendlog.type = MSG_STOP;
        sendlog.length = 0;
@@ -606,7 +606,7 @@ static Eina_Bool target_event_cb(void *data, Ecore_Fd_Handler *fd_handler)
  */
 static int targetServerHandler(void)
 {
-       msg_target_t log;
+       struct msg_target_t log;
 
        int index = getEmptyTargetSlot();
        if (index == MAX_TARGET_COUNT) {
index 5389d9d..8f92e39 100644 (file)
@@ -35,7 +35,6 @@
 
 #include <stdint.h>            // for uint64_t, int64_t
 #include <pthread.h>   // for pthread_mutex_t
-
 #include "da_protocol.h"
 
 #ifdef __cplusplus
@@ -48,12 +47,9 @@ extern "C" {
 #define RUN_APP_LOADER
 
 #define MAX_TARGET_COUNT                       8
-#define DA_MSG_MAX                                     4096
-#define RECV_BUF_MAX                           4104    // = sizeof(msg_t)
 
 #define MAX_DEVICE                             10
 #define MAX_FILENAME                   128
-
 /*
 enum ErrorCode
 {
@@ -99,6 +95,7 @@ enum HostMessageType
        MSG_BATT_START = 106,
        MSG_BATT_STOP = 107,
        MSG_CAPTURE_SCREEN = 108,
+       MSG_MAPS_INST_LIST = 109,
        MSG_RECORD = 801,
        MSG_REPLAY = 802,
        MSG_OK = 901,
@@ -134,12 +131,7 @@ enum DAState
 #define EVENT_PID              0x00000002
 #define EVENT_ERROR            0x00000004
 
-typedef struct
-{
-       unsigned int    type;
-       unsigned int    length;
-       char                    data[DA_MSG_MAX];
-} msg_target_t;
+
 
 typedef struct
 {
index fbf0e54..82425c7 100644 (file)
@@ -91,7 +91,7 @@ static inline void do_log(const char *prefix, const char *funcname, int line, ..
        const char *fmt;
        fprintf(stderr, "[%s][%f] (%s:%d):", prefix, get_uptime(), funcname, line);
 
-       va_start(ap, funcname);
+       va_start(ap, line);
        fmt = va_arg(ap, const char *);
        vfprintf(stderr, fmt, ap);
        va_end(ap);
index f00e2d0..10ad9d9 100644 (file)
@@ -56,7 +56,7 @@ static void* recvThread(void* data)
        int pass = 0;
        uint64_t event;
        ssize_t recvLen;
-       msg_target_t log;
+       struct msg_target_t log;
 
        // initialize target variable
        target->pid = -1;
@@ -143,6 +143,7 @@ static void* recvThread(void* data)
                                event = EVENT_PID;
                                write(target->event_fd, &event, sizeof(uint64_t));
                        }
+                       send_maps_inst_msg_to(target->socket);
                        continue;               // don't send to host
                }
                else if(log.type == MSG_TERMINATE)
@@ -264,7 +265,9 @@ static void *samplingThread(void *data)
                        if (write_to_buf(msg) != 0)
                                LOGE("write to buf fail\n");
 
+#ifdef THREAD_SAMPLING_DEBUG
                        printBuf((char *)msg, MSG_DATA_HDR_LEN + msg->len);
+#endif
 
                        free_msg_data(msg);
                        reset_system_info(&sys_info);