7

Recently, I discovered that LibraryLink technique is a very useful tool to improve the performance of function. Firstly, I need to use LibraryLink template to write C/C++ program. Then compiling the program to dynamically linked library(i.e. *.dll file) via the CCompilerDriver package and C compiler. Please see this answer.

If there is no error in your code, the compiling process will successful and no error information occurs. Unfortunately, there will always be some errors/bugs in the code in practice. For instance, today I applied a right algorithm to LibraryLink template to calculate the nonzero values of B-spline basis, but I forgot the variable declaration of saved, and misuse the MArgument_setReal() operation.

In this process, I discovered that the debuging is very critical. While the error information that the CreateLibrary[]given is very limitted. Even if the compiling process is smoothly, the bug maybe exists. Please see Update of that question. Although NonzeroBasis[] gives me the result, it is wrong and I cannot debug it to find where the error is.

Thank for Albert Retey and Szabolcs's suggestions 1, 2, so I write a new thread here.

  • If and how it is possible to attach a debugger to such a dynamically linked library on the supported operating systems?
xyz
  • 605
  • 4
  • 38
  • 117
  • 2
    The LibraryLink libraries are loaded into the kernel process, so you may attach a debugger to it ($ProcessID can be helpful) and then set a break point in one of your functions. – ilian Jun 22 '16 at 14:53
  • Strictly speaking this is not a Mathematica question. It is about handling debuggers. Yet I think it's still a very important question to ask on this site because many Mathematica users won't have experience with advanced use of debuggers, such as this, and could really use a short walkthrough on how to do it with various debuggers/IDEs/compilers ... – Szabolcs Jun 23 '16 at 08:09

2 Answers2

4

Yes, this is possible. In Mathematica print out $ProcessID then in GDB you can use attach to attach to that process id and you should be able to debug.

user21
  • 39,710
  • 8
  • 110
  • 167
4

https://mathematica.stackexchange.com/a/118739/6804 discusses compiling a library link dll and attaching a debugger.

With say Visual Studio, you can just attach to process -> WolframKernel.exe after the dll has been loaded (via LibraryFunctionLoad).

masterxilo
  • 5,739
  • 17
  • 39