emulator: cleaned common codes up
authorSeokYeon Hwang <syeon.hwang@samsung.com>
Sat, 1 Aug 2015 08:02:37 +0000 (17:02 +0900)
committerSeokYeon Hwang <syeon.hwang@samsung.com>
Tue, 4 Aug 2015 05:08:24 +0000 (14:08 +0900)
Change-Id: Ia6b6ba6094233d743ab15f6d2257e14f489e31d0
Signed-off-by: SeokYeon Hwang <syeon.hwang@samsung.com>
tizen/src/emulator.c
tizen/src/emulator.h
tizen/src/emulator_legacy.c
tizen/src/util/maru_device_hotplug.c
tizen/src/util/maru_device_hotplug.h
tizen/src/util/maru_err_table.c
tizen/src/util/maru_err_table.h
tizen/src/util/sdb.c
tizen/src/util/sdb.h
vl.c

index ab70685f399a98d35bb5371731c53dfc5d5dd896..67d4ae7478cc04dad422882fc24b78778a9f1d68 100644 (file)
@@ -121,13 +121,19 @@ static void emulator_notify_exit(Notifier *notifier, void *data)
 
     LOG_INFO("Exit emulator...\n");
 
-    maru_atexit();
+    handle_error_at_exit();
 }
 
 static Notifier emulator_exit = { .notify = emulator_notify_exit };
 
-static void print_system_info(void)
+void print_system_info(void)
 {
+    static bool is_printed = false;
+
+    if (is_printed) {
+        return;
+    }
+
     LOG_INFO("* Board name : %s\n", build_version);
     LOG_INFO("* Package %s\n", pkginfo_version);
     LOG_INFO("* Package %s\n", pkginfo_maintainer);
@@ -158,6 +164,8 @@ static void print_system_info(void)
 #endif
 
     print_system_info_os();
+
+    is_printed = true;
 }
 
 static void set_qemu_input_mode(void)
@@ -201,11 +209,15 @@ static void print_options_info(void)
 const char *prepare_maru(const char * const kernel_cmdline)
 {
     emulator_add_exit_notifier(&emulator_exit);
-    socket_init();
     print_system_info();
-    set_base_port();
 
-    // assemble new kernel cmdline
+    // We should call socket_init() here since this function called
+    // before calling socket_init() in "vl.c".
+    // It is safe to call socket_init() multiple times.
+    socket_init();
+    init_vm_base_port();
+
+    // Assemble new kernel cmdline
     maru_kernel_cmdline = g_strdup_printf("%s sdb_port=%d, "
             "vm_resolution=%dx%d", kernel_cmdline, get_emul_vm_base_port(),
             get_emul_resolution_width(), get_emul_resolution_height());
@@ -219,11 +231,11 @@ void prepare_maru_after_device_init(void)
 {
     make_vm_lock_os();
     set_qemu_input_mode();
-    maru_device_hotplug_init();
+    init_device_hotplug();
     start_ecs();
-    sdb_setup(get_emul_vm_base_port() + SDB_UDP_SENSOR_INDEX);
+    init_sdb(get_emul_vm_base_port() + SDB_UDP_SENSOR_INDEX);
 
-    // only for intent logging of essential information
+    // Only for intent logging of essential information
     get_vm_name();
     get_vm_data_path();
 }
@@ -337,7 +349,7 @@ static int emulator_main(int argc, char *argv[], char **envp)
         if (!assemble_emulator_args(&_qemu_argc, _qemu_argv)) {
             return -1;
         }
-#if defined(CONFIG_JAVA_UI)
+ #if defined(CONFIG_JAVA_UI)
         // java skin is deprecated, it is used for only debugging...
         _skin_argv[_skin_argc++] =
             g_strdup_printf("skin.path=%s", get_emul_skin_path());
@@ -346,7 +358,7 @@ static int emulator_main(int argc, char *argv[], char **envp)
         _skin_argv[_skin_argc++] =
             g_strdup_printf("vm.path=%s/%s",
                     get_variable("vms_path"), get_variable("vm_name"));
-#endif
+ #endif
 #endif
     }
 
