How can I monitor the communication between an installable MathLink program and the kernel? Can LinkSnooper do this?
3 Answers
Let me present an alternative approach. The whole commandline work and link naming and connecting can be simplified. What we need is the location of the LinkSnooper program and the location of your MathLink program and then you can set up everything in exactly one call to Install from Mathematica.
I have tried to make the following, where I demonstrate it with the addtwo MathLink example, as general as possible and it hopefully works on all operating systems.
Remark: In the following we will work with paths which probably contain evil characters like spaces which need a correct quoting. If something goes wrong, please check this first!
Unfortunately, a Java VM is not provided by Wolfram for all systems. You have to ensure, that java is found. First, let's build the call for Install
cmdline = StringJoin[
"java -cp ",
FileNameJoin[{$InstallationDirectory, "SystemFiles", "Links", "JLink", "JLink.jar"}],
" com.wolfram.jlink.util.LinkSnooper -kernelname '",
FileNameJoin[{$InstallationDirectory, "SystemFiles", "Links", "MathLink",
"DeveloperKit", $SystemID, "PrebuiltExamples", "addtwo"}],
"'"
]
On my machine this gives
java -cp /usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/JLink/JLink.jar
com.wolfram.jlink.util.LinkSnooper -kernelname
'/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/\
Linux-x86-64/PrebuiltExamples/addtwo'
As you can see, the call to the LinkSnooper is equivalent to the one in Szabolcs answer, but we let LinkSnooper start our MathLink program directly.
The good thing is, that we are finished at this point. Just use
link = Install[cmdline]
and the LinkSnooper pops up with all the traffic between you and the addtwo program.

- 112,764
- 7
- 263
- 474
LinkSnooper is a great tool any time you need to see what's traveling across a link. You can actually get it completely set up to monitor an installable MathLink program in a single Install command.
The syntax for Install is
Install["path to .exe"]
When using LinkSnooper, it is not the .exe that you want to launch directly, but rather LinkSnooper as the intermediary. So this becomes
Install["command line to launch Java, with LinkSnooper as the main class,
passing LinkSnooper the path to the exe to launch via the kernelname parameter"]
This example, for Windows, uses " to quote all embedded paths. On other platforms you might have to use ' instead.
Install["java -cp \"" <>
FileNameJoin[{$InstallationDirectory, "SystemFiles", "Links", "JLink", "JLink.jar"}] <>
"\" com.wolfram.jlink.util.LinkSnooper -kernelname \"path/to/installable/exe\""]
- 4,198
- 20
- 19
Here are step by step instructions. In the below commands change $InstallationDirectory to the Mathematica install location on your system.
First launch LinkSnooper from the command line like this:
java -cp $InstallationDirectory/SystemFiles/Links/JLink/JLink.jar com.wolfram.jlink.util.LinkSnooper -kernelname klink -kernelmode listen
Be sure to quote paths with spaces properly. Here klink is the name of the Kernel <-> LinkSnooper link.
LinkSnooper will prompt you for the name of LinkSnooper <-> program link. Type plink, then Enter.
Now connect to klink in Mathematica using
link = LinkConnect["klink"]
Connect to plink by launching the MathLink program, skipping on the Create link: prompt (press Enter) and typing plink at the Connect to link: prompt.
Now "install" the program into the kernel using the
Install[link]
command, and watch what's happening in the LinkSnooper console.
- 234,956
- 30
- 623
- 1,263
-
-
@ruebenko My own program. It was not completely clear how to handle
Manualargument passing andManualreturn in installable programs (e.g. do I need toMLPutSymbol(stdlink, "Null")if I don't want to return anything? Or can I just not write anything to the link?) As far as I understand, if I want to get aReal64Array, I must useManualargument passing. The templates (.tmfiles) seem to only supportRealList, not n-dimensional arrays. – Szabolcs Jan 30 '13 at 03:00 -
@ruebenko We're trying to build a MATLABLink with rm-rf, see here and here – Szabolcs Jan 30 '13 at 03:01
-
I don't know the answer to those questions but I'll try to find out. Concernig the MATLABLink, sounds like a big project. Good luck with it! – Jan 30 '13 at 03:15
-
Installsimply hangs here. It prints on up to (but not including)Start Monitoring...I verified the paths toaddtwoand it is correct (pasting it to a terminal starts it). – Szabolcs Jan 30 '13 at 14:54Installjust hangs ... – Szabolcs Jan 30 '13 at 15:25-kernelnameto escaped spaces. (I haveMathematica.apprenamed toMathematica 9.apphere) – Szabolcs Jan 30 '13 at 15:29