Nemiver  0.3
nmv-asm-instr.h
Go to the documentation of this file.
1 // Author: Dodji Seketeli
2 /*
3  *This file is part of the Nemiver project
4  *
5  *Nemiver is free software; you can redistribute
6  *it and/or modify it under the terms of
7  *the GNU General Public License as published by the
8  *Free Software Foundation; either version 2,
9  *or (at your option) any later version.
10  *
11  *Nemiver is distributed in the hope that it will
12  *be useful, but WITHOUT ANY WARRANTY;
13  *without even the implied warranty of
14  *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  *See the GNU General Public License for more details.
16  *
17  *You should have received a copy of the
18  *GNU General Public License along with Nemiver;
19  *see the file COPYING.
20  *If not, write to the Free Software Foundation,
21  *Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  *
23  *See COPYRIGHT file copyright information.
24  */
25 #ifndef __NMV_ASM_INSTR_H__
26 #define __NMV_ASM_INSTR_H__
27 
28 #include <list>
29 #include <boost/variant.hpp>
30 #include "nmv-namespace.h"
31 #include "nmv-api-macros.h"
32 #include "nmv-exception.h"
33 
34 NEMIVER_BEGIN_NAMESPACE (nemiver)
35 NEMIVER_BEGIN_NAMESPACE (common)
36 
37 class AsmInstr {
43  string m_address;
44  string m_func;
45  string m_offset;
46  string m_instr;
47 
48  public:
49  explicit AsmInstr ()
50  {
51  }
52 
53  AsmInstr (const string &a_address,
54  const string &a_func,
55  const string &a_offset,
56  const string &a_instr):
57  m_address (a_address),
58  m_func (a_func),
59  m_offset (a_offset),
60  m_instr (a_instr)
61  {
62  }
63 
64  virtual ~AsmInstr ()
65  {
66  }
67 
68  const string& address () const {return m_address;}
69  void address (string &a) {m_address = a;}
70 
71  const string& function () const {return m_func;}
72  void function (const string &a_str) {m_func = a_str;}
73 
74  const string& offset () const {return m_offset;}
75  void offset (string &a_o) {m_offset = a_o;}
76 
77  const string& instruction () const {return m_instr;}
78  void instruction (const string &a_instr) {m_instr = a_instr;}
79 };//end class AsmInstr
80 
82  // No need of copy constructor or assignment operator yet.
83  UString m_file_path;
84  int m_line_number;
85  list<AsmInstr> m_instrs;
86 
87  public:
88 
90  m_line_number (-1)
91  {
92  }
93 
94  MixedAsmInstr (const UString &a_path,
95  int a_line_num) :
96  m_file_path (a_path),
97  m_line_number (a_line_num)
98  {
99  }
100 
101  MixedAsmInstr (const UString &a_path,
102  int a_line_num,
103  list<AsmInstr> &a_instrs) :
104  m_file_path (a_path),
105  m_line_number (a_line_num),
106  m_instrs (a_instrs)
107  {
108  }
109 
110  const UString& file_path () const {return m_file_path;}
111  void file_path (const UString &a) {m_file_path = a;}
112 
113  int line_number () const {return m_line_number;}
114  void line_number (int a) {m_line_number = a;}
115 
116  const list<AsmInstr>& instrs () const {return m_instrs;}
117  list<AsmInstr>& instrs () {return m_instrs;}
118  void instrs (const list<AsmInstr> &a) {m_instrs = a;}
119 };
120 
121 // Okay, so the result of a call to IDebugger::disassemble returns a
122 // list of Asm. An Asm is a variant type that is either an AsmInstr
123 // (a pure asm instruction) or a mixed asm/source that is basically
124 // a source code locus associated to the list of AsmInstr it
125 // generated. Asm::which will return an ASM_TYPE_PURE if it's an
126 // AsmInstr or ASM_TYPE_MIXED if it's an MixedAsmInstr.
127 class Asm {
128  boost::variant<AsmInstr, MixedAsmInstr> m_asm;
129 
130  public:
131  enum Type {
132  TYPE_PURE = 0,
133  TYPE_MIXED
134  };
135 
136  Asm (const AsmInstr &a) :
137  m_asm (a)
138  {
139  }
140 
141  Asm (const MixedAsmInstr &a) :
142  m_asm (a)
143  {
144  }
145 
146  Type which () const
147  {
148  return static_cast<Type> (m_asm.which ());
149  }
150 
151  bool empty () const
152  {
153  switch (which ()) {
154  case TYPE_PURE: {
155  const AsmInstr instr = boost::get<AsmInstr> (m_asm);
156  return instr.address ().empty ();
157  }
158  break;
159  case TYPE_MIXED: {
160  const MixedAsmInstr &mixed =
161  boost::get<MixedAsmInstr> (m_asm);
162  return mixed.instrs ().empty ();
163  }
164  default:
165  THROW ("unknown asm type");
166  }
167  return false;
168  }
169 
170  const AsmInstr& instr () const
171  {
172  switch (which ()) {
173  case TYPE_PURE:
174  return boost::get<AsmInstr> (m_asm);
175  case TYPE_MIXED: {
176  const MixedAsmInstr &mixed =
177  boost::get<MixedAsmInstr> (m_asm);
178  if (mixed.instrs ().empty ()) {
179  stringstream msg;
180  msg << "mixed asm has empty instrs at "
181  << mixed.file_path ()
182  << ":"
183  << mixed.line_number ();
184  THROW (msg.str ());
185  }
186  return mixed.instrs ().front ();
187  }
188  default:
189  break;
190  }
191  THROW ("reached unreachable");
192  }
193 
194  const MixedAsmInstr& mixed_instr () const
195  {
196  THROW_IF_FAIL (which () == TYPE_MIXED);
197  return boost::get<MixedAsmInstr> (m_asm);
198  }
199 };
200 
202  // no need of copy constructor yet,
203  // as we don't have any pointer member.
204  UString m_function_name;
205  UString m_file_name;
206  string m_start_address;
207  string m_end_address;
208 
209  public:
211  {
212  }
214  {
215  }
216 
217  const UString& function_name () const {return m_function_name;}
218  void function_name (const UString &a_name) {m_function_name = a_name;}
219 
220  const UString& file_name () const {return m_file_name;}
221  void file_name (const UString &a_name) {m_file_name = a_name;}
222 
223  const std::string& start_address () const {return m_start_address;}
224  void start_address (const std::string &a) {m_start_address = a;}
225 
226  const std::string& end_address () const {return m_end_address;}
227  void end_address (const std::string &a) {m_end_address = a;}
228 };// end class DisassembleInfo
229 
230 NEMIVER_END_NAMESPACE (common)
231 NEMIVER_END_NAMESPACE (nemiver)
232 
233 #endif // __NMV_ASM_INSTR_H__
nemiver::common::DisassembleInfo
Definition: nmv-asm-instr.h:201
nemiver::common::AsmInstr::offset
const string & offset() const
Definition: nmv-asm-instr.h:74
nemiver
Definition: nmv-address.h:31
nemiver::common::MixedAsmInstr::MixedAsmInstr
MixedAsmInstr(const UString &a_path, int a_line_num, list< AsmInstr > &a_instrs)
Definition: nmv-asm-instr.h:101
nemiver::common::MixedAsmInstr::MixedAsmInstr
MixedAsmInstr()
Definition: nmv-asm-instr.h:89
THROW
#define THROW(a_reason)
Definition: nmv-exception.h:99
nemiver::common::MixedAsmInstr::file_path
const UString & file_path() const
Definition: nmv-asm-instr.h:110
nmv-api-macros.h
nemiver::common::Asm
Definition: nmv-asm-instr.h:127
nemiver::common::MixedAsmInstr::instrs
void instrs(const list< AsmInstr > &a)
Definition: nmv-asm-instr.h:118
nemiver::common::Asm::which
Type which() const
Definition: nmv-asm-instr.h:146
nemiver::common::MixedAsmInstr::MixedAsmInstr
MixedAsmInstr(const UString &a_path, int a_line_num)
Definition: nmv-asm-instr.h:94
nemiver::common::DisassembleInfo::end_address
const std::string & end_address() const
Definition: nmv-asm-instr.h:226
nemiver::common::MixedAsmInstr::line_number
void line_number(int a)
Definition: nmv-asm-instr.h:114
nemiver::common::DisassembleInfo::start_address
void start_address(const std::string &a)
Definition: nmv-asm-instr.h:224
nemiver::common::UString
Definition: nmv-ustring.h:45
nemiver::common::AsmInstr::AsmInstr
AsmInstr(const string &a_address, const string &a_func, const string &a_offset, const string &a_instr)
Definition: nmv-asm-instr.h:53
nemiver::common::DisassembleInfo::start_address
const std::string & start_address() const
Definition: nmv-asm-instr.h:223
nemiver::common::Asm::mixed_instr
const MixedAsmInstr & mixed_instr() const
Definition: nmv-asm-instr.h:194
nemiver::common::MixedAsmInstr::instrs
const list< AsmInstr > & instrs() const
Definition: nmv-asm-instr.h:116
nemiver::common::MixedAsmInstr::line_number
int line_number() const
Definition: nmv-asm-instr.h:113
nemiver::common::MixedAsmInstr::file_path
void file_path(const UString &a)
Definition: nmv-asm-instr.h:111
nemiver::common::AsmInstr::address
const string & address() const
Definition: nmv-asm-instr.h:68
nemiver::common::Asm::empty
bool empty() const
Definition: nmv-asm-instr.h:151
nemiver::common::AsmInstr::address
void address(string &a)
Definition: nmv-asm-instr.h:69
nemiver::common::Asm::Asm
Asm(const MixedAsmInstr &a)
Definition: nmv-asm-instr.h:141
nemiver::common::AsmInstr::offset
void offset(string &a_o)
Definition: nmv-asm-instr.h:75
nemiver::common::DisassembleInfo::end_address
void end_address(const std::string &a)
Definition: nmv-asm-instr.h:227
nemiver::common::AsmInstr::AsmInstr
AsmInstr()
Definition: nmv-asm-instr.h:49
nemiver::common::AsmInstr::instruction
void instruction(const string &a_instr)
Definition: nmv-asm-instr.h:78
nemiver::common::AsmInstr
Definition: nmv-asm-instr.h:42
nemiver::common::DisassembleInfo::function_name
const UString & function_name() const
Definition: nmv-asm-instr.h:217
nemiver::common::DisassembleInfo::file_name
void file_name(const UString &a_name)
Definition: nmv-asm-instr.h:221
nemiver::common::Asm::Asm
Asm(const AsmInstr &a)
Definition: nmv-asm-instr.h:136
nemiver::common::DisassembleInfo::file_name
const UString & file_name() const
Definition: nmv-asm-instr.h:220
nemiver::common::AsmInstr::instruction
const string & instruction() const
Definition: nmv-asm-instr.h:77
nemiver::common::Asm::instr
const AsmInstr & instr() const
Definition: nmv-asm-instr.h:170
nemiver::common::DisassembleInfo::function_name
void function_name(const UString &a_name)
Definition: nmv-asm-instr.h:218
nemiver::common::AsmInstr::~AsmInstr
virtual ~AsmInstr()
Definition: nmv-asm-instr.h:64
nmv-namespace.h
nemiver::common::Asm::Type
Type
Definition: nmv-asm-instr.h:131
nemiver::common::DisassembleInfo::~DisassembleInfo
~DisassembleInfo()
Definition: nmv-asm-instr.h:213
THROW_IF_FAIL
#define THROW_IF_FAIL(a_cond)
Definition: nmv-exception.h:65
nmv-exception.h
nemiver::common::DisassembleInfo::DisassembleInfo
DisassembleInfo()
Definition: nmv-asm-instr.h:210
common
Definition: nmv-proc-list-dialog.h:32
nemiver::common::MixedAsmInstr::instrs
list< AsmInstr > & instrs()
Definition: nmv-asm-instr.h:117
nemiver::common::MixedAsmInstr
Definition: nmv-asm-instr.h:81