Hi Dennis,
Your input is very useful to me, thanks! The method to attach a running
MATLAB process fits my need better.
After some tests on your scripts, I just realized that a secondary emacs
instance is preferred, given that a first emacs instance is running
matlab-shell.
A bit of summary of what I do:
Put Dennis' first piece of code in matalb-attach.py
Launch the first emacs, using emacsclient, to run matlab-shell.
Launch the second emacs, using emacsclient, to run gdb.
M-x: gdb
gdb -i=mi -x matalb-attach.py
(gdb) break mexFunction
In the first emacs,
(matlab-shell) K>> *some commands to call mexFunction*
In the second emacs,
(gdb) window will grab the control on MATLAB and show the source code of
mexFunction. Make sure using -g when compiling the mex function.
Thank you,
Dong
On Mon, Jul 24, 2017 at 02:12:34PM -0400, Dennis Ogbe wrote:
> You can use Python as a scripting language in gdb to find the PID of a
> currently running MATLAB process and attach to it. I remember doing this
> a while back. The code was something around the lines of:
>
> <code>
>
> # debugging MEX files in emacs on linux
>
> import sys
> pythondir = '/usr/share/gcc-6.2.1/python'
> sys.path.append(pythondir)
>
> from libstdcxx.v6 import register_libstdcxx_printers
> from subprocess import check_output
>
> try:
> register_libstdcxx_printers(None)
> except Exception as e:
> pass
>
> # find the PID of the MATLAB process
> def get_pid(name):
> return list(map(int, check_output(["pidof", name]).split()))
>
> # set a new breakpoint, deleting any breakpoints referring to the same
> location
> def new_bp(loc):
> bps = gdb.breakpoints()
> if bps:
> for bp in bps:
> if bp.location == loc:
> gdb.execute("d " + str(bp.number))
> # set the dummy breakpoint
> return gdb.Breakpoint(loc)
>
> # attach to the MATLAB process. if gdb attach fails, do
> # sudo sh -c "echo 0 > /proc/sys/kernel/yama/ptrace_scope"
> pids = get_pid("MATLAB")
> gdb.execute("attach {:d}".format(pids[0]))
>
> <end code>
>
> an alternative approach is to launch matlab from gdb:
>
> <code>
>
> run_matlab = True
>
> if run_matlab:
> # spawn a new MATLAB process
> matlab_dir = "/path/to/MATLAB/R2016b/bin/glnxa64/MATLAB"
> gdb.execute("file " + matlab_dir)
>
> # set the MATLAB_DEBUG env
> gdb_loc = "/usr/bin/gdb"
> gdb.execute("set environment MATLAB_DEBUG = " + gdb_loc)
>
> # need to execute this according to
> #
> https://www.mathworks.com/help/matlab/matlab_external/debugging-on-linux-platforms.html
> gdb.execute("handle SIGSEGV SIGBUS nostop noprint")
>
> # set the breakpoint at the mexFunction
> mex_Bp = new_bp("mexFunction")
>
> # run matlab
> gdb.execute("run -nojvm -r debug_encoder -singleCompThread")
>
> <end code>
>
> Dennis
>
> Dong Tian <[email protected]> writes:
>
> > Hi Eric, all,
> >
> > Thanks for replying.
> >
> > It is non-trivial to attach the matlab process to gdb.
> >
> > Matlab provides a command line option -Dgdb to associate the Matlab process
> > for gdb debugging. Would it be easier to launch "matlab-shell" with -Dgdb
> > as option? Maybe your or someone else could put lights on this approach?
> >
> > Thanks,
> > Dong
> >
> > On Wed, Jul 19, 2017 at 09:01:56PM +0000, Eric Ludlam wrote:
> >> Hi Dong,
> >>
> >> You can fire up GDB from within Emacs and "attach" to a process.
> >> Using this to attach to a process running MATLAB inside Emacs'
> >> matlab-shell will let you debug through MATLAB code and into your C++
> >> code.
> >>
> >> I haven't done this in a few years, and not with Mex (I was debugging
> >> MATLAB directly) but I'm guessing the principle is the same.
> >>
> >> Eric
> >>
> >> -----Original Message-----
> >> From: Dong Tian [mailto:[email protected]]
> >> Sent: Wednesday, July 19, 2017 1:56 PM
> >> To: [email protected]
> >> Subject: [Matlab-emacs-discuss] How to debug MEX files (C code) from Emacs?
> >>
> >> Hello,
> >>
> >> Could anyone provide a pointer on how to debug MEX files from Emacs?
> >> E.g. using gdb?
> >>
> >> I am familiar with the procedure of debugging pure c/c++ code or .m
> >> code from Emacs using gdb, but I don't know a good way to debug MEX
> >> file coded in C from Emacs.
> >>
> >> Thanks in advance,
> >> Dong
> >>
> >> ------------------------------------------------------------------------------
> >> Check out the vibrant tech community on one of the world's most engaging
> >> tech sites, Slashdot.org!
> >> http://secure-web.cisco.com/1ujKNEvoZ9nB-3MswM8yQpeWgC6aSas93lzT8a00GA11SdB5SpQ6CVRIOx-heKXnqKECDVK85FoUunZq98vpNfzj9z-dAEXYdTSCYMo17rGews2sXFe04QVklpkIgysqCMwIaoJQltBgN0vzTKcyy6uH42EmnOZN9d_7d6GI8HTluD2CTM_kIvO0I4ApdEW1uyzzeVHPDe01AEhL8z1lCXCd9Ls5JK37FPBnlqU-vHsqgajNbrh54-xMsjRMjU-khdIzpnhQOzvN53aQbsoH_mYU759kv1n9L6l97WEaazGtUV61gkLrwcRmPqtZUkz2IU_aA6HkROB1JRv1L63MCOuGFFTjdNrbynGp78WL3jRQ/http%3A%2F%2Fsdm.link%2Fslashdot
> >> _______________________________________________
> >> Matlab-emacs-discuss mailing list
> >> [email protected]
> >> https://secure-web.cisco.com/10eOO6rYM1RqFCHmlXsWS1TYTfQt0mblS7KZvYNUIxTp-ZGg9Gb7geEfnffV5gC9W3jjN5jlZyDrKFrGw14cD9PgfSBzm5qQ_6jbAPG5AQ61KZoTDufZTTsMhvlc7k-8mkewh3PKUPkO-9wtKK_ayCNqBB37kEYCLOH673oZPhzvewWVdd2KYIRf8sWleZLty8CyehfWXIWwMAX6yxqj9reri0LJu_hSOjSDE7bqwujPw_GaFGbRLesA4iu9JVaGg--IWyW7nW0t2ki22_ESPWiRYdGq7hqiMg7EYsAtkPymllsorUJDO3-uBqjP-SI1ky0GgTCRGnzpyoe5id6tfPy19K959g_PDiyGSEevC8Us/https%3A%2F%2Flists.sourceforge.net%2Flists%2Flistinfo%2Fmatlab-emacs-discuss
> >
> > ------------------------------------------------------------------------------
> > Check out the vibrant tech community on one of the world's most
> > engaging tech sites, Slashdot.org!
> > http://secure-web.cisco.com/1ujKNEvoZ9nB-3MswM8yQpeWgC6aSas93lzT8a00GA11SdB5SpQ6CVRIOx-heKXnqKECDVK85FoUunZq98vpNfzj9z-dAEXYdTSCYMo17rGews2sXFe04QVklpkIgysqCMwIaoJQltBgN0vzTKcyy6uH42EmnOZN9d_7d6GI8HTluD2CTM_kIvO0I4ApdEW1uyzzeVHPDe01AEhL8z1lCXCd9Ls5JK37FPBnlqU-vHsqgajNbrh54-xMsjRMjU-khdIzpnhQOzvN53aQbsoH_mYU759kv1n9L6l97WEaazGtUV61gkLrwcRmPqtZUkz2IU_aA6HkROB1JRv1L63MCOuGFFTjdNrbynGp78WL3jRQ/http%3A%2F%2Fsdm.link%2Fslashdot
> > _______________________________________________
> > Matlab-emacs-discuss mailing list
> > [email protected]
> > https://secure-web.cisco.com/10eOO6rYM1RqFCHmlXsWS1TYTfQt0mblS7KZvYNUIxTp-ZGg9Gb7geEfnffV5gC9W3jjN5jlZyDrKFrGw14cD9PgfSBzm5qQ_6jbAPG5AQ61KZoTDufZTTsMhvlc7k-8mkewh3PKUPkO-9wtKK_ayCNqBB37kEYCLOH673oZPhzvewWVdd2KYIRf8sWleZLty8CyehfWXIWwMAX6yxqj9reri0LJu_hSOjSDE7bqwujPw_GaFGbRLesA4iu9JVaGg--IWyW7nW0t2ki22_ESPWiRYdGq7hqiMg7EYsAtkPymllsorUJDO3-uBqjP-SI1ky0GgTCRGnzpyoe5id6tfPy19K959g_PDiyGSEevC8Us/https%3A%2F%2Flists.sourceforge.net%2Flists%2Flistinfo%2Fmatlab-emacs-discuss
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Matlab-emacs-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matlab-emacs-discuss