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

Viewed 266

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.

2 Answers

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

lldb has three stages of customization for printing values of a given type. The simplest is "type format" which specifies the format to use (hex, chars, etc) to display scalar entities. The second is "type summary" which allows you to print a one-line string summary of the value. The third is "type synthetic" which allows you to present a value as some kind of structured object (e.g. making std::vector look like an vector of values rather than its internal form.)

You want to add a "type summary" for this type that parses the data and prints it as a human-readable string.

Related