How to find if a method is being executed in a given process

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

How to find if a method is being executed in a given process

Steven Costiou-2

Hi,

how can i know if a given method is being executed ?

For ex.:

process := [[mycondition] whileTrue:[ myObject doStuff. myDelay wait]] fork

I want to suspend process and find if doStuff is being executed or if process is waiting.

How could i do that ?

Steven.

Reply | Threaded
Open this post in threaded view
|

Re: How to find if a method is being executed in a given process

jtuchel
Steven


You could add use a semaphore if changing code is an option.
We once used MethodWrappers in VAST to answer a similar question
regarding whether a body of code can be removed from a system.

Joachim



Am 25.09.17 um 12:06 schrieb Steven Costiou:

>
> Hi,
>
> how can i know if a given method is being executed ?
>
> For ex.:
>
> process := [[mycondition] whileTrue:[ myObject doStuff. myDelay wait]]
> fork
>
> I want to suspend process and find if doStuff is being executed or if
> process is waiting.
>
> How could i do that ?
>
> Steven.
>

--
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel          mailto:[hidden email]
Fliederweg 1                         http://www.objektfabrik.de
D-71640 Ludwigsburg                  http://joachimtuchel.wordpress.com
Telefon: +49 7141 56 10 86 0         Fax: +49 7141 56 10 86 1


Reply | Threaded
Open this post in threaded view
|

Re: How to find if a method is being executed in a given process

Marcus Denker-4
In reply to this post by Steven Costiou-2

On 25 Sep 2017, at 12:06, Steven Costiou <[hidden email]> wrote:

Hi,

how can i know if a given method is being executed ?

For ex.:

process := [[mycondition] whileTrue:[ myObject doStuff. myDelay wait]] fork

I want to suspend process and find if doStuff is being executed or if process is waiting.

How could i do that ?]


you could iterate all processes, there then get the stack and look up the sender chain
if the method is somewhere to be found. If yes -> it is exectuted right now.

One problem is that which methods are on the stack changes, so you should
try to avoid process switches while doing the analysis.

Marcus



Reply | Threaded
Open this post in threaded view
|

Re: How to find if a method is being executed in a given process

Ben Coman
In reply to this post by Steven Costiou-2


On Mon, Sep 25, 2017 at 6:06 PM, Steven Costiou <[hidden email]> wrote:

Hi,

how can i know if a given method is being executed ?

For ex.:

process := [[mycondition] whileTrue:[ myObject doStuff. myDelay wait]] fork

I want to suspend process and find if doStuff is being executed or if process is waiting.

I'm not sure if your wanting something more built in, or just "some way",
so for the latter, consider wrapping in with your own status flags...

status := #setup.
watcher := [[true] whileTrue:[ 
Transcript crShow: status. 
900 milliSeconds wait]
] forkAt:36.
result := 0.
worker := [[true] whileTrue:[ 
status := #WORKING. 
1 second wait.
result := result + 1. 
Transcript crShow: result. 
status:=#waiting. 
3 seconds wait]
] forkAt:35.
worker suspend.
worker resume.
 
cheers -ben
Reply | Threaded
Open this post in threaded view
|

Re: How to find if a method is being executed in a given process

Steven Costiou-2
In reply to this post by Steven Costiou-2

 

Le 2017-09-25 12:21, [hidden email] wrote :

Steven

You could add use a semaphore if changing code is an option.
We once used MethodWrappers in VAST to answer a similar question regarding whether a body of code can be removed from a system.

Joachim

 

This is exactly my usecase, i need to remove code that may be called by a method on the stack - and i know exactly which method. However my base hypothesis is that it is in an already running process (maybe a loop) and that part i cannot change. Is there an existing Pharo implementation of MethodWrappers ?

 

Le 2017-09-25 14:32, Ben Coman wrote :


I'm not sure if your wanting something more built in, or just "some way",
so for the latter, consider wrapping in with your own status flags...
 
status := #setup.
watcher := [[true] whileTrue:[ 
Transcript crShow: status. 
900 milliSeconds wait]
] forkAt:36.
result := 0.
worker := [[true] whileTrue:[ 
status := #WORKING. 
1 second wait.
result := result + 1. 
Transcript crShow: result. 
status:=#waiting. 
3 seconds wait]
] forkAt:35.
worker suspend.
worker resume.
 
cheers -ben
 

 I want to avoid adding control in the code, because hypothetically the process i want to check is already running and possibly written by somebody else. I've think about a similar solution using metalinks, but if the loop is on the stack then i don't know what i can do.

 

