• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_CORE_PLATFORM_DEFAULT_STACKTRACE_H_
17 #define TENSORFLOW_CORE_PLATFORM_DEFAULT_STACKTRACE_H_
18 
19 #include "tensorflow/core/platform/platform.h"
20 #if !defined(IS_MOBILE_PLATFORM) && !defined(PLATFORM_WINDOWS) && \
21     defined(PLATFORM_POSIX) && (defined(__clang__) || defined(__GNUC__))
22 #define TF_GENERATE_BACKTRACE
23 #endif
24 
25 #if defined(TF_GENERATE_BACKTRACE)
26 #include <dlfcn.h>
27 #include <execinfo.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #endif  // defined(TF_GENERATE_BACKTRACE)
32 
33 #include <sstream>
34 #include <string>
35 #include "tensorflow/core/platform/abi.h"
36 
37 namespace tensorflow {
38 
39 // Function to create a pretty stacktrace.
CurrentStackTrace()40 inline std::string CurrentStackTrace() {
41 #if defined(TF_GENERATE_BACKTRACE)
42   std::stringstream ss("");
43   ss << "*** Begin stack trace ***" << std::endl;
44 
45   // Get the mangled stack trace.
46   int buffer_size = 128;
47   void* trace[128];
48   buffer_size = backtrace(trace, buffer_size);
49 
50   for (int i = 0; i < buffer_size; ++i) {
51     const char* symbol = "";
52     Dl_info info;
53     if (dladdr(trace[i], &info)) {
54       if (info.dli_sname != nullptr) {
55         symbol = info.dli_sname;
56       }
57     }
58 
59     std::string demangled = tensorflow::port::MaybeAbiDemangle(symbol);
60     if (demangled.length()) {
61       ss << "\t" << demangled << std::endl;
62     } else {
63       ss << "\t" << symbol << std::endl;
64     }
65   }
66 
67   ss << "*** End stack trace ***" << std::endl;
68   return ss.str();
69 #else
70   return std::string();
71 #endif  // defined(TF_GENERATE_BACKTRACE)
72 }
73 
DebugWriteToString(const char * data,void * arg)74 inline void DebugWriteToString(const char* data, void* arg) {
75   reinterpret_cast<std::string*>(arg)->append(data);
76 }
77 
78 // A dummy class that does nothing.  Someday, add real support.
79 class SavedStackTrace {
80  public:
SavedStackTrace()81   SavedStackTrace() {}
82 
CreateCurrent(int skip_count)83   void CreateCurrent(int skip_count) {}
84 
Reset()85   void Reset() {}
86 
87   typedef void DebugWriter(const char*, void*);
Dump(DebugWriter * writerfn,void * arg)88   void Dump(DebugWriter* writerfn, void* arg) const {}
89 
depth()90   int depth() const { return 0; }
stack()91   void* const* stack() const { return stack_; }
92 
93  private:
94   void* stack_[32];
95 };
96 
97 }  // namespace tensorflow
98 
99 #endif  // TENSORFLOW_CORE_PLATFORM_DEFAULT_STACKTRACE_H_
100