ROSE 0.11.145.147
Linux.h
1#ifndef ROSE_BinaryAnalysis_Debugger_Linux_H
2#define ROSE_BinaryAnalysis_Debugger_Linux_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_DEBUGGER_LINUX
5#include <Rose/BinaryAnalysis/Debugger/Base.h>
6
7#include <Rose/BinaryAnalysis/AddressIntervalSet.h>
8#include <Rose/BinaryAnalysis/SystemCall.h>
9
10#include <Sawyer/Optional.h>
11#include <sys/ptrace.h>
12
13namespace Rose {
14namespace BinaryAnalysis {
15namespace Debugger {
16
18class Linux: public Base {
20 // Types
22public:
24 using Ptr = LinuxPtr;
25
27 enum class DetachMode {
28 KILL,
29 DETACH,
30 CONTINUE,
31 NOTHING
32 };
33
35 enum class Flag {
36 ATTACH = 0x00000001,
37 REDIRECT_INPUT = 0x00000002,
38 REDIRECT_OUTPUT = 0x00000004,
39 REDIRECT_ERROR = 0x00000008,
40 CLOSE_FILES = 0x00000010,
41 DEFAULT_FLAGS = 0x00000013
42 };
43
47 class Specimen {
48 BitFlags<Flag> flags_; // operational flags
49 unsigned long persona_; // personality(2) flags
50
51 // Members for running a program
52 boost::filesystem::path program_; // full path of executable program
53 std::vector<std::string> arguments_; // command-line arguments of executable program
54 boost::filesystem::path workingDirectory_; // name or working directory, or use CWD if empty
55 std::vector<boost::regex> clearEnvVars_; // clear environment variables matching these patterns
56 std::map<std::string, std::string> setEnvVars_; // environment variables to be set
57
58 // Members for attaching to a process
59 int pid_ = -1; // process ID (int instead of pid_t for portability)
60
61 public:
63 Specimen();
64
66 Specimen(int pid); // implicit
67
69 Specimen(const boost::filesystem::path&); // implicit
70
72 Specimen(const boost::filesystem::path &name, const std::vector<std::string> &args);
73
75 Specimen(const std::vector<std::string> &nameAndArgs); // implicit
76
77 public:
84 const boost::filesystem::path& program() const;
85 void program(const boost::filesystem::path&);
94 const std::vector<std::string>& arguments() const;
95 void arguments(const std::vector<std::string>&);
103 void eraseEnvironmentVariable(const std::string&);
104
110 void eraseMatchingEnvironmentVariables(const boost::regex&);
111
115 void eraseAllEnvironmentVariables();
116
122 void insertEnvironmentVariable(const std::string &name, const std::string &value);
123
131 boost::filesystem::path workingDirectory() const;
132 void workingDirectory(const boost::filesystem::path&);
140 const BitFlags<Flag>& flags() const;
141 BitFlags<Flag>& flags();
151 unsigned long persona() const;
152 void persona(unsigned long bits);
163 bool randomizedAddresses() const;
164 void randomizedAddresses(bool);
173 int process() const;
174 void process(int pid);
178 void print(std::ostream &out) const;
179
180 // Used internally.
181 char** prepareEnvAdjustments() const;
182 };
183
185 // Private types
187private:
189
190 // What type of register values are cached?
191 enum class RegCacheType { NONE, REGS, FPREGS };
192
193 // Low-level collection of register values.
194 using RegPage = std::array<uint8_t, 512>;
195
196 // Low-level structure containing values for all registers.
197 struct AllRegValues {
198 RegPage regs; // integer registers
199 RegPage fpregs; // floating-point registers
200 };
201
202
204 // Data members
206private:
207 Specimen specimen_; // description of specimen being debugged
208 int child_ = 0; // process being debugged (int, not pid_t, for Windows portability)
209 DetachMode autoDetach_ = DetachMode::KILL; // how to detach from the subordinate when deleting this debugger
210 int wstat_ = -1; // last status from waitpid
211 AddressIntervalSet breakPoints_; // list of break point addresses
212 int sendSignal_ = 0; // pending signal
213 UserRegDefs userRegDefs_; // how registers map to user_regs_struct in <sys/user.h>
214 UserRegDefs userFpRegDefs_; // how registers map to user_fpregs_struct in <sys/user.h>
215 size_t kernelWordSize_ = 0; // cached width in bits of kernel's words
216 RegPage regCache_; // latest register information read from subordinate
217 RegCacheType regCacheType_ = RegCacheType::NONE; // what are the contents of regsPage_?
218 Sawyer::Optional<rose_addr_t> syscallVa_; // address of some executable system call instruction.
219 SystemCall syscallDecls_; // to get declarations for system calls
220
222 // Initialization and destruction.
224public:
228 static Ptr instance();
229
231 static Ptr instance(const Specimen &specimen, Sawyer::Optional<DetachMode> onDelete = Sawyer::Nothing());
232
233 Linux();
234 ~Linux();
235
237 // Attaching and detaching
239public:
247 void attach(const Specimen&, Sawyer::Optional<DetachMode> onDelete = Sawyer::Nothing());
248
250 Sawyer::Optional<int> processId() const;
251
257 DetachMode detachMode() const;
258 void detachMode(DetachMode);
264 size_t kernelWordSize();
265
267 int waitpidStatus() const;
268
270 // Running
272public:
274 void stepIntoSystemCall(ThreadId);
275
280 void runToSystemCall(ThreadId);
281
283 // System calls
285public:
289 int64_t remoteSystemCall(ThreadId, int syscallNumber);
290 int64_t remoteSystemCall(ThreadId, int syscallNumber,
291 uint64_t arg1);
292 int64_t remoteSystemCall(ThreadId, int syscallNumber,
293 uint64_t arg1, uint64_t arg2);
294 int64_t remoteSystemCall(ThreadId, int syscallNumber,
295 uint64_t arg1, uint64_t arg2, uint64_t arg3);
296 int64_t remoteSystemCall(ThreadId, int syscallNumber,
297 uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4);
298 int64_t remoteSystemCall(ThreadId, int syscallNumber,
299 uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5);
300 int64_t remoteSystemCall(ThreadId, int syscallNumber,
301 uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5, uint64_t arg6);
302 int64_t remoteSystemCall(ThreadId, int syscallNumber, std::vector<uint64_t> args);
309 int remoteOpenFile(ThreadId, const boost::filesystem::path &fileName, unsigned flags, mode_t mode);
310
312 int remoteCloseFile(ThreadId, unsigned remoteFd);
313
317 rose_addr_t remoteMmap(ThreadId, rose_addr_t va, size_t nBytes, unsigned prot, unsigned flags, const boost::filesystem::path&,
318 off_t offset);
319
321 // Overrides for methods declared and documented in the super class.
323public:
324 virtual bool isAttached() override;
325 virtual void detach() override;
326 virtual void terminate() override;
327 virtual std::vector<ThreadId> threadIds() override;
328 virtual void executionAddress(ThreadId, rose_addr_t) override;
329 virtual rose_addr_t executionAddress(ThreadId) override;
330 virtual void setBreakPoint(const AddressInterval&) override;
331 virtual void clearBreakPoint(const AddressInterval&) override;
332 virtual void clearBreakPoints() override;
333 virtual void singleStep(ThreadId) override;
334 virtual void runToBreakPoint(ThreadId) override;
335 virtual Sawyer::Container::BitVector readRegister(ThreadId, RegisterDescriptor) override;
336 virtual void writeRegister(ThreadId, RegisterDescriptor, const Sawyer::Container::BitVector&) override;
337 virtual void writeRegister(ThreadId, RegisterDescriptor, uint64_t value) override;
338 virtual size_t readMemory(rose_addr_t va, size_t nBytes, uint8_t *buffer) override;
339 virtual std::vector<uint8_t> readMemory(rose_addr_t va, size_t nBytes) override;
340 virtual Sawyer::Container::BitVector readMemory(rose_addr_t va, size_t nBytes, ByteOrder::Endianness order) override;
341 virtual size_t writeMemory(rose_addr_t va, size_t nBytes, const uint8_t *bytes) override;
342 virtual bool isTerminated() override;
343 virtual std::string howTerminated() override;
344 virtual std::vector<RegisterDescriptor> availableRegisters() override;
345 virtual Sawyer::Container::BitVector readAllRegisters(ThreadId) override;
346 virtual void writeAllRegisters(ThreadId, const Sawyer::Container::BitVector&) override;
347
348private:
349 // Wait for subordinate or throw on error
350 void waitForChild();
351
352 // Open /dev/null with the specified flags as the indicated file descriptor, closing what was previously on that
353 // descriptor. If an error occurs, the targetFd is closed anyway.
354 void devNullTo(int targetFd, int openFlags);
355
356 // Get/set personality in a portable way
357 static unsigned long getPersonality();
358 static void setPersonality(unsigned long);
359
360 // Address of a system call instruction. The initial search can be expensive, so the result is cached.
361 Sawyer::Optional<rose_addr_t> findSystemCall();
362
363 // Low-level methods to read and write data for all known registers.
364 AllRegValues loadAllRegisters(ThreadId);
365 void saveAllRegisters(ThreadId, const AllRegValues&);
366
367 // Update the register cache to contain the specified register. Return the offset to the start of the register
368 // inside the register cache.
369 size_t updateRegCache(RegisterDescriptor);
370
371 // Send a ptrace command
372 long sendCommand(__ptrace_request, void *addr = nullptr, void *data = nullptr);
373 long sendCommandInt(__ptrace_request, void *addr, int i);
374
375 // Load system call declarations from the appropriate header file
376 void declareSystemCalls(size_t nBits);
377};
378
379std::ostream& operator<<(std::ostream&, const Linux::Specimen&);
380
381} // namespace
382} // namespace
383} // namespace
384
385#endif
386#endif
Container associating values with keys.
Definition Sawyer/Map.h:72
Represents no value.
Definition Optional.h:36
Holds a value or nothing.
Definition Optional.h:56
Flag
Flag to pass as type stringification style.
Sawyer::SharedPointer< Node > Ptr
Reference counting pointer.
@ CONTINUE
Continue the traversal as normal.
void print(const StackVariables &, const Partitioner2::PartitionerConstPtr &, std::ostream &out, const std::string &prefix="")
Print info about multiple local variables.
Sawyer::Container::IntervalSet< AddressInterval > AddressIntervalSet
A set of virtual addresses.
The ROSE library.
const char * RegCacheType(int64_t)
Convert Rose::BinaryAnalysis::Debugger::Linux::RegCacheType enum constant to a string.
const char * DetachMode(int64_t)
Convert Rose::BinaryAnalysis::Debugger::Linux::DetachMode enum constant to a string.