mach_thread_self()
implies adding aMACH_PORT_RIGHT_SEND
on the thread port. It must be followed with a matchingmach_port_deallocate
call when the reference is no longer needed.
- Mach messages received from the kernel after registering with
thread_swap_exception_ports
may contain Mach ports, depending on the flavor of message. Any such ports must be deallocated by the receiver. If you pass the message along, then you can get away with just transferring ownership along as well, presuming that the next mach port in the chain wants a sufficiently similar flavor of exception message as you do.
Although I was able to use the dtrace example earlier to do some early diagnostics, since
mach_port_allocate
was not the only generator of Mach ports in the process (e.g., the kernel when sending us exception messages), it was not sufficient. Instead, I ended up using Apple’s own sample, MachPortDump, in combination with some strategically placed breakpoints in gdb to try and narrow down when the ports were coming into existence, and what the ports actually were.2It really is too bad that Apple deems these APIs to be unworthy of documenting. Admittedly, only a few developers really need to get down into this nitty-gritty, but if the only choices are reading the 17-year-old Programming Under Mach
----
1What does happen when you wrap the refcount number on a Mach port? Hm…
2MachPortDump just lists what ports are in the process and what kind/rights it has, not what the port is related to. You have to be a little creative to figure out what the port being incremented belongs to, e.g.,
(mach_port_t)pthread_mach_thread_np((pthread_t)pthread_self())
to figure out what the current thread’s port is. AFAICT, there’s no Mac OS X analog to netmsgserver
to annotate ports.