Re: inspect a thread’s stack

2018-04-10 Thread Mario Torre
On Tue, Apr 10, 2018 at 9:26 AM, Pietro Paolini
 wrote:
>>
>> I think the conversation will shift a bit if you explain what you mean with:
>>
>> "// inspect the frames of that thread doing any needed business with them"
>>
>> What exactly do you have in mind? Do you want to change the stack in some
>> way?
>
> I would like to inspect the variable's name/value on the stack at a specific 
> point in time for diagnostic purposes, I don't want to change their value. I 
> don't think that should be allowed anyway :-)
>
>>
>> Because, depending on what you want, Andrew's comment on:
>> ThreadMXBean.getThreadInfo(id).getStackTrace() ?
>>
>>
>> seems reasonable to me :)
>
> I had a look to the API's Javadoc, my understanding is that I could to get to 
> the StackTraceElement array with it . That is OK to see the
> calls' stack but it does not provide any API to inspect what is actually on 
> the stack in terms of variable's  pair.
>
> Did I miss anything ?

Just an idea, but did you try out Byteman too? That may be a simpler
alternative than writing an agent in C. What Martin suggested may also
work, but the API is hidden behind reflection and that may not work
with external programs in 9+, I'm not sure any of that is exported but
you can try it, I haven't used that API much lately unfortunately.
Nevertheless, Byteman seems a more stable option, as this code may
change internally any time.

Cheers,
Mario

-- 
Mario Torre
Associate Manager, Software Engineering
Red Hat GmbH 
9704 A60C B4BE A8B8 0F30  9205 5D7E 4952 3F65 7898


RE: inspect a thread’s stack

2018-04-10 Thread Pietro Paolini
> 
> I think the conversation will shift a bit if you explain what you mean with:
> 
> "// inspect the frames of that thread doing any needed business with them"
> 
> What exactly do you have in mind? Do you want to change the stack in some
> way?

I would like to inspect the variable's name/value on the stack at a specific 
point in time for diagnostic purposes, I don't want to change their value. I 
don't think that should be allowed anyway :-)

> 
> Because, depending on what you want, Andrew's comment on:
> ThreadMXBean.getThreadInfo(id).getStackTrace() ?
> 
> 
> seems reasonable to me :)

I had a look to the API's Javadoc, my understanding is that I could to get to 
the StackTraceElement array with it . That is OK to see the 
calls' stack but it does not provide any API to inspect what is actually on the 
stack in terms of variable's  pair.

Did I miss anything ?

Thanks,
P.



Re: inspect a thread’s stack

2018-04-09 Thread JC Beyler
I think the conversation will shift a bit if you explain what you mean with:

"// inspect the frames of that thread doing any needed business with them"

What exactly do you have in mind? Do you want to change the stack in some
way?

Because, depending on what you want, Andrew's comment on:
ThreadMXBean.getThreadInfo(id).getStackTrace() ?

seems reasonable to me :)
Jc


On Mon, Apr 9, 2018 at 1:51 AM Pietro Paolini <
pietro.paol...@alfasystems.com> wrote:

>
> >Access to stacktraces with locals is demoed in this test
> >
> http://hg.openjdk.java.net/jdk/jdk/file/tip/test/jdk/java/lang/StackWalker/LocalsAndOperands.java
>
> Maybe I haven't read it well enough but isn't that accessible through
> https://docs.oracle.com/javase/9/docs/api/java/lang/StackWalker.html ? As
> long as you are on Java 9 that should not
> be a problem.
>
> >but the functionality does not seem to be available (yet!) via a public
> API.
>
> What do you mean ? Isn't that a public API ?
>
> Thanks,
> P.
>
>
>


RE: inspect a thread’s stack

2018-04-09 Thread Pietro Paolini

>Access to stacktraces with locals is demoed in this test
>http://hg.openjdk.java.net/jdk/jdk/file/tip/test/jdk/java/lang/StackWalker/LocalsAndOperands.java

