# Using LinkSnooper for monitoring the communication with an installable MathLink program

How can I monitor the communication between an installable MathLink program and the kernel? Can LinkSnooper do this?

-

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. - Can you test this on OS X? LinkSnooper launches fine but then the Install simply hangs here. It prints on up to (but not including) Start Monitoring... I verified the paths to addtwo and it is correct (pasting it to a terminal starts it). – Szabolcs Jan 30 '13 at 14:54 @Szabolcs I did test it. That's why I had to edit the post and simplify the java-executable, because on OSX they don't come with Mathematica. I tried it on my retina-MB and on my MacPro in the office. – halirutan Jan 30 '13 at 15:16 Does this look okay to you? Do you see any difference from what you get, e.g. in the command line options listed by LinkSnooper? At this point Install just hangs ... – Szabolcs Jan 30 '13 at 15:25 Never mind, it was a quoting issue. I need to change all spaces in the -kernelname to escaped spaces. (I have Mathematica.app renamed to Mathematica 9.app here) – Szabolcs Jan 30 '13 at 15:29 Ahh, the bad spaces in filenames.. – halirutan Jan 30 '13 at 15:37 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. - LinkSnooping? Just curious, what are you connecting too? – user21 Jan 30 '13 at 2:54 @ruebenko My own program. It was not completely clear how to handle Manual argument passing and Manual return in installable programs (e.g. do I need to MLPutSymbol(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 a Real64Array, I must use Manual argument passing. The templates (.tm files) seem to only support RealList, not n-dimensional arrays. – Szabolcs Jan 30 '13 at 3:00 @ruebenko We're trying to build a MATLABLink with rm-rf, see here and here – Szabolcs Jan 30 '13 at 3: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! – user21 Jan 30 '13 at 3:15 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"}] <>