rem: haiku_read_pending_debug_events(&sTeamDebugInfo,
add: haiku_read_pending_debug_events(teamDebugInfo,
context: haiku_child_wait_internal (ptid_t ptid, struct target_waitstatus *ourstatus,	bool *ignore){	team_id teamID = ptid_get_pid(ptid);	team_id threadID = ptid_get_tid(ptid);	debug_event *event = NULL;	ptid_t retval = pid_to_ptid(-1);	struct thread_debug_info *thread;	int pendingSignal = -1;	pending_signal_status pendingSignalStatus = SIGNAL_FAKED;	int reprocessEvent = -1;	if (teamID < 0 || threadID == 0)		threadID = -1;	// if we're waiting for any thread, search the thread list for already	// stopped threads (needed for reprocessing events)	if (threadID < 0) {		for (thread = sTeamDebugInfo.threads; thread; thread = thread->next) {			if (thread->stopped) {				threadID = thread->thread;				break;			}		}	}	// check, if the thread exists and is already stopped	if (threadID >= 0		&& (thread = haiku_find_thread(&sTeamDebugInfo, threadID)) != NULL		&& thread->stopped) {		// remove the event that stopped the thread from the thread (we will		// add it again, if it shall not be ignored)		event = thread->last_event;		thread->last_event = NULL;		thread->stopped = false;		reprocessEvent = thread->reprocess_event;		TRACE(("haiku_child_wait_internal(): thread %ld already stopped. "			"re-digesting the last event (%p).\n", threadID, event));	} else {		// prepare closure for finding interesting events		thread_event_closure threadEventClosure;		threadEventClosure.context = &sTeamDebugInfo;		threadEventClosure.thread = threadID;		threadEventClosure.event = NULL;		// find the first interesting queued event		threadEventClosure.event = haiku_find_next_debug_event(			&sTeamDebugInfo.events, haiku_thread_event_predicate,			&threadEventClosure);		// read all events pending on the port		haiku_read_pending_debug_events(&sTeamDebugInfo,			(threadEventClosure.event == NULL), haiku_thread_event_predicate,			&threadEventClosure);		// get the event of interest		event = haiku_remove_debug_event(&sTeamDebugInfo.events,			threadEventClosure.event);		if (!event) {			TRACE(("haiku_child_wait_internal(): got no event!\n"));			return retval;		}		TRACE(("haiku_child_wait_internal(): got event: %u, thread: %ld, "			"team: %ld, nub port: %ld\n", event->message,			event->data.origin.thread, event->data.origin.team,			event->data.origin.nub_port));	}	if (event->data.origin.team != sTeamDebugInfo.team) {		// Spurious debug message. Doesn't concern our team. Ignore.		xfree(event);		return retval;	}	retval = ptid_build(event->data.origin.team, 0, event->data.origin.thread);	*ignore = false;	switch (event->message) {		case B_DEBUGGER_MESSAGE_DEBUGGER_CALL:			// TODO: Print the debugger message.		case B_DEBUGGER_MESSAGE_THREAD_DEBUGGED:		case B_DEBUGGER_MESSAGE_BREAKPOINT_HIT:		case B_DEBUGGER_MESSAGE_WATCHPOINT_HIT:		case B_DEBUGGER_MESSAGE_SINGLE_STEP:			ourstatus->kind = TARGET_WAITKIND_STOPPED;			ourstatus->value.sig = TARGET_SIGNAL_TRAP;			pendingSignal = SIGTRAP;			pendingSignalStatus = SIGNAL_FAKED;			break;		case B_DEBUGGER_MESSAGE_PRE_SYSCALL:			ourstatus->kind = TARGET_WAITKIND_SYSCALL_ENTRY;			ourstatus->value.syscall_id = event->data.pre_syscall.syscall;			break;		case B_DEBUGGER_MESSAGE_POST_SYSCALL:			ourstatus->kind = TARGET_WAITKIND_SYSCALL_RETURN;			ourstatus->value.syscall_id = event->data.post_syscall.syscall;			break;		case B_DEBUGGER_MESSAGE_SIGNAL_RECEIVED:			pendingSignal = event->data.signal_received.signal;			pendingSignalStatus = SIGNAL_ARRIVED;			ourstatus->kind = TARGET_WAITKIND_STOPPED;			ourstatus->value.sig = haiku_to_target_signal(pendingSignal);			break;		case B_DEBUGGER_MESSAGE_EXCEPTION_OCCURRED:			// TODO: Print the exception message.			pendingSignal = event->data.exception_occurred.signal;			pendingSignalStatus = SIGNAL_WILL_ARRIVE;			ourstatus->kind = TARGET_WAITKIND_STOPPED;			ourstatus->value.sig = haiku_to_target_signal(pendingSignal);			break;		case B_DEBUGGER_MESSAGE_TEAM_CREATED:			// ignore			*ignore = true;			break;		case B_DEBUGGER_MESSAGE_TEAM_DELETED:			ourstatus->kind = TARGET_WAITKIND_EXITED;			ourstatus->value.integer = 0;				// TODO: Extend the debugger interface?			break;		case B_DEBUGGER_MESSAGE_THREAD_CREATED:			// internal bookkeeping only			haiku_add_thread(&sTeamDebugInfo,				event->data.thread_created.new_thread);			*ignore = true;			break;		case B_DEBUGGER_MESSAGE_THREAD_DELETED:			// internal bookkeeping			haiku_remove_thread(&sTeamDebugInfo,				event->data.thread_deleted.origin.thread);			*ignore = true;			// TODO: What if this is the current thread?			break;		case B_DEBUGGER_MESSAGE_IMAGE_CREATED:			if (reprocessEvent < 0) {				// first time we see the event: update our image list//				haiku_add_image(&sTeamDebugInfo,//					&event->data.image_created.info);haiku_cleanup_image_list(&sTeamDebugInfo);haiku_init_image_list(&sTeamDebugInfo);// TODO: We don't get events when images have been removed in preparation of// an exec*() yet.			}			ourstatus->kind = TARGET_WAITKIND_LOADED;			ourstatus->value.integer = 0;			if (event->data.image_created.info.type == B_APP_IMAGE) {				// An app image has been loaded, i.e. the application is now				// fully loaded. We need to send a TARGET_WAITKIND_LOADED				// first, so that GDB's shared object list is updated correctly.				// But we also need to send an TARGET_WAITKIND_EXECD event.				// We use the reprocessEvent mechanism here.				if (reprocessEvent == 2) {					// the second time the event is processed: send the `exec'					// event					if (inferior_ignoring_startup_exec_events > 0) {						// we shall not send an exec event, so we skip that						// and send the `trap' immediatelyTRACE(("haiku_child_wait_internal(): ignoring exec (%d)\n",inferior_ignoring_startup_exec_events));						inferior_ignoring_startup_exec_events--;						reprocessEvent = 1;					}				}				if (reprocessEvent < 0) {TRACE(("haiku_child_wait_internal(): B_APP_IMAGE created, reprocess < 0\n"));					// the first time the event is processed: send the					// `loaded' event					reprocessEvent = 2;				} else if (reprocessEvent == 2) {TRACE(("haiku_child_wait_internal(): B_APP_IMAGE created, reprocess -> exec\n"));					// the second time the event is processed: send the `exec'					// event					ourstatus->kind = TARGET_WAITKIND_EXECD;					ourstatus->value.execd_pathname						= event->data.image_created.info.name;					reprocessEvent = 1;				} else if (reprocessEvent <= 1) {					// the third time the event is processed: send the `trap'					// event					ourstatus->kind = TARGET_WAITKIND_STOPPED;					ourstatus->value.sig = TARGET_SIGNAL_TRAP;					pendingSignal = SIGTRAP;					pendingSignalStatus = SIGNAL_FAKED;					reprocessEvent = 0;				}			}			// re_enable_breakpoints_in_shlibs ();				// TODO: Needed?			break;		case B_DEBUGGER_MESSAGE_IMAGE_DELETED:			haiku_remove_image(&sTeamDebugInfo,				event->data.image_created.info.id);			// send TARGET_WAITKIND_LOADED here too, it causes the shared			// object list to be updated			ourstatus->kind = TARGET_WAITKIND_LOADED;			ourstatus->value.integer = 0;			break;		default:			// unknown message, ignore			*ignore = true;			break;	}	// make the event the thread's last event or delete it	if (!*ignore && event->data.origin.thread >= 0) {		struct thread_debug_info *thread = haiku_find_thread(&sTeamDebugInfo,			event->data.origin.thread);		if (thread->last_event)			xfree(thread->last_event);			thread->last_event = event;			thread->stopped = true;			thread->signal = pendingSignal;			thread->signal_status = pendingSignalStatus;			thread->reprocess_event				= (reprocessEvent >= 0 ? reprocessEvent : 0);	} else {		xfree(event);		// continue the thread		if (event->data.origin.thread >= 0) {			haiku_continue_thread(&sTeamDebugInfo, event->data.origin.thread,				B_THREAD_DEBUG_HANDLE_EVENT, false);		}//		*ignore = true;// TODO: This should indeed not be needed. It definitely eats the// `team deleted' events.	}	return retval;}