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