2 * Copyright (c) 2010 The FreeBSD Foundation
5 * This software was developed by Rui Paulo under sponsorship from the
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
41 static void rdl2prmap(const rd_loadobj_t *, prmap_t *);
44 map_iter(const rd_loadobj_t *lop, void *arg)
46 struct file_info *file;
47 struct map_info *mapping, *tmp;
48 struct proc_handle *phdl;
52 if (phdl->nmappings >= phdl->maparrsz) {
54 tmp = reallocarray(phdl->mappings, phdl->maparrsz,
55 sizeof(*phdl->mappings));
61 mapping = &phdl->mappings[phdl->nmappings];
62 rdl2prmap(lop, &mapping->map);
63 if (strcmp(lop->rdl_path, phdl->execpath) == 0 &&
64 (lop->rdl_prot & RD_RDL_X) != 0)
65 phdl->exec_map = &mapping->map;
68 if (lop->rdl_path[0] != '\0') {
69 /* Look for an existing mapping of the same file. */
70 for (i = 0; i < phdl->nmappings; i++)
71 if (strcmp(mapping->map.pr_mapname,
72 phdl->mappings[i].map.pr_mapname) == 0) {
73 file = phdl->mappings[i].file;
78 file = malloc(sizeof(*file));
93 rdl2prmap(const rd_loadobj_t *rdl, prmap_t *map)
96 map->pr_vaddr = rdl->rdl_saddr;
97 map->pr_size = rdl->rdl_eaddr - rdl->rdl_saddr;
98 map->pr_offset = rdl->rdl_offset;
100 if (rdl->rdl_prot & RD_RDL_R)
101 map->pr_mflags |= MA_READ;
102 if (rdl->rdl_prot & RD_RDL_W)
103 map->pr_mflags |= MA_WRITE;
104 if (rdl->rdl_prot & RD_RDL_X)
105 map->pr_mflags |= MA_EXEC;
106 (void)strlcpy(map->pr_mapname, rdl->rdl_path,
107 sizeof(map->pr_mapname));
111 proc_rdagent(struct proc_handle *phdl)
114 if (phdl->rdap == NULL && phdl->status != PS_UNDEAD &&
115 phdl->status != PS_IDLE) {
116 if ((phdl->rdap = rd_new(phdl)) == NULL)
120 phdl->mappings = calloc(phdl->maparrsz,
121 sizeof(*phdl->mappings));
122 if (phdl->mappings == NULL)
124 if (rd_loadobj_iter(phdl->rdap, map_iter, phdl) != RD_OK)
131 proc_updatesyms(struct proc_handle *phdl)
134 memset(phdl->mappings, 0, sizeof(*phdl->mappings) * phdl->maparrsz);
135 rd_loadobj_iter(phdl->rdap, map_iter, phdl);