+/* See exec.h. */
+
+void
+try_open_exec_file (const char *exec_file_host, struct inferior *inf,
+ symfile_add_flags add_flags)
+{
+ struct cleanup *old_chain;
+ struct gdb_exception prev_err = exception_none;
+
+ old_chain = make_cleanup (free_current_contents, &prev_err.message);
+
+ /* exec_file_attach and symbol_file_add_main may throw an error if the file
+ cannot be opened either locally or remotely.
+
+ This happens for example, when the file is first found in the local
+ sysroot (above), and then disappears (a TOCTOU race), or when it doesn't
+ exist in the target filesystem, or when the file does exist, but
+ is not readable.
+
+ Even without a symbol file, the remote-based debugging session should
+ continue normally instead of ending abruptly. Hence we catch thrown
+ errors/exceptions in the following code. */
+ TRY
+ {
+ /* We must do this step even if exec_file_host is NULL, so that
+ exec_file_attach will clear state. */
+ exec_file_attach (exec_file_host, add_flags & SYMFILE_VERBOSE);
+ }
+ CATCH (err, RETURN_MASK_ERROR)
+ {
+ if (err.message != NULL)
+ warning ("%s", err.message);
+
+ prev_err = err;
+
+ /* Save message so it doesn't get trashed by the catch below. */
+ if (err.message != NULL)
+ prev_err.message = xstrdup (err.message);
+ }
+ END_CATCH
+
+ if (exec_file_host != NULL)
+ {
+ TRY
+ {
+ symbol_file_add_main (exec_file_host, add_flags);
+ }
+ CATCH (err, RETURN_MASK_ERROR)
+ {
+ if (!exception_print_same (prev_err, err))
+ warning ("%s", err.message);
+ }
+ END_CATCH
+ }
+
+ do_cleanups (old_chain);
+}
+
+/* See gdbcore.h. */
+
+void
+exec_file_locate_attach (int pid, int defer_bp_reset, int from_tty)
+{
+ char *exec_file_target;
+ symfile_add_flags add_flags = 0;
+
+ /* Do nothing if we already have an executable filename. */
+ if (get_exec_file (0) != NULL)
+ return;
+
+ /* Try to determine a filename from the process itself. */
+ exec_file_target = target_pid_to_exec_file (pid);
+ if (exec_file_target == NULL)
+ {
+ warning (_("No executable has been specified and target does not "
+ "support\n"
+ "determining executable automatically. "
+ "Try using the \"file\" command."));
+ return;
+ }
+
+ gdb::unique_xmalloc_ptr<char> exec_file_host
+ = exec_file_find (exec_file_target, NULL);
+
+ if (defer_bp_reset)
+ add_flags |= SYMFILE_DEFER_BP_RESET;
+
+ if (from_tty)
+ add_flags |= SYMFILE_VERBOSE;
+
+ /* Attempt to open the exec file. */
+ try_open_exec_file (exec_file_host.get (), current_inferior (), add_flags);
+}
+