[FIX] common app crash on start 27/29327/5
authorVitaliy Cherepanov <v.cherepanov@samsung.com>
Fri, 24 Oct 2014 08:36:39 +0000 (12:36 +0400)
committerVyacheslav Cherkashin <v.cherkashin@samsung.com>
Thu, 30 Oct 2014 13:39:05 +0000 (06:39 -0700)
redirect stdout stderr

Change-Id: I270876d2acf39ad259f0901945a111125c6150a7
Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
Makefile
helper/dastdout.c [new file with mode: 0644]
helper/dastdout.h [new file with mode: 0644]
helper/libdaprobe.c

index 99750f3d88f887c6482da7d8144f19e85565b9ce..3c310e880d7c733dc0ed4318e873095b58aed272 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -3,6 +3,8 @@ INSTALLDIR = usr/lib
 ## Since include directives do not impose additional dependencies, we can make
 ## Makefile more clear, simply putting all includes we ever need in single
 ## variable. Keep it alphabetic, please.
+DEBUG_FLAGS=                                   \
+#              -DSTDTOFILE                             \#
 
 INCLUDE_CPPFLAGS =                             \
                -I./include                     \
@@ -71,6 +73,7 @@ UTILITY_SRCS =                                \
        ./helper/dacapture.c            \
        ./helper/daforkexec.c           \
        ./helper/damaps.c                       \
+       ./helper/dastdout.c                     \
        ./custom_chart/da_chart.c       \
 
 PROBE_SRCS =                                   \
@@ -150,6 +153,7 @@ $(GENERATED_HEADERS):
 
 $(TIZEN_TARGET): LDFLAGS+=$(TIZEN_LDFLAGS)
 $(TIZEN_TARGET): CPPFLAGS+=$(TIZEN_CPPFLAGS)
+$(TIZEN_TARGET): CPPFLAGS+=$(DEBUG_FLAGS)
 $(TIZEN_TARGET): $(TIZEN_OBJS)
        $(CC) $(LDFLAGS) $^ -o $@
 
diff --git a/helper/dastdout.c b/helper/dastdout.c
new file mode 100644 (file)
index 0000000..b9208e3
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ *  DA manager
+ *
+ * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ *
+ * Cherepanov Vitaliy <v.cherepanov@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ * - Samsung RnD Institute Russia
+ *
+ */
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <errno.h>
+#include "dahelper.h"
+
+static inline int __redirect(int old, int new)
+{
+       int ret = 0;
+
+       if (dup2(old, new) == -1) {
+               /* fail to print up there. there is no sdterr */
+               /* TODO inform about problem */
+               fprintf(stderr, "dup2 fail\n");
+               ret = -1;
+       }
+
+       return ret;
+}
+
+int __redirect_std(void)
+{
+#ifdef STDTOFILE
+       char STDOUT[MAX_PATH_LENGTH];
+       snprintf(STDOUT,sizeof(STDOUT), "/tmp/da_preloaded_%d_%d.log", getppid(), getpid());
+#else
+       #define STDOUT          "/dev/null"
+#endif
+
+       int ret = 0;
+       int fd = open(STDOUT, O_WRONLY | O_CREAT | O_TRUNC, 0777);
+       if (fd != -1) {
+               if (__redirect(fd, 1) != 0 ||
+                   __redirect(fd, 2) != 0) {
+                       /* fail to print up there. there is no sdterr */
+                       /* TODO inform about problem */
+                       /* fprintf(sdterr, "duplicate fd fail\n"); */
+                       ret = -1;
+               }
+
+               close(fd);
+       } else {
+               /* TODO inform about problem */
+               close(1);
+               close(2);
+       }
+
+       close(0);
+       return ret;
+}
diff --git a/helper/dastdout.h b/helper/dastdout.h
new file mode 100644 (file)
index 0000000..491744c
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ *  DA manager
+ *
+ * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ *
+ * Cherepanov Vitaliy <v.cherepanov@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ * - Samsung RnD Institute Russia
+ *
+ */
+
+#ifndef __DASDTOUT_H__
+#define __DASDTOUT_H__
+
+int __redirect_std(void);
+
+#endif /* __DASDTOUT_H__ */
index f633a01800f1863b85279e81c32bb1b35fd23737..0edaded11344a2c7beb5a5c9a4ea65530e37dad4 100755 (executable)
@@ -9,6 +9,7 @@
  * Woojin Jung <woojin2.jung@samsung.com>
  * Juyoung Kim <j0.kim@samsung.com>
  * Nikita Kalyazin <n.kalyazin@samsung.com>
+ * Vitaliy Cherepanov <v.cherepanov@samsung.com>
  * Anastasia Lyupa <a.lyupa@samsung.com>
  *
  * This library is free software; you can redistribute it and/or modify it under
