Medical Imaging Interaction Toolkit  2018.4.99-b20efe7f
Medical Imaging Interaction Toolkit
berryDebugBreakpointManager.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 #include "berryDebugUtil.h"
15 #include "berryObject.h"
16 
17 #include "berryLog.h"
18 
19 #include <Poco/NumberParser.h>
20 #include <Poco/DOM/NodeList.h>
21 #include <Poco/DOM/DOMParser.h>
22 #include <Poco/DOM/Document.h>
23 #include <Poco/DOM/Element.h>
24 #include <Poco/DOM/DOMWriter.h>
25 #include <Poco/SAX/InputSource.h>
26 #include <Poco/SAX/SAXException.h>
27 
28 #include <Poco/FileStream.h>
29 
30 #include <sstream>
31 
32 namespace berry
33 {
34 
35 const std::string DebugBreakpointManager::BREAKPOINTS_XML = "breakpoints.xml";
36 
37 const std::string DebugBreakpointManager::BREAKPOINTS_TAG = "breakpoints";
38 const std::string DebugBreakpointManager::OBJECT_TAG = "object";
39 const std::string DebugBreakpointManager::SMARTPOINTER_TAG = "smartpointer";
40 
41 const std::string DebugBreakpointManager::ID_ATTR = "id";
42 const std::string DebugBreakpointManager::CLASSNAME_ATTR = "className";
43 const std::string DebugBreakpointManager::OBJECTID_ATTR = "objectId";
44 const std::string DebugBreakpointManager::ENABLED_ATTR = "enabled";
45 
47  const Object* obj)
48 {
49  //BERRY_INFO << "Smartpointer breakpoint added: " << smartPointerId;
50  m_SmartPointerBreakpoints[smartPointerId] = obj;
51 }
52 
53 void DebugBreakpointManager::AddObjectBreakpoint(unsigned long objectTraceId)
54 {
55  //BERRY_INFO << "Object breakpoint added: " << objectTraceId;
56  m_ObjectBreakpoints.insert(objectTraceId);
57 }
58 
60 {
61  m_SmartPointerBreakpoints.erase(smartPointerId);
62 }
63 
64 void DebugBreakpointManager::RemoveObjectBreakpoint(unsigned long objectTraceId)
65 {
66  m_ObjectBreakpoints.erase(objectTraceId);
67 }
68 
69 const std::set<unsigned long>& DebugBreakpointManager::GetObjectBreakpoints() const
70 {
71  return m_ObjectBreakpoints;
72 }
73 
74 const Poco::HashMap<int, const Object*>& DebugBreakpointManager::GetSmartPointerBreakpoints() const
75 {
76  return m_SmartPointerBreakpoints;
77 }
78 
79 bool DebugBreakpointManager::BreakAtObject(unsigned long traceId) const
80 {
81  return m_ObjectBreakpoints.find(traceId) != m_ObjectBreakpoints.end();
82 }
83 
85 {
86  return m_SmartPointerBreakpoints.find(spId)
87  != m_SmartPointerBreakpoints.end();
88 }
89 
90 #ifdef BLUEBERRY_DEBUG_SMARTPOINTER
91 void DebugBreakpointManager::SaveState(const QString& path) const
92 {
93  Poco::XML::Document* doc = new Poco::XML::Document();
94  Poco::XML::Element* breakpoints = doc->createElement(BREAKPOINTS_TAG);
95  doc->appendChild(breakpoints)->release();
96 
97  for (std::set<unsigned long>::const_iterator i = m_ObjectBreakpoints.begin(); i
98  != m_ObjectBreakpoints.end(); ++i)
99  {
100  Poco::XML::Element* objectBreakpoint = doc->createElement(OBJECT_TAG);
101  breakpoints->appendChild(objectBreakpoint)->release();
102  std::stringstream ss;
103  ss << *i;
104  objectBreakpoint->setAttribute(ID_ATTR, ss.str());
105 
106  const Object* obj = DebugUtil::GetObject(*i);
107  if (obj)
108  {
109  objectBreakpoint->setAttribute(CLASSNAME_ATTR, obj->GetClassName().toStdString());
110 
111  }
112  }
113 
114  for (Poco::HashMap<int, const Object*>::ConstIterator i =
115  m_SmartPointerBreakpoints.begin(); i != m_SmartPointerBreakpoints.end(); ++i)
116  {
117  Poco::XML::Element* spBreakpoint = doc->createElement(SMARTPOINTER_TAG);
118  breakpoints->appendChild(spBreakpoint)->release();
119  std::stringstream ss;
120  ss << i->first;
121  spBreakpoint->setAttribute(ID_ATTR, ss.str());
122 
123  const Object* obj = i->second;
124  if (i->second)
125  {
126  spBreakpoint->setAttribute(CLASSNAME_ATTR, obj->GetClassName().toStdString());
127  ss.clear();
128  ss << obj->GetTraceId();
129  spBreakpoint->setAttribute(OBJECTID_ATTR, ss.str());
130  }
131  }
132 
133  Poco::FileOutputStream writer(path.toStdString());
134  Poco::XML::DOMWriter out;
135  out.setOptions(3); //write declaration and pretty print
136  out.writeNode(writer, doc);
137 
138  doc->release();
139 }
140 #else
141 void DebugBreakpointManager::SaveState(const QString& /*path*/) const
142 {}
143 #endif
144 
145 #ifdef BLUEBERRY_DEBUG_SMARTPOINTER
146 void DebugBreakpointManager::RestoreState(const QString& path)
147 {
148  try
149  {
150  Poco::XML::DOMParser parser;
151 
152  Poco::FileInputStream reader(path.toStdString());
153  Poco::XML::InputSource source(reader);
154 
155  //source.setSystemId(baseDir);
156  Poco::XML::Document* doc = parser.parse(&source);
157  Poco::XML::Element* breakpoints = doc->documentElement();
158 
159  if (breakpoints)
160  {
161  // restore object breakpoints
162  Poco::XML::NodeList* elementList = breakpoints->getElementsByTagName(
163  OBJECT_TAG);
164  for (std::size_t i = 0; i < elementList->length(); i++)
165  {
166  Poco::XML::Element* elem =
167  dynamic_cast<Poco::XML::Element*> (elementList->item(i));
168 
169  if (!elem->hasAttribute(ID_ATTR))
170  continue;
171 
172  const std::string& attr = elem->getAttribute(ID_ATTR);
173 
174  int traceId = 0;
175  try
176  {
177  traceId = Poco::NumberParser::parse(attr);
178  } catch (const Poco::SyntaxException& e)
179  {
180  BERRY_WARN << e.displayText();
181  }
182 
184  }
185  elementList->release();
186 
187  // restore smartpointer breakpoints
188  elementList = breakpoints->getElementsByTagName(SMARTPOINTER_TAG);
189  for (std::size_t i = 0; i < elementList->length(); i++)
190  {
191  Poco::XML::Element* elem =
192  dynamic_cast<Poco::XML::Element*> (elementList->item(i));
193 
194  if (!elem->hasAttribute(ID_ATTR))
195  continue;
196 
197  const std::string& attr = elem->getAttribute(ID_ATTR);
198 
199  int spId = 0;
200  try
201  {
202  spId = Poco::NumberParser::parse(attr);
203  } catch (const Poco::SyntaxException& e)
204  {
205  BERRY_WARN << e.displayText();
206  }
207 
209  }
210  elementList->release();
211  }
212 
213  doc->release();
214  } catch (Poco::XML::SAXParseException& e)
215  {
216  BERRY_WARN << e.displayText();
217  }
218  catch (Poco::FileNotFoundException& /*e*/)
219  {
220 
221  }
222  catch (Poco::FileException& e)
223  {
224  BERRY_WARN << e.displayText();
225  }
226 }
227 #else
228 void DebugBreakpointManager::RestoreState(const QString& /*path*/)
229 {
230 }
231 #endif
232 
233 }
void AddSmartpointerBreakpoint(int smartPointerId, const Object *obj=nullptr)
virtual QString GetClassName() const
Definition: berryObject.cpp:71
void RemoveSmartpointerBreakpoint(int smartPointerId)
Light weight base class for most BlueBerry classes.
Definition: berryObject.h:72
static const Object * GetObject(unsigned int traceId)
void AddObjectBreakpoint(unsigned long objectTraceId)
void SaveState(const QString &path) const
const Poco::HashMap< int, const Object * > & GetSmartPointerBreakpoints() const
static const std::string BREAKPOINTS_XML
void RemoveObjectBreakpoint(unsigned long objectTraceId)
bool BreakAtObject(unsigned long traceId) const
#define BERRY_WARN
Definition: berryLog.h:21
static DebugBreakpointManager * GetBreakpointManager()
const std::set< unsigned long > & GetObjectBreakpoints() const