@@ -366,44 +378,16 @@ static int emulator_main(int argc, char *argv[], char **envp)
     return 0;
 }
 
-#if defined(CONFIG_DARWIN) && !defined(CONFIG_QT)
-int g_argc;
-
-static void* main_thread(void* args)
-{
-    char** argv;
-    int argc = g_argc;
-    argv = (char**) args;
-
-    emulator_main(argc, argv, NULL);
-
-    thread_running = 0;
-    pthread_exit(NULL);
-}
-
-int main(int argc, char *argv[])
-{
-    char** args;
-    QemuThread main_thread_id;
-    g_argc = argc;
-    args = argv;
-    qemu_thread_create(&main_thread_id, "main_thread", main_thread, (void *)args, QEMU_THREAD_DETACHED);
-    ns_event_loop(&thread_running);
-
-    return 0;
-}
-#elif defined (CONFIG_LINUX)
+#if defined (CONFIG_LINUX)
 int main(int argc, char *argv[], char **envp)
 {
-    maru_register_exception_handler();
+    register_exception_handler();
     return emulator_main(argc, argv, envp);
 }
-#else // WIN32
+#else
 int main(int argc, char *argv[])
 {
- #ifndef CONFIG_DARWIN
-    maru_register_exception_handler();
- #endif
+    register_exception_handler();
     return emulator_main(argc, argv, NULL);
 }
 #endif
index 4a62b7289611a7ef4af14a89e240fb9f01089b01..b4ff368b01ea40dabab5c3a94a2db9a21cafa0af 100644 (file)
 
 const char *prepare_maru(const gchar * const kernel_cmdline);
 void prepare_maru_after_device_init(void);
-void start_skin(void);
-
+void print_system_info(void);
 void emulator_add_exit_notifier(Notifier *notify);
 
+#if defined(CONFIG_JAVA_UI)
+void start_skin(void);
+#endif
+
 #endif /* __EMULATOR_H__ */
index f9f7667fc97343fac9b5bcb0cb52cace6c1f0ad6..619b24f0ea1b58fbe21c9eb8b756a3488fc7c4ec 100644 (file)
@@ -89,7 +89,7 @@ char **_skin_argv;
 extern int _qemu_argc;
 extern char **_qemu_argv;
 
-static void print_system_info(void)
+static void print_system_info_legacy(void)
 {
 #define DIV 1024
 
@@ -294,7 +294,7 @@ int legacy_emulator_main(int argc, char * argv[], char **envp)
     extract_qemu_info(_qemu_argc, _qemu_argv);
 
     INFO("Emulator start !!!\n");
-    atexit(maru_atexit);
+    atexit(handle_error_at_exit);
     emulator_add_exit_notifier(&emulator_exit);
 
     extract_skin_info(_skin_argc, _skin_argv);
@@ -302,7 +302,7 @@ int legacy_emulator_main(int argc, char * argv[], char **envp)
     /* Redirect stdout and stderr after debug_ch is initialized. */
     redir_output();
 
-    print_system_info();
+    print_system_info_legacy();
 
     INFO("Prepare running...\n");
     INFO("drive_image_flle: %s\n", get_drive_image_file());
index 6241880cc1ae8741a15b3efd7ec97eca4ddb1eb8..e22605e4a52d682bc3ed3020c1ddb03948aafca9 100644 (file)
@@ -285,16 +285,16 @@ static void device_hotplug_handler(EventNotifier *e)
     }
 }
 
-static void maru_device_hotplug_deinit(Notifier *notifier, void *data)
+static void deinit_maru_device_hotplug(Notifier *notifier, void *data)
 {
     event_notifier_cleanup(&state->notifier);
 
     g_free(state);
 }
 
