OpenASIP  2.0
TPEFDebugSectionReader.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file TPEFDebugSectionReader.cc
26  *
27  * Definition of TPEFDebugSectionReader class.
28  *
29  * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30  *
31  * @note rating: yellow
32  */
33 
35 #include "SafePointer.hh"
36 #include "Exception.hh"
37 #include "ReferenceKey.hh"
38 #include "SectionReader.hh"
39 #include "TPEFBaseType.hh"
40 #include "DebugSection.hh"
41 #include "DebugStabElem.hh"
42 #include "BinaryStream.hh"
43 
44 
45 namespace TPEF {
46 
47 using ReferenceManager::SafePointer;
48 using ReferenceManager::SectionIndexKey;
49 using ReferenceManager::SectionOffsetKey;
50 using ReferenceManager::SectionKey;
51 
52 TPEFDebugSectionReader TPEFDebugSectionReader::proto_;
53 
54 /**
55  * Constructor.
56  *
57  * Registers itself to SectionReader.
58  */
61 }
62 
63 /**
64  * Destructor.
65  */
67 }
68 
69 /**
70  * Returns the type of section it is meant to read.
71  *
72  * @return The type of section it can read.
73  */
76  return Section::ST_DEBUG;
77 }
78 
79 /**
80  * Reads section data from TPEF binary file.
81  *
82  * @param stream Stream to be read from.
83  * @param section Section where the information is to be stored.
84  * @exception UnreachableStream If reading of section fails.
85  * @exception KeyAlreadyExists Key was in use when trying to register object.
86  * @exception EndOfFile If end of file were reached while it shouldn't.
87  * @exception OutOfRange Some of read value were out of range.
88  * @exception WrongSubclass Some class couldn't do what it was asked for.
89  * @exception UnexpectedValue If there was unexpected value when reading.
90  */
91 void
93  // size taken by fields common to any type of debug section element
94  const std::size_t BASE_ELEM_SIZE = 10;
95 
96  // base classes implementation must be called with these.
97  TPEFSectionReader::readData(stream, section);
98 
99  DebugSection* debugSection =
100  dynamic_cast<DebugSection*>(section);
101  assert(debugSection != NULL);
102 
103  // check that link section is defined properly
104  assert(header().linkId != 0);
105 
106  if (!section->noBits()) {
107  // start of first element
108  SectionOffset currElement = header().bodyOffset;
109 
110  while (currElement < header().bodyOffset + header().bodyLength) {
111 
112  HalfWord type = stream.readHalfWord();
113  Word stringOffset = stream.readWord();
114  Word size = stream.readWord();
115 
116  std::vector<Byte> data;
117 
118  for (unsigned i = 0; i < size; i++) {
119  data.push_back(stream.readByte());
120  }
121 
122  DebugElement* newElem = NULL;
123 
124  switch (static_cast<DebugElement::ElementType>(type)) {
126  newElem = new DebugStabElem(data);
127  break;
128  default:
129  throw UnexpectedValue(
130  __FILE__, __LINE__, __func__,
131  "Unknown debug element type: " +
133  }
134 
135  // set name string for element
136  SectionOffsetKey sOffKey(header().linkId, stringOffset);
137  newElem->setDebugString(CREATE_SAFEPOINTER(sOffKey));
138 
139  section->addElement(newElem);
140 
141  currElement += size + BASE_ELEM_SIZE;
142  stream.setReadPosition(currElement);
143  }
144  }
145 }
146 
147 } // namespace TPEF
TPEF::Section::ST_DEBUG
@ ST_DEBUG
Debug section.
Definition: Section.hh:73
TPEF::TPEFSectionReader::Header::bodyOffset
Word bodyOffset
Definition: TPEFSectionReader.hh:69
TPEF::BinaryStream::setReadPosition
void setReadPosition(unsigned int position)
Definition: BinaryStream.cc:629
Exception.hh
DebugSection.hh
TPEF::BinaryStream
Definition: BinaryStream.hh:59
SafePointer.hh
TPEF::DebugSection
Definition: DebugSection.hh:44
TPEF::DebugElement::DE_STAB
@ DE_STAB
Definition: DebugElement.hh:59
Conversion::toString
static std::string toString(const T &source)
TPEF::DebugStabElem
Definition: DebugStabElem.hh:48
TPEF::Section
Definition: Section.hh:64
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::TPEFDebugSectionReader::~TPEFDebugSectionReader
virtual ~TPEFDebugSectionReader()
Definition: TPEFDebugSectionReader.cc:66
TPEF::Section::addElement
virtual void addElement(SectionElement *element)
Definition: Section.cc:133
UnexpectedValue
Definition: Exception.hh:455
TPEF::TPEFDebugSectionReader::proto_
static TPEFDebugSectionReader proto_
Prototype instance of TPEFDebugSectionReader to be registered to SectionReader.
Definition: TPEFDebugSectionReader.hh:63
SectionReader.hh
__func__
#define __func__
Definition: Application.hh:67
TPEF::SectionReader::registerSectionReader
static void registerSectionReader(const SectionReader *sReader)
Definition: SectionReader.cc:145
TPEF::BinaryStream::readHalfWord
HalfWord readHalfWord()
Definition: BinaryStream.cc:150
TPEF::TPEFSectionReader
Definition: TPEFSectionReader.hh:48
TPEF::BinaryStream::readByte
Byte readByte()
Definition: BinaryStream.cc:120
TPEF::TPEFDebugSectionReader::type
virtual Section::SectionType type() const
Definition: TPEFDebugSectionReader.cc:75
TPEFDebugSectionReader.hh
TPEF::Section::noBits
bool noBits() const
TPEF::SectionOffset
Word SectionOffset
Type for storing offsets relative to a given base offset value.
Definition: TPEFBaseType.hh:49
TPEF::TPEFSectionReader::readData
virtual void readData(BinaryStream &stream, Section *section) const
Definition: TPEFSectionReader.cc:86
TPEF::DebugElement
Definition: DebugElement.hh:56
TPEF::TPEFDebugSectionReader::readData
virtual void readData(BinaryStream &stream, Section *section) const
Definition: TPEFDebugSectionReader.cc:92
TPEF::ReferenceManager::SectionOffsetKey
Definition: ReferenceKey.hh:93
BinaryStream.hh
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::DebugElement::ElementType
ElementType
Definition: DebugElement.hh:58
TPEFBaseType.hh
ReferenceKey.hh
TPEF::BinaryStream::readWord
Word readWord()
Definition: BinaryStream.cc:187
TPEF::TPEFDebugSectionReader::TPEFDebugSectionReader
TPEFDebugSectionReader()
Definition: TPEFDebugSectionReader.cc:59
DebugStabElem.hh
TPEF::TPEFSectionReader::header
static const Header & header()
Definition: TPEFSectionReader.cc:174
TPEF
Definition: Assembler.hh:43
TPEF::DebugElement::setDebugString
void setDebugString(const ReferenceManager::SafePointer *aString)
Definition: DebugElement.cc:68