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


3 Answers 3


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 

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.

enter image description here

  • 2
    $\begingroup$ 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). $\endgroup$
    – Szabolcs
    Jan 30, 2013 at 14:54
  • $\begingroup$ @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. $\endgroup$
    – halirutan
    Jan 30, 2013 at 15:16
  • 1
    $\begingroup$ 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 ... $\endgroup$
    – Szabolcs
    Jan 30, 2013 at 15:25
  • $\begingroup$ 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) $\endgroup$
    – Szabolcs
    Jan 30, 2013 at 15:29
  • 1
    $\begingroup$ Note the LinkSnooper is a built-in tool in 11.1 here $\endgroup$
    – yode
    Mar 27, 2017 at 11:39

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\""]
  • 2
    $\begingroup$ Great, halirutan beat me by 20 minutes! Plus, he said it better. $\endgroup$ Jan 30, 2013 at 5:38

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


command, and watch what's happening in the LinkSnooper console.

  • $\begingroup$ LinkSnooping? Just curious, what are you connecting too? $\endgroup$
    – user21
    Jan 30, 2013 at 2:54
  • $\begingroup$ @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. $\endgroup$
    – Szabolcs
    Jan 30, 2013 at 3:00
  • $\begingroup$ @ruebenko We're trying to build a MATLABLink with rm-rf, see here and here $\endgroup$
    – Szabolcs
    Jan 30, 2013 at 3:01
  • $\begingroup$ 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! $\endgroup$
    – user21
    Jan 30, 2013 at 3:15
  • $\begingroup$ Note the LinkSnooper is a built-in tool in 11.1 here $\endgroup$
    – yode
    Mar 27, 2017 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.