forked from crash-utility/crash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crash_target.c
135 lines (110 loc) · 4.09 KB
/
crash_target.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* crash_target.c
*
* Copyright (c) 2021 VMware, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Author: Alexey Makhalov <[email protected]>
*/
#include <defs.h>
#include "top.h"
#include "target.h"
#include "inferior.h"
#include "regcache.h"
#include "gdbarch.h"
void crash_target_init (void);
extern "C" int gdb_readmem_callback(unsigned long, void *, int, int);
extern "C" int crash_get_nr_cpus(void);
extern "C" int crash_get_cpu_reg (int cpu, int regno, const char *regname,
int regsize, void *val);
/* The crash target. */
static const target_info crash_target_info = {
"crash",
N_("Local core dump file"),
N_("Use a built-in crash instance as a target.")
};
class crash_target final : public process_stratum_target
{
public:
const target_info &info () const override
{ return crash_target_info; }
void fetch_registers (struct regcache *, int) override;
enum target_xfer_status xfer_partial (enum target_object object,
const char *annex,
gdb_byte *readbuf,
const gdb_byte *writebuf,
ULONGEST offset, ULONGEST len,
ULONGEST *xfered_len) override;
bool has_all_memory () override { return true; }
bool has_memory () override { return true; }
bool has_stack () override { return true; }
bool has_registers () override { return true; }
bool thread_alive (ptid_t ptid) override { return true; }
std::string pid_to_str (ptid_t ptid) override
{ return string_printf ("CPU %ld", ptid.tid ()); }
};
/* We just get all the registers, so we don't use regno. */
void
crash_target::fetch_registers (struct regcache *regcache, int regno)
{
gdb_byte regval[16];
int cpu = inferior_ptid.tid();
struct gdbarch *arch = regcache->arch ();
for (int r = 0; r < gdbarch_num_regs (arch); r++)
{
const char *regname = gdbarch_register_name(arch, r);
int regsize = register_size (arch, r);
if (regsize > sizeof (regval))
error (_("fatal error: buffer size is not enough to fit register value"));
if (crash_get_cpu_reg (cpu, r, regname, regsize, (void *)®val))
regcache->raw_supply (r, regval);
else
regcache->raw_supply (r, NULL);
}
}
enum target_xfer_status
crash_target::xfer_partial (enum target_object object, const char *annex,
gdb_byte *readbuf, const gdb_byte *writebuf,
ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
{
if (object != TARGET_OBJECT_MEMORY && object != TARGET_OBJECT_STACK_MEMORY
&& object != TARGET_OBJECT_CODE_MEMORY)
return TARGET_XFER_E_IO;
if (gdb_readmem_callback(offset, (void *)(readbuf ? readbuf : writebuf), len, !readbuf))
{
*xfered_len = len;
return TARGET_XFER_OK;
}
return TARGET_XFER_E_IO;
}
#define CRASH_INFERIOR_PID 1
void
crash_target_init (void)
{
int nr_cpus = crash_get_nr_cpus();
crash_target *target = new crash_target ();
/* Own the target until it is successfully pushed. */
target_ops_up target_holder (target);
push_target (std::move (target_holder));
inferior_appeared (current_inferior (), CRASH_INFERIOR_PID);
for (int i = 0; i < nr_cpus; i++)
{
thread_info *thread = add_thread_silent (target,
ptid_t(CRASH_INFERIOR_PID, 0, i));
if (!i)
switch_to_thread (thread);
}
/* Fetch all registers from core file. */
target_fetch_registers (get_current_regcache (), -1);
/* Now, set up the frame cache. */
reinit_frame_cache ();
}