1 //===-- CoreMedia.cpp --------------------------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 #include "CoreMedia.h"
12 
13 #include "lldb/Core/Flags.h"
14 #include "lldb/Symbol/TypeSystem.h"
15 #include "lldb/Target/Target.h"
16 #include <inttypes.h>
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 using namespace lldb_private::formatters;
21 
22 bool lldb_private::formatters::CMTimeSummaryProvider(
23     ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
24   CompilerType type = valobj.GetCompilerType();
25   if (!type.IsValid())
26     return false;
27 
28   TypeSystem *type_system =
29       valobj.GetExecutionContextRef()
30           .GetTargetSP()
31           ->GetScratchTypeSystemForLanguage(nullptr, lldb::eLanguageTypeC);
32   if (!type_system)
33     return false;
34 
35   // fetch children by offset to compensate for potential lack of debug info
36   auto int64_ty =
37       type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 64);
38   auto int32_ty =
39       type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 32);
40 
41   auto value_sp(valobj.GetSyntheticChildAtOffset(0, int64_ty, true));
42   auto timescale_sp(valobj.GetSyntheticChildAtOffset(8, int32_ty, true));
43   auto flags_sp(valobj.GetSyntheticChildAtOffset(12, int32_ty, true));
44 
45   if (!value_sp || !timescale_sp || !flags_sp)
46     return false;
47 
48   auto value = value_sp->GetValueAsUnsigned(0);
49   auto timescale = (int32_t)timescale_sp->GetValueAsUnsigned(
50       0); // the timescale specifies the fraction of a second each unit in the
51           // numerator occupies
52   auto flags = Flags(flags_sp->GetValueAsUnsigned(0) &
53                      0x00000000000000FF); // the flags I need sit in the LSB
54 
55   const unsigned int FlagPositiveInf = 4;
56   const unsigned int FlagNegativeInf = 8;
57   const unsigned int FlagIndefinite = 16;
58 
59   if (flags.AnySet(FlagIndefinite)) {
60     stream.Printf("indefinite");
61     return true;
62   }
63 
64   if (flags.AnySet(FlagPositiveInf)) {
65     stream.Printf("+oo");
66     return true;
67   }
68 
69   if (flags.AnySet(FlagNegativeInf)) {
70     stream.Printf("-oo");
71     return true;
72   }
73 
74   if (timescale == 0)
75     return false;
76 
77   switch (timescale) {
78   case 0:
79     return false;
80   case 1:
81     stream.Printf("%" PRId64 " seconds", value);
82     return true;
83   case 2:
84     stream.Printf("%" PRId64 " half seconds", value);
85     return true;
86   case 3:
87     stream.Printf("%" PRId64 " third%sof a second", value,
88                   value == 1 ? " " : "s ");
89     return true;
90   default:
91     stream.Printf("%" PRId64 " %" PRId32 "th%sof a second", value, timescale,
92                   value == 1 ? " " : "s ");
93     return true;
94   }
95 }
96