NCBI C++ ToolKit
ncbi_stack.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef CORELIB___NCBI_STACK__HPP
2 #define CORELIB___NCBI_STACK__HPP
3 
4 /* $Id: ncbi_stack.hpp 82273 2018-05-16 15:00:13Z ucko $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Mike DiCuccio
30  *
31  * File Description:
32  *
33  */
34 
35 #include <corelib/ncbistre.hpp>
36 #include <list>
37 #include <memory>
38 
40 
41 
42 // Forward declaration - internal class for holding stack trace data.
43 class CStackTraceImpl;
44 
45 
47 {
48 public:
49  /// Structure for holding stack trace data
51  {
52  string func;
53  string file;
54  string module;
55  const void* addr;
56  size_t offs;
57  size_t line;
58 
60  : addr(0), offs(0), line(0) {}
61 
62  SStackFrameInfo(const void* address)
63  : addr(address), offs(0), line(0) {}
64 
65  bool operator ==(const SStackFrameInfo& other) const
66  {
67  return func == other.func &&
68  file == other.file &&
69  module == other.module &&
70  addr == other.addr &&
71  offs == other.offs &&
72  line == other.line;
73  }
74 
75  string AsString(void) const;
76 
77  };
78  typedef list<SStackFrameInfo> TStack;
79 
80  /// Get and store current stack trace. When printing the stack trace
81  /// to a stream, each line is prepended with "prefix".
82  CStackTrace(const string& prefix = "");
83  ~CStackTrace(void);
84 
85  // Copy - required by some compilers for operator<<()
86  CStackTrace(const CStackTrace& stack_trace);
87  CStackTrace& operator=(const CStackTrace& stack_trace);
88 
89  /// Check if stack trace information is available
90  bool Empty(void) const
91  {
92  x_ExpandStackTrace();
93  return m_Stack.empty();
94  }
95 
96  /// Get the stack trace data
97  const TStack& GetStack(void) const
98  {
99  x_ExpandStackTrace(); return m_Stack;
100  }
101 
102  /// Get current prefix
103  const string& GetPrefix(void) const { return m_Prefix; }
104  /// Set new prefix
105  void SetPrefix(const string& prefix) const { m_Prefix = prefix; }
106  /// Write stack trace to the stream, prepend each line with the prefix.
107  void Write(CNcbiOstream& os) const;
108 
109  static unsigned int s_GetStackTraceMaxDepth(void);
110  static void s_HonorSignalHandlingConfiguration(void);
111 
112 private:
113  // Convert internal stack trace data (collected addresses)
114  // to the list of SStackFrameInfo.
115  void x_ExpandStackTrace(void) const;
116 
117  mutable unique_ptr<CStackTraceImpl> m_Impl;
118  mutable TStack m_Stack;
119  mutable string m_Prefix;
120 };
121 
122 
123 /// Output stack trace
124 inline
126 {
127  stack_trace.Write(os);
128  return os;
129 }
130 
131 
133 
134 #endif // CORELIB___NCBI_STACK__HPP
const string & GetPrefix(void) const
Get current prefix.
Definition: ncbi_stack.hpp:103
bool Empty(void) const
Check if stack trace information is available.
Definition: ncbi_stack.hpp:90
unique_ptr< CStackTraceImpl > m_Impl
Definition: ncbi_stack.hpp:117
void SetPrefix(const string &prefix) const
Set new prefix.
Definition: ncbi_stack.hpp:105
list< SStackFrameInfo > TStack
Definition: ncbi_stack.hpp:78
void Write(CNcbiOstream &os) const
Write stack trace to the stream, prepend each line with the prefix.
Definition: ncbi_stack.cpp:127
string m_Prefix
Definition: ncbi_stack.hpp:119
const TStack & GetStack(void) const
Get the stack trace data.
Definition: ncbi_stack.hpp:97
TStack m_Stack
Definition: ncbi_stack.hpp:118
bool operator==(const CEquivRange &A, const CEquivRange &B)
void Write(CObjectOStream &out, TConstObjectPtr object, const CTypeRef &type)
Definition: serial.cpp:55
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
#define NCBI_XNCBI_EXPORT
Definition: ncbi_export.h:1283
FILE * file
CNcbiOstream & operator<<(CNcbiOstream &os, const CStackTrace &stack_trace)
Output stack trace.
Definition: ncbi_stack.hpp:125
NCBI C++ stream class wrappers for triggering between "new" and "old" C++ stream libraries.
static const char * prefix[]
Definition: pcregrep.c:405
Structure for holding stack trace data.
Definition: ncbi_stack.hpp:51
SStackFrameInfo(const void *address)
Definition: ncbi_stack.hpp:62
Modified on Sun Jul 14 04:57:53 2024 by modify_doxy.py rev. 669887