-static Notifier maru_device_hotplug_exit = { .notify = maru_device_hotplug_deinit };
+static Notifier maru_device_hotplug_exit = { .notify = deinit_maru_device_hotplug };
 
-void maru_device_hotplug_init(void)
+void init_device_hotplug(void)
 {
     state = g_malloc0(sizeof(struct maru_device_hotplug));
 
index 4db1bdc4b76c66d3b5defc8724d260ca2328d0b2..6001ee3b813925c59df5de7c367fd1af4672b85a 100644 (file)
@@ -38,7 +38,7 @@ enum command {
     DETACH_HDS,
 };
 
-void maru_device_hotplug_init(void);
+void init_device_hotplug(void);
 
 void do_hotplug(int command, void *opaque, size_t size);
 
index 18b80027f5c48c8df6d285975117932d1ef1f55f..fc5edc5f1fcd1384c887454ce38ed8b76f462ec8 100644 (file)
  *
  */
 
-#include "qemu-common.h"
-#include "emulator_common.h"
-#include "maru_err_table.h"
-#include "debug_ch.h"
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <glib.h>
-
 #ifdef CONFIG_WIN32
 #include <windows.h>
 #else
 #include <execinfo.h>
 #endif
 
+#include "qemu-common.h"
+
+#include "emulator_common.h"
+#include "emulator.h"
+#include "maru_err_table.h"
+
+#include "debug_ch.h"
 
 MULTI_DEBUG_CHANNEL(qemu, backtrace);
+
 /* This table must match the enum definition */
 static char _maru_string_table[][JAVA_MAX_COMMAND_LENGTH] = {
     /* 0 */ "",
@@ -132,19 +134,6 @@ void maru_register_exit_msg(int maru_exit_index, char const *format, ...)
         maru_exit_status, maru_exit_msg);
 }
 
-void maru_atexit(void)
-{
-
-    if (maru_exit_status != MARU_EXIT_NORMAL || maru_exit_msg) {
-        start_simple_client(maru_exit_msg);
-    }
-    g_free(maru_exit_msg);
-
-    INFO("normal exit called\n");
-    // dump backtrace log no matter what
-    maru_dump_backtrace(NULL, 0);
-}
-
 /* Print 'backtrace' */
 #ifdef _WIN32
 struct frame_layout {
@@ -180,7 +169,7 @@ static HMODULE aqua_get_module_handle(DWORD dwAddress)
 }
 #endif
 
-void maru_dump_backtrace(void *ptr, int depth)
+static void dump_backtrace(void *ptr, int depth)
 {
 #ifdef _WIN32
     int nCount;
@@ -258,9 +247,27 @@ void maru_dump_backtrace(void *ptr, int depth)
 #endif
 }
 
+void handle_error_at_exit(void)
+{
+
+    if (maru_exit_status != MARU_EXIT_NORMAL || maru_exit_msg) {
+        start_simple_client(maru_exit_msg);
+    }
+    g_free(maru_exit_msg);
+
+    // dump backtrace log no matter what
+    INFO("This is not a error.\n");
+    INFO("Stack backtrace for tracing...\n");
+    dump_backtrace(NULL, 0);
+}
+
 #ifdef CONFIG_WIN32
 static WINAPI LONG maru_unhandled_exception_filter(LPEXCEPTION_POINTERS pExceptionInfo){
     char module_buf[1024];
+
+    // print system information again
+    print_system_info();
+
     DWORD dwException = pExceptionInfo->ExceptionRecord->ExceptionCode;
     ERR("%d\n ", (int)dwException);
 
@@ -285,7 +292,7 @@ static WINAPI LONG maru_unhandled_exception_filter(LPEXCEPTION_POINTERS pExcepti
        );
 
     pContext = pExceptionInfo->ContextRecord;
-    maru_dump_backtrace(pContext, 0);
+    dump_backtrace(pContext, 0);
     _exit(0);
     //return EXCEPTION_CONTINUE_SEARCH;
 }
