Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch foreground process group #831

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions ext/byebug/byebug.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,21 @@ Start(VALUE self)
return Qtrue;
}

static VALUE
Foreground_process_group_id(VALUE self, VALUE tty_fd)
{
Check_Type(tty_fd, T_FIXNUM);
return INT2NUM(tcgetpgrp(NUM2INT(tty_fd)));
}

static VALUE
Set_foreground_process_group_id(VALUE self, VALUE tty_fd, VALUE pgrp)
{
Check_Type(tty_fd, T_FIXNUM);
Check_Type(pgrp, T_FIXNUM);
return INT2NUM(tcsetpgrp(NUM2INT(tty_fd), NUM2INT(pgrp)));
}

/*
* call-seq:
* Byebug.debug_load(file, stop = false) -> nil
Expand Down Expand Up @@ -884,6 +899,8 @@ Init_byebug()
rb_define_module_function(mByebug, "tracing=", Set_tracing, 1);
rb_define_module_function(mByebug, "verbose?", Verbose, 0);
rb_define_module_function(mByebug, "verbose=", Set_verbose, 1);
rb_define_module_function(mByebug, "foreground_process_group_id", Foreground_process_group_id, 1);
rb_define_module_function(mByebug, "set_foreground_process_group_id", Set_foreground_process_group_id, 2);

Init_threads_table(mByebug);
Init_byebug_context(mByebug);
Expand Down
1 change: 1 addition & 0 deletions lib/byebug/attacher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def self.attach

start
run_init_script
ensure_foreground
end

current_context.step_out(3, true)
Expand Down
16 changes: 16 additions & 0 deletions lib/byebug/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ def self.load_settings
end
end

def ensure_foreground
tty = File.open('/dev/tty', 'r')
group_id = Process.getpgrp

unless group_id == foreground_process_group_id(tty.fileno)
begin
prev_ttou_handler = Signal.trap('SIGTTOU', 'IGNORE')
set_foreground_process_group_id(tty.fileno, group_id)
ensure
Signal.trap('SIGTTOU', prev_ttou_handler)
end
end
ensure
tty.close
end

#
# Saves information about the unhandled exception and gives a byebug
# prompt back to the user before program termination.
Expand Down