@@ -59,6 +60,7 @@
 #include "binproto.h"
 #include "daforkexec.h"
 #include "damaps.h"
+#include "dastdout.h"
 #include "common_probe_init.h"
 
 #define APP_INSTALL_PATH               "/opt/apps"
@@ -168,8 +170,8 @@ static int createSocket(void)
                        while (((recved & MSG_CONFIG_RECV) == 0) ||
                               ((recved & MSG_MAPS_INST_LIST_RECV) == 0))
                        {
-                               fprintf(stderr, "wait message\n");
-                               PRINTMSG("wait incoming message");
+                               PRINTMSG("wait incoming message %d\n",
+                                        gTraceInfo.socket.daemonSock);
                                /* recv header */
                                recvlen = recv(gTraceInfo.socket.daemonSock, &log,
                                               sizeof(log.type) + sizeof(log.length),
@@ -206,12 +208,18 @@ static int createSocket(void)
                                                free(data_buf);
 
                                } else if (recvlen < 0) {
-                                       fprintf(stderr, "recv failed in socket creation with error(%d)\n", recvlen);
+                                       close(gTraceInfo.socket.daemonSock);
+                                       gTraceInfo.socket.daemonSock = -1;
+                                       PRINTERR("recv failed with error(%d)\n",
+                                                recvlen);
                                        ret = -1;
                                        application_exit();
                                        break;
                                } else {
                                        /* closed by other peer */
+                                       close(gTraceInfo.socket.daemonSock);
+                                       gTraceInfo.socket.daemonSock = -1;
+                                       PRINTERR("closed by other peer\n");
                                        ret = -1;
                                        application_exit();
                                        break;
@@ -223,9 +231,13 @@ static int createSocket(void)
                } else {
                        close(gTraceInfo.socket.daemonSock);
                        gTraceInfo.socket.daemonSock = -1;
+                       PRINTERR("cannot connect to da_manager. err <%s>\n",
+                                strerror(errno));
                        ret = -1;
                }
        } else {
+               PRINTERR("cannot create socket. err <%s>\n",
+                        strerror(errno));
                ret = -1;
        }
 
@@ -462,6 +474,10 @@ void _init_(void)
 {
        probeBlockStart();
 
+       /* redirect stderr and stdout.*/
+       /* if there is no std - print call will crash app */
+       __redirect_std();
+
        init_exec_fork();
        initialize_hash_table();
 
@@ -564,6 +580,20 @@ void __attribute__((destructor)) _fini_probe()
 /************************************************************************
  * manipulate and print log functions
  ************************************************************************/
+const char *msg_code_to_srt(enum MessageType type)
+{
+       switch (type) {
+               case MSG_MSG:
+                       return "[INF]";
+               case MSG_ERROR:
+                       return "[ERR]";
+               case MSG_WARNING:
+                       return "[WRN]";
+               default:
+                       return "[???]";
+       }
+}
+
 bool printLog(log_t *log, int msgType)
 {
        ssize_t res, len;
@@ -632,15 +662,13 @@ bool print_log_str(int msgType, char *str)
  */
 bool print_log_fmt(int msgType, const char *func_name, int line, ...)
 {
-       ssize_t res, len;
+       ssize_t res = 0, len = 0;
        char *fmt, *p;
        int n;
        log_t log;
        va_list ap;
 
        /* Check connection status */
-       if(unlikely(gTraceInfo.socket.daemonSock == -1))
-               return false;
 
        probeBlockStart();
 
@@ -675,7 +703,15 @@ bool print_log_fmt(int msgType, const char *func_name, int line, ...)
 
        /* lock socket and send */
        real_pthread_mutex_lock(&(gTraceInfo.socket.sockMutex));
-       res = send(gTraceInfo.socket.daemonSock, &log, len, MSG_DONTWAIT);
+
+       if(unlikely(gTraceInfo.socket.daemonSock != -1)) {
+               res = send(gTraceInfo.socket.daemonSock, &log, len, MSG_DONTWAIT);
+       } else {
+               /* if socket is not connected, out to stderr */
+               fprintf(stderr, "%s %s\n", msg_code_to_srt(msgType), log.data);
+               fflush(stderr);
+       }
+
        real_pthread_mutex_unlock(&(gTraceInfo.socket.sockMutex));
 
        probeBlockEnd();
@@ -931,7 +967,7 @@ void *rtdl_next(const char *symname)
 
        symbol = dlsym(RTLD_NEXT, symname);
        if (symbol == NULL || dlerror() != NULL) {
-               fprintf(stderr, "dlsym failed <%s>\n", symname);
+               PRINTERR("dlsym failed <%s>\n", symname);
                exit(41);
        }