@@ -294,9 +301,12 @@ static WINAPI LONG maru_unhandled_exception_filter(LPEXCEPTION_POINTERS pExcepti
 #ifdef CONFIG_LINUX
 void maru_sighandler(int sig)
 {
+    // print system information again
+    print_system_info();
+
     pthread_spin_lock(&siglock);
     ERR("Got signal %d\n", sig);
-    maru_dump_backtrace(NULL, 0);
+    dump_backtrace(NULL, 0);
     pthread_spin_unlock(&siglock);
     _exit(0);
 }
@@ -304,11 +314,11 @@ void maru_sighandler(int sig)
 
 
 #ifndef CONFIG_DARWIN
-void maru_register_exception_handler(void)
+void register_exception_handler(void)
 {
-#ifdef CONFIG_WIN32
+ #ifdef CONFIG_WIN32
     prevExceptionFilter = SetUnhandledExceptionFilter(maru_unhandled_exception_filter);
-#else // LINUX
+ #else // LINUX
     void *trace[1];
     struct sigaction sa;
 
@@ -327,6 +337,11 @@ void maru_register_exception_handler(void)
     // main thread only
     sigaction(SIGTERM, &sa, NULL);
     sigaction(SIGINT, &sa, NULL);
-#endif
+ #endif
+}
+#else // CONFIG_DARWIN
+void register_exception_handler(void)
+{
+    // TODO: Exception handling on darwin
 }
 #endif
index f27dc910138886935ffccd3f9b4625fbe1ec41e5..f7f8b810723af9c75d2e2a32534c8a7685d0a253 100644 (file)
@@ -50,9 +50,7 @@ enum {
 
 char *get_canonical_path(char const *const path);
 void maru_register_exit_msg(int maru_exit_status, char const *format, ...);
-void maru_atexit(void);
+void register_exception_handler(void);
+void handle_error_at_exit(void);
 
-void maru_dump_backtrace(void *ptr, int depth);
-
-void maru_register_exception_handler(void);
 #endif /* __EMUL_ERR_TABLE_H__ */
index 1b4a70bfc8200042de1116a56cee3c9a30857ec3..f9f5f7066bc652b44f5cf0c6a157e81868a2d516 100644 (file)
@@ -164,7 +164,7 @@ int check_port_bind_listen(uint32_t port)
 #define ECS_INDEX 3
 #define SDB_GUEST_PORT 26101
 #define GDB_GUEST_PORT 26102
-void set_base_port(void)
+void init_vm_base_port(void)
 {
     int   tries     = 10;
     int   success   = 0;
@@ -196,7 +196,7 @@ void set_base_port(void)
 }
 
 static void start_sdb_noti_server(int server_port);
-void sdb_setup(int server_port)
+void init_sdb(int server_port)
 {
     start_sdb_noti_server(server_port);
 
index 49cbcb0cf16c3791719d229286ad531d29d6ab08..cadb6ee657a80209738ebaab4a175a0a8039ddda 100644 (file)
@@ -52,8 +52,8 @@
 #define SDB_TCP_OPENGL_INDEX  3   /* opengl server port */
 #define SDB_UDP_SENSOR_INDEX  3   /* sensor server port */
 
-void sdb_setup(int server_port);
-void set_base_port(void);
+void init_sdb(int server_port);
+void init_vm_base_port(void);
 int inet_strtoip(const char*  str, uint32_t  *ip);
 int socket_send(int fd, const void*  buf, int  buflen);
 void socket_close(int fd);
diff --git a/vl.c b/vl.c
index 62f118f82f9e52bcbb0fa24f2d27c3882e367b83..e8891c920ef15161554eebd31b33f915d81aff43 100644 (file)
--- a/vl.c
+++ b/vl.c
@@ -4231,9 +4231,7 @@ int main(int argc, char **argv, char **envp)
     }
 #endif
 
-#ifndef CONFIG_MARU
     socket_init();
-#endif
 
     if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
         exit(1);