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