robotology / wb-toolbox

Simulink toolbox to rapidly prototype robot controllers
https://robotology.github.io/wb-toolbox/
GNU Lesser General Public License v2.1
24 stars 17 forks source link

Collection of resources for debugging in Matlab #134

Open diegoferigo opened 6 years ago

diegoferigo commented 6 years ago

Disclaimer: this will be ported sooner or later in the website.

What follows is a problem I wanted to solve since long time, but always postponed for laziness. However, this laziness has the consequence that during development I have to open and close matlab very often.


For some while we have been able during development to reload the mex library created from the S-Function without restarting Matlab. @francesco-romano developed the setupForMatlabDebug.m.in matlab script for this purpose. In this case, the mex file from the build tree was used instead of the installed one.

With wb-toolbox v3.0 the file was split in libWBToolbox.so and WBToolbox.mexa64. Unfortunately, Matlab is able to reload only mex files, not shared library (which are somehow cached). After some investigation it turned out that Matlab supports loading / unloading shared library with the loadlibrary command, but it works only for headers that offer a C interface. Furthermore, it would require to list all the headers of the library, which can be annoying if done manually (probably a solution which involves creating a prototype file with CMake would automate the process).

A reasonable solution might be the following:

Any other idea? @traversaro

Other resources:

diegoferigo commented 5 years ago

Even with the new plugin approach, it is still not possible to avoid rebooting Matlab when the plugin library containing the blocks is updated. Here follows some details for future reference.

At the end of the simulation, with the Simulink model still open, this is the output of the inmem command:

>> [pf, mexfiles]=inmem('-completenames')

