NCBI C++ ToolKit
ddump_viewer.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: ddump_viewer.cpp 81721 2018-03-28 12:46:32Z ivanov $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Andrei Gourianov
27  *
28  * File Description:
29  * Console Debug Dump Viewer
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 #include <typeinfo>
35 #include <corelib/ncbiapp.hpp>
36 #include <corelib/ncbifile.hpp>
37 #include <corelib/ncbireg.hpp>
38 #include <util/ddump_viewer.hpp>
39 
40 #ifdef NCBI_OS_MSWIN
41 # include <windows.h>
42 #else
43 # include <signal.h>
44 #endif
45 
46 
48 
49 
50 //---------------------------------------------------------------------------
51 // CDebugDumpViewer implementation
52 
54 {
55  char cBuf[512];
56  cout << "command>";
57  cin.getline(cBuf, sizeof(cBuf)/sizeof(cBuf[0]));
58  input = cBuf;
59  return (input != "go");
60 }
61 
62 const void* CDebugDumpViewer::x_StrToPtr(const string& str)
63 {
64  void* addr = 0;
65 #if SIZEOF_VOIDP == 8
66  addr = reinterpret_cast<void*>(NStr::StringToUInt8(str, 0, 16));
67 #else
68  addr = reinterpret_cast<void*>(NStr::StringToULong(str, 0, 16));
69 #endif
70  return addr;
71 }
72 
73 bool CDebugDumpViewer::x_CheckAddr( const void* addr, bool report)
74 {
75  bool res = false;
76  try {
77  const CDebugDumpable *p = static_cast<const CDebugDumpable*>(addr);
78  const type_info& t = typeid( *p);
79  if (report) {
80  cout << "typeid of " << addr
81  << " is \"" << t.name() << "\"" << endl;
82  }
83  res = true;
84  } catch (exception& e) {
85  if (report) {
86  cout << e.what() << endl;
87  cout << "address " << addr
88  << " does not point to a dumpable object " << endl;
89  }
90  }
91  return res;
92 }
93 
94 bool CDebugDumpViewer::x_CheckLocation(const char* file, int line)
95 {
97  string section("DebugDumpBpt");
98  string value = cfg.Get( section, "enabled");
99  // the section is absent? - enable all
100  if (value.empty()) {
101  return true;
102  }
103  // prerequisite
104  bool enabled = ((value != "false") && (value != "0"));
105  // Now only listed locations will be treated accordingly
106 
107  // smth about this particular file?
108  string name = CDirEntry(file).GetName();
109  value = cfg.Get(section, name);
110  if (value.empty() || (value=="none")) {
111  return !enabled; // none are "enabled"
112  } else if (value == "all") {
113  return enabled; // all are "enabled"
114  }
115  // otherwise - look for this particular line
116  // location range must be in the form "10,20-30,150-200"
117  list<string> loc;
119  list<string>::iterator it_loc;
120  for (it_loc = loc.begin(); it_loc != loc.end(); ++it_loc) {
121  list<string> range;
122  list<string>::iterator it_range;
124  int from=0, to;
125  try {
126  it_range = range.begin();
127  from = NStr::StringToInt( *it_range);
128  to = NStr::StringToInt( *(++it_range));
129  } catch (...) {
130  to = from;
131  }
132  if ((line >= from) && (line <= to)) {
133  return enabled;
134  }
135  }
136  return !enabled;
137 }
138 
140  const string& name, const CDebugDumpable* curr_object,
141  const string& location)
142 {
143  cout << endl;
144  cout << "Console Debug Dump Viewer" << endl << endl;
145  cout << "Stopped at " << location << endl;
146  cout << "current object: " << name << " = " <<
147  static_cast<const void*>(curr_object) << endl << endl;
148  cout << "Available commands: " << endl;
149  cout << " t[ypeid] <address>" << endl;
150  cout << " d[ump] <address> <depth>" << endl;
151 #ifdef NCBI_OS_MSWIN
152  cout << " b[reak]" << endl;
153 #endif
154  cout << " go" << endl << endl;
155 }
156 
158  const string& name, const CDebugDumpable* curr_object,
159  const char* file, int line)
160 {
161  if (!x_CheckLocation(file, line)) {
162  return;
163  }
164 
165  string location, input, cmnd0, cmnd1, cmnd2;
166  list<string> cmnd;
167  list<string>::iterator it_cmnd;
168  size_t narg;
169  unsigned int depth;
170  bool need_info;
171 
172  location = string(file) + "(" + NStr::IntToString(line) + ")";
173  x_Info( name, curr_object, location);
174  curr_object->DebugDumpText(cout, location + ": " + name, 0);
175 
176  while (x_GetInput(input)) {
177  cmnd.clear();
179  narg = cmnd.size();
180  need_info = true;
181 
182  if (narg > 0) {
183  cmnd0 = *(it_cmnd = cmnd.begin());
184  cmnd1 = (narg > 1) ? *(++it_cmnd) : string("");
185  cmnd2 = (narg > 2) ? *(++it_cmnd) : string("");
186 
187  switch (cmnd0[0]) {
188  case 'b': // break
189 #ifdef NCBI_OS_MSWIN
190  DebugBreak();
191 //#else
192 // raise(SIGSTOP);
193 #endif
194  break;
195  case 't': // typeid
196  if (narg > 1) {
197  const void* addr = x_StrToPtr( cmnd1);
198  x_CheckAddr( addr, true);
199  need_info = false;
200  }
201  break;
202  case 'd': // dump
203  if (narg>1) {
204  const void* addr = x_StrToPtr( cmnd1);
205  if (x_CheckAddr( addr, false))
206  {
207  depth = (narg>2) ? NStr::StringToUInt( cmnd2) : 0;
208  const CDebugDumpable *p =
209  static_cast<const CDebugDumpable*>(addr);
210  try {
211  const type_info& t = typeid( *p);
212  p->DebugDumpText(cout,
213  string(t.name()) + " " + cmnd1, depth);
214  } catch (...) {
215  cout << "Exception: Dump failed" << endl;
216  }
217  }
218  need_info = false;
219  }
220  break;
221  default:
222  break;
223  }
224  }
225  // default = help
226  if (need_info) {
227  x_Info( name, curr_object, location);
228  }
229  }
230 }
231 
void DebugDumpText(ostream &out, const string &bundle, unsigned int depth) const
Definition: ddumpable.cpp:56
CDirEntry –.
Definition: ncbifile.hpp:262
static CNcbiApplication * Instance(void)
Singleton method.
Definition: ncbiapp.cpp:264
CNcbiRegistry –.
Definition: ncbireg.hpp:913
static unsigned char depth[2 *(256+1+29)+1]
static const char * str(char *buf, int n)
Definition: stats.c:84
static const char location[]
Definition: config.c:97
const CNcbiRegistry & GetConfig(void) const
Get the application's cached configuration parameters (read-only).
string
Definition: cgiapp.hpp:687
bool x_GetInput(string &input)
const void * x_StrToPtr(const string &str)
void x_Info(const string &name, const CDebugDumpable *curr_object, const string &location)
bool x_CheckLocation(const char *file, int line)
void Bpt(const string &name, const CDebugDumpable *curr_object, const char *file, int line)
bool x_CheckAddr(const void *addr, bool report)
string GetName(void) const
Get the base entry name with extension (if any).
Definition: ncbifile.hpp:3916
virtual const string & Get(const string &section, const string &name, TFlags flags=0) const
Get the parameter value.
Definition: ncbireg.cpp:262
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static int StringToInt(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to int.
Definition: ncbistr.cpp:630
static list< string > & Split(const CTempString str, const CTempString delim, list< string > &arr, TSplitFlags flags=0, vector< SIZE_TYPE > *token_pos=NULL)
Split a string using specified delimiters.
Definition: ncbistr.cpp:3461
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5084
static unsigned long StringToULong(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to unsigned long.
Definition: ncbistr.cpp:665
static Uint8 StringToUInt8(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to Uint8.
Definition: ncbistr.cpp:873
static unsigned int StringToUInt(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to unsigned int.
Definition: ncbistr.cpp:642
@ fSplit_Truncate
Definition: ncbistr.hpp:2501
@ fSplit_MergeDelimiters
Merge adjacent delimiters.
Definition: ncbistr.hpp:2498
FILE * file
static int input()
range(_Ty, _Ty) -> range< _Ty >
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
EIPRangeType t
Definition: ncbi_localip.c:101
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
Process information in the NCBI Registry, including working with configuration files.
Modified on Tue May 21 10:54:37 2024 by modify_doxy.py rev. 669887