Add backtrace function 74/70274/2
authorJaemin Ryu <jm77.ryu@samsung.com>
Thu, 19 May 2016 04:37:25 +0000 (13:37 +0900)
committerJaemin Ryu <jm77.ryu@samsung.com>
Fri, 20 May 2016 01:05:05 +0000 (18:05 -0700)
Change-Id: I25e06209e4c3e0a8f600b8be4186e7bc8b8e1cb2
Signed-off-by: Jaemin Ryu <jm77.ryu@samsung.com>
common/CMakeLists.txt
common/backtrace.cpp [new file with mode: 0644]
common/backtrace.h [new file with mode: 0644]

index a727335..13d4eda 100644 (file)
@@ -22,6 +22,7 @@ SET (COMMON_SOURCES     ${DPM_COMMON}/error.cpp
                         ${DPM_COMMON}/eventfd.cpp
                         ${DPM_COMMON}/mainloop.cpp
                         ${DPM_COMMON}/filesystem.cpp
+                        ${DPM_COMMON}/backtrace.cpp
                         ${DPM_COMMON}/thread-pool.cpp
                         ${DPM_COMMON}/rmi/socket.cpp
                         ${DPM_COMMON}/rmi/client.cpp
diff --git a/common/backtrace.cpp b/common/backtrace.cpp
new file mode 100644 (file)
index 0000000..f566ede
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  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
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <execinfo.h>
+
+#include "backtrace.h"
+#include "error.h"
+#include "exception.h"
+
+#define BACKTRACING_ENTRIES    128
+
+namespace runtime {
+
+void backtrace(const std::string& filename)
+{
+    int fd, ret;
+    void *buffer[BACKTRACING_ENTRIES];
+
+    do {
+        fd = ::open(filename.c_str(), O_WRONLY | O_CREAT, 0777);
+    } while ((fd == -1) && (errno == EINTR));
+
+    if (fd == -1) {
+        throw runtime::Exception(runtime::GetSystemErrorMessage());
+    }
+
+    int nptrs = ::backtrace(buffer, BACKTRACING_ENTRIES);
+    ::backtrace_symbols_fd(buffer, nptrs, fd);
+
+    do {
+        ret = ::close(fd);
+    } while ((ret == -1) && (errno == EINTR));
+}
+
+} // namespace runtime
diff --git a/common/backtrace.h b/common/backtrace.h
new file mode 100644 (file)
index 0000000..bb4465b
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  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
+ */
+
+ #ifndef __RUNTIME_BACKTRACE_H__
+ #define __RUNTIME_BACKTRACE_H__
+
+#include <string>
+
+namespace runtime {
+
+void backtrace(const std::string& filename);
+
+} // namespace runtime
+
+ #endif //!__RUNTIME_BACKTRACE_H__