+static void
+inferior_call_waitpid_cleanup (void *fp)
+{
+ struct fork_info *oldfp = (struct fork_info *) fp;
+
+ if (oldfp)
+ {
+ /* Switch back to inferior_ptid. */
+ remove_breakpoints ();
+ fork_load_infrun_state (oldfp);
+ insert_breakpoints ();
+ }
+}
+
+static int
+inferior_call_waitpid (ptid_t pptid, int pid)
+{
+ struct objfile *waitpid_objf;
+ struct value *waitpid_fn = NULL;
+ struct value *argv[4], *retv;
+ struct gdbarch *gdbarch = get_current_arch ();
+ struct fork_info *oldfp = NULL, *newfp = NULL;
+ struct cleanup *old_cleanup;
+ int ret = -1;
+
+ if (pptid != inferior_ptid)
+ {
+ /* Switch to pptid. */
+ oldfp = find_fork_ptid (inferior_ptid);
+ gdb_assert (oldfp != NULL);
+ newfp = find_fork_ptid (pptid);
+ gdb_assert (newfp != NULL);
+ fork_save_infrun_state (oldfp, 1);
+ remove_breakpoints ();
+ fork_load_infrun_state (newfp);
+ insert_breakpoints ();
+ }
+
+ old_cleanup = make_cleanup (inferior_call_waitpid_cleanup, oldfp);
+
+ /* Get the waitpid_fn. */
+ if (lookup_minimal_symbol ("waitpid", NULL, NULL).minsym != NULL)
+ waitpid_fn = find_function_in_inferior ("waitpid", &waitpid_objf);
+ if (!waitpid_fn
+ && lookup_minimal_symbol ("_waitpid", NULL, NULL).minsym != NULL)
+ waitpid_fn = find_function_in_inferior ("_waitpid", &waitpid_objf);
+ if (!waitpid_fn)
+ goto out;
+
+ /* Get the argv. */
+ argv[0] = value_from_longest (builtin_type (gdbarch)->builtin_int, pid);
+ argv[1] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr, 0);
+ argv[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
+ argv[3] = 0;
+
+ retv = call_function_by_hand (waitpid_fn, NULL, 3, argv);
+ if (value_as_long (retv) < 0)
+ goto out;
+
+ ret = 0;
+
+out:
+ do_cleanups (old_cleanup);
+ return ret;
+}
+