22
33#if defined(__APPLE__ ) && TARGET_OS_OSX
44
5+ #include <mach/notify.h>
6+
57#ifndef VM_FLAGS_RETURN_DATA_ADDR
68# define VM_FLAGS_RETURN_DATA_ADDR 0
79#endif
@@ -37,6 +39,12 @@ _Py_RemoteDebug_AliasCacheClear(RemoteUnwinderObject *unwinder)
3739 for (int i = 0 ; i < MAX_ALIAS_PAGES ; i ++ ) {
3840 alias_deallocate_entry (& cache -> pages [i ]);
3941 }
42+ if (cache -> notify_port != MACH_PORT_NULL ) {
43+ (void )mach_port_mod_refs (mach_task_self (), cache -> notify_port ,
44+ MACH_PORT_RIGHT_RECEIVE , -1 );
45+ cache -> notify_port = MACH_PORT_NULL ;
46+ }
47+ cache -> notify_armed = 0 ;
4048}
4149
4250static void
@@ -64,10 +72,38 @@ _Py_RemoteDebug_AliasCacheInit(RemoteUnwinderObject *unwinder)
6472{
6573 AliasReadCache * cache = & unwinder -> alias_cache ;
6674 memset (cache , 0 , sizeof (* cache ));
75+
76+ mach_port_t notify_port = MACH_PORT_NULL ;
77+ kern_return_t kr = mach_port_allocate (mach_task_self (),
78+ MACH_PORT_RIGHT_RECEIVE ,
79+ & notify_port );
80+ if (kr != KERN_SUCCESS ) {
81+ return ;
82+ }
83+
84+ mach_port_t previous = MACH_PORT_NULL ;
85+ kr = mach_port_request_notification (mach_task_self (),
86+ unwinder -> handle .task ,
87+ MACH_NOTIFY_DEAD_NAME ,
88+ 1 ,
89+ notify_port ,
90+ MACH_MSG_TYPE_MAKE_SEND_ONCE ,
91+ & previous );
92+ if (previous != MACH_PORT_NULL ) {
93+ (void )mach_port_deallocate (mach_task_self (), previous );
94+ }
95+ if (kr != KERN_SUCCESS ) {
96+ (void )mach_port_mod_refs (mach_task_self (), notify_port ,
97+ MACH_PORT_RIGHT_RECEIVE , -1 );
98+ return ;
99+ }
100+
101+ cache -> notify_port = notify_port ;
102+ cache -> notify_armed = 1 ;
67103}
68104
69105static int
70- alias_identity_matches (RemoteUnwinderObject * unwinder )
106+ alias_identity_matches_fallback (RemoteUnwinderObject * unwinder )
71107{
72108 mach_port_type_t type = 0 ;
73109 kern_return_t kr = mach_port_type (mach_task_self (),
@@ -79,6 +115,47 @@ alias_identity_matches(RemoteUnwinderObject *unwinder)
79115 !(type & MACH_PORT_TYPE_DEAD_NAME )) ? 1 : 0 ;
80116}
81117
118+ static int
119+ alias_identity_matches (RemoteUnwinderObject * unwinder )
120+ {
121+ AliasReadCache * cache = & unwinder -> alias_cache ;
122+ if (!cache -> notify_armed ) {
123+ return alias_identity_matches_fallback (unwinder );
124+ }
125+
126+ union {
127+ mach_dead_name_notification_t dead_name ;
128+ struct {
129+ mach_msg_header_t header ;
130+ mach_msg_trailer_t trailer ;
131+ } msg ;
132+ char storage [sizeof (mach_dead_name_notification_t ) +
133+ sizeof (mach_msg_trailer_t )];
134+ } msg ;
135+ memset (& msg , 0 , sizeof (msg ));
136+
137+ mach_msg_return_t mr = mach_msg (& msg .msg .header ,
138+ MACH_RCV_MSG | MACH_RCV_TIMEOUT ,
139+ 0 ,
140+ (mach_msg_size_t )sizeof (msg ),
141+ cache -> notify_port ,
142+ 0 ,
143+ MACH_PORT_NULL );
144+ if (mr == MACH_RCV_TIMED_OUT ) {
145+ return 1 ;
146+ }
147+ if (mr == MACH_MSG_SUCCESS &&
148+ msg .msg .header .msgh_id == MACH_NOTIFY_DEAD_NAME )
149+ {
150+ if (msg .dead_name .not_port != MACH_PORT_NULL ) {
151+ (void )mach_port_deallocate (mach_task_self (),
152+ msg .dead_name .not_port );
153+ }
154+ return 0 ;
155+ }
156+ return 0 ;
157+ }
158+
82159static int
83160alias_maybe_probe_identity (RemoteUnwinderObject * unwinder )
84161{
0 commit comments