Maybe I haven't read it well enough but isn't that accessible through 
https://docs.oracle.com/javase/9/docs/api/java/lang/StackWalker.html ? As long 
as you are on Java 9 that should not
be a problem. 

>but the functionality does not seem to be available (yet!) via a public API.

What do you mean ? Isn't that a public API ?

Thanks,
P.




RE: inspect a thread’s stack

2018-04-09 Thread Pietro Paolini
Hi Martin,


>Hi Pietro

>Not sure JDI is what you really want, but if you would like to play with it I 
>have some code here that uses the PID of the JVM to open a 
>connection to itself and among other things print stack frames with variables:

>https://github.com/skarsaune/kantega.debug and some demo here: 
>https://www.youtube.com/watch?v=5sXxIfjaALg

>So an example of what you can do, but not suitable for anything serious.

I don't want to setup a connection to myself and I was wondering if that could 
be avoided altogether, it is more complex than I would like it to be, for 
instance I would need to factor in the connection, what if it goes wrong etc 
etc .

>For inspecting the stack, there is an cool reflection hack to the Java 9 API 
>demonstrated by Andrei Pangin here that is >able to capture stack values: 
>https://vimeo.com/233820012

Do you think that is suitable for serious work ? I mean, production code.

>For serious work I suppose an JVMTI agent is the best option. Others are in a 
>better position to offer guidance on that.

Reading the docs it seems that the agent has to be written in C/C++ and 
unfortunately that is not an option on my current project, I quote from there 
(https://docs.oracle.com/javase/8/docs/platform/jvmti/jvmti.html#whatIs) :

"Tools can be written directly to JVM TI or indirectly through higher level 
interfaces. The Java Platform Debugger Architecture includes JVM TI, but also 
contains higher-level, out-of-process debugger interfaces. The higher-level 
interfaces are more appropriate than JVM TI for many tools. For more 
information on the Java Platform Debugger Architecture, see the Java Platform 
Debugger Architecture website."

It easy to get lost among acronyms - me being a newbie in the Java JVM related 
tooling - but when I open the 
https://docs.oracle.com/javase/7/docs/technotes/guides/jpda/architecture.html 
(Java Platform Debugger Architecture website) it lists three "things":

1) JVM TI if native it is not an option
2) JDWP not sure I need to look into that
3) JDI which is why I ended up here

Wrapping up, my hope is that the Java 9 reflection hack can work well or that 
JDI allows me to do inspect frames without the need of having a connection, 
reading your answer that does not seem to be possible and
I should exclude the possibility altogether. Is that right ?


Thanks a lot for the answers.
P.


fre. 6. apr. 2018 kl. 18:14 skrev Pietro Paolini 
:
Hi all,
 
I apologise if this is not the right ML for it but  I couldn’t find exactly 
what I was looking for when Googling the problem. I am a bit new to the JDI 
world.
 
I would like to inspect the stack-frame of a specific  thread, I came across 
the StackFrame/ThreadReference classes but I couldn’t find a way examples where 
their usage is shown 
without connecting to the VM somehow, like a debugger would do.
 
Is it possible to 
 
inspect a thread’s stack “locally”  ? In my mind I could be able to have a 
function such as :
 
static void hook(Thread thread) {
 
thread.wait() // stop that thread
 
// inspect the frames of that thread doing any needed business with them
}
 
I’d need this for diagnostic purposes of my application.
 
Thanks,
Pietro
 
 


Pietro Paolini
Consultant

Alfa

e: pietro.paol...@alfasystems.com | w: alfasystems.com
t: +44 (0) 20 7920-2643 | Moor Place, 1 Fore Street Avenue, London, EC2Y 9DT, 
GB 


The contents of this communication are not intended to be binding or constitute 
any form of offer or acceptance or give rise to any legal obligations on behalf 
of the sender or Alfa. The views or opinions expressed represent those of the 
author and not necessarily those of Alfa. This email and any attachments are 
strictly confidential and are intended solely for use by the individual or 
entity to whom it is addressed. If you are not the addressee (or responsible 
for delivery of the message to the addressee) you may not copy, forward, 
disclose or use any part of the message or its attachments. At present the 
integrity of email across the internet cannot be guaranteed and messages sent 
via this medium are potentially at risk. All liability is excluded to the 
extent permitted by law for any claims arising as a result of the use of this 
medium to transmit information by or to Alfa or its affiliates. 

Alfa Financial Software Ltd 
Reg. in England No: 0248 2325 


Re: inspect a thread’s stack

2018-04-08 Thread Martin Buchholz
Access to stacktraces with locals is demoed in this test
http://hg.openjdk.java.net/jdk/jdk/file/tip/test/jdk/java/lang/StackWalker/LocalsAndOperands.java
but the functionality does not seem to be available (yet!) via a public API.


Re: inspect a thread’s stack

2018-04-07 Thread Andrew Haley
On 04/06/2018 05:13 PM, Pietro Paolini wrote:
> Is it possible to
> 
> inspect a thread’s stack “locally”  ? 

Have you looked at ThreadMXBean.getThreadInfo(id).getStackTrace() ?

-- 
Andrew Haley
Java Platform Lead Engineer
Red Hat UK Ltd. 
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671


Re: inspect a thread’s stack

2018-04-06 Thread Martin Skarsaune
Hi Pietro

Not sure JDI is what you really want, but if you would like to play with it
I have some code here that uses the PID of the JVM to open a connection to
itself and among other things print stack frames with variables:

https://github.com/skarsaune/kantega.debug and some demo here:
https://www.youtube.com/watch?v=5sXxIfjaALg

So an example of what you can do, but not suitable for anything serious.

For inspecting the stack, there is an cool reflection hack to the Java 9
API demonstrated by Andrei Pangin here that is able to capture stack
values: https://vimeo.com/233820012

For serious work I suppose an JVMTI agent is the best option. Others are in
a better position to offer guidance on that.

Martin



fre. 6. apr. 2018 kl. 18:14 skrev Pietro Paolini <
pietro.paol...@alfasystems.com>:

> Hi all,
>
>
>
> I apologise if this is not the right ML for it but  I couldn’t find
> exactly what I was looking for when Googling the problem. I am a bit new to
> the JDI world.
>
>
>
> I would like to inspect the stack-frame of a specific  thread, I came
> across the StackFrame/ThreadReference classes but I couldn’t find a way
> examples where their usage is shown
>
> without connecting to the VM somehow, like a debugger would do.
>
>
>
> Is it possible to
>
>
>
> inspect a thread’s stack “locally”  ? In my mind I could be able to have a
> function such as :
>
>
>
> static void hook(Thread thread) {
>
>
>
> thread.wait() // stop that thread
>
>
>
> // inspect the frames of that thread doing any needed business with them
>
> }
>
>
>
> I’d need this for diagnostic purposes of my application.
>
>
>
> Thanks,
>
> Pietro
>
>
>
>
>
>
> Pietro Paolini
> Consultant
>
> Alfa
> --
> e: pietro.paol...@alfasystems.com | w: alfasystems.com
> 
> t: +44 (0) 20 7920-2643 <+44%2020%207920%202643> | Moor Place, 1 Fore
> Street Avenue, London, EC2Y 9DT
> ,
> GB
> --
>
> The contents of this communication are not intended to be binding or
> constitute any form of offer or acceptance or give rise to any legal
> obligations on behalf of the sender or Alfa. The views or opinions
> expressed represent those of the author and not necessarily those of Alfa.
> This email and any attachments are strictly confidential and are intended
> solely for use by the individual or entity to whom it is addressed. If you
> are not the addressee (or responsible for delivery of the message to the
> addressee) you may not copy, forward, disclose or use any part of the
> message or its attachments. At present the integrity of email across the
> internet cannot be guaranteed and messages sent via this medium are
> potentially at risk. All liability is excluded to the extent permitted by
> law for any claims arising as a result of the use of this medium to
> transmit information by or to Alfa or its affiliates.
>
> Alfa Financial Software Ltd
> Reg. in England No: 0248 2325
>
>