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.
-
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 toaddtwo
and it is correct (pasting it to a terminal starts it). $\endgroup$– SzabolcsCommented 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$ Commented Jan 30, 2013 at 15:16
-
1
-
$\begingroup$ Never mind, it was a quoting issue. I need to change all spaces in the
-kernelname
to escaped spaces. (I haveMathematica.app
renamed toMathematica 9.app
here) $\endgroup$– SzabolcsCommented Jan 30, 2013 at 15:29 -
1
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$ Commented 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
Install[link]
command, and watch what's happening in the LinkSnooper console.
-
$\begingroup$
LinkSnooping
? Just curious, what are you connecting too? $\endgroup$– user21Commented Jan 30, 2013 at 2:54 -
$\begingroup$ @ruebenko My own program. It was not completely clear how to handle
Manual
argument passing andManual
return 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 useManual
argument passing. The templates (.tm
files) seem to only supportRealList
, not n-dimensional arrays. $\endgroup$– SzabolcsCommented Jan 30, 2013 at 3:00 -
-
$\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$– user21Commented Jan 30, 2013 at 3:15
-