{"id":8797,"date":"2017-08-11T09:00:22","date_gmt":"2017-08-11T13:00:22","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=8797"},"modified":"2022-07-13T10:14:10","modified_gmt":"2022-07-13T14:14:10","slug":"trace-your-calls-to-your-methods","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2017\/08\/11\/trace-your-calls-to-your-methods\/","title":{"rendered":"Trace your calls (to your methods)"},"content":{"rendered":"\r\n<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871\">Jiro<\/a>&#8216;s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/28929\">tracer4m<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1670\">per isakson<\/a>.<\/p><p>I teach a training course on <a href=\"https:\/\/www.mathworks.com\/training-schedule\/object-oriented-programming-with-matlab.html\">&#8220;Object-Oriented Programming (OOP) with MATLAB&#8221;<\/a>, and I just taught the course this week. Half of the students were new to the concept of OOP, while the other half had programmed in OOP languages like C++ and C#. One of the concepts I find fascinating, and thus love to teach, is <a title=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_oop\/events-and-listeners.html (link no longer works)\">events and listeners<\/a>. The idea of each object having their own responsibilities and commicating via triggering events makes the program easier to maintain. At the same time, many new-comers find it difficult to follow the chain of events that are intricately intertwined. Sometimes you don&#8217;t realize a certain operation has occurred because on the surface it seems unrelated to the task at hand. But it happened to be some action that occurred as a result of a cascade of events. Debugging such related actions can sometimes be difficult. Adding breakpoints and stepping through in the debugger doesn&#8217;t let you easily capture the timing of the chain of events.<\/p><p>This is where per&#8217;s tracer4m comes into play. He brilliantly uses <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/set-breakpoints.html#buvvmfu-1\">conditional breakpoints<\/a> and sneaks in instrumentation to capture entry and exits to functions and methods. I tested it with an <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/59801-demo-files-for-2016-matlab-expo-japan--object-oriented-programming--session--b5-\">OOP example<\/a> I showed in last year&#8217;s MATLAB Expo. It has some events and listeners as well as handle graphics callbacks.<\/p><p>This is how it works. I just create a tracer instance.<\/p><pre class=\"codeinput\">log = TraceHistory.Instance;\r\n<\/pre><p>Specify which files I want to monitor.<\/p><pre class=\"codeinput\">setup(log,{fullfile(pwd,<span class=\"string\">'@Data1d'<\/span>,<span class=\"string\">'Data1d'<\/span>),<span class=\"keyword\">...<\/span>\r\n    fullfile(pwd,<span class=\"string\">'@Data1d'<\/span>,<span class=\"string\">'interpolate'<\/span>),<span class=\"keyword\">...<\/span>\r\n    fullfile(pwd,<span class=\"string\">'@Data1d'<\/span>,<span class=\"string\">'plot'<\/span>)})\r\n<\/pre><p>This adds conditional breakpoints at the beginning and end of all methods and functions in those files.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_tracer\/tracer_breakpoint.png\" alt=\"\"> <\/p><p>Then I just do my normal operation with the app.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_tracer\/potw_oop_app.gif\" alt=\"\"> <\/p><p>After some time, I just check the log.<\/p><pre class=\"codeinput\">disp(log)\r\n<\/pre><pre class=\"codeoutput\">subsref\r\n    Data1d.get.X\r\nsubsref\r\n    Data1d.get.Data\r\naddDraggablePoints\/movePt\r\n    plot\/updateDataPlot\r\n        Data1d.get.X\r\n        Data1d.get.Data\r\n        plot\/updateInterpPlot\r\n            Data1d.get.X\r\n            Data1d.get.X\r\n            interpolate\r\n                Data1d.get.X\r\n                Data1d.get.Data\r\nsubsref\r\n    Data1d.get.X\r\nsubsref\r\n    Data1d.get.Data\r\naddDraggablePoints\/movePt\r\n    plot\/updateDataPlot\r\n        Data1d.get.X\r\n        Data1d.get.Data\r\n        plot\/updateInterpPlot\r\n            Data1d.get.X\r\n            Data1d.get.X\r\n            interpolate\r\n                Data1d.get.X\r\n                Data1d.get.Data\r\n              .\r\n              .\r\n              .<\/pre><p>This repeats over and over again. Basically, as I moved my mouse, a series of methods were being called to update the graphics.<\/p><p>Very nice!<\/p><p><b>Comments<\/b><\/p><p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=8797#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/289-ellipse-m#comment\">comment<\/a> for per.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_5b46fe70b10142b5b7942bea244393cd() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='5b46fe70b10142b5b7942bea244393cd ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 5b46fe70b10142b5b7942bea244393cd';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2017 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_5b46fe70b10142b5b7942bea244393cd()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2017a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2017a<br><\/p><\/div><!--\r\n5b46fe70b10142b5b7942bea244393cd ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871 Jiro>'s\r\n% pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/289-ellipse-m tracer4m> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1670 per\r\n% isakson>.\r\n%\r\n% I teach a training course on\r\n% <https:\/\/www.mathworks.com\/training-schedule\/object-oriented-programming-with-matlab.html\r\n% \"Object-Oriented Programming (OOP) with MATLAB\">, and I just taught the\r\n% course this week. Half of the students were new to the concept of OOP,\r\n% while the other half had programmed in OOP languages like C++ and C#. One\r\n% of the concepts I find fascinating, and thus love to teach, is\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_oop\/events-and-listeners.html\r\n% events and listeners>. The idea of each object having their own\r\n% responsibilities and commicating via triggering events makes the program\r\n% easier to maintain. At the same time, many new-comers find it difficult\r\n% to follow the chain of events that are intricately intertwined. Sometimes\r\n% you don't realize a certain operation has occurred because on the surface\r\n% it seems unrelated to the task at hand. But it happened to be some action\r\n% that occurred as a result of a cascade of events. Debugging such related\r\n% actions can sometimes be difficult. Adding breakpoints and stepping\r\n% through in the debugger doesn't let you easily capture the timing of the\r\n% chain of events.\r\n%\r\n% This is where per's tracer4m comes into play. He brilliantly uses\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/set-breakpoints.html#buvvmfu-1\r\n% conditional breakpoints> and sneaks in instrumentation to capture entry\r\n% and exits to functions and methods. I tested it with an\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/59801-demo-files-for-2016-matlab-expo-japan--object-oriented-programming--session--b5- OOP example> I\r\n% showed in last year's MATLAB Expo. It has some events and listeners as\r\n% well as handle graphics callbacks.\r\n%\r\n% This is how it works. I just create a tracer instance. \r\n\r\nlog = TraceHistory.Instance;\r\n\r\n%%\r\n% Specify which files I want to monitor.\r\n\r\nsetup(log,{fullfile(pwd,'@Data1d','Data1d'),...\r\n    fullfile(pwd,'@Data1d','interpolate'),...\r\n    fullfile(pwd,'@Data1d','plot')})\r\n\r\n%%\r\n% This adds conditional breakpoints at the beginning and end of all methods\r\n% and functions in those files.\r\n%\r\n% <<tracer_breakpoint.png>>\r\n%\r\n% Then I just do my normal operation with the app.\r\n%\r\n% <<potw_oop_app.gif>>\r\n%\r\n% After some time, I just check the log.\r\n\r\ndisp(log)\r\n\r\n%%\r\n%  subsref\r\n%      Data1d.get.X\r\n%  subsref\r\n%      Data1d.get.Data\r\n%  addDraggablePoints\/movePt\r\n%      plot\/updateDataPlot\r\n%          Data1d.get.X\r\n%          Data1d.get.Data\r\n%          plot\/updateInterpPlot\r\n%              Data1d.get.X\r\n%              Data1d.get.X\r\n%              interpolate\r\n%                  Data1d.get.X\r\n%                  Data1d.get.Data\r\n%  subsref\r\n%      Data1d.get.X\r\n%  subsref\r\n%      Data1d.get.Data\r\n%  addDraggablePoints\/movePt\r\n%      plot\/updateDataPlot\r\n%          Data1d.get.X\r\n%          Data1d.get.Data\r\n%          plot\/updateInterpPlot\r\n%              Data1d.get.X\r\n%              Data1d.get.X\r\n%              interpolate\r\n%                  Data1d.get.X\r\n%                  Data1d.get.Data\r\n%                .\r\n%                .\r\n%                .\r\n%\r\n% This repeats over and over again. Basically, as I moved my mouse, a\r\n% series of methods were being called to update the graphics.\r\n%\r\n% Very nice!\r\n%               \r\n% *Comments*\r\n%\r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=8797#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/289-ellipse-m#comment\r\n% comment> for per.\r\n\r\n##### SOURCE END ##### 5b46fe70b10142b5b7942bea244393cd\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_tracer\/tracer_breakpoint.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\nJiro&#8216;s pick this week is tracer4m by per isakson.I teach a training course on &#8220;Object-Oriented Programming (OOP) with MATLAB&#8221;, and I just taught the course this week. Half of the&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2017\/08\/11\/trace-your-calls-to-your-methods\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8797"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=8797"}],"version-history":[{"count":9,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8797\/revisions"}],"predecessor-version":[{"id":16127,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8797\/revisions\/16127"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=8797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=8797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=8797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}