/inc/dfinstance.h

https://code.google.com/p/dwarftherapist/ · C Header · 184 lines · 118 code · 28 blank · 38 comment · 0 complexity · 9c7b6c5f46ab20c17cfe17c17ac49f57 MD5 · raw file

  1. /*
  2. Dwarf Therapist
  3. Copyright (c) 2009 Trey Stout (chmod)
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #ifndef DFINSTANCE_H
  21. #define DFINSTANCE_H
  22. #include <QtGui>
  23. #include "utils.h"
  24. class Dwarf;
  25. class Squad;
  26. class Word;
  27. class MemoryLayout;
  28. struct MemorySegment;
  29. class DFInstance : public QObject {
  30. Q_OBJECT
  31. public:
  32. DFInstance(QObject *parent=0);
  33. virtual ~DFInstance();
  34. // factory ctor
  35. virtual bool find_running_copy(bool connectUnknown = false) = 0;
  36. // accessors
  37. VIRTADDR get_heap_start_address() {return m_heap_start_address;}
  38. quint32 get_memory_correction() {return m_memory_correction;}
  39. VIRTADDR get_base_address() {return m_base_addr;}
  40. bool is_ok() {return m_is_ok;}
  41. WORD dwarf_race_id() {return m_dwarf_race_id;}
  42. QList<MemoryLayout*> get_layouts() { return m_memory_layouts.values(); }
  43. QDir get_df_dir() { return m_df_dir; }
  44. // brute force memory scanning methods
  45. bool is_valid_address(const VIRTADDR &addr);
  46. bool looks_like_vector_of_pointers(const VIRTADDR &addr);
  47. // revamped memory reading
  48. virtual int read_raw(const VIRTADDR &addr, int bytes, QByteArray &buf) = 0;
  49. virtual BYTE read_byte(const VIRTADDR &addr);
  50. virtual WORD read_word(const VIRTADDR &addr);
  51. virtual VIRTADDR read_addr(const VIRTADDR &addr);
  52. virtual qint16 read_short(const VIRTADDR &addr);
  53. virtual qint32 read_int(const VIRTADDR &addr);
  54. // memory reading
  55. virtual QVector<VIRTADDR> enumerate_vector(const VIRTADDR &addr) = 0;
  56. virtual QString read_string(const VIRTADDR &addr) = 0;
  57. QVector<VIRTADDR> scan_mem(const QByteArray &needle, const uint start_addr=0, const uint end_addr=0xffffffff);
  58. QByteArray get_data(const VIRTADDR &addr, int size);
  59. QString pprint(const VIRTADDR &addr, int size);
  60. QString pprint(const QByteArray &ba, const VIRTADDR &start_addr=0);
  61. Word * read_dwarf_word(const VIRTADDR &addr);
  62. QString read_dwarf_name(const VIRTADDR &addr);
  63. // Mapping methods
  64. QVector<VIRTADDR> find_vectors_in_range(const int &max_entries,
  65. const VIRTADDR &start_address,
  66. const int &range_length);
  67. QVector<VIRTADDR> find_vectors(int num_entries, int fuzz=0,
  68. int entry_size=4);
  69. QVector<VIRTADDR> find_vectors_ext(int num_entries, const char op,
  70. const uint start_addr, const uint end_addr, int entry_size=4);
  71. QVector<VIRTADDR> find_vectors(int num_entries, const QVector<VIRTADDR> & search_set,
  72. int fuzz=0, int entry_size=4);
  73. // Methods for when we know how the data is layed out
  74. MemoryLayout *memory_layout() {return m_layout;}
  75. void read_raws();
  76. QVector<Dwarf*> load_dwarves();
  77. QVector<Squad*> load_squads();
  78. // Set layout
  79. void set_memory_layout(MemoryLayout * layout) { m_layout = layout; }
  80. // Writing
  81. virtual int write_raw(const VIRTADDR &addr, const int &bytes,
  82. void *buffer) = 0;
  83. virtual int write_string(const VIRTADDR &addr, const QString &str) = 0;
  84. virtual int write_int(const VIRTADDR &addr, const int &val) = 0;
  85. bool add_new_layout(const QString & version, QFile & file);
  86. void layout_not_found(const QString & checksum);
  87. bool is_attached() {return m_attach_count > 0;}
  88. virtual bool attach() = 0;
  89. virtual bool detach() = 0;
  90. static bool authorize();
  91. static DFInstance * newInstance();
  92. // Windows string offsets
  93. #ifdef Q_WS_WIN
  94. static const int STRING_BUFFER_OFFSET = 4; // Default value for older windows releases
  95. static const int STRING_LENGTH_OFFSET = 16; // Relative to STRING_BUFFER_OFFSET
  96. static const int STRING_CAP_OFFSET = 20; // Relative to STRING_BUFFER_OFFSET
  97. static const int VECTOR_POINTER_OFFSET = 4;
  98. #endif
  99. #ifdef Q_WS_X11
  100. static const int STRING_BUFFER_OFFSET = 0;
  101. static const int STRING_LENGTH_OFFSET = 0; // Dummy value
  102. static const int STRING_CAP_OFFSET = 0; // Dummy value
  103. static const int VECTOR_POINTER_OFFSET = 0;
  104. #endif
  105. #ifdef Q_WS_MAC
  106. static const int STRING_BUFFER_OFFSET = 0;
  107. static const int STRING_LENGTH_OFFSET = 0; // Dummy value
  108. static const int STRING_CAP_OFFSET = 0; // Dummy value
  109. static const int VECTOR_POINTER_OFFSET = 0;
  110. #endif
  111. // handy util methods
  112. virtual quint32 calculate_checksum() = 0;
  113. MemoryLayout *get_memory_layout(QString checksum, bool warn = true);
  114. public slots:
  115. // if a menu cancels our scan, we need to know how to stop
  116. void cancel_scan() {m_stop_scan = true;}
  117. protected:
  118. int m_pid;
  119. VIRTADDR m_base_addr;
  120. quint32 m_memory_correction;
  121. VIRTADDR m_lowest_address;
  122. VIRTADDR m_highest_address;
  123. VIRTADDR m_heap_start_address;
  124. bool m_stop_scan; // flag that gets set to stop scan loops
  125. bool m_is_ok;
  126. int m_bytes_scanned;
  127. MemoryLayout *m_layout;
  128. QVector<MemorySegment*> m_regions;
  129. int m_attach_count;
  130. QTimer *m_heartbeat_timer;
  131. QTimer *m_memory_remap_timer;
  132. QTimer *m_scan_speed_timer;
  133. WORD m_dwarf_race_id;
  134. QDir m_df_dir;
  135. /*! this hash will hold a map of all loaded and valid memory layouts found
  136. on disk, the key is a QString of the checksum since other OSs will use
  137. an MD5 of the binary instead of a PE timestamp */
  138. QHash<QString, MemoryLayout*> m_memory_layouts; // checksum->layout
  139. private slots:
  140. void heartbeat();
  141. void calculate_scan_rate();
  142. virtual void map_virtual_memory() = 0;
  143. signals:
  144. // methods for sending progress information to QWidgets
  145. void scan_total_steps(int steps);
  146. void scan_progress(int step);
  147. void scan_message(const QString &message);
  148. void connection_interrupted();
  149. void progress_message(const QString &message);
  150. void progress_range(int min, int max);
  151. void progress_value(int value);
  152. };
  153. #endif // DFINSTANCE_H