PageRenderTime 48ms CodeModel.GetById 27ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

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