xref: /freebsd/sys/fs/procfs/procfs_map.c (revision 0fa02ea5f786ef02befd46f8f083f48c8cd9630b)
1 /*
2  * Copyright (c) 1993 Jan-Simon Pendry
3  * Copyright (c) 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Jan-Simon Pendry.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)procfs_status.c	8.3 (Berkeley) 2/17/94
38  *
39  * $FreeBSD$
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/lock.h>
45 #include <sys/filedesc.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/proc.h>
49 #include <sys/uio.h>
50 #include <sys/vnode.h>
51 
52 #include <fs/pseudofs/pseudofs.h>
53 #include <fs/procfs/procfs.h>
54 
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_object.h>
60 
61 
62 #define MEBUFFERSIZE 256
63 
64 /*
65  * The map entries can *almost* be read with programs like cat.  However,
66  * large maps need special programs to read.  It is not easy to implement
67  * a program that can sense the required size of the buffer, and then
68  * subsequently do a read with the appropriate size.  This operation cannot
69  * be atomic.  The best that we can do is to allow the program to do a read
70  * with an arbitrarily large buffer, and return as much as we can.  We can
71  * return an error code if the buffer is too small (EFBIG), then the program
72  * can try a bigger buffer.
73  */
74 int
75 procfs_doprocmap(PFS_FILL_ARGS)
76 {
77 	int len;
78 	int error;
79 	vm_map_t map = &p->p_vmspace->vm_map;
80 	pmap_t pmap = vmspace_pmap(p->p_vmspace);
81 	vm_map_entry_t entry;
82 	char mebuffer[MEBUFFERSIZE];
83 	char *fullpath, *freepath;
84 
85 	GIANT_REQUIRED;
86 
87 	PROC_LOCK(p);
88 	error = p_candebug(td, p);
89 	PROC_UNLOCK(p);
90 	if (error)
91 		return (error);
92 
93 	if (uio->uio_rw != UIO_READ)
94 		return (EOPNOTSUPP);
95 
96 	if (uio->uio_offset != 0)
97 		return (0);
98 
99 	error = 0;
100 	if (map != &curthread->td_proc->p_vmspace->vm_map)
101 		vm_map_lock_read(map);
102 	for (entry = map->header.next;
103 		((uio->uio_resid > 0) && (entry != &map->header));
104 		entry = entry->next) {
105 		vm_object_t obj, tobj, lobj;
106 		int ref_count, shadow_count, flags;
107 		vm_offset_t addr;
108 		int resident, privateresident;
109 		char *type;
110 
111 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
112 			continue;
113 
114 		obj = entry->object.vm_object;
115 		if (obj && (obj->shadow_count == 1))
116 			privateresident = obj->resident_page_count;
117 		else
118 			privateresident = 0;
119 
120 		resident = 0;
121 		addr = entry->start;
122 		while (addr < entry->end) {
123 			if (pmap_extract( pmap, addr))
124 				resident++;
125 			addr += PAGE_SIZE;
126 		}
127 
128 		for (lobj = tobj = obj; tobj; tobj = tobj->backing_object)
129 			lobj = tobj;
130 
131 		freepath = NULL;
132 		fullpath = "-";
133 		if (lobj) {
134 			switch(lobj->type) {
135 			default:
136 			case OBJT_DEFAULT:
137 				type = "default";
138 				break;
139 			case OBJT_VNODE:
140 				type = "vnode";
141 				vn_fullpath(td,
142 				    (struct vnode *)lobj->handle,
143 				    &fullpath,
144 				    &freepath);
145 				break;
146 			case OBJT_SWAP:
147 				type = "swap";
148 				break;
149 			case OBJT_DEVICE:
150 				type = "device";
151 				break;
152 			}
153 
154 			flags = obj->flags;
155 			ref_count = obj->ref_count;
156 			shadow_count = obj->shadow_count;
157 		} else {
158 			type = "none";
159 			flags = 0;
160 			ref_count = 0;
161 			shadow_count = 0;
162 		}
163 
164 		/*
165 		 * format:
166 		 *  start, end, resident, private resident, cow, access, type.
167 		 */
168 		snprintf(mebuffer, sizeof mebuffer,
169 		    "0x%lx 0x%lx %d %d %p %s%s%s %d %d 0x%x %s %s %s %s\n",
170 			(u_long)entry->start, (u_long)entry->end,
171 			resident, privateresident, obj,
172 			(entry->protection & VM_PROT_READ)?"r":"-",
173 			(entry->protection & VM_PROT_WRITE)?"w":"-",
174 			(entry->protection & VM_PROT_EXECUTE)?"x":"-",
175 			ref_count, shadow_count, flags,
176 			(entry->eflags & MAP_ENTRY_COW)?"COW":"NCOW",
177 			(entry->eflags & MAP_ENTRY_NEEDS_COPY)?"NC":"NNC",
178 			type, fullpath);
179 
180 		if (freepath != NULL)
181 			free(freepath, M_TEMP);
182 
183 		len = strlen(mebuffer);
184 		if (len > uio->uio_resid) {
185 			error = EFBIG;
186 			break;
187 		}
188 		error = uiomove(mebuffer, len, uio);
189 		if (error)
190 			break;
191 	}
192 	if (map != &curthread->td_proc->p_vmspace->vm_map)
193 		vm_map_unlock_read(map);
194 
195 	return (error);
196 }
197