Le 2017-09-25 12:42, Marcus Denker a wrote :

you could iterate all processes, there then get the stack and look up the sender chain
if the method is somewhere to be found. If yes -> it is exectuted right now.

One problem is that which methods are on the stack changes, so you should
try to avoid process switches while doing the analysis.
 
Marcus

Ok so if i know which process to analyse, i can just do something like the following that i found in the Halt class:

[ cntxt isNil ] whileFalse: [
        cntxt selector = aSelector ifTrue: [ ... ].
        cntxt := cntxt sender ]

But if i actually found the method i was looking for on the stack, can i somehow ask the suspended context to "execute" until it returns from this method ?

Steven.

 

Reply | Threaded
Open this post in threaded view
|

Re: How to find if a method is being executed in a given process

Ben Coman


On Mon, Sep 25, 2017 at 9:19 PM, Steven Costiou <[hidden email]> wrote:

 

Le 2017-09-25 12:21, [hidden email] wrote :

Steven

You could add use a semaphore if changing code is an option.
We once used MethodWrappers in VAST to answer a similar question regarding whether a body of code can be removed from a system.

Joachim

 

This is exactly my usecase, i need to remove code that may be called by a method on the stack - and i know exactly which method. However my base hypothesis is that it is in an already running process (maybe a loop) and that part i cannot change. Is there an existing Pharo implementation of MethodWrappers ?

 

Le 2017-09-25 14:32, Ben Coman wrote :


I'm not sure if your wanting something more built in, or just "some way",
so for the latter, consider wrapping in with your own status flags...
 
status := #setup.
watcher := [[true] whileTrue:[ 
Transcript crShow: status. 
900 milliSeconds wait]
] forkAt:36.
result := 0.
worker := [[true] whileTrue:[ 
status := #WORKING. 
1 second wait.
result := result + 1. 
Transcript crShow: result. 
status:=#waiting. 
3 seconds wait]
] forkAt:35.
worker suspend.
worker resume.
 
cheers -ben
 

 I want to avoid adding control in the code, because hypothetically the process i want to check is already running and possibly written by somebody else. I've think about a similar solution using metalinks, but if the loop is on the stack then i don't know what i can do.

 

Le 2017-09-25 12:42, Marcus Denker a wrote :

you could iterate all processes, there then get the stack and look up the sender chain
if the method is somewhere to be found. If yes -> it is exectuted right now.

One problem is that which methods are on the stack changes, so you should
try to avoid process switches while doing the analysis.
 
Marcus

Ok so if i know which process to analyse, i can just do something like the following that i found in the Halt class:

[ cntxt isNil ] whileFalse: [
        cntxt selector = aSelector ifTrue: [ ... ].
        cntxt := cntxt sender ]

But if i actually found the method i was looking for on the stack, can i somehow ask the suspended context to "execute" until it returns from this method ?

Steven.

 

See Halt class >> now has pragma <debuggerCompleteToSender>.
Look at its senders and maybe Process>>compelte: give you some dieas.

cheers -ben 
Reply | Threaded
Open this post in threaded view
|

Re: How to find if a method is being executed in a given process

Marcus Denker-4
In reply to this post by Steven Costiou-2

On 25 Sep 2017, at 15:19, Steven Costiou <[hidden email]> wrote:

 

Le 2017-09-25 12:21, [hidden email] wrote :

Steven

You could add use a semaphore if changing code is an option.
We once used MethodWrappers in VAST to answer a similar question regarding whether a body of code can be removed from a system.

Joachim
 

This is exactly my usecase, i need to remove code that may be called by a method on the stack - and i know exactly which method. However my base hypothesis is that it is in an already running process (maybe a loop) and that part i cannot change. Is there an existing Pharo implementation of MethodWrappers ?



I do not know how MethodWrappers would solve this.. in the end, they do something very similar to what happens when you put a MetaLink on a methodNode…
(we do not wrap but recompile the AST instead, wrapping is used if the method has a primitive, though).

You can even see MetaLinks as “just” a generalisation of methodWrappers to any AST node.

So you would have the same problem: the wrapper is used *for the next call*.

Marcus
Reply | Threaded
Open this post in threaded view
|

Re: How to find if a method is being executed in a given process

Stephane Ducasse-3
This is exactly my usecase, i need to remove code that may be called
by a method on the stack - and i know exactly which method. However my
base hypothesis is that it is in an already running process (maybe a
loop) and that part i cannot change. Is there an existing Pharo
implementation of MethodWrappers ?

