NCBI C++ Toolkit Cross Reference

C++/src/util/ddump_viewer.cpp


  1 /*  $Id: ddump_viewer.cpp 103491 2007-05-04 17:18:18Z kazimird $
  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 
 47 BEGIN_NCBI_SCOPE
 48 
 49 
 50 //---------------------------------------------------------------------------
 51 //  CDebugDumpViewer implementation
 52 
 53 bool CDebugDumpViewer::x_GetInput(string& input)
 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 {
 96     CNcbiRegistry& cfg = CNcbiApplication::Instance()->GetConfig();
 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;
118     NStr::Split( value,",",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;
123         NStr::Split( *it_loc,"-",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 
139 void CDebugDumpViewer::x_Info(
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 
157 void CDebugDumpViewer::Bpt(
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();
178         NStr::Split( input, " ", cmnd);
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 
232 END_NCBI_SCOPE
233 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.