2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 The FreeBSD Foundation
7 * This software was developed by Rui Paulo under sponsorship from the
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
43 static void rdl2prmap(const rd_loadobj_t *, prmap_t *);
46 map_iter(const rd_loadobj_t *lop, void *arg)
48 struct file_info *file;
49 struct map_info *mapping, *tmp;
50 struct proc_handle *phdl;
54 if (phdl->nmappings >= phdl->maparrsz) {
56 tmp = reallocarray(phdl->mappings, phdl->maparrsz,
57 sizeof(*phdl->mappings));
63 mapping = &phdl->mappings[phdl->nmappings];
64 rdl2prmap(lop, &mapping->map);
65 if (strcmp(lop->rdl_path, phdl->execpath) == 0 &&
66 (lop->rdl_prot & RD_RDL_X) != 0)
67 phdl->exec_map = phdl->nmappings;
70 if (lop->rdl_path[0] != '\0') {
71 /* Look for an existing mapping of the same file. */
72 for (i = 0; i < phdl->nmappings; i++)
73 if (strcmp(mapping->map.pr_mapname,
74 phdl->mappings[i].map.pr_mapname) == 0) {
75 file = phdl->mappings[i].file;
80 file = malloc(sizeof(*file));
95 rdl2prmap(const rd_loadobj_t *rdl, prmap_t *map)
98 map->pr_vaddr = rdl->rdl_saddr;
99 map->pr_size = rdl->rdl_eaddr - rdl->rdl_saddr;
100 map->pr_offset = rdl->rdl_offset;
102 if (rdl->rdl_prot & RD_RDL_R)
103 map->pr_mflags |= MA_READ;
104 if (rdl->rdl_prot & RD_RDL_W)
105 map->pr_mflags |= MA_WRITE;
106 if (rdl->rdl_prot & RD_RDL_X)
107 map->pr_mflags |= MA_EXEC;
108 (void)strlcpy(map->pr_mapname, rdl->rdl_path,
109 sizeof(map->pr_mapname));
113 proc_rdagent(struct proc_handle *phdl)
116 if (phdl->rdap == NULL && phdl->status != PS_UNDEAD &&
117 phdl->status != PS_IDLE) {
118 if ((phdl->rdap = rd_new(phdl)) == NULL)
122 phdl->mappings = calloc(phdl->maparrsz,
123 sizeof(*phdl->mappings));
124 if (phdl->mappings == NULL)
126 if (rd_loadobj_iter(phdl->rdap, map_iter, phdl) != RD_OK)
133 proc_updatesyms(struct proc_handle *phdl)
136 memset(phdl->mappings, 0, sizeof(*phdl->mappings) * phdl->maparrsz);
137 rd_loadobj_iter(phdl->rdap, map_iter, phdl);