2019-06-13 16:34:50

by Shyam Saini

[permalink] [raw]
Subject: Re: Pause a process execution from external program

Hi Pintu,


> Hi All,
> I was just wondering if this is possible in the Linux world.
> My requirement is:
> For some reason, I want to halt/pause the execution (for some
> specified time) of a running process/thread (at some location),
> without modified the source, may be by firing some events/signals from
> an another external program, by specifying the address location or a
> line number.
>
> Is this possible ?
> May be by using some system call, or other mechanism using the process PID.
> Assume that its a debugging system with all root privileges.
>
> Basically, its just like how "gdb" is able to set the break-point in a
> program, and able to stop its execution exactly at that location.
> I am wondering what mechanism "gdb" uses to do this?

gdb uses ptrace system call, may you can explore ptrace?

> I tried to check here, but could find the exact place, where this is handled:
> https://github.com/bminor/binutils-gdb/blob/master/gdb/breakpoint.c

from command line we use ctrl-z to stop execution of a foreground
process but you can program
SIGTSTP signal handler in your application code to do the same.

is that you want ?

Thanks a lot,
Shyam


2019-06-13 19:45:18

by Valdis Klētnieks

[permalink] [raw]
Subject: Re: Pause a process execution from external program

On Thu, 13 Jun 2019 13:22:12 +0530, Shyam Saini said:

> from command line we use ctrl-z to stop execution of a foreground
> process but you can program
> SIGTSTP signal handler in your application code to do the same.

Note that if you simply fail to include a signal handler for SIGSTOP and
SIGCONT, it will Do The Right Thing. The only programs that need worry about
SIGTSTP are ones like 'vi' that may want to do something (like restore the
terminal state from raw to cooked mode, etc) before they stop. That's why you
can control-z /bin/cat without it having to include a signal handler for it.

% kill -STOP `pidof process-to-stop` # stop it
% kill -CONT `pidof process-to-stop` # and make it run again.

No source code modifications needed. No source needed.

Now, if you want to make it stop at a *specific point*, then you're into
ptrace territory, and source will be helpful.


Attachments:
(No filename) (849.00 B)

2019-06-17 12:21:38

by Pintu Kumar

[permalink] [raw]
Subject: Re: Pause a process execution from external program

On Fri, Jun 14, 2019 at 1:13 AM Valdis Klētnieks
<[email protected]> wrote:
>
> On Thu, 13 Jun 2019 13:22:12 +0530, Shyam Saini said:
>
> > from command line we use ctrl-z to stop execution of a foreground
> > process but you can program
> > SIGTSTP signal handler in your application code to do the same.
>
> Note that if you simply fail to include a signal handler for SIGSTOP and
> SIGCONT, it will Do The Right Thing. The only programs that need worry about
> SIGTSTP are ones like 'vi' that may want to do something (like restore the
> terminal state from raw to cooked mode, etc) before they stop. That's why you
> can control-z /bin/cat without it having to include a signal handler for it.
>
> % kill -STOP `pidof process-to-stop` # stop it
> % kill -CONT `pidof process-to-stop` # and make it run again.
>
> No source code modifications needed. No source needed.
>
> Now, if you want to make it stop at a *specific point*, then you're into
> ptrace territory, and source will be helpful.
>
Yes, I think ptrace can serve our purpose.
Thank you so much.