Pass pthread's init data to preload module 58/131758/2
authorAlexander Aksenov <a.aksenov@samsung.com>
Fri, 26 May 2017 11:50:07 +0000 (14:50 +0300)
committerAlexander Aksenov <a.aksenov@samsung.com>
Fri, 2 Jun 2017 13:20:06 +0000 (16:20 +0300)
This commit is related to swap-modules commit
Add per-process pthread init probes installation

Change-Id: If34de94641f0589c27d6be40c325ed21c89411b4
Signed-off-by: Alexander Aksenov <a.aksenov@samsung.com>
daemon/da_inst.c
daemon/da_inst.h
daemon/da_protocol.c
scripts/gen_preload_header.sh

index 33b25f9..e48b0a5 100644 (file)
@@ -863,17 +863,44 @@ static int write_bins_to_preload(struct user_space_inst_t *us_inst)
 
 
 
-#define GTP_ADD_BY_PATH "/sys/kernel/debug/swap/got_patcher/by_path/add"
-#define GTP_ADD_BY_PID "/sys/kernel/debug/swap/got_patcher/by_pid/add"
-#define GTP_ADD_BY_ID "/sys/kernel/debug/swap/got_patcher/by_id/add"
-
-static int add_bins_to_gtp(struct user_space_inst_t *us_inst)
+struct feature_paths_t {
+       const char *by_path;
+       const char *by_pid;
+       const char *by_id;
+       const char *clean;
+};
+
+static const char gtp_path[] = "/sys/kernel/debug/swap/got_patcher/by_path/add";
+static const char gtp_pid[] = "/sys/kernel/debug/swap/got_patcher/by_pid/add";
+static const char gtp_id[] = "/sys/kernel/debug/swap/got_patcher/by_id/add";
+static const char gtp_clean[] = "/sys/kernel/debug/swap/got_patcher/del_all";
+static const char preload_path[] = "/sys/kernel/debug/swap/preload/by_path/add";
+static const char preload_pid[] = "/sys/kernel/debug/swap/preload/by_pid/add";
+static const char preload_id[] = "/sys/kernel/debug/swap/preload/by_id/add";
+static const char preload_clean[] = "/sys/kernel/debug/swap/preload/del_all";
+
+static struct feature_paths_t gtp_paths = {
+       .by_path = gtp_path,
+       .by_pid = gtp_pid,
+       .by_id = gtp_id,
+       .clean = gtp_clean
+};
+
+static struct feature_paths_t preload_paths = {
+       .by_path = preload_path,
+       .by_pid = preload_pid,
+       .by_id = preload_id,
+       .clean = preload_clean
+};
+
+static int add_procs(struct user_space_inst_t *us_inst,
+                    struct feature_paths_t *ps)
 {
        struct app_list_t *app = us_inst->app_inst_list;
        uint32_t total_maps_count = 0;
        char real_path_buf[PATH_MAX];
        char *to_write;
-       FILE *gtp_p;
+       FILE *file;
 
        while (app != NULL) {
                switch (app->app->app_type) {
@@ -884,10 +911,9 @@ static int add_bins_to_gtp(struct user_space_inst_t *us_inst)
                         * functionality
                         */
 
-                       gtp_p = fopen(GTP_ADD_BY_PATH, "w");
-                       if (gtp_p == NULL) {
-                               LOGE("Cannot open <%s> to write!",
-                                    GTP_ADD_BY_PATH);
+                       file = fopen(ps->by_path, "w");
+                       if (file == NULL) {
+                               LOGE("Cannot open <%s> to write!", ps->by_path);
                                break;
                        }
 
@@ -895,34 +921,31 @@ static int add_bins_to_gtp(struct user_space_inst_t *us_inst)
                                            real_path_buf);
                        if (to_write != NULL) {
                                total_maps_count++;
-                               fwrite(to_write, strlen(to_write) + 1, 1,
-                                      gtp_p);
-                               fflush(gtp_p);
+                               fwrite(to_write, strlen(to_write) + 1, 1, file);
+                               fflush(file);
 
                                LOGI("app #%u <%s>\n", total_maps_count,
                                     to_write);
                        }
-                       fclose(gtp_p);
+                       fclose(file);
                        break;
                case APP_TYPE_WEB:
-                       gtp_p = fopen(GTP_ADD_BY_ID, "w");
-                       if (gtp_p == NULL) {
-                               LOGE("Cannot open <%s> to write!",
-                                    GTP_ADD_BY_ID);
+                       file = fopen(ps->by_id, "w");
+                       if (file == NULL) {
+                               LOGE("Cannot open <%s> to write!", ps->by_id);
                                break;
                        }
 
                        to_write = app->app->app_id;
                        if (to_write != NULL) {
                                total_maps_count++;
-                               fwrite(to_write, strlen(to_write) + 1, 1,
-                                      gtp_p);
-                               fflush(gtp_p);
+                               fwrite(to_write, strlen(to_write) + 1, 1, file);
+                               fflush(file);
 
                                LOGI("app #%u <%s>\n", total_maps_count,
                                     to_write);
                        }
-                       fclose(gtp_p);
+                       fclose(file);
                        break;
                }
 
@@ -933,46 +956,72 @@ static int add_bins_to_gtp(struct user_space_inst_t *us_inst)
        return 0;
 }
 
-#undef GTP_ADD_BY_ID
-#undef GTP_ADD_BY_PID
-#undef GTP_ADD_BY_PATH
+static int add_procs_to_gtp(struct user_space_inst_t *us_inst)
+{
+       return add_procs(us_inst, &gtp_paths);
+}
 
-#define GTP_CLEAN "/sys/kernel/debug/swap/got_patcher/del_all"
+static int add_procs_to_preload(struct user_space_inst_t *us_inst)
+{
+       return add_procs(us_inst, &preload_paths);
+}
 
-static int clean_bins_gtp(void)
+static int clean_procs(struct feature_paths_t *ps)
 {
        const char clean_str[] = "c";
-       FILE *gtp_p;
+       FILE *file;
 
-       gtp_p = fopen(GTP_CLEAN, "w");
-       if (gtp_p == NULL)
+       file = fopen(ps->clean, "w");
+       if (!file) {
+               LOGE("Error while cleaning at <%s>!", ps->clean);
                return -EINVAL;
+       }
 
-       fwrite(clean_str, sizeof(clean_str), 1, gtp_p);
+       fwrite(clean_str, sizeof(clean_str), 1, file);
 
-       fclose(gtp_p);
+       fclose(file);
 
        return 0;
 }
 
-#undef GTP_CLEAN
+static int clean_procs_gtp(void)
+{
+       return clean_procs(&gtp_paths);
+}
+
+static int clean_procs_preload(void)
+{
+       return clean_procs(&preload_paths);
+}
 
-static int write_bins_to_gtp(struct user_space_inst_t *us_inst)
+static int write_procs(struct user_space_inst_t *us_inst)
 {
        int ret;
 
-       ret = clean_bins_gtp();
-       if (ret != 0)
+       ret = clean_procs_gtp();
+       if (ret)
                return ret;
 
-       ret = add_bins_to_gtp(us_inst);
+       ret = clean_procs_preload();
+       if (ret)
+               return ret;
 
+       ret = add_procs_to_gtp(us_inst);
+       if (ret)
+               return ret;
+
+       ret = add_procs_to_preload(us_inst);
        return ret;
 }
 
 int got_patcher_stop(void)
 {
-       return clean_bins_gtp();
+       return clean_procs_gtp();
+}
+
+int preload_stop(void)
+{
+       return clean_procs_preload();
 }
 
 void send_target_bins_to(struct target *t)
@@ -1029,7 +1078,7 @@ int msg_start(struct msg_buf_t *data, struct user_space_inst_t *us_inst,
                goto msg_start_exit;
        }
 
-       if (write_bins_to_gtp(us_inst))
+       if (write_procs(us_inst))
                LOGE("Error adding binaries\n");
 
        generate_target_bins(us_inst);
@@ -1112,7 +1161,7 @@ int msg_swap_inst_add(struct msg_buf_t *data, struct user_space_inst_t *us_inst,
        if (write_bins_to_preload(us_inst))
                LOGE("Error adding binaries\n");
 
-       if (write_bins_to_gtp(us_inst))
+       if (write_procs(us_inst))
                LOGE("Error adding binaries\n");
 
        generate_target_bins(us_inst);
@@ -1166,7 +1215,7 @@ int msg_swap_inst_remove(struct msg_buf_t *data, struct user_space_inst_t *us_in
        if (write_bins_to_preload(us_inst))
                LOGE("Error adding binaries\n");
 
-       if (write_bins_to_gtp(us_inst))
+       if (write_procs(us_inst))
                LOGE("Error adding binaries\n");
        generate_target_bins(us_inst);
        send_target_bins_to_all_targets();
index 9c1a12f..2caad88 100644 (file)
@@ -80,6 +80,7 @@ extern int msg_swap_inst_add(struct msg_buf_t *data, struct user_space_inst_t *u
 extern int msg_start(struct msg_buf_t *data, struct user_space_inst_t *us_inst,
                     struct msg_t **msg, enum ErrorCode *err);
 int got_patcher_stop(void);
+int preload_stop(void);
 
 struct probe_list_t *new_probe(void);
 struct lib_list_t *new_lib(void);
index 16667fe..2989d93 100644 (file)
@@ -763,6 +763,9 @@ enum ErrorCode stop_all(void)
        if (got_patcher_stop())
                LOGE("failed to stop GOT patcher\n");
 
+       if (preload_stop())
+               LOGE("failed to stop Preload\n");
+
        pthread_mutex_lock(&stop_all_mutex);
        error_code = stop_all_no_lock();
        pthread_mutex_unlock(&stop_all_mutex);
index cce8a06..cff7e12 100755 (executable)
@@ -5,6 +5,7 @@ linker_library_pattern="ld-.*"
 libc_pkg_name="glibc"
 libpthread_pkg_name="glibc"
 libsmack_pkg_name="libsmack"
+libpthread_init_func="__pthread_initialize_minimal"
 
 output=$1
 
@@ -52,7 +53,11 @@ function print_libpthread()
     filename=$1
 
     libpthread_path=$(rpm -ql $libpthread_pkg_name | grep "libpthread" | head -1)
+    addr=$(parse_elf -f $libpthread_path --addr_format=swap --syms | grep $libpthread_init_func | awk '{print "0x" $1}')
+
     echo -e "/bin/echo \"$libpthread_path\" > /sys/kernel/debug/swap/preload/ignored_binaries/bins_add" >> $filename
+    echo -e "/bin/echo \"$libpthread_path\" > /sys/kernel/debug/swap/preload/pthread/path" >> $filename
+    echo -e "/bin/echo $addr > /sys/kernel/debug/swap/preload/pthread/minimal_init_off" >> $filename
 }
 
 function print_libsmack()
@@ -74,6 +79,13 @@ function print_ignored()
     print_libsmack $filename
 }
 
+function print_enable()
+{
+    filename=$1
+
+    echo -e "/bin/echo \"1\" > /sys/kernel/debug/swap/preload/enable" >> $filename
+}
+
 ##################################
 #       Script entry point       #
 ##################################
@@ -82,6 +94,7 @@ function print_ignored()
 print_header $output
 print_probe_lib $output
 print_ignored $output
+print_enable $output
 
 # check addresses
 grep 0x0000000000000000 $output && echo "ERROR: generate preload info" >&2 && exit 1