pf =

  619×1 cell array

    {'/usr/local/MATLAB/toolbox/local/pathdef.m'                                                                                                                   }
    {'/usr/local/MATLAB/toolbox/local/userpath.m'                                                                                                                  }
    {'/usr/local/MATLAB/toolbox/local/matlabrc.m'                                                                                                                  }
    {'/usr/local/MATLAB/toolbox/matlab/general/usejava.m'
[ ... ]

mexfiles =

  6×1 cell array

    {'/usr/local/MATLAB/toolbox/matlab/connector2/logger/+connector/+internal/log.mexa64'}
    {'/usr/local/MATLAB/toolbox/sldv/sldv/slavteng.mexa64'                               }
    {'/usr/local/MATLAB/toolbox/stateflow/stateflow/sf.mexa64'                           }
    {'/usr/local/MATLAB/toolbox/simulink/fixedandfloat/hostcpuinfo.mexa64'               }
    {'/usr/local/MATLAB/toolbox/shared/cgxe/cgxe/+CGXE/+Utils/md5.mexa64'                }
    {'/iit/sources/robotology-superbuild/build/install/mex/BlockFactory.mexa64'          }

... The mex library of the S-Function (BlockFactory.mexa64) is still loaded, as expected, and also the plugin library is there:

>> version('-modules')
linux-vdso.so.1                                                                                                                                
/usr/lib/x86_64-linux-gnu/libstdc++.so.6                                                                                                       
/usr/local/MATLAB/bin/glnxa64/libmwi18n.so                                                                                                      9.4.0.802882 (Feb  6 2018)
/usr/local/MATLAB/bin/glnxa64/libmwmvm.so                                                                                                       9.4.0.802882 (Feb  6 2018)
/usr/local/MATLAB/bin/glnxa64/libmwgraphics_state.so                                                                                            9.4.0.802882 (Feb  6 2018)
/usr/local/MATLAB/bin/glnxa64/libmwfl.so
[...]
/usr/local/MATLAB/bin/glnxa64/libprotobuf.so.9                                                                                                 
/usr/local/MATLAB/bin/glnxa64/libmwslcheck_component_base.so                                                                                    9.4.0.802882 (Feb  6 2018)
/usr/local/MATLAB/bin/glnxa64/mex_builtin.so                                                                                                    9.4.0.810697 (Feb 19 2018)
/usr/local/MATLAB/bin/glnxa64/libmexcmd.so                                                                                                      9.4.0.810697 (Feb 19 2018)
/iit/sources/robotology-superbuild/build/install/mex/BlockFactory.mexa64                                                                       
/iit/sources/robotology-superbuild/build/install/lib/libBlockFactoryCore.so.1.0                                                                
/iit/sources/robotology-superbuild/build/install/lib/libMxAnyType.so.0.1                                                                       
/iit/sources/robotology-superbuild/build/install/lib/libshlibpp.so.1                                                                           
/iit/sources/robotology-superbuild/build/install/lib/libWBToolboxLibrary.so                                                                    
/iit/sources/robotology-superbuild/build/install/lib/libWBToolboxBase.so                                                                       
/iit/local/lib/libClockRpc.so                                                                                                                  
/iit/local/lib/libidyntree-high-level.so                                                                                                       
/iit/sources/robotology-superbuild/build/install/lib/libqpOASES.so.3.2                                                                         
/usr/lib/x86_64-linux-gnu/libmatio.so.4                                                                                                        
/iit/local/lib/libidyntree-modelio-urdf.so                                                                                                     
/iit/local/lib/libidyntree-modelio-xml.so                                                                                                      
/iit/local/lib/libidyntree-sensors.so                                                                                                          
/iit/local/lib/libidyntree-model.so                                                                                                            
/iit/local/lib/libidyntree-core.so                                                                                                             
/iit/local/lib/libYARP_dev.so.3                                                                                                                
/iit/local/lib/libYARP_math.so.3                                                                                                               
/iit/local/lib/libYARP_init.so.3                                                                                                               
/iit/local/lib/libYARP_sig.so.3                                                                                                                
/iit/local/lib/libYARP_OS.so.3                                                                                                                 
/usr/lib/x86_64-linux-gnu/libgsl.so.23                                                                                                         
/usr/lib/x86_64-linux-gnu/libgslcblas.so.0                                                                                                     
/usr/lib/x86_64-linux-gnu/libhdf5_serial.so.100                                                                                                
/usr/local/MATLAB/bin/glnxa64/libxml2.so.2                                                                                                     
/usr/lib/x86_64-linux-gnu/libjpeg.so.8                                                                                                         
/usr/lib/x86_64-linux-gnu/libedit.so.2                                                                                                         
/usr/lib/x86_64-linux-gnu/libsz.so.2                                                                                                           
/lib/x86_64-linux-gnu/libtinfo.so.5                                                                                                            
/usr/lib/x86_64-linux-gnu/libaec.so.0

This is confirmed by lsof:

dferigo@9aed5e6ce8bd:~> lsof -p 9638 | grep ' mem ' | grep iit
MATLAB  9638 dferigo  mem       REG              259,7           3954296 /iit/local/lib/libYARP_OS.so.3.1.0 (path dev=0,58, inode=5802857)
MATLAB  9638 dferigo  mem       REG              259,7           3954252 /iit/local/lib/libYARP_sig.so.3.1.0 (path dev=0,58, inode=5802892)
MATLAB  9638 dferigo  mem       REG              259,7           3954255 /iit/local/lib/libYARP_init.so.3.1.0 (path dev=0,58, inode=5802869)
MATLAB  9638 dferigo  mem       REG              259,7           3954283 /iit/local/lib/libYARP_math.so.3.1.0 (path dev=0,58, inode=5802874)
MATLAB  9638 dferigo  mem       REG              259,7           3954231 /iit/local/lib/libYARP_dev.so.3.1.0 (path dev=0,58, inode=5802863)
MATLAB  9638 dferigo  mem       REG              259,7           3936470 /iit/local/lib/libidyntree-core.so (path dev=0,58, inode=4873178)
MATLAB  9638 dferigo  mem       REG              259,7           3936872 /iit/local/lib/libidyntree-model.so (path dev=0,58, inode=4873184)
MATLAB  9638 dferigo  mem       REG              259,7           3936421 /iit/local/lib/libidyntree-sensors.so (path dev=0,58, inode=4873189)
MATLAB  9638 dferigo  mem       REG              259,7           3940884 /iit/local/lib/libidyntree-modelio-xml.so (path dev=0,58, inode=4873186)
MATLAB  9638 dferigo  mem       REG              259,7           3936437 /iit/local/lib/libidyntree-modelio-urdf.so (path dev=0,58, inode=4873185)
MATLAB  9638 dferigo  mem       REG              259,7           4064965 /iit/sources/robotology-superbuild/build/install/lib/libqpOASES.so.3.2 (path dev=0,58)
MATLAB  9638 dferigo  mem       REG              259,7           3936736 /iit/local/lib/libidyntree-high-level.so (path dev=0,58, inode=4873181)
MATLAB  9638 dferigo  mem       REG              259,7           3988655 /iit/local/lib/libClockRpc.so (path dev=0,58)
MATLAB  9638 dferigo  mem       REG              259,7           4064610 /iit/sources/robotology-superbuild/build/install/lib/libWBToolboxBase.so (path dev=0,58)
# Here it is:
MATLAB  9638 dferigo  mem       REG              259,7           4064618 /iit/sources/robotology-superbuild/build/install/lib/libWBToolboxLibrary.so (path dev=0,58)
MATLAB  9638 dferigo  mem       REG              259,7           4063376 /iit/sources/robotology-superbuild/build/install/lib/libshlibpp.so.0.0.1 (path dev=0,58)
MATLAB  9638 dferigo  mem       REG              259,7           4064596 /iit/sources/robotology-superbuild/build/install/lib/libMxAnyType.so.0.1 (path dev=0,58)
MATLAB  9638 dferigo  mem       REG              259,7           4064054 /iit/sources/robotology-superbuild/build/install/lib/libBlockFactoryCore.so.1.0 (path dev=0,58)
MATLAB  9638 dferigo  mem       REG              259,7           4196025 /iit/sources/robotology-superbuild/build/install/mex/BlockFactory.mexa64 (path dev=0,58)

And:

dferigo@9aed5e6ce8bd:~> lsof | grep libWBToolboxLibrary.so
MATLAB     9638      dferigo  mem       REG              259,7            4064618 /iit/sources/robotology-superbuild/build/install/lib/libWBToolboxLibrary.so (path dev=0,58)
MATLAB     9638 9676 dferigo  mem       REG              259,7            4064618 /iit/sources/robotology-superbuild/build/install/lib/libWBToolboxLibrary.so (path dev=0,58)
MATLAB     9638 9677 dferigo  mem       REG              259,7            4064618 /iit/sources/robotology-superbuild/build/install/lib/libWBToolboxLibrary.so (path dev=0,58)

After updating the plugin on the fs

The mem column became DEL:

dferigo@9aed5e6ce8bd:~/git/blockfactory (master)> lsof | grep libWBToolboxLibrary.so
MATLAB     9638      dferigo  DEL       REG              259,7            4064618 /iit/sources/robotology-superbuild/build/install/lib/libWBToolboxLibrary.so
MATLAB     9638 9676 dferigo  DEL       REG              259,7            4064618 /iit/sources/robotology-superbuild/build/install/lib/libWBToolboxLibrary.so
MATLAB     9638 9677 dferigo  DEL       REG              259,7            4064618 /iit/sources/robotology-superbuild/build/install/lib/libWBToolboxLibrary.so
MATLAB     9638 9678 dferigo  DEL       REG              259,7            4064618 /iit/sources/robotology-superbuild/build/install/lib/libWBToolboxLibrary.so

And it disappeared from the process in-memory list:

dferigo@9aed5e6ce8bd:~> lsof -p 9638 | grep ' mem ' | grep iit
MATLAB  9638 dferigo  mem       REG              259,7           3954296 /iit/local/lib/libYARP_OS.so.3.1.0 (path dev=0,58, inode=5802857)
MATLAB  9638 dferigo  mem       REG              259,7           3954252 /iit/local/lib/libYARP_sig.so.3.1.0 (path dev=0,58, inode=5802892)
MATLAB  9638 dferigo  mem       REG              259,7           3954255 /iit/local/lib/libYARP_init.so.3.1.0 (path dev=0,58, inode=5802869)
MATLAB  9638 dferigo  mem       REG              259,7           3954283 /iit/local/lib/libYARP_math.so.3.1.0 (path dev=0,58, inode=5802874)
MATLAB  9638 dferigo  mem       REG              259,7           3954231 /iit/local/lib/libYARP_dev.so.3.1.0 (path dev=0,58, inode=5802863)
MATLAB  9638 dferigo  mem       REG              259,7           3936470 /iit/local/lib/libidyntree-core.so (path dev=0,58, inode=4873178)
MATLAB  9638 dferigo  mem       REG              259,7           3936872 /iit/local/lib/libidyntree-model.so (path dev=0,58, inode=4873184)
MATLAB  9638 dferigo  mem       REG              259,7           3936421 /iit/local/lib/libidyntree-sensors.so (path dev=0,58, inode=4873189)
MATLAB  9638 dferigo  mem       REG              259,7           3940884 /iit/local/lib/libidyntree-modelio-xml.so (path dev=0,58, inode=4873186)
MATLAB  9638 dferigo  mem       REG              259,7           3936437 /iit/local/lib/libidyntree-modelio-urdf.so (path dev=0,58, inode=4873185)
MATLAB  9638 dferigo  mem       REG              259,7           4064965 /iit/sources/robotology-superbuild/build/install/lib/libqpOASES.so.3.2 (path dev=0,58)
MATLAB  9638 dferigo  mem       REG              259,7           3936736 /iit/local/lib/libidyntree-high-level.so (path dev=0,58, inode=4873181)
MATLAB  9638 dferigo  mem       REG              259,7           3988655 /iit/local/lib/libClockRpc.so (path dev=0,58)
MATLAB  9638 dferigo  mem       REG              259,7           4064610 /iit/sources/robotology-superbuild/build/install/lib/libWBToolboxBase.so (path dev=0,58)
MATLAB  9638 dferigo  mem       REG              259,7           4063376 /iit/sources/robotology-superbuild/build/install/lib/libshlibpp.so.0.0.1 (path dev=0,58)
MATLAB  9638 dferigo  mem       REG              259,7           4064596 /iit/sources/robotology-superbuild/build/install/lib/libMxAnyType.so.0.1 (path dev=0,58)
MATLAB  9638 dferigo  mem       REG              259,7           4064054 /iit/sources/robotology-superbuild/build/install/lib/libBlockFactoryCore.so.1.0 (path dev=0,58)
MATLAB  9638 dferigo  mem       REG              259,7           4196025 /iit/sources/robotology-superbuild/build/install/mex/BlockFactory.mexa64 (path dev=0,58)

By executing clear mex in Matlab, the mex library does not appear anymore in the inmem output but it is still present in the version output.

I am not sure that it is possible to force the reload of the updated plugin at this point without restarting the entire matlab process.

Resources: