Catch the Stack Trace from a javac call - exception

I am looking to do something a little, well interesting I think would be a good word. I was wondering if there was a way to catch the text associated with a stack trace from a build (currently I use ANT to build) using the javac task.
A little history, I am using a CI server, CruiseControl, and want to write an adapter that will catch the stack trace from a failed build, and allow for me to parse out which files caused the build to fail from a javac task call. So for example, if code was checked into a repository that had a method signature from another class in it, but that class was never added to the repository, the javac task would fail with a cannot find symbol exception in the class. I want to be able to read the stack trace to get the class that caused the build failure.
Any ideas on how to do this? I would prefer not to have to just read in the log file and parse it out manually (I feel like there should be a better way) but if there isn't then I can just go that route as well.

First, for terminology: The compiler normally does not throw exceptions (if it does, there likely is a bug in the compiler, or your file system makes problems, or something like this), so there is no stack trace.
What you see when compiling is the compiler output, including any compiler error messages.
Looking at ant's javac task, there seems to be no way to redirect the output somewhere, which means parsing it is only possible by parsing the output of the whole ant run.
You might do better by using the Compiler API (javax.tools) and adding a DiagnosticListener to the compiler run. You would have to wrap this into an ant task yourself, though.

Related

Crash reporting and C++ Coroutines?

I use a crash reporting feature that allows the user to submit a crash report if the application crashed with an uncaught exception.
After adopting C++20 coroutines entered the application.
If there is an unexpected exception thrown in a coroutine the exception is caught before it is rethrown.
This causes crashreports to not show the stacktrace needed to figure out what happened, but only the stacktrace to the coroutine that rethrew the exception. This basically makes any crash reporting useless.
As far as I could find there is no way to prevent the catching of any exceptions by the coroutine because it is a required part of the design.
Is there a way to improve this I cant see?
I am curious because I found nobody else complaining yet. :->
Edit: To clarify the app is running on Windows, I mean the stacktrace of a minidump that is created at the point of the unhandled exception using: SetUnhandledExceptionFilter + MiniDumpWriteDump
C++ does not have standard stack tracing yet, so there is no nice builtin way to do this.
However, there are ways, which rely on keeping information in the promise objects.
Clang has documentation for some common debugging methods for coroutines.
The best solution we have found is as follows (Windows specific!):
Until now we used SetUnhandledExceptionFilter at the start of the app to set an exceptionfilter function that writes a minidump.
Instead we now use _set_se_translator.
If we want the program to just crash (f.e. if windows is set to write dumps) we set a function which calls std::abort.
If we want to handle it interactively we set a function which asks the user whether to send a minidump, the dump is written as before at this point.
Both cases provide the full callstack in the dump.
The only downside remaining is we cant let the program crash for "normal" exceptions to dump, this was possible before. But the "most important" exceptions (f.e. access violations) work.

How to show line number of exception throw in IntelliJ IDEA

How do I show a line number that says where an exception was thrown on runtime? Currently the IDE only displays the exception name, and no stack trace of any kind, making it very difficult to debug. I have searched the IntelliJ docs and haven't been able to find a simple answer (I don't want to have to use breakpoints and debugging commands).
It is basic Java stuff. You must print the stacktrace in your application to see line numbers, not just the exception itself which produces #toString() output.

Disabling .NET handling of native exceptions

I have a C# application that calls a mixed mode C++ dll. I enabled dump generation via HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps.
When the dll accesses invalid memory, the runtime converts the win32 exception to a managed System.AccessViolationException, and unwinds the stack before generating the dump, destroying the native stack information. I could catch the exception myself before .net gets at it and generate the dump manually, but running code on an already corrupt program could hang it, according to the msdn. So, how can I disable SEH translation?
You cannot disable that. The CLR will not unwind the stack unless you catch the exception. Make sure you don't. This needs to go through an AppDomain.UnhandledException event handler. The essential function you need is Marshal.GetExceptionPointers(), that's the one that will pinpoint the exception when you open the minidump.
You'll find resources in my answer in this MSDN forum thread and this pinvoke.net snippet, should be enough to cobble your own together.

Razor exceptions

I have undoubtedly set something up wrong but frequently I get exceptions thrown by my Razor templates even though there is no problem with the templates. These are usually fixed by my doing a build.
If I do actually have an error in the template I get a popup asking me to debug in VS, but of course this does not actually allow me to debug the template.
Errors in my log are not all that helpful (see below).
Is it possible to both avoid spurious errors and get better information when there is actually a problem?
ServiceStack.Razor.Templating.TemplateCompilationException: Unable to compile template. Check the Errors list for details.
at ServiceStack.Razor.Templating.TemplateService.CreateTemplate(String template, Type modelType)
at ServiceStack.Razor.Templating.TemplateService.Compile(ViewPageRef viewPageRef, String template, Type modelType, String name)
at ServiceStack.Razor.Templating.TemplateService.Compile(ViewPageRef viewPageRef, String template, String name)
at ServiceStack.Razor.ViewPageRef.Compile(Boolean force)
I was having similar problems. I found the "easiest" way to find out what the error was, was to download all of service stack, build a debug version of the razor libary and link it into my project. I then set a break point in the ServiceStack.Razor.Templating.TemplateService.CreateTemplate method and was able to see the full exception details. From there I learnt that I had included an import in my razor page that was not referenced in my project.
Since I solved this it's been very reliable.
I had trouble with this myself, because ServiceStack swallowed the exceptions, and the logs, as you said, don't show the Errors collection. There are two ways to get that information:
Uncheck Enable Just My Code in the debugging options in Visual Studio (Debug -> Options and Settings). If you have checked Thrown for Common Language Runtime Exceptions in Debug -> Exceptions, you will get the exceptions, and be able to view the Errors collection.
A merge was committed some days ago to the ServiceStack repository, which makes it log the Errors collection. Demis Bellot apparently pushes new versions to NuGet fairly often, so it'll probably be there in a week or two.
I had the same problem. And my case, I have removed some libraries referenced in the project but the reference to them remained (eventhought I think removed it, but anyway) and this has been the problem.
After I deleted the references to libraries which don't exsits anymore in the project, it worked immediatelly.

Is there any authoritative reference on what exceptions can be thrown by Matlab's built-in functions?

For example, I want to catch a couldn't-read-a-file-at-that-path exception from imread(). I can do this.
imagePath = 'a_picture.jpg';
try
im = imread(imagePath);
catch exception
if strcmp(exception.identifier, 'MATLAB:imread:fileOpen')
fprintf('Couldn''t open %s.\n', imagePath);
im = [];
else
fprintf('Unexpected error (%s): %s\n', ...
exception.identifier, exception.message);
throw(exception);
end
end
But the only ways I know to discover the magic string to compare with ('MATLAB:imread:fileOpen' in this case), are:
Cause the error, catch the exception, and look at the identifier. But it would take a long time to do this right. For example, does Matlab use a different exception identifier if the file exists but is not actually an image file? How about if it exists but I don't have read permission? What if it's a directory?
Look at the source code. imread() is written in Matlab, so this is possible, but it wouldn't be for other functions. And of course imread() calls other functions that are not written in Matlab, and exceptions could bubble up from them.
Is there any authoritative way for me to know all the exceptions imread() can throw? I'm hoping this is in the documentation somewhere, but I can't find it.
No, there is not.
The problem is that even if there would be an authoritative reference on what a given function in MatLab throws, it could change from version to version. So even if you could do it, you probably should not.
I would recommend only checking for the ones that you know you can handle, and generate some generic errors for others (or reuse what MatLab gives you).
Some comments based on other languages/frameworks:
In .NET, the only list of exceptions that can be thrown from a method, is in documentation, and is not liked to the source code. These are often out of date, invalid, and incomplete.
In Java, you can specify what exception is thrown form what method. This is then verified by the compiler, and therefore an authoritative reference can be built by the compiler. MatLab lacks such a feature, therefore the best you can do is searching as outlined in other answers.
I search for that, and I did not find anything... The only thing I see it could help you, would be analyse the source code of imread, which I don't think it's possible. However, you could always try to see the source code of the same function in Octave, since it almost the same (I suppose).
If you're willing to enumerate files to generate your own reference, try
grep -r "MATLAB:" <matlab root>
you'll get a long list...but it appears errors are either thrown by error() or mexErrMsgIdAndTxt. Those plus function names let you be more specific. Pretty slow though.
grep will also note that some binary files match. Feed it -a and it will be able to pull out the error messages from many of them.