How can you remove a method if it may be called?

Stef

On Mon, Sep 25, 2017 at 4:16 PM, Marcus Denker <[hidden email]> wrote:

>
> On 25 Sep 2017, at 15:19, Steven Costiou <[hidden email]> wrote:
>
>
>
> Le 2017-09-25 12:21, [hidden email] wrote :
>
> Steven
>
> You could add use a semaphore if changing code is an option.
> We once used MethodWrappers in VAST to answer a similar question regarding
> whether a body of code can be removed from a system.
>
> Joachim
>
>
>
> This is exactly my usecase, i need to remove code that may be called by a
> method on the stack - and i know exactly which method. However my base
> hypothesis is that it is in an already running process (maybe a loop) and
> that part i cannot change. Is there an existing Pharo implementation of
> MethodWrappers ?
>
>
>
> I do not know how MethodWrappers would solve this.. in the end, they do
> something very similar to what happens when you put a MetaLink on a
> methodNode…
> (we do not wrap but recompile the AST instead, wrapping is used if the
> method has a primitive, though).
>
> You can even see MetaLinks as “just” a generalisation of methodWrappers to
> any AST node.
>
> So you would have the same problem: the wrapper is used *for the next call*.
>
> Marcus

Reply | Threaded
Open this post in threaded view
|

Re: How to find if a method is being executed in a given process

Marcus Denker-4

> On 25 Sep 2017, at 17:47, Stephane Ducasse <[hidden email]> wrote:
>
> This is exactly my usecase, i need to remove code that may be called
> by a method on the stack - and i know exactly which method.

I do not understand.

I think we are all not in sync and talk about four different things.

> However my
> base hypothesis is that it is in an already running process (maybe a
> loop) and that part i cannot change. Is there an existing Pharo
> implementation of MethodWrappers ?
>

Yes, method wrappers exist. But as much as I know they do not solve the problem of
on-stack replacement.

Else, whatever you do with MethodWrappers you should be able to do with MetaLinks on
the methodNode. (I would be interested in cases where this is not possible).

        Marcus


Reply | Threaded
Open this post in threaded view
|

Re: How to find if a method is being executed in a given process

Steven Costiou-2
In reply to this post by Stephane Ducasse-3

Le 2017-09-25 17:47, Stephane Ducasse a écrit :

This is exactly my usecase, i need to remove code that may be called
by a method on the stack - and i know exactly which method. However my
base hypothesis is that it is in an already running process (maybe a
loop) and that part i cannot change. Is there an existing Pharo
implementation of MethodWrappers ?

How can you remove a method if it may be called?

Stef

Lets say i have the following method:

m

^self m1

I have dynamically changed the code of m for a specific object o by the following:

m

self m2.

^self m1

m2 is behavior that is dynamically added to the object o.

If i want to revert the object o to its original behavior, that will change back m and remove m2, i must be sure that the changed m is not on the stack, else it could still call m2 that no longer exists.

Actually i tried Ben's suggestion, and it works. I just ask the process to run until the method i target is popped out of the stack and then i can remoev my code.

Steven.

Reply | Threaded
Open this post in threaded view
|

Re: How to find if a method is being executed in a given process

Stephane Ducasse-3
Ah ok.... So you should look at what Pablo did because he is handling
this case for code update,
the idea is that he will put on the stack a kind of notifier so that
you get aware when the m is not on the stack anymore.

Stef

On Mon, Sep 25, 2017 at 6:05 PM, Steven Costiou <[hidden email]> wrote:

> Le 2017-09-25 17:47, Stephane Ducasse a écrit :
>
> This is exactly my usecase, i need to remove code that may be called
> by a method on the stack - and i know exactly which method. However my
> base hypothesis is that it is in an already running process (maybe a
> loop) and that part i cannot change. Is there an existing Pharo
> implementation of MethodWrappers ?
>
> How can you remove a method if it may be called?
>
> Stef
>
> Lets say i have the following method:
>
> m
>
> ^self m1
>
> I have dynamically changed the code of m for a specific object o by the
> following:
>
> m
>
> self m2.
>
> ^self m1
>
> m2 is behavior that is dynamically added to the object o.
>
> If i want to revert the object o to its original behavior, that will change
> back m and remove m2, i must be sure that the changed m is not on the stack,
> else it could still call m2 that no longer exists.
>
> Actually i tried Ben's suggestion, and it works. I just ask the process to
> run until the method i target is popped out of the stack and then i can
> remoev my code.
>
> Steven.