Skip to main content

StackTrace implementation in J2ME

8 replies [Last post]
ergoo
Offline
Joined: 2008-06-18

Hi,

Does anyone know if there is a working solution for getting stacktrace-like information when running MIDlet on real devices? I'm not looking for any vendor specific solutions. It should work on all devices.

I have been looking for that kind of solution quite long time but haven't found not much information. Also I'm thinking about developing some preprocessor which in compile time adds a variable after each line into source code to detect the stack point when exception is thrown.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ergoo
Offline
Joined: 2008-06-18

Currently I am thinking about to start implementing a little project that automatically pre-processes source code and adds additional instructions after each line (e.g. integer which value is increased after each line). It should be enough to identify the point where code execution stopped.

Does anybody have suggestions how to proceed?

terrencebarr
Offline
Joined: 2004-03-04

You may want to search around the web a bit. I have the feeling something like that already exists ... but I can't put my finger on it.

If it really doesn't exist it sounds like a good feature to have. I would start by doing a little architectural write-up that describes how it will work and what the implementation would entail and then post it on the forum for feedback. Next, you can request a new project on java.net in the M&E Community and start your coding there:

https://mobileandembedded.dev.java.net/governance.html#ProjectOwner

-- Terrence

okallioi
Offline
Joined: 2010-07-07

I have implemented such a tool recently and it works like a charm.
The tool is now available at http://jarrut.sourceforge.net. It's not that well documented yet and it has some rough edges but it does the job (a job that should really be done by the platform in the first place!!).

I am not aware of any previous implementations and I just found this thread while I was googling around to find places to spread the word.

terrencebarr
Offline
Joined: 2004-03-04

ergoo,

exception.printStackTrace() ?

-- Terrence

ergoo
Offline
Joined: 2008-06-18

It doesn't help when you're running MIDlet on a real device. It is like System.out.println() which you actually don't see. The problem is that in J2ME it is impossible to use Exception.getStackTrace() like in J2SE.

terrencebarr
Offline
Joined: 2004-03-04

ergoo,

Ok ... could you explain what you need the stack trace for? If it is for development purposes then with many current phones you *can* view the System.out.println() console via the PC. If it is for remote error diagnosis once the application is installed in a handset in the field then you could print the stack trace into a StringBuffer and then send it over the network back to your server to capture and analyze it.

Not sure I fully understand what you want to do ...

-- Terrence

ergoo
Offline
Joined: 2008-06-18

Thanks Terrence,

The solution I am looking is for remote diagnostics (also it would be helpful during the development in case there are any problems with devices that does not provide a good debugging mechanism). It should be platform independent and provide the method calls and exact line where exception is thrown.

Btw, how can I print StackTrace to StringBuffer? In J2ME it is impossible to direct the output anywhere else than just System.err because Exception class provides only printStackTrace() method. If it would be possible to get the stacktrace as a String, then it would be the solution I am looking for.

terrencebarr
Offline
Joined: 2004-03-04

ergoo,

Ah, yes, sorry, I forgot you cannot redirect Throwable.printStackTrace() from err to another PrintStream in CLDC. Unfortunately, there is no good universal solution here ... it's up to the implementation as to whether or not you can capture the err output in some way.

What people do is to add a bit of their own tracing code at the beginning and end of interesting methods and stick the information into a round-robin buffer. For remote monitoring purposes that should be sufficient.

-- Terrence