-/*
+/*
* Emulator
*
* Copyright (C) 2011, 2012 Samsung Electronics Co., Ltd. All rights reserved.
*
- * Contact:
+ * Contact:
* SeokYeon Hwang <syeon.hwang@samsung.com>
* HyunJun Son <hj79.son@samsung.com>
* MunKyu Im <munkyu.im@samsung.com>
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
*
* Contributors:
* - S-Core Co., Ltd
#include <linux/version.h>
#include <sys/utsname.h>
#include <sys/sysinfo.h>
-#include <sys/ipc.h>
+#include <sys/ipc.h>
#include <sys/shm.h>
#endif
#define LOGS_SUFFIX "/logs/"
#define LOGFILE "emulator.log"
#define MIDBUF 128
-int tizen_base_port = 0;
-char tizen_target_path[MAXLEN] = {0, };
-char logpath[MAXLEN] = { 0, };
+int tizen_base_port;
+char tizen_target_path[MAXLEN];
+char logpath[MAXLEN];
-static int skin_argc = 0;
-static char** skin_argv = NULL;
-static int qemu_argc = 0;
-static char** qemu_argv = NULL;
+static int skin_argc;
+static char **skin_argv;
+static int qemu_argc;
+static char **qemu_argv;
+
+void maru_display_fini(void);
-extern void maru_display_fini(void);
void exit_emulator(void)
{
mloop_ev_stop();
int val;
struct shmid_ds shm_info;
- for(port=26100;port < 26200; port += 10)
- {
- if ( -1 != ( shm_id = shmget( (key_t)port, 0, 0)))
- {
- if((void *)-1 == (shm_addr = shmat(shm_id, (void *)0, 0)))
- {
- ERR( "error occured at shmat()\n");
+ for (port = 26100; port < 26200; port += 10) {
+ shm_id = shmget((key_t)port, 0, 0);
+ if (shm_id != -1) {
+ shm_addr = shmat(shm_id, (void *)0, 0);
+ if ((void *)-1 == shm_addr) {
+ ERR("error occured at shmat()\n");
break;
}
val = shmctl(shm_id, IPC_STAT, &shm_info);
- if(val != -1)
- {
- INFO( "count of process that use shared memory : %d\n", shm_info.shm_nattch);
- if(shm_info.shm_nattch > 0 && strcmp(tizen_target_path, (char*)shm_addr) == 0)
- {
- if(check_port_bind_listen(port+1) > 0){
+ if (val != -1) {
+ INFO("count of process that use shared memory : %d\n",
+ shm_info.shm_nattch);
+ if ((shm_info.shm_nattch > 0) &&
+ strcmp(tizen_target_path, (char *)shm_addr) == 0) {
+ if (check_port_bind_listen(port + 1) > 0) {
shmdt(shm_addr);
continue;
}
shmdt(shm_addr);
- maru_register_exit_msg(MARU_EXIT_UNKNOWN, (char*)"Can not execute this VM.\nThe same name is running now.");
+ maru_register_exit_msg(MARU_EXIT_UNKNOWN,
+ (char *)"Can not execute this VM.\n \
+ The same name is running now.");
exit(0);
- }
- else{
+ } else {
shmdt(shm_addr);
}
}
#elif defined(CONFIG_WIN32)
u_int port;
- char* base_port = NULL;
- char* pBuf;
+ char *base_port = NULL;
+ char *pBuf;
HANDLE hMapFile;
- for(port=26100;port < 26200; port += 10)
- {
+ for (port = 26100; port < 26200; port += 10) {
base_port = g_strdup_printf("%d", port);
- hMapFile = OpenFileMapping(
- FILE_MAP_READ,
- TRUE,
- base_port);
- if(hMapFile == NULL)
- {
+ hMapFile = OpenFileMapping(FILE_MAP_READ, TRUE, base_port);
+ if (hMapFile == NULL) {
INFO("port %s is not used.\n", base_port);
continue;
- }
- else
- {
- pBuf = (char*)MapViewOfFile(hMapFile,
- FILE_MAP_READ,
- 0,
- 0,
- 50);
- if (pBuf == NULL)
- {
+ } else {
+ pBuf = (char *)MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 50);
+ if (pBuf == NULL) {
ERR("Could not map view of file (%d).\n", GetLastError());
CloseHandle(hMapFile);
}
- if(strcmp(pBuf, tizen_target_path) == 0)
- {
- maru_register_exit_msg(MARU_EXIT_UNKNOWN, "Can not execute this VM.\nThe same name is running now.");
+ if (strcmp(pBuf, tizen_target_path) == 0) {
+ maru_register_exit_msg(MARU_EXIT_UNKNOWN,
+ "Can not execute this VM.\nThe same name is running now.");
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
free(base_port);
exit(0);
- }
- else
+ } else {
UnmapViewOfFile(pBuf);
+ }
}
CloseHandle(hMapFile);
free(base_port);
}
#elif defined(CONFIG_DARWIN)
- //TODO:
+ /* TODO: */
#endif
}
void make_shdmem(void)
{
#if defined(CONFIG_LINUX)
- int shmid;
- char *shared_memory;
- shmid = shmget((key_t)tizen_base_port, MAXLEN, 0666|IPC_CREAT);
- if (shmid == -1)
- {
- ERR("shmget failed\n");
- return;
- }
- shared_memory = shmat(shmid, (char*)0x00, 0);
- if (shared_memory == (void *)-1)
- {
- ERR("shmat failed\n");
- return;
- }
- sprintf(shared_memory, "%s", tizen_target_path);
- INFO( "shared memory key: %d value: %s\n", tizen_base_port, (char*)shared_memory);
+ int shmid;
+ char *shared_memory;
+
+ shmid = shmget((key_t)tizen_base_port, MAXLEN, 0666|IPC_CREAT);
+ if (shmid == -1) {
+ ERR("shmget failed\n");
+ return;
+ }
+
+ shared_memory = shmat(shmid, (char *)0x00, 0);
+ if (shared_memory == (void *)-1) {
+ ERR("shmat failed\n");
+ return;
+ }
+ sprintf(shared_memory, "%s", tizen_target_path);
+ INFO("shared memory key: %d value: %s\n",
+ tizen_base_port, (char *)shared_memory);
#elif defined(CONFIG_WIN32)
- HANDLE hMapFile;
- char* pBuf;
- char* port_in_use;
- char *shared_memory;
- shared_memory = g_strdup_printf("%s", tizen_target_path);
- port_in_use = g_strdup_printf("%d", tizen_base_port);
+ HANDLE hMapFile;
+ char *pBuf;
+ char *port_in_use;
+ char *shared_memory;
+
+ shared_memory = g_strdup_printf("%s", tizen_target_path);
+ port_in_use = g_strdup_printf("%d", tizen_base_port);
hMapFile = CreateFileMapping(
- INVALID_HANDLE_VALUE, // use paging file
- NULL, // default security
- PAGE_READWRITE, // read/write access
- 0, // maximum object size (high-order DWORD)
- 50, // maximum object size (low-order DWORD)
- port_in_use); // name of mapping object
- if (hMapFile == NULL)
- {
- ERR("Could not create file mapping object (%d).\n", GetLastError());
- return;
+ INVALID_HANDLE_VALUE, /* use paging file */
+ NULL, /* default security */
+ PAGE_READWRITE, /* read/write access */
+ 0, /* maximum object size (high-order DWORD) */
+ 50, /* maximum object size (low-order DWORD) */
+ port_in_use); /* name of mapping object */
+ if (hMapFile == NULL) {
+ ERR("Could not create file mapping object (%d).\n", GetLastError());
+ return;
}
- pBuf = MapViewOfFile(hMapFile, // handle to map object
- FILE_MAP_ALL_ACCESS, // read/write permission
- 0,
- 0,
- 50);
-
- if (pBuf == NULL)
- {
- ERR("Could not map view of file (%d).\n", GetLastError());
- CloseHandle(hMapFile);
- return;
+ pBuf = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 50);
+
+ if (pBuf == NULL) {
+ ERR("Could not map view of file (%d).\n", GetLastError());
+ CloseHandle(hMapFile);
+ return;
}
-
- CopyMemory((PVOID)pBuf, shared_memory, strlen(shared_memory));
- free(port_in_use);
- free(shared_memory);
+
+ CopyMemory((PVOID)pBuf, shared_memory, strlen(shared_memory));
+ free(port_in_use);
+ free(shared_memory);
#elif defined(CONFIG_DARWIN)
- //TODO:
+ /* TODO: */
#endif
- return;
+ return;
}
-
-
-static void construct_main_window(int skin_argc, char* skin_argv[], int qemu_argc, char* qemu_argv[] )
+static void construct_main_window(int skin_argc, char *skin_argv[],
+ int qemu_argc, char *qemu_argv[])
{
INFO("construct main window\n");
- start_skin_server( skin_argc, skin_argv, qemu_argc, qemu_argv );
+ start_skin_server(skin_argc, skin_argv, qemu_argc, qemu_argv);
- if (get_emul_skin_enable() == 1) { //this line is check for debugging, etc..
- if ( 0 > start_skin_client(skin_argc, skin_argv) ) {
+ /* the next line checks for debugging and etc.. */
+ if (get_emul_skin_enable() == 1) {
+ if (0 > start_skin_client(skin_argc, skin_argv)) {
maru_register_exit_msg(MARU_EXIT_SKIN_SERVER_FAILED, NULL);
- exit( -1 );
+ exit(-1);
}
}
set_emul_num_lock_state(0);
}
-static void parse_options(int argc, char* argv[], int* skin_argc, char*** skin_argv, int* qemu_argc, char*** qemu_argv)
+static void parse_options(int argc, char *argv[], int *skin_argc,
+ char ***skin_argv, int *qemu_argc, char ***qemu_argv)
{
int i;
int j;
-// FIXME !!!
-// TODO:
-
- for(i = 1; i < argc; ++i)
- {
- if(strncmp(argv[i], "--skin-args", 11) == 0)
- {
+ /* TODO: */
+
+ for (i = 1; i < argc; ++i) {
+ if (strncmp(argv[i], "--skin-args", 11) == 0) {
*skin_argv = &(argv[i + 1]);
break;
}
}
- for(j = i; j < argc; ++j)
- {
- if(strncmp(argv[j], "--qemu-args", 11) == 0)
- {
+
+ for (j = i; j < argc; ++j) {
+ if (strncmp(argv[j], "--qemu-args", 11) == 0) {
*skin_argc = j - i - 1;
*qemu_argc = argc - j - i + 1;
}
}
-static void get_bin_dir( char* exec_argv ) {
+static void get_bin_dir(char *exec_argv)
+{
- if ( !exec_argv ) {
+ if (!exec_argv) {
return;
}
- char* data = strdup( exec_argv );
- if ( !data ) {
- fprintf( stderr, "Fail to strdup for paring a binary directory.\n" );
+ char *data = strdup(exec_argv);
+ if (!data) {
+ fprintf(stderr, "Fail to strdup for paring a binary directory.\n");
return;
}
- char* p = NULL;
+ char *p = NULL;
#ifdef _WIN32
- p = strrchr( data, '\\' );
- if ( !p ) {
- p = strrchr( data, '/' );
+ p = strrchr(data, '\\');
+ if (!p) {
+ p = strrchr(data, '/');
}
#else
- p = strrchr( data, '/' );
+ p = strrchr(data, '/');
#endif
- if ( !p ) {
- free( data );
+ if (!p) {
+ free(data);
return;
}
- strncpy( bin_dir, data, strlen( data ) - strlen( p ) );
+ strncpy(bin_dir, data, strlen(data) - strlen(p));
- free( data );
+ free(data);
}
-void set_image_and_log_path(char* qemu_argv)
+void set_image_and_log_path(char *qemu_argv)
{
- int i;
- int j = 0;
+ int i, j = 0;
int name_len = 0;
int prefix_len = 0;
int suffix_len = 0;
prefix_len = strlen(IMAGE_PATH_PREFIX);
suffix_len = strlen(IMAGE_PATH_SUFFIX);
max = name_len - suffix_len;
- for(i = prefix_len , j = 0; i < max; i++)
- {
+ for (i = prefix_len , j = 0; i < max; i++) {
path[j++] = qemu_argv[i];
}
path[j] = '\0';
- if(!g_path_is_absolute(path))
+ if (!g_path_is_absolute(path)) {
strcpy(tizen_target_path, g_get_current_dir());
- else
+ } else {
strcpy(tizen_target_path, g_path_get_dirname(path));
+ }
strcpy(logpath, tizen_target_path);
strcat(logpath, LOGS_SUFFIX);
#ifdef CONFIG_WIN32
- if(access(g_win32_locale_filename_from_utf8(logpath), R_OK) != 0) {
- g_mkdir(g_win32_locale_filename_from_utf8(logpath), 0755);
+ if (access(g_win32_locale_filename_from_utf8(logpath), R_OK) != 0) {
+ g_mkdir(g_win32_locale_filename_from_utf8(logpath), 0755);
}
#else
- if(access(logpath, R_OK) != 0) {
- g_mkdir(logpath, 0755);
+ if (access(logpath, R_OK) != 0) {
+ g_mkdir(logpath, 0755);
}
#endif
- strcat(logpath, LOGFILE);
+ strcat(logpath, LOGFILE);
set_log_path(logpath);
}
void redir_output(void)
{
- FILE *fp;
+ FILE *fp;
- fp = freopen(logpath, "a+", stdout);
- if(fp ==NULL)
- fprintf(stderr, "log file open error\n");
- fp = freopen(logpath, "a+", stderr);
- if(fp ==NULL)
- fprintf(stderr, "log file open error\n");
+ fp = freopen(logpath, "a+", stdout);
+ if (fp == NULL) {
+ fprintf(stderr, "log file open error\n");
+ }
- setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
- setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
+ fp = freopen(logpath, "a+", stderr);
+ if (fp == NULL) {
+ fprintf(stderr, "log file open error\n");
+ }
+ setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
+ setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
}
-void extract_info(int qemu_argc, char** qemu_argv)
+void extract_info(int qemu_argc, char **qemu_argv)
{
int i;
- for(i = 0; i < qemu_argc; ++i)
- {
- if(strstr(qemu_argv[i], IMAGE_PATH_PREFIX) != NULL) {
+ for (i = 0; i < qemu_argc; ++i) {
+ if (strstr(qemu_argv[i], IMAGE_PATH_PREFIX) != NULL) {
set_image_and_log_path(qemu_argv[i]);
break;
}
}
-
+
tizen_base_port = get_sdb_base_port();
}
#ifdef CONFIG_SDL
/* Gets the version of the dynamically linked SDL library */
INFO("* Host sdl version : (%d, %d, %d)\n",
- SDL_Linked_Version()->major, SDL_Linked_Version()->minor, SDL_Linked_Version()->patch);
+ SDL_Linked_Version()->major,
+ SDL_Linked_Version()->minor,
+ SDL_Linked_Version()->patch);
#endif
#if defined(CONFIG_WIN32)
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (GetVersionEx(&osvi)) {
- INFO("* MajorVersion : %d, MinorVersion : %d, BuildNumber : %d, PlatformId : %d, CSDVersion : %s\n",
- osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber, osvi.dwPlatformId, osvi.szCSDVersion);
+ INFO("* MajorVersion : %d, MinorVersion : %d, BuildNumber : %d, \
+ PlatformId : %d, CSDVersion : %s\n", osvi.dwMajorVersion,
+ osvi.dwMinorVersion, osvi.dwBuildNumber,
+ osvi.dwPlatformId, osvi.szCSDVersion);
}
/* Retrieves information about the current system */
ZeroMemory(&sysi, sizeof(SYSTEM_INFO));
GetSystemInfo(&sysi);
- INFO("* Processor type : %d, Number of processors : %d\n", sysi.dwProcessorType, sysi.dwNumberOfProcessors);
+ INFO("* Processor type : %d, Number of processors : %d\n",
+ sysi.dwProcessorType, sysi.dwNumberOfProcessors);
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memInfo);
INFO("* Total Ram : %llu kB, Free: %lld kB\n",
- memInfo.ullTotalPhys / DIV, memInfo.ullAvailPhys / DIV);
+ memInfo.ullTotalPhys / DIV, memInfo.ullAvailPhys / DIV);
#elif defined(CONFIG_LINUX)
/* depends on building */
INFO("* Qemu build machine linux kernel version : (%d, %d, %d)\n",
- LINUX_VERSION_CODE >> 16, (LINUX_VERSION_CODE >> 8) & 0xff, LINUX_VERSION_CODE & 0xff);
+ LINUX_VERSION_CODE >> 16,
+ (LINUX_VERSION_CODE >> 8) & 0xff,
+ LINUX_VERSION_CODE & 0xff);
/* depends on launching */
struct utsname host_uname_buf;
if (uname(&host_uname_buf) == 0) {
- INFO("* Host machine uname : %s %s %s %s %s\n", host_uname_buf.sysname, host_uname_buf.nodename,
- host_uname_buf.release, host_uname_buf.version, host_uname_buf.machine);
+ INFO("* Host machine uname : %s %s %s %s %s\n",
+ host_uname_buf.sysname, host_uname_buf.nodename,
+ host_uname_buf.release, host_uname_buf.version,
+ host_uname_buf.machine);
}
struct sysinfo sys_info;
char lscmd[MAXLEN] = "lspci >> ";
strcat(lscmd, logpath);
int i = system(lscmd);
- INFO("system function command : %s, system function returned value : %d\n", lscmd, i);
+ INFO("system function command : %s, \
+ system function returned value : %d\n", lscmd, i);
#elif defined(CONFIG_DARWIN)
- //TODO:
+ /* TODO: */
#endif
INFO("\n");
construct_main_window(skin_argc, skin_argv, qemu_argc, qemu_argv);
int guest_server_port = tizen_base_port + SDB_UDP_SENSOR_INDEX;
- start_guest_server( guest_server_port );
+ start_guest_server(guest_server_port);
mloop_ev_init();
}
-int qemu_main(int argc, char** argv, char** envp);
+int qemu_main(int argc, char **argv, char **envp);
-int main(int argc, char* argv[])
+int main(int argc, char *argv[])
{
parse_options(argc, argv, &skin_argc, &skin_argv, &qemu_argc, &qemu_argv);
- get_bin_dir( qemu_argv[0] );
+ get_bin_dir(qemu_argv[0]);
socket_init();
extract_info(qemu_argc, qemu_argv);
INFO("Emulator start !!!\n");
-
atexit(maru_atexit);
-
+
check_shdmem();
make_shdmem();
sdb_setup();
system_info();
INFO("Prepare running...\n");
- redir_output(); // Redirect stdout, stderr after debug_ch is initialized...
+ /* Redirect stdout and stderr after debug_ch is initialized. */
+ redir_output();
int i;
- fprintf(stdout, "qemu args : ==========================================\n");
- for(i = 0; i < qemu_argc; ++i)
- {
+ fprintf(stdout, "qemu args : =========================================\n");
+ for (i = 0; i < qemu_argc; ++i) {
fprintf(stdout, "%s ", qemu_argv[i]);
}
fprintf(stdout, "\n");
- fprintf(stdout, "======================================================\n");
+ fprintf(stdout, "=====================================================\n");
- fprintf(stdout, "skin args : ==========================================\n");
- for(i = 0; i < skin_argc; ++i)
- {
+ fprintf(stdout, "skin args : =========================================\n");
+ for (i = 0; i < skin_argc; ++i) {
fprintf(stdout, "%s ", skin_argv[i]);
}
fprintf(stdout, "\n");
- fprintf(stdout, "======================================================\n");
+ fprintf(stdout, "=====================================================\n");
INFO("qemu main start!\n");
qemu_main(qemu_argc, qemu_argv, NULL);
*
* Copyright (C) 2011, 2012 Samsung Electronics Co., Ltd. All rights reserved.
*
- * Contact:
+ * Contact:
* SeokYeon Hwang <syeon.hwang@samsung.com>
* GiWoong Kim <giwoong.kim@samsung.com>
* YeongKyoon Lee <yeongkyoon.lee@samsung.com>
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
*
* Contributors:
* - S-Core Co., Ltd
/* This table must match the enum definition */
static char _maru_string_table[][JAVA_MAX_COMMAND_LENGTH] = {
- /* 0 */ "", //MARU_EXIT_UNKNOWN
- /* 1 */ "Failed to allocate memory in qemu.", //MARU_EXIT_MEMORY_EXCEPTION
- /* 2 */ "Failed to load a kernel file the following path. Check if the file is corrupted or missing.\n\n", //MARU_EXIT_KERNEL_FILE_EXCEPTION
- /* 3 */ "Failed to load a bios file the following path. Check if the file is corrupted or missing.\n\n", //MARU_EXIT_BIOS_FILE_EXCEPTION
+ /* 0 */ "",
+ /* 1 */ "Failed to allocate memory in qemu.",
+ /* 2 */ "Failed to load a kernel file the following path.\
+Check if the file is corrupted or missing.\n\n",
+ /* 3 */ "Failed to load a bios file the following path.\
+Check if the file is corrupted or missing.\n\n",
/* 4 */ "Skin process cannot be initialized. Skin server is not ready.",
/* add here.. */
- "" //MARU_EXIT_NORMAL
+ ""
};
static int maru_exit_status = MARU_EXIT_NORMAL;
static char maru_exit_msg[JAVA_MAX_COMMAND_LENGTH] = { 0, };
-void maru_register_exit_msg(int maru_exit_index, char* additional_msg)
+void maru_register_exit_msg(int maru_exit_index, char *additional_msg)
{
int len = 0;
if (maru_exit_status != MARU_EXIT_UNKNOWN) {
if (additional_msg != NULL) {
- len = strlen(_maru_string_table[maru_exit_status]) + strlen(additional_msg) + 1;
+ len = strlen(_maru_string_table[maru_exit_status])
+ + strlen(additional_msg) + 1;
if (len > JAVA_MAX_COMMAND_LENGTH) {
len = JAVA_MAX_COMMAND_LENGTH;
}
- snprintf(maru_exit_msg, len, "%s%s", _maru_string_table[maru_exit_status], additional_msg);
+ snprintf(maru_exit_msg, len, "%s%s",
+ _maru_string_table[maru_exit_status], additional_msg);
} else {
len = strlen(_maru_string_table[maru_exit_status]) + 1;
if (len > JAVA_MAX_COMMAND_LENGTH) {
len = JAVA_MAX_COMMAND_LENGTH;
}
- snprintf(maru_exit_msg, len, "%s", _maru_string_table[maru_exit_status]);
+ snprintf(maru_exit_msg, len,
+ "%s", _maru_string_table[maru_exit_status]);
}
} else if (additional_msg != NULL) {
len = strlen(additional_msg);
}
}
-char* maru_convert_path(char *msg, const char *path)
+char *maru_convert_path(char *msg, const char *path)
{
- char* current_path = NULL;
- char* err_msg = NULL;
+ char *current_path = NULL;
+ char *err_msg = NULL;
#ifdef _WIN32
- char* dos_err_msg = NULL;
-#endif
- int total_len = 0;
- int msg_len = 0;
- int cur_path_len = 0;
- int path_len = 0;
- int res = -1;
-
- res = (int)g_path_is_absolute(path);
- path_len = (strlen(path) + 1);
- if (msg) {
- msg_len = strlen(msg) + 1;
- }
-
- if (!res) {
- current_path = (char*)g_get_current_dir();
- cur_path_len = strlen(current_path) + strlen("/") + 1;
- total_len += cur_path_len;
- }
- total_len += (path_len + msg_len);
-
- err_msg = g_malloc0(total_len * sizeof(char));
-#if 0
- if (!err_msg) {
- fprintf(stderr, "failed to allocate memory\n");
- if (current_path) {
- g_free(current_path);
- }
- return NULL;
- }
+ char *dos_err_msg = NULL;
#endif
+ int total_len = 0;
+ int msg_len = 0;
+ int cur_path_len = 0;
+ int path_len = 0;
+ int res = -1;
+
+ res = (int)g_path_is_absolute(path);
+ path_len = (strlen(path) + 1);
+ if (msg) {
+ msg_len = strlen(msg) + 1;
+ }
+
+ if (!res) {
+ current_path = (char *)g_get_current_dir();
+ cur_path_len = strlen(current_path) + strlen("/") + 1;
+ total_len += cur_path_len;
+ }
+ total_len += (path_len + msg_len);
- if (msg) {
- snprintf(err_msg, msg_len, "%s", msg);
- total_len = msg_len - 1;
- } else {
- total_len = 0;
- }
+ err_msg = g_malloc0(total_len * sizeof(char));
- if (!res) {
- snprintf(err_msg + total_len, cur_path_len, "%s%s", current_path, "/");
- total_len += (cur_path_len - 1);
- }
- snprintf(err_msg + total_len, path_len, "%s", path);
+ if (msg) {
+ snprintf(err_msg, msg_len, "%s", msg);
+ total_len = msg_len - 1;
+ } else {
+ total_len = 0;
+ }
+
+ if (!res) {
+ snprintf(err_msg + total_len, cur_path_len, "%s%s", current_path, "/");
+ total_len += (cur_path_len - 1);
+ }
+ snprintf(err_msg + total_len, path_len, "%s", path);
#ifdef _WIN32
- {
- int i;
-
- dos_err_msg = g_strdup(err_msg);
- if (!dos_err_msg) {
- printf(stderr, "failed to duplicate an error message from %p\n", err_msg);
- if (current_path) {
- g_free(current_path);
- }
- g_free(err_msg);
- return NULL;
- }
-
- for (i = (total_len - 1); dos_err_msg[i]; i++) {
- if (dos_err_msg[i] == '/') {
- dos_err_msg[i] = '\\';
- }
- }
- pstrcpy(err_msg, sizeof(err_msg), dos_err_msg);
- g_free(dos_err_msg);
- }
+ {
+ int i;
+
+ dos_err_msg = g_strdup(err_msg);
+ if (!dos_err_msg) {
+ printf(stderr,
+ "failed to duplicate an error message from %p\n", err_msg);
+ if (current_path) {
+ g_free(current_path);
+ }
+ g_free(err_msg);
+ return NULL;
+ }
+
+ for (i = (total_len - 1); dos_err_msg[i]; i++) {
+ if (dos_err_msg[i] == '/') {
+ dos_err_msg[i] = '\\';
+ }
+ }
+ pstrcpy(err_msg, sizeof(err_msg), dos_err_msg);
+ g_free(dos_err_msg);
+ }
#endif
- if (current_path) {
- g_free(current_path);
- }
+ if (current_path) {
+ g_free(current_path);
+ }
- return err_msg;
+ return err_msg;
}
-// for pirnt 'backtrace'
+/* Print 'backtrace' */
#ifdef _WIN32
struct frame_layout {
- void* pNext;
- void* pReturnAddr;
+ void *pNext;
+ void *pReturnAddr;
};
-static char * aqua_get_filename_from_path(char * path_buf){
- char * ret_slash;
- char * ret_rslash;
+static char *aqua_get_filename_from_path(char *path_buf)
+{
+ char *ret_slash;
+ char *ret_rslash;
ret_slash = strrchr(path_buf, '/');
ret_rslash = strrchr(path_buf, '\\');
- if(ret_slash || ret_rslash){
- if(ret_slash > ret_rslash){
+ if (ret_slash || ret_rslash) {
+ if (ret_slash > ret_rslash) {
return ret_slash + 1;
- }
- else{
+ } else{
return ret_rslash + 1;
}
}
static HMODULE aqua_get_module_handle(DWORD dwAddress)
{
- MEMORY_BASIC_INFORMATION Buffer;
- return VirtualQuery((LPCVOID) dwAddress, &Buffer, sizeof(Buffer)) ? (HMODULE) Buffer.AllocationBase : (HMODULE) 0;
+ MEMORY_BASIC_INFORMATION Buffer;
+ return VirtualQuery((LPCVOID) dwAddress, &Buffer, sizeof(Buffer))
+ ? (HMODULE) Buffer.AllocationBase : (HMODULE) 0;
}
#endif
-void maru_dump_backtrace(void* ptr, int depth)
+void maru_dump_backtrace(void *ptr, int depth)
{
#ifdef _WIN32
- int nCount;
- void* pTopFrame;
- struct frame_layout currentFrame;
- struct frame_layout * pCurrentFrame;
+ int nCount;
+ void *pTopFrame;
+ struct frame_layout currentFrame;
+ struct frame_layout *pCurrentFrame;
char module_buf[1024];
HMODULE hModule;
PCONTEXT pContext = ptr;
- if(!pContext){
- __asm__ __volatile__ (
- "movl %%ebp, %0"
- : "=m" (pTopFrame)
- );
+ if (!pContext) {
+ __asm__ __volatile__ ("movl %%ebp, %0" : "=m" (pTopFrame));
} else {
- pTopFrame = (void*)((PCONTEXT)pContext)->Ebp;
+ pTopFrame = (void *)((PCONTEXT)pContext)->Ebp;
}
nCount = 0;
- currentFrame.pNext = ((struct frame_layout*)pTopFrame)->pNext;
- currentFrame.pReturnAddr = ((struct frame_layout*)pTopFrame)->pReturnAddr;
- pCurrentFrame = (struct frame_layout*)pTopFrame;
+ currentFrame.pNext = ((struct frame_layout *)pTopFrame)->pNext;
+ currentFrame.pReturnAddr = ((struct frame_layout *)pTopFrame)->pReturnAddr;
+ pCurrentFrame = (struct frame_layout *)pTopFrame;
- fprintf(stderr, "\nBacktrace Dump Start : \n");
- if(pContext){
+ fprintf(stderr, "\nBacktrace Dump Start :\n");
+ if (pContext) {
fprintf(stderr, "[%02d]Addr = 0x%p", nCount, ((PCONTEXT)pContext)->Eip);
memset(module_buf, 0, sizeof(module_buf));
hModule = aqua_get_module_handle((DWORD)((PCONTEXT)pContext)->Eip);
- if(hModule){
- if(!GetModuleFileNameA(hModule, module_buf, sizeof(module_buf))){
+ if (hModule) {
+ if (!GetModuleFileNameA(hModule, module_buf, sizeof(module_buf))) {
memset(module_buf, 0, sizeof(module_buf));
}
}
nCount++;
}
- while (1){
- if ((void*)pCurrentFrame < pTopFrame || (void*)pCurrentFrame >= (void*)0xC0000000){
- break;
+ while (1) {
+ if (((void *)pCurrentFrame < pTopFrame)
+ || ((void *)pCurrentFrame >= (void *)0xC0000000)) {
+ break;
}
fprintf(stderr, "[%02d]Addr = 0x%p", nCount, currentFrame.pReturnAddr);
memset(module_buf, 0, sizeof(module_buf));
hModule = aqua_get_module_handle((DWORD)currentFrame.pReturnAddr);
- if(hModule){
- if(!GetModuleFileNameA(hModule, module_buf, sizeof(module_buf))){
+ if (hModule) {
+ if (!GetModuleFileNameA(hModule, module_buf, sizeof(module_buf))) {
memset(module_buf, 0, sizeof(module_buf));
}
}
fprintf(stderr, " : %s\n", aqua_get_filename_from_path(module_buf));
- if(!ReadProcessMemory(GetCurrentProcess(), currentFrame.pNext, (void*)¤tFrame, sizeof(struct frame_layout), NULL)){
+ if (!ReadProcessMemory(GetCurrentProcess(), currentFrame.pNext,
+ (void *)¤tFrame, sizeof(struct frame_layout), NULL)) {
break;
}
pCurrentFrame = (struct frame_layout *)pCurrentFrame->pNext;
- if(depth){
- if(!--depth){
+ if (depth) {
+ if (!--depth) {
break;
}
}
nCount++;
}
#else
- int i, ndepth;
- void* trace[1024];
- char** symbols;
+ int i, ndepth;
+ void *trace[1024];
+ char **symbols;
- fprintf(stderr, "\n Backtrace Dump Start : \n");
+ fprintf(stderr, "\n Backtrace Dump Start :\n");
- ndepth = backtrace(trace, 1024);
- fprintf(stderr, "Backtrace depth is %d. \n", ndepth);
+ ndepth = backtrace(trace, 1024);
+ fprintf(stderr, "Backtrace depth is %d.\n", ndepth);
- symbols = backtrace_symbols(trace, ndepth);
- if (symbols == NULL) {
- fprintf(stderr, "'backtrace_symbols()' return error");
- return;
- }
+ symbols = backtrace_symbols(trace, ndepth);
+ if (symbols == NULL) {
+ fprintf(stderr, "'backtrace_symbols()' return error");
+ return;
+ }
- for (i = 0; i < ndepth; i++) {
- fprintf(stderr,"%s\n", symbols[i]);
- }
- free(symbols);
+ for (i = 0; i < ndepth; i++) {
+ fprintf(stderr, "%s\n", symbols[i]);
+ }
+ free(symbols);
#endif
}