-
Notifications
You must be signed in to change notification settings - Fork 3
/
listing2.html
executable file
·351 lines (291 loc) · 13.8 KB
/
listing2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<!-- BEGIN META TAG INFO -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="home" href="http://developer.apple.com/">
<link rel="find" href="http://developer.apple.com/search/">
<link rel="stylesheet" type="text/css" href="../../documentation/css/adcstyle.css" title="fonts">
<script language="JavaScript" src="../../documentation/js/adc.js" type="text/javascript"></script>
<!-- END META TAG INFO -->
<!-- BEGIN TITLE -->
<title>MachPortDump - /Read Me About MachPortDump.txt</title>
<!-- END TITLE -->
<script language="JavaScript">
function JumpToNewPage() {
window.location=document.scpopupmenu.gotop.value;
return true;
}
</script>
</head>
<!-- BEGIN BODY OPEN -->
<body>
<!--END BODY OPEN -->
<!-- START CENTER OPEN -->
<center>
<!-- END CENTER OPEN -->
<!-- BEGIN LOGO AND SEARCH -->
<!--#include virtual="/includes/adcnavbar"-->
<!-- END LOGO AND SEARCH -->
<!-- START BREADCRUMB -->
<div id="breadcrumb">
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td scope="row"><img width="340" height="10" src="images/1dot.gif" alt=""></td>
<td><img width="340" height="10" src="images/1dot.gif" alt=""></td>
</tr>
<tr valign="middle">
<td align="left" colspan="2">
<a href="http://developer.apple.com/">ADC Home</a> > <a href="../../referencelibrary/index.html">Reference Library</a> > <a href="../../samplecode/index.html">Sample Code</a> > <a href="../../samplecode/Darwin/index.html">Darwin</a> > <a href="../../samplecode/Darwin/idxResourceManagement-date.html">Resource Management</a> > <A HREF="javascript:location.replace('index.html');">MachPortDump</A> >
</td>
</tr>
<tr>
<td colspan="2" scope="row"><img width="680" height="35" src="images/1dot.gif" alt=""></td>
</tr>
</table>
</div>
<!-- END BREADCRUMB -->
<!-- START MAIN CONTENT -->
<!-- START TITLE GRAPHIC AND INTRO-->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td><h1><div id="pagehead">MachPortDump</div></h1></td>
</tr>
</table>
<!-- END TITLE GRAPHIC AND INTRO -->
<!-- START WIDE COLUMN -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td id="scdetails">
<h2>/Read Me About MachPortDump.txt</h2>
<form name="scpopupmenu" onSubmit="return false;" method=post>
<p><strong>View Source Code:</strong>
<select name="gotop" onChange="JumpToNewPage();" style="width:340px"><option selected value="ingnore">Select File</option>
<option value="listing1.html">/MachPortDump.c</option>
<option value="listing2.html">/Read Me About MachPortDump.txt</option></select>
</p>
</form>
<p><strong><a href="MachPortDump.zip">Download Sample</a></strong> (“MachPortDump.zip”, 34.3K)<BR>
<strong><a href="MachPortDump.dmg">Download Sample</a></strong> (“MachPortDump.dmg”, 94.2K)</p>
<!--
<p><strong><a href="#">Download Sample</a></strong> (“filename.sit”, 500K)</p>
-->
</td>
</tr>
<tr>
<td scope="row"><img width="680" height="10" src="images/1dot.gif" alt=""><br>
<img height="1" width="680" src="images/1dot_919699.gif" alt=""><br>
<img width="680" height="20" src="images/1dot.gif" alt=""></td>
</tr>
<tr>
<td scope="row">
<!--googleon: index -->
<pre class="sourcecodebox">Read Me About MachPortDump
==========================
1.1
This sample dumps out the Mach port name space of a process. It is designed to
be used as a tool by developers who are writing inter-process communication code
on Mac OS X. Typically this code is based on Mach ports, or some higher-level
wrapper around Mach ports. You can run this sample to check to see whether
your code leaks ports.
MachPortDump should be compatible with any versions of Mac OS X. It was built
and tested on Mac OS X 10.4.x with Xcode 2.1.
Packing List
------------
The sample contains the following items:
o Read Me About MachPortDump.txt -- This file.
o MachPortDump.c -- C source code for the program.
o MachPortDump.xcodeproj -- An Xcode 2.1 project for the program.
o MachPortDump.xcode -- An Xcode 1.5 project for the program.
o build -- A folder containing a built universal binary.
Using the Sample
----------------
To test the sample, you should change into the directory containing the
pre-built executable.
$ cd Desktop/MachPortDump/build/Development
$ ls -lh
total 24
-rwxr-xr-x 1 quinn staff 9K 1 Nov 13:30 MachPortDump
If you run the program with the "-?" argument, you get usage.
$ ./MachPortDump -?
usage: MachPortDump [options] [ [ pid | name ]... ]
Send, Receive, SendOnce, PortSet, DeadName = right reference counts
DNR = dead name request
-w wide output, with lots of extra info
flg = N (no senders) P (port dead) S (send rights)
seqno = sequence number
mscount = make-send count
qlimit = queue limit
msgcount = message count
sorights = send-once right count
pset = port set count
If you run the program with no arguments, it prints out a dump of its own Mach
port name space.
$ ./MachPortDump
Name Send Receive SendOnce PortSet DeadName DNR
---- ---- ------- -------- ------- -------- ---
0x107 1
0x213 1
0x307 4
0x40b 2
0x50b 1
0x60b 1
0x707 1
0x80b 2
0x907 3
0xa03 1
0xc03 1
Each row represents a Mach port right name (that is, the small integer value
used by the program as a 'handle' to the port right). Each column represents a
count of the number of times that the name references a particular right. For
example, in the above listing, the name 0x307 holds 4 send right references for
a port, while the name 0xc03 holds a single receive right for a port.
If you supply a process as an argument (either via PID or name), MachPortDump
prints the name space for that process.
$ ./MachPortDump Finder
Mach ports for 'Finder' (385):
Name Send Receive SendOnce PortSet DeadName DNR
---- ---- ------- -------- ------- -------- ---
0x107 1
0x207 1
0x313 1
0x40b 12
0x507 2
0x60f 1
0x707 1
0x807 1
0x90b 4128
0xa07 7
0xb07 1
0xc03 1
0xd03 1
0xe03 1
0xf03 1 yes
0x1003 1
0x1103 48957 1
0x1203 1
[...]
0xd833 1 1
0xd96b 1
0xda17 1 1
[...]
In this example you can see that the Finder holds references to many Mach port
rights, including a reference to a port set (0x1203) and a send-once right
(0xd96b). Also note that some names (for example, 0xd833) can reference more
than one type of right. Finally, note that Finder holds 48957 references to a
particular send right (0x1103), which is probably a bug. I'll discuss this in
more depth below.
If you supply the "-w" flag, MachPortDump prints a bunch of extra information.
$ ./MachPortDump -w Finder
[...]
I've not included any output because it's too wide to view meaningfully in a
standard text document. You might want to pipe the output to a text editor
with soft word wrap turned off.
$ ./MachPortDump -w Finder | bbedit
[...]
Bug Hunting with MachPortDump
-----------------------------
If you program with Mach directly, or even if you only use high-level wrappers,
it's quite easy to accidentally leak Mach port rights. MachPortDump can help
you find those leaks.
1. You can run MachPortDump against your program to see if any of the port
rights have an excessive reference count. For example, the right with 48957
references in the Finder (shown above) is very likely to be a bug.
2. You can run MachPortDump, capture its output, perform some operation, then
run MachPortDump again, and compare its output to the original. If any new
names exist, or any rights have an increased reference count, you should
investigate why.
3. It might make sense for you to copy chunks of MachPortDump into your own
program to implement an automatic debugging facility. If you plan to do this,
read the "Caveats" section (below) before proceeding.
While debugging with MachPortDump, remember that many system services are based
on top of Mach ports. Thus, if you notice a port right 'leak', it may be a leak
of some other object. For example, in the System Configuration framework, the
SCDynamicStoreRef object contains a reference to a Mach port; if you notice a
Mach port leak, it might be that you're actually leaking a SCDynamicStoreRef.
Building the Sample
-------------------
The sample was built using Xcode 2.1 on Mac OS X 10.4. You should be able to
just open the project and choose Build from the Build menu. This will build
the MachPortDump tool in the "build" directory.
How it Works
------------
Fundamentally this program relies on three Mach routines to do its job.
o task_for_pid maps a BSD process ID (PID) to a send right for the controlling
task port for the associated Mach task
o mach_port_names returns a snapshot of a Mach task's port name space
o mach_port_get_refs gets the reference count for a particular right
These routines are all public Mach routines, although see the following section
for critical information about the Mach API on Mac OS X.
Caveats
-------
Apple strongly recommends that developers avoid using Mach APIs directly. Mach
APIs represent the lowest-level interface to the kernel, and thus they are the
most likely to change (or become unsupportable) as the kernel evolves.
Apple strongly recommends that developers use high-level wrappers around Mach
APIs where possible. For example, rather than use Mach messages directly, you
could use CFMessagePort. You should only use Mach APIs if there is no
higher-level alternatively.
This sample uses Mach APIs directly, and thus seems to violate the above
recommendations. However, this is justified by the last sentence of the
previous paragraph: the job this sample does, displaying information about a
Mach task's port right name space, is only possible via the use of Mach APIs.
It might make sense for you to copy the techniques used by MachPortDump into
your application, to help detect port right leaks and so on. However, I
strongly recommend that you include this code only in your debug build.
Credits and Version History
---------------------------
If you find any problems with this sample, mail <[email protected]> and I'll try to
fix them up.
1.0 (Nov 2004) was the first shipping version.
1.1 (Jul 2005) was updated to include an Xcode 2.1 project to produce universal
binaries; there were no code changes required for it to run correctly on the
Developer Transition Systems.
Share and Enjoy.
Apple Developer Technical Support
Networking, Communications, Hardware
20 Jul 2005
$Log: Read\040Me\040About\040MachPortDump.txt,v $
Revision 1.3 2005/07/20 15:00:44
Updated to describe version 1.1 changes.
Revision 1.2 2004/11/12 16:23:29
Added a missing "be".
Revision 1.1.1.1 2004/11/01 14:47:10
First Imported.
</pre>
<!--googleoff: index -->
</td>
</tr>
</table>
<!-- END WIDE COLUMN -->
<!-- END MAIN CONTENT -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><div style="width: 100%; height: 1px; background-color: #919699; margin-top: 5px; margin-bottom: 15px"></div></td>
</tr>
<tr>
<td align="center"><br/>
<table border="0" cellpadding="0" cellspacing="0" class="graybox">
<tr>
<th>Did this document help you?</th>
</tr>
<tr>
<td>
<div style="margin-bottom: 8px"><a href="http://developer.apple.com/feedback/?v=1&url=/samplecode/MachPortDump/listing2.html%3Fid%3DDTS10003448-1.0&media=dvd" target=_new>Yes</a>: Tell us what works for you.</div>
<div style="margin-bottom: 8px"><a href="http://developer.apple.com/feedback/?v=2&url=/samplecode/MachPortDump/listing2.html%3Fid%3DDTS10003448-1.0&media=dvd" target=_new>It’s good, but:</a> Report typos, inaccuracies, and so forth.</div>
<div><a href="http://developer.apple.com/feedback/?v=3&url=/samplecode/MachPortDump/listing2.html%3Fid%3DDTS10003448-1.0&media=dvd" target=_new>It wasn’t helpful</a>: Tell us what would have helped.</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!-- START BOTTOM APPLE NAVIGATION -->
<!--#include virtual="/includes/footer"-->
<!-- END BOTTOM APPLE NAVIGATION -->
<!-- START CENTER CLOSE -->
</center>
<!-- END CENTER CLOSE -->
</body>
</html>