Search code examples
rustlldb

How do I see a user-friendly format when debugging chrono::DateTime in vscode-lldb?


The debugger shows this for DateTime structs:

enter image description here

After looking at the docs for lldb to see how to format it, I see I can format integers as hex:

type format add -f hex i32

I tried something similar for DateTime

type format add --format hex chrono::datetime::DateTime<chrono::offset::utc::Utc>

It's not picking up the chrono type even though frame variable shows this as the type. Even after this step is done, not sure how to parse the date to string.


Solution

  • Using @Jim Ingham hints in the other answer and a touchup to this formula

    1. Modify the launch.json file by adding
    "initCommands": [
        "command source '${workspaceFolder}/my_type_formatters'"
    ]
    
    1. Add the following file called my_type_formatters to the root of the project.
    command script import simd.py 
    
    type summary add -F simd.GetSummary chrono::datetime::DateTime<chrono::offset::utc::Utc>
    
    1. Add a simd.py file to the root of the project with:
    from datetime import datetime,timedelta
    
    
    def GetSummary(valobj, internal_dict):
        ymdf = valobj.GetChildMemberWithName('datetime').GetChildMemberWithName('date').GetChildMemberWithName('ymdf').GetValue();
        secs = valobj.GetChildMemberWithName('datetime').GetChildMemberWithName('time').GetChildMemberWithName('secs').GetValue();
        year = int(ymdf) >> 13  
        day_of_year = (int(ymdf) & 8191) >> 4;
        date = datetime(year -1, 12, 31) + timedelta(days=day_of_year) + timedelta(seconds=int(secs))
        return date.strftime('%Y-%m-%d %H:%M:%S')
    

    Result:

    enter image description here