Refactor parsing of /proc/<pid>/smaps
The Linux kernel exposes the information about MTE-protected pages via the proc filesystem, more specifically through the smaps file. What we're looking for is a mapping with the 'mt' flag, which tells us that mapping was created with a PROT_MTE flag and, thus, is capable of using memory tagging. We already parse that file for other purposes (core file generation/filtering), so this patch refactors the code to make the parsing of the smaps file reusable for memory tagging. The function linux_address_in_memtag_page uses the refactored code to allow querying for memory tag support in a particular address, and it gets used in the next patch. gdb/ChangeLog: 2021-03-24 Luis Machado <luis.machado@linaro.org> * linux-tdep.c (struct smaps_vmflags) <memory_tagging>: New flag bit. (struct smaps_data): New struct. (decode_vmflags): Handle the 'mt' flag. (parse_smaps_data): New function, refactored from linux_find_memory_regions_full. (linux_address_in_memtag_page): New function. (linux_find_memory_regions_full): Refactor into parse_smaps_data. * linux-tdep.h (linux_address_in_memtag_page): New prototype.
This commit is contained in:
parent
93e447c605
commit
1e735120b9
3 changed files with 251 additions and 122 deletions
|
@ -1,3 +1,15 @@
|
||||||
|
2021-03-24 Luis Machado <luis.machado@linaro.org>
|
||||||
|
|
||||||
|
* linux-tdep.c (struct smaps_vmflags) <memory_tagging>: New flag
|
||||||
|
bit.
|
||||||
|
(struct smaps_data): New struct.
|
||||||
|
(decode_vmflags): Handle the 'mt' flag.
|
||||||
|
(parse_smaps_data): New function, refactored from
|
||||||
|
linux_find_memory_regions_full.
|
||||||
|
(linux_address_in_memtag_page): New function.
|
||||||
|
(linux_find_memory_regions_full): Refactor into parse_smaps_data.
|
||||||
|
* linux-tdep.h (linux_address_in_memtag_page): New prototype.
|
||||||
|
|
||||||
2021-03-24 Luis Machado <luis.machado@linaro.org>
|
2021-03-24 Luis Machado <luis.machado@linaro.org>
|
||||||
|
|
||||||
* linux-tdep.c (linux_find_memory_regions_full): Use std::string
|
* linux-tdep.c (linux_find_memory_regions_full): Use std::string
|
||||||
|
|
245
gdb/linux-tdep.c
245
gdb/linux-tdep.c
|
@ -88,8 +88,33 @@ struct smaps_vmflags
|
||||||
/* Is this a MAP_SHARED mapping (VM_SHARED, "sh"). */
|
/* Is this a MAP_SHARED mapping (VM_SHARED, "sh"). */
|
||||||
|
|
||||||
unsigned int shared_mapping : 1;
|
unsigned int shared_mapping : 1;
|
||||||
|
|
||||||
|
/* Memory map has memory tagging enabled. */
|
||||||
|
|
||||||
|
unsigned int memory_tagging : 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Data structure that holds the information contained in the
|
||||||
|
/proc/<pid>/smaps file. */
|
||||||
|
|
||||||
|
struct smaps_data
|
||||||
|
{
|
||||||
|
ULONGEST start_address;
|
||||||
|
ULONGEST end_address;
|
||||||
|
std::string filename;
|
||||||
|
struct smaps_vmflags vmflags;
|
||||||
|
bool read;
|
||||||
|
bool write;
|
||||||
|
bool exec;
|
||||||
|
bool priv;
|
||||||
|
bool has_anonymous;
|
||||||
|
bool mapping_anon_p;
|
||||||
|
bool mapping_file_p;
|
||||||
|
|
||||||
|
ULONGEST inode;
|
||||||
|
ULONGEST offset;
|
||||||
|
};
|
||||||
|
|
||||||
/* Whether to take the /proc/PID/coredump_filter into account when
|
/* Whether to take the /proc/PID/coredump_filter into account when
|
||||||
generating a corefile. */
|
generating a corefile. */
|
||||||
|
|
||||||
|
@ -476,6 +501,8 @@ decode_vmflags (char *p, struct smaps_vmflags *v)
|
||||||
v->exclude_coredump = 1;
|
v->exclude_coredump = 1;
|
||||||
else if (strcmp (s, "sh") == 0)
|
else if (strcmp (s, "sh") == 0)
|
||||||
v->shared_mapping = 1;
|
v->shared_mapping = 1;
|
||||||
|
else if (strcmp (s, "mt") == 0)
|
||||||
|
v->memory_tagging = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1271,63 +1298,24 @@ typedef int linux_dump_mapping_p_ftype (filter_flags filterflags,
|
||||||
ULONGEST addr,
|
ULONGEST addr,
|
||||||
ULONGEST offset);
|
ULONGEST offset);
|
||||||
|
|
||||||
/* List memory regions in the inferior for a corefile. */
|
/* Helper function to parse the contents of /proc/<pid>/smaps into a data
|
||||||
|
structure, for easy access.
|
||||||
|
|
||||||
static int
|
DATA is the contents of the smaps file. The parsed contents are stored
|
||||||
linux_find_memory_regions_full (struct gdbarch *gdbarch,
|
into the SMAPS vector. */
|
||||||
linux_dump_mapping_p_ftype *should_dump_mapping_p,
|
|
||||||
linux_find_memory_region_ftype *func,
|
static std::vector<struct smaps_data>
|
||||||
void *obfd)
|
parse_smaps_data (const char *data,
|
||||||
|
const std::string maps_filename)
|
||||||
{
|
{
|
||||||
pid_t pid;
|
|
||||||
/* Default dump behavior of coredump_filter (0x33), according to
|
|
||||||
Documentation/filesystems/proc.txt from the Linux kernel
|
|
||||||
tree. */
|
|
||||||
filter_flags filterflags = (COREFILTER_ANON_PRIVATE
|
|
||||||
| COREFILTER_ANON_SHARED
|
|
||||||
| COREFILTER_ELF_HEADERS
|
|
||||||
| COREFILTER_HUGETLB_PRIVATE);
|
|
||||||
|
|
||||||
/* We need to know the real target PID to access /proc. */
|
|
||||||
if (current_inferior ()->fake_pid_p)
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
pid = current_inferior ()->pid;
|
|
||||||
|
|
||||||
if (use_coredump_filter)
|
|
||||||
{
|
|
||||||
std::string core_dump_filter_name
|
|
||||||
= string_printf ("/proc/%d/coredump_filter", pid);
|
|
||||||
|
|
||||||
gdb::unique_xmalloc_ptr<char> coredumpfilterdata
|
|
||||||
= target_fileio_read_stralloc (NULL, core_dump_filter_name.c_str ());
|
|
||||||
|
|
||||||
if (coredumpfilterdata != NULL)
|
|
||||||
{
|
|
||||||
unsigned int flags;
|
|
||||||
|
|
||||||
sscanf (coredumpfilterdata.get (), "%x", &flags);
|
|
||||||
filterflags = (enum filter_flag) flags;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string maps_filename = string_printf ("/proc/%d/smaps", pid);
|
|
||||||
|
|
||||||
gdb::unique_xmalloc_ptr<char> data
|
|
||||||
= target_fileio_read_stralloc (NULL, maps_filename.c_str ());
|
|
||||||
|
|
||||||
if (data == NULL)
|
|
||||||
{
|
|
||||||
/* Older Linux kernels did not support /proc/PID/smaps. */
|
|
||||||
maps_filename = string_printf ("/proc/%d/maps", pid);
|
|
||||||
data = target_fileio_read_stralloc (NULL, maps_filename.c_str ());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data != NULL)
|
|
||||||
{
|
|
||||||
char *line, *t;
|
char *line, *t;
|
||||||
|
|
||||||
line = strtok_r (data.get (), "\n", &t);
|
gdb_assert (data != nullptr);
|
||||||
|
|
||||||
|
line = strtok_r ((char *) data, "\n", &t);
|
||||||
|
|
||||||
|
std::vector<struct smaps_data> smaps;
|
||||||
|
|
||||||
while (line != NULL)
|
while (line != NULL)
|
||||||
{
|
{
|
||||||
ULONGEST addr, endaddr, offset, inode;
|
ULONGEST addr, endaddr, offset, inode;
|
||||||
|
@ -1336,7 +1324,6 @@ linux_find_memory_regions_full (struct gdbarch *gdbarch,
|
||||||
size_t permissions_len, device_len;
|
size_t permissions_len, device_len;
|
||||||
int read, write, exec, priv;
|
int read, write, exec, priv;
|
||||||
int has_anonymous = 0;
|
int has_anonymous = 0;
|
||||||
int should_dump_p = 0;
|
|
||||||
int mapping_anon_p;
|
int mapping_anon_p;
|
||||||
int mapping_file_p;
|
int mapping_file_p;
|
||||||
|
|
||||||
|
@ -1427,12 +1414,138 @@ linux_find_memory_regions_full (struct gdbarch *gdbarch,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/* Save the smaps entry to the vector. */
|
||||||
|
struct smaps_data map;
|
||||||
|
|
||||||
if (has_anonymous)
|
map.start_address = addr;
|
||||||
should_dump_p = should_dump_mapping_p (filterflags, &v, priv,
|
map.end_address = endaddr;
|
||||||
mapping_anon_p,
|
map.filename = filename;
|
||||||
mapping_file_p,
|
map.vmflags = v;
|
||||||
filename, addr, offset);
|
map.read = read? true : false;
|
||||||
|
map.write = write? true : false;
|
||||||
|
map.exec = exec? true : false;
|
||||||
|
map.priv = priv? true : false;
|
||||||
|
map.has_anonymous = has_anonymous;
|
||||||
|
map.mapping_anon_p = mapping_anon_p? true : false;
|
||||||
|
map.mapping_file_p = mapping_file_p? true : false;
|
||||||
|
map.offset = offset;
|
||||||
|
map.inode = inode;
|
||||||
|
|
||||||
|
smaps.emplace_back (map);
|
||||||
|
}
|
||||||
|
|
||||||
|
return smaps;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* See linux-tdep.h. */
|
||||||
|
|
||||||
|
bool
|
||||||
|
linux_address_in_memtag_page (CORE_ADDR address)
|
||||||
|
{
|
||||||
|
if (current_inferior ()->fake_pid_p)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
pid_t pid = current_inferior ()->pid;
|
||||||
|
|
||||||
|
std::string smaps_file = string_printf ("/proc/%d/smaps", pid);
|
||||||
|
|
||||||
|
gdb::unique_xmalloc_ptr<char> data
|
||||||
|
= target_fileio_read_stralloc (NULL, smaps_file.c_str ());
|
||||||
|
|
||||||
|
if (data == nullptr)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
/* Parse the contents of smaps into a vector. */
|
||||||
|
std::vector<struct smaps_data> smaps
|
||||||
|
= parse_smaps_data (data.get (), smaps_file);
|
||||||
|
|
||||||
|
for (const smaps_data &map : smaps)
|
||||||
|
{
|
||||||
|
/* Is the address within [start_address, end_address) in a page
|
||||||
|
mapped with memory tagging? */
|
||||||
|
if (address >= map.start_address
|
||||||
|
&& address < map.end_address
|
||||||
|
&& map.vmflags.memory_tagging)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* List memory regions in the inferior for a corefile. */
|
||||||
|
|
||||||
|
static int
|
||||||
|
linux_find_memory_regions_full (struct gdbarch *gdbarch,
|
||||||
|
linux_dump_mapping_p_ftype *should_dump_mapping_p,
|
||||||
|
linux_find_memory_region_ftype *func,
|
||||||
|
void *obfd)
|
||||||
|
{
|
||||||
|
pid_t pid;
|
||||||
|
/* Default dump behavior of coredump_filter (0x33), according to
|
||||||
|
Documentation/filesystems/proc.txt from the Linux kernel
|
||||||
|
tree. */
|
||||||
|
filter_flags filterflags = (COREFILTER_ANON_PRIVATE
|
||||||
|
| COREFILTER_ANON_SHARED
|
||||||
|
| COREFILTER_ELF_HEADERS
|
||||||
|
| COREFILTER_HUGETLB_PRIVATE);
|
||||||
|
|
||||||
|
/* We need to know the real target PID to access /proc. */
|
||||||
|
if (current_inferior ()->fake_pid_p)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
pid = current_inferior ()->pid;
|
||||||
|
|
||||||
|
if (use_coredump_filter)
|
||||||
|
{
|
||||||
|
std::string core_dump_filter_name
|
||||||
|
= string_printf ("/proc/%d/coredump_filter", pid);
|
||||||
|
|
||||||
|
gdb::unique_xmalloc_ptr<char> coredumpfilterdata
|
||||||
|
= target_fileio_read_stralloc (NULL, core_dump_filter_name.c_str ());
|
||||||
|
|
||||||
|
if (coredumpfilterdata != NULL)
|
||||||
|
{
|
||||||
|
unsigned int flags;
|
||||||
|
|
||||||
|
sscanf (coredumpfilterdata.get (), "%x", &flags);
|
||||||
|
filterflags = (enum filter_flag) flags;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string maps_filename = string_printf ("/proc/%d/smaps", pid);
|
||||||
|
|
||||||
|
gdb::unique_xmalloc_ptr<char> data
|
||||||
|
= target_fileio_read_stralloc (NULL, maps_filename.c_str ());
|
||||||
|
|
||||||
|
if (data == NULL)
|
||||||
|
{
|
||||||
|
/* Older Linux kernels did not support /proc/PID/smaps. */
|
||||||
|
maps_filename = string_printf ("/proc/%d/maps", pid);
|
||||||
|
data = target_fileio_read_stralloc (NULL, maps_filename.c_str ());
|
||||||
|
|
||||||
|
if (data == nullptr)
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Parse the contents of smaps into a vector. */
|
||||||
|
std::vector<struct smaps_data> smaps
|
||||||
|
= parse_smaps_data (data.get (), maps_filename.c_str ());
|
||||||
|
|
||||||
|
for (const struct smaps_data &map : smaps)
|
||||||
|
{
|
||||||
|
int should_dump_p = 0;
|
||||||
|
|
||||||
|
if (map.has_anonymous)
|
||||||
|
{
|
||||||
|
should_dump_p
|
||||||
|
= should_dump_mapping_p (filterflags, &map.vmflags,
|
||||||
|
map.priv,
|
||||||
|
map.mapping_anon_p,
|
||||||
|
map.mapping_file_p,
|
||||||
|
map.filename.c_str (),
|
||||||
|
map.start_address,
|
||||||
|
map.offset);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* Older Linux kernels did not support the "Anonymous:" counter.
|
/* Older Linux kernels did not support the "Anonymous:" counter.
|
||||||
|
@ -1442,16 +1555,16 @@ linux_find_memory_regions_full (struct gdbarch *gdbarch,
|
||||||
|
|
||||||
/* Invoke the callback function to create the corefile segment. */
|
/* Invoke the callback function to create the corefile segment. */
|
||||||
if (should_dump_p)
|
if (should_dump_p)
|
||||||
func (addr, endaddr - addr, offset, inode,
|
{
|
||||||
read, write, exec, 1, /* MODIFIED is true because we
|
func (map.start_address, map.end_address - map.start_address,
|
||||||
want to dump the mapping. */
|
map.offset, map.inode, map.read, map.write, map.exec,
|
||||||
filename, obfd);
|
1, /* MODIFIED is true because we want to dump
|
||||||
|
the mapping. */
|
||||||
|
map.filename.c_str (), obfd);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* A structure for passing information through
|
/* A structure for passing information through
|
||||||
|
|
|
@ -43,6 +43,10 @@ DEF_ENUM_FLAGS_TYPE (enum linux_siginfo_extra_field_values,
|
||||||
struct type *linux_get_siginfo_type_with_fields (struct gdbarch *gdbarch,
|
struct type *linux_get_siginfo_type_with_fields (struct gdbarch *gdbarch,
|
||||||
linux_siginfo_extra_fields);
|
linux_siginfo_extra_fields);
|
||||||
|
|
||||||
|
/* Return true if ADDRESS is within the boundaries of a page mapped with
|
||||||
|
memory tagging protection. */
|
||||||
|
bool linux_address_in_memtag_page (CORE_ADDR address);
|
||||||
|
|
||||||
typedef char *(*linux_collect_thread_registers_ftype) (const struct regcache *,
|
typedef char *(*linux_collect_thread_registers_ftype) (const struct regcache *,
|
||||||
ptid_t,
|
ptid_t,
|
||||||
bfd *, char *, int *,
|
bfd *, char *, int *,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue