remote_gdb.hh revision 2986:99640058db70
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 */
30
31#ifndef __REMOTE_GDB_HH__
32#define __REMOTE_GDB_HH__
33
34#include <map>
35
36#include "arch/types.hh"
37#include "base/kgdb.h"
38#include "cpu/pc_event.hh"
39#include "base/pollevent.hh"
40#include "base/socket.hh"
41
42class System;
43class ThreadContext;
44class PhysicalMemory;
45
46class GDBListener;
47class RemoteGDB
48{
49  protected:
50    typedef TheISA::MachInst MachInst;
51  private:
52    friend void debugger();
53    friend class GDBListener;
54
55  protected:
56    class Event : public PollEvent
57    {
58      protected:
59        RemoteGDB *gdb;
60
61      public:
62        Event(RemoteGDB *g, int fd, int e);
63        void process(int revent);
64    };
65
66    friend class Event;
67    Event *event;
68    GDBListener *listener;
69    int number;
70
71  protected:
72    int fd;
73    uint64_t gdbregs[KGDB_NUMREGS];
74
75  protected:
76#ifdef notyet
77    label_t recover;
78#endif
79    bool active;
80    bool attached;
81
82    System *system;
83    PhysicalMemory *pmem;
84    ThreadContext *context;
85
86  protected:
87    uint8_t getbyte();
88    void putbyte(uint8_t b);
89
90    int recv(char *data, int len);
91    void send(const char *data);
92
93  protected:
94    // Machine memory
95    bool read(Addr addr, size_t size, char *data);
96    bool write(Addr addr, size_t size, const char *data);
97
98    template <class T> T read(Addr addr);
99    template <class T> void write(Addr addr, T data);
100
101  public:
102    RemoteGDB(System *system, ThreadContext *context);
103    ~RemoteGDB();
104
105    void replaceThreadContext(ThreadContext *tc) { context = tc; }
106
107    void attach(int fd);
108    void detach();
109    bool isattached();
110
111    bool acc(Addr addr, size_t len);
112    static int signal(int type);
113    bool trap(int type);
114
115  protected:
116    void getregs();
117    void setregs();
118
119    void clearSingleStep();
120    void setSingleStep();
121
122    PCEventQueue *getPcEventQueue();
123
124  protected:
125    class HardBreakpoint : public PCEvent
126    {
127      private:
128        RemoteGDB *gdb;
129
130      public:
131        int refcount;
132
133      public:
134        HardBreakpoint(RemoteGDB *_gdb, Addr addr);
135        std::string name() { return gdb->name() + ".hwbkpt"; }
136
137        virtual void process(ThreadContext *tc);
138    };
139    friend class HardBreakpoint;
140
141    typedef std::map<Addr, HardBreakpoint *> break_map_t;
142    typedef break_map_t::iterator break_iter_t;
143    break_map_t hardBreakMap;
144
145    bool insertSoftBreak(Addr addr, size_t len);
146    bool removeSoftBreak(Addr addr, size_t len);
147    bool insertHardBreak(Addr addr, size_t len);
148    bool removeHardBreak(Addr addr, size_t len);
149
150  protected:
151    struct TempBreakpoint {
152        Addr	address;		// set here
153        MachInst	bkpt_inst;		// saved instruction at bkpt
154        int		init_count;		// number of times to skip bkpt
155        int		count;			// current count
156    };
157
158    TempBreakpoint notTakenBkpt;
159    TempBreakpoint takenBkpt;
160
161    void clearTempBreakpoint(TempBreakpoint &bkpt);
162    void setTempBreakpoint(TempBreakpoint &bkpt, Addr addr);
163
164  public:
165    std::string name();
166};
167
168template <class T>
169inline T
170RemoteGDB::read(Addr addr)
171{
172    T temp;
173    read(addr, sizeof(T), (char *)&temp);
174    return temp;
175}
176
177template <class T>
178inline void
179RemoteGDB::write(Addr addr, T data)
180{ write(addr, sizeof(T), (const char *)&data); }
181
182class GDBListener
183{
184  protected:
185    class Event : public PollEvent
186    {
187      protected:
188        GDBListener *listener;
189
190      public:
191        Event(GDBListener *l, int fd, int e);
192        void process(int revent);
193    };
194
195    friend class Event;
196    Event *event;
197
198  protected:
199    ListenSocket listener;
200    RemoteGDB *gdb;
201    int port;
202
203  public:
204    GDBListener(RemoteGDB *g, int p);
205    ~GDBListener();
206
207    void accept();
208    void listen();
209    std::string name();
210};
211
212#endif /* __REMOTE_GDB_H__ */
213