Recent

Author Topic: Problem with Xcode/LLDB and debugging nested procedure [SOLVED]  (Read 3102 times)

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Hello, Suppose I have a method in a class, with a nested procedure along these lines:

Code: Pascal  [Select][+][-]
  1. procedure TMyClass.DoSomething(CodePos: TCodePos);
  2.  
  3.    procedure DoAnotherThing;
  4.    var
  5.       A: Integer;
  6.       B: Integer;
  7.    begin
  8.       ...
  9.    end;
  10.  
  11. begin
  12.   ...
  13.   DoAnotherThing;
  14.   ...
  15. end;
  16.  

When I am debugging using Xcode, I enter the main code for the DoSomething method, and I am interested especially in the value of the parameter CodePos, so I keep an eye on it in Xcode's variable list. The problem occurs when the nested procedure DoAnotherThing is entered. At that point, Xcode's list of variables is replaced by those variables only the immediate scope of DoAnotherThing, in this case A and B.

This is a dramatically simplified example. The code I am trying to debug, the MethodJumping.pas is proving to be much more tricky because the Codetools code has some large methods with large, nested procedures, and it is really like trying to debug a 1000 line app with global variables!

Does anyone know how I can make Xcode hang onto the display of the outer method's variables when the nested procedure is entered?

I've tried to search for a solution but the combination of Xcode and Pascal as Google search terms give no useful results that I can find.

Thanks,
Carl
« Last Edit: July 21, 2017, 10:23:59 pm by carl_caulkett »
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: Problem with Xcode/LLDB and debugging nested procedure [SOLVED]
« Reply #1 on: July 21, 2017, 10:35:21 pm »
I don't know if this is the best way to do this but it seems to work. In this case, I was interested in the value of a variable called CodePos of type TCodeXYPosition. So I declared a global variable TestPos of type PCodeXYPosition (pointer to TCodeXYPosition), and assigned TestPos := @CodePos;

The global variable is visible at any depth of nested procedure and since it is a pointer to the variable of interest, it shows a change of contents at any level, even if that variable is not directly in scope.

In other words:

Code: Pascal  [Select][+][-]
  1. var
  2.   TestPos: PCodeXYPosition;
  3.  
  4. procedure TMyClass.DoSomething(CodePos: TCodeXYPosition);
  5.  
  6.    procedure DoAnotherThing;
  7.    var
  8.       A: Integer;
  9.       B: Integer;
  10.    begin
  11.       ...
  12.       // TestPos is still visible to Xcodes variables display in its debugger (Set scope combo to "All")
  13.       ...
  14.    end;
  15.  
  16. begin
  17.   TestPos := @CodePos;
  18.   ...
  19.   DoAnotherThing;
  20.   ...
  21. end;
  22.  

Cheers,
Carl
« Last Edit: July 21, 2017, 10:43:08 pm by carl_caulkett »
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Problem with Xcode/LLDB and debugging nested procedure [SOLVED]
« Reply #2 on: July 21, 2017, 10:40:51 pm »
I don't know if this is the best way to do this but it seems to work. In this case, I was interested in the value of a variable called CodePos of type TCodeXYPosition. So I declared a global variable TestPos of type PCodeXYPosition (pointer to TCodeXYPosition), and assigned TestPos := @CodePos;

Yes, that seems like a pretty decent workaround.

I would guess that lldb doesn't support nested functions because most languages don't support them.

In general, I'm not a big fan of nested functions since their use can lead to what you're seeing: code that isn't much different from code that uses a lot of global variables.

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: Problem with Xcode/LLDB and debugging nested procedure [SOLVED]
« Reply #3 on: July 21, 2017, 10:46:28 pm »
You tell that to Mattias Gaertner. The Codetools code is riddled with nested procedures! Don't get me wrong - it's mighty clever code but not that easy to understand.
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Problem with Xcode/LLDB and debugging nested procedure [SOLVED]
« Reply #4 on: July 21, 2017, 11:05:10 pm »
You tell that to Mattias Gaertner. The Codetools code is riddled with nested procedures! Don't get me wrong - it's mighty clever code but not that easy to understand.

I suppose everyone has their preferences. I tend to write long methods. And while that works for the kind of work I do (scientific modeling), allowing you to read the code linearly, like a book, it does tend to pile up local variables, and since Pascal doesn't let you declare on the fly, they all go at the top of the method. Others like lots of very short methods, but then you jump around endlessly in the file trying to figure out how it works.

Elsewhere I posted a link to a file of code that I cited as an example of almost model code:

https://github.com/mapbox/mapbox-gl-native/blob/master/platform/macos/src/MGLMapView.mm

Even if one doesn't know any Objective C, it seems to me that this code is still eminently readable. And I asked the question, half rhetorically, how they achieved that. Well, generous white space, consistent indenting and variable naming, and, perhaps most important, comments where they're needed, in clear, well-written English. I'm only writing a Pascal wrapper for this class, so really all I need is its header file and a description of each method, but it's useful to look at the source as well and I was amazed at how readable this file was.

Oftentimes open source code is not very well documented. I think Lazarus can be accused of this. Not a lot of comments in the code.

 

TinyPortal © 2005-2018