1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:37:37 +00:00

Shell: Replace '#if SH_DEBUG` with dbgln_if() and if constexpr

This commit is contained in:
AnotherTest 2021-03-30 02:17:20 +04:30 committed by Andreas Kling
parent 3b201da473
commit 47080941cc

View file

@ -64,9 +64,7 @@ void Shell::setup_signals()
{ {
if (m_should_reinstall_signal_handlers) { if (m_should_reinstall_signal_handlers) {
Core::EventLoop::register_signal(SIGCHLD, [this](int) { Core::EventLoop::register_signal(SIGCHLD, [this](int) {
#if SH_DEBUG dbgln_if(SH_DEBUG, "SIGCHLD!");
dbgln("SIGCHLD!");
#endif
notify_child_event(); notify_child_event();
}); });
@ -502,9 +500,7 @@ String Shell::format(const StringView& source, ssize_t& cursor) const
Shell::Frame Shell::push_frame(String name) Shell::Frame Shell::push_frame(String name)
{ {
m_local_frames.append(make<LocalFrame>(name, decltype(LocalFrame::local_variables) {})); m_local_frames.append(make<LocalFrame>(name, decltype(LocalFrame::local_variables) {}));
#if SH_DEBUG dbgln_if(SH_DEBUG, "New frame '{}' at {:p}", name, &m_local_frames.last());
dbgln("New frame '{}' at {:p}", name, &m_local_frames.last());
#endif
return { m_local_frames, m_local_frames.last() }; return { m_local_frames, m_local_frames.last() };
} }
@ -568,10 +564,10 @@ int Shell::run_command(const StringView& cmd, Optional<SourcePosition> source_po
if (!command) if (!command)
return 0; return 0;
#if SH_DEBUG if constexpr (SH_DEBUG) {
dbgln("Command follows"); dbgln("Command follows");
command->dump(0); command->dump(0);
#endif }
if (command->is_syntax_error()) { if (command->is_syntax_error()) {
auto& error_node = command->syntax_error_node(); auto& error_node = command->syntax_error_node();
@ -665,9 +661,7 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
auto apply_rewirings = [&] { auto apply_rewirings = [&] {
for (auto& rewiring : rewirings) { for (auto& rewiring : rewirings) {
#if SH_DEBUG dbgln_if(SH_DEBUG, "in {}<{}>, dup2({}, {})", command.argv.is_empty() ? "(<Empty>)" : command.argv[0].characters(), getpid(), rewiring.old_fd, rewiring.new_fd);
dbgln("in {}<{}>, dup2({}, {})", command.argv.is_empty() ? "(<Empty>)" : command.argv[0].characters(), getpid(), rewiring.old_fd, rewiring.new_fd);
#endif
int rc = dup2(rewiring.old_fd, rewiring.new_fd); int rc = dup2(rewiring.old_fd, rewiring.new_fd);
if (rc < 0) { if (rc < 0) {
perror("dup2(run)"); perror("dup2(run)");
@ -780,9 +774,7 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
} }
} }
#if SH_DEBUG dbgln_if(SH_DEBUG, "Synced up with parent, we're good to exec()");
dbgln("Synced up with parent, we're good to exec()");
#endif
close(sync_pipe[0]); close(sync_pipe[0]);
@ -977,25 +969,25 @@ NonnullRefPtrVector<Job> Shell::run_commands(Vector<AST::Command>& commands)
NonnullRefPtrVector<Job> spawned_jobs; NonnullRefPtrVector<Job> spawned_jobs;
for (auto& command : commands) { for (auto& command : commands) {
#if SH_DEBUG if constexpr (SH_DEBUG) {
dbgln("Command"); dbgln("Command");
for (auto& arg : command.argv) for (auto& arg : command.argv)
dbgln("argv: {}", arg); dbgln("argv: {}", arg);
for (auto& redir : command.redirections) { for (auto& redir : command.redirections) {
if (redir.is_path_redirection()) { if (redir.is_path_redirection()) {
auto path_redir = (const AST::PathRedirection*)&redir; auto path_redir = (const AST::PathRedirection*)&redir;
dbgln("redir path '{}' <-({})-> {}", path_redir->path, (int)path_redir->direction, path_redir->fd); dbgln("redir path '{}' <-({})-> {}", path_redir->path, (int)path_redir->direction, path_redir->fd);
} else if (redir.is_fd_redirection()) { } else if (redir.is_fd_redirection()) {
auto* fdredir = (const AST::FdRedirection*)&redir; auto* fdredir = (const AST::FdRedirection*)&redir;
dbgln("redir fd {} -> {}", fdredir->old_fd, fdredir->new_fd); dbgln("redir fd {} -> {}", fdredir->old_fd, fdredir->new_fd);
} else if (redir.is_close_redirection()) { } else if (redir.is_close_redirection()) {
auto close_redir = (const AST::CloseRedirection*)&redir; auto close_redir = (const AST::CloseRedirection*)&redir;
dbgln("close fd {}", close_redir->fd); dbgln("close fd {}", close_redir->fd);
} else { } else {
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
}
} }
} }
#endif
auto job = run_command(command); auto job = run_command(command);
if (!job) if (!job)
continue; continue;
@ -1648,13 +1640,9 @@ void Shell::notify_child_event()
auto& job = *it.value; auto& job = *it.value;
int wstatus = 0; int wstatus = 0;
#if SH_DEBUG dbgln_if(SH_DEBUG, "waitpid({} = {}) = ...", job.pid(), job.cmd());
dbgln("waitpid({}) = ...", job.pid());
#endif
auto child_pid = waitpid(job.pid(), &wstatus, WNOHANG | WUNTRACED); auto child_pid = waitpid(job.pid(), &wstatus, WNOHANG | WUNTRACED);
#if SH_DEBUG dbgln_if(SH_DEBUG, "... = {} - exited: {}, suspended: {}", child_pid, WIFEXITED(wstatus), WIFSTOPPED(wstatus));
dbgln("... = {} - {}", child_pid, wstatus);
#endif
if (child_pid < 0) { if (child_pid < 0) {
if (errno == ECHILD) { if (errno == ECHILD) {
@ -1801,9 +1789,7 @@ void Shell::stop_all_jobs()
printf("Killing active jobs\n"); printf("Killing active jobs\n");
for (auto& entry : jobs) { for (auto& entry : jobs) {
if (entry.value->is_suspended()) { if (entry.value->is_suspended()) {
#if SH_DEBUG dbgln_if(SH_DEBUG, "Job {} is suspended", entry.value->pid());
dbgln("Job {} is suspended", entry.value->pid());
#endif
kill_job(entry.value, SIGCONT); kill_job(entry.value, SIGCONT);
} }
@ -1813,9 +1799,7 @@ void Shell::stop_all_jobs()
usleep(10000); // Wait for a bit before killing the job usleep(10000); // Wait for a bit before killing the job
for (auto& entry : jobs) { for (auto& entry : jobs) {
#if SH_DEBUG dbgln_if(SH_DEBUG, "Actively killing {} ({})", entry.value->pid(), entry.value->cmd());
dbgln("Actively killing {} ({})", entry.value->pid(), entry.value->cmd());
#endif
kill_job(entry.value, SIGKILL); kill_job(entry.value, SIGKILL);
} }