1554f68d3SGreg Clayton //===-- FormatEntity.cpp ----------------------------------------*- C++ -*-===//
2554f68d3SGreg Clayton //
3554f68d3SGreg Clayton //                     The LLVM Compiler Infrastructure
4554f68d3SGreg Clayton //
5554f68d3SGreg Clayton // This file is distributed under the University of Illinois Open Source
6554f68d3SGreg Clayton // License. See LICENSE.TXT for details.
7554f68d3SGreg Clayton //
8554f68d3SGreg Clayton //===----------------------------------------------------------------------===//
9554f68d3SGreg Clayton 
10554f68d3SGreg Clayton #include "llvm/ADT/StringRef.h"
11554f68d3SGreg Clayton 
12554f68d3SGreg Clayton #include "lldb/Core/FormatEntity.h"
13554f68d3SGreg Clayton 
14554f68d3SGreg Clayton #include "lldb/Core/Address.h"
15554f68d3SGreg Clayton #include "lldb/Core/Debugger.h"
16554f68d3SGreg Clayton #include "lldb/Core/Module.h"
17554f68d3SGreg Clayton #include "lldb/Core/Stream.h"
18554f68d3SGreg Clayton #include "lldb/Core/StreamString.h"
19554f68d3SGreg Clayton #include "lldb/Core/ValueObject.h"
20554f68d3SGreg Clayton #include "lldb/Core/ValueObjectVariable.h"
21554f68d3SGreg Clayton #include "lldb/DataFormatters/DataVisualization.h"
22554f68d3SGreg Clayton #include "lldb/DataFormatters/FormatManager.h"
23ad91c7ceSEnrico Granata #include "lldb/DataFormatters/ValueObjectPrinter.h"
24937e3964SBruce Mitchener #include "lldb/Expression/ExpressionVariable.h"
25554f68d3SGreg Clayton #include "lldb/Host/FileSpec.h"
26554f68d3SGreg Clayton #include "lldb/Interpreter/CommandInterpreter.h"
27554f68d3SGreg Clayton #include "lldb/Symbol/Block.h"
28554f68d3SGreg Clayton #include "lldb/Symbol/CompileUnit.h"
29554f68d3SGreg Clayton #include "lldb/Symbol/Function.h"
30554f68d3SGreg Clayton #include "lldb/Symbol/LineEntry.h"
31554f68d3SGreg Clayton #include "lldb/Symbol/Symbol.h"
32554f68d3SGreg Clayton #include "lldb/Symbol/VariableList.h"
33554f68d3SGreg Clayton #include "lldb/Target/ExecutionContext.h"
340e0984eeSJim Ingham #include "lldb/Target/Language.h"
35554f68d3SGreg Clayton #include "lldb/Target/Process.h"
36554f68d3SGreg Clayton #include "lldb/Target/RegisterContext.h"
37554f68d3SGreg Clayton #include "lldb/Target/SectionLoadList.h"
38554f68d3SGreg Clayton #include "lldb/Target/StackFrame.h"
39554f68d3SGreg Clayton #include "lldb/Target/StopInfo.h"
40554f68d3SGreg Clayton #include "lldb/Target/Target.h"
41554f68d3SGreg Clayton #include "lldb/Target/Thread.h"
42554f68d3SGreg Clayton #include "lldb/Utility/AnsiTerminal.h"
43554f68d3SGreg Clayton 
44554f68d3SGreg Clayton using namespace lldb;
45554f68d3SGreg Clayton using namespace lldb_private;
46554f68d3SGreg Clayton 
47554f68d3SGreg Clayton 
48554f68d3SGreg Clayton enum FileKind
49554f68d3SGreg Clayton {
50554f68d3SGreg Clayton     FileError = 0,
51554f68d3SGreg Clayton     Basename,
52554f68d3SGreg Clayton     Dirname,
53554f68d3SGreg Clayton     Fullpath
54554f68d3SGreg Clayton };
55554f68d3SGreg Clayton 
56554f68d3SGreg Clayton #define ENTRY(n,t,f)            { n, NULL, FormatEntity::Entry::Type::t, FormatEntity::Entry::FormatType::f, 0,0,NULL, false}
57554f68d3SGreg Clayton #define ENTRY_VALUE(n,t,f,v)    { n, NULL, FormatEntity::Entry::Type::t, FormatEntity::Entry::FormatType::f, v,0,NULL, false}
58554f68d3SGreg Clayton #define ENTRY_CHILDREN(n,t,f,c) { n, NULL, FormatEntity::Entry::Type::t, FormatEntity::Entry::FormatType::f, 0,llvm::array_lengthof(c),c, false}
59554f68d3SGreg Clayton #define ENTRY_CHILDREN_KEEP_SEP(n,t,f,c) { n, NULL, FormatEntity::Entry::Type::t, FormatEntity::Entry::FormatType::f, 0,llvm::array_lengthof(c),c, true}
60554f68d3SGreg Clayton #define ENTRY_STRING(n,s)       { n, s, FormatEntity::Entry::Type::InsertString, FormatEntity::Entry::FormatType::None, 0,0, NULL, false}
61554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_string_entry[] =
62554f68d3SGreg Clayton {
63554f68d3SGreg Clayton     ENTRY("*", ParentString, None)
64554f68d3SGreg Clayton };
65554f68d3SGreg Clayton 
66554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_addr_entries[] =
67554f68d3SGreg Clayton {
68554f68d3SGreg Clayton     ENTRY ("load", AddressLoad      , UInt64),
69554f68d3SGreg Clayton     ENTRY ("file", AddressFile      , UInt64),
70554f68d3SGreg Clayton     ENTRY ("load", AddressLoadOrFile, UInt64),
71554f68d3SGreg Clayton };
72554f68d3SGreg Clayton 
73554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_file_child_entries[] =
74554f68d3SGreg Clayton {
75554f68d3SGreg Clayton     ENTRY_VALUE("basename", ParentNumber, CString, FileKind::Basename),
76554f68d3SGreg Clayton     ENTRY_VALUE("dirname", ParentNumber, CString, FileKind::Dirname),
77554f68d3SGreg Clayton     ENTRY_VALUE("fullpath", ParentNumber, CString, FileKind::Fullpath)
78554f68d3SGreg Clayton };
79554f68d3SGreg Clayton 
80554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_frame_child_entries[] =
81554f68d3SGreg Clayton {
82554f68d3SGreg Clayton 
83554f68d3SGreg Clayton     ENTRY ("index", FrameIndex        , UInt32),
84554f68d3SGreg Clayton     ENTRY ("pc"   , FrameRegisterPC   , UInt64),
85554f68d3SGreg Clayton     ENTRY ("fp"   , FrameRegisterFP   , UInt64),
86554f68d3SGreg Clayton     ENTRY ("sp"   , FrameRegisterSP   , UInt64),
87554f68d3SGreg Clayton     ENTRY ("flags", FrameRegisterFlags, UInt64),
88554f68d3SGreg Clayton     ENTRY_CHILDREN ("reg", FrameRegisterByName, UInt64, g_string_entry),
89554f68d3SGreg Clayton };
90554f68d3SGreg Clayton 
91554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_function_child_entries[] =
92554f68d3SGreg Clayton {
93554f68d3SGreg Clayton     ENTRY ("id"                  , FunctionID             , UInt64),
94554f68d3SGreg Clayton     ENTRY ("name"                , FunctionName           , CString),
95554f68d3SGreg Clayton     ENTRY ("name-without-args"   , FunctionNameNoArgs     , CString),
96554f68d3SGreg Clayton     ENTRY ("name-with-args"      , FunctionNameWithArgs   , CString),
97554f68d3SGreg Clayton     ENTRY ("addr-offset"         , FunctionAddrOffset     , UInt64),
98554f68d3SGreg Clayton     ENTRY ("concrete-only-addr-offset-no-padding", FunctionAddrOffsetConcrete, UInt64),
99554f68d3SGreg Clayton     ENTRY ("line-offset"         , FunctionLineOffset     , UInt64),
100c980fa92SJason Molenda     ENTRY ("pc-offset"           , FunctionPCOffset       , UInt64),
101c980fa92SJason Molenda     ENTRY ("initial-function"    , FunctionInitial        , None),
1026ab659a9SJason Molenda     ENTRY ("changed"             , FunctionChanged        , None),
1036ab659a9SJason Molenda     ENTRY ("is-optimized"        , FunctionIsOptimized    , None)
104554f68d3SGreg Clayton };
105554f68d3SGreg Clayton 
106554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_line_child_entries[] =
107554f68d3SGreg Clayton {
108554f68d3SGreg Clayton     ENTRY_CHILDREN("file", LineEntryFile        , None  , g_file_child_entries),
109554f68d3SGreg Clayton     ENTRY("number"       , LineEntryLineNumber  , UInt32),
110554f68d3SGreg Clayton     ENTRY("start-addr"   , LineEntryStartAddress, UInt64),
111554f68d3SGreg Clayton     ENTRY("end-addr"     , LineEntryEndAddress  , UInt64),
112554f68d3SGreg Clayton };
113554f68d3SGreg Clayton 
114554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_module_child_entries[] =
115554f68d3SGreg Clayton {
116554f68d3SGreg Clayton     ENTRY_CHILDREN("file", ModuleFile, None, g_file_child_entries),
117554f68d3SGreg Clayton };
118554f68d3SGreg Clayton 
119554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_process_child_entries[] =
120554f68d3SGreg Clayton {
121554f68d3SGreg Clayton     ENTRY           ( "id"      , ProcessID     , UInt64    ),
122554f68d3SGreg Clayton     ENTRY_VALUE     ( "name"    , ProcessFile   , CString   , FileKind::Basename),
123554f68d3SGreg Clayton     ENTRY_CHILDREN  ( "file"    , ProcessFile   , None      , g_file_child_entries),
124554f68d3SGreg Clayton };
125554f68d3SGreg Clayton 
126554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_svar_child_entries[] =
127554f68d3SGreg Clayton {
128554f68d3SGreg Clayton     ENTRY           ( "*"       , ParentString  , None)
129554f68d3SGreg Clayton };
130554f68d3SGreg Clayton 
131554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_var_child_entries[] =
132554f68d3SGreg Clayton {
133554f68d3SGreg Clayton     ENTRY           ( "*"       , ParentString  , None)
134554f68d3SGreg Clayton };
135554f68d3SGreg Clayton 
136554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_thread_child_entries[] =
137554f68d3SGreg Clayton {
138554f68d3SGreg Clayton     ENTRY           ( "id"                  , ThreadID                   , UInt64   ),
139554f68d3SGreg Clayton     ENTRY           ( "protocol_id"         , ThreadProtocolID           , UInt64   ),
140554f68d3SGreg Clayton     ENTRY           ( "index"               , ThreadIndexID              , UInt32   ),
141554f68d3SGreg Clayton     ENTRY_CHILDREN  ( "info"                , ThreadInfo                 , None     , g_string_entry),
142554f68d3SGreg Clayton     ENTRY           ( "queue"               , ThreadQueue                , CString  ),
143554f68d3SGreg Clayton     ENTRY           ( "name"                , ThreadName                 , CString  ),
144554f68d3SGreg Clayton     ENTRY           ( "stop-reason"         , ThreadStopReason           , CString  ),
145554f68d3SGreg Clayton     ENTRY           ( "return-value"        , ThreadReturnValue          , CString  ),
146554f68d3SGreg Clayton     ENTRY           ( "completed-expression", ThreadCompletedExpression  , CString  ),
147554f68d3SGreg Clayton };
148554f68d3SGreg Clayton 
149554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_target_child_entries[] =
150554f68d3SGreg Clayton {
151554f68d3SGreg Clayton     ENTRY           ( "arch"    , TargetArch    , CString   ),
152554f68d3SGreg Clayton };
153554f68d3SGreg Clayton 
154554f68d3SGreg Clayton #define _TO_STR2(_val) #_val
155554f68d3SGreg Clayton #define _TO_STR(_val) _TO_STR2(_val)
156554f68d3SGreg Clayton 
157554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_ansi_fg_entries[] =
158554f68d3SGreg Clayton {
159554f68d3SGreg Clayton     ENTRY_STRING ("black"   , ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_BLACK)      ANSI_ESC_END),
160554f68d3SGreg Clayton     ENTRY_STRING ("red"     , ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_RED)        ANSI_ESC_END),
161554f68d3SGreg Clayton     ENTRY_STRING ("green"   , ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_GREEN)      ANSI_ESC_END),
162554f68d3SGreg Clayton     ENTRY_STRING ("yellow"  , ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_YELLOW)     ANSI_ESC_END),
163554f68d3SGreg Clayton     ENTRY_STRING ("blue"    , ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_BLUE)       ANSI_ESC_END),
164554f68d3SGreg Clayton     ENTRY_STRING ("purple"  , ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_PURPLE)     ANSI_ESC_END),
165554f68d3SGreg Clayton     ENTRY_STRING ("cyan"    , ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_CYAN)       ANSI_ESC_END),
166554f68d3SGreg Clayton     ENTRY_STRING ("white"   , ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_WHITE)      ANSI_ESC_END),
167554f68d3SGreg Clayton };
168554f68d3SGreg Clayton 
169554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_ansi_bg_entries[] =
170554f68d3SGreg Clayton {
171554f68d3SGreg Clayton     ENTRY_STRING ("black"   , ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_BLACK)      ANSI_ESC_END),
172554f68d3SGreg Clayton     ENTRY_STRING ("red"     , ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_RED)        ANSI_ESC_END),
173554f68d3SGreg Clayton     ENTRY_STRING ("green"   , ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_GREEN)      ANSI_ESC_END),
174554f68d3SGreg Clayton     ENTRY_STRING ("yellow"  , ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_YELLOW)     ANSI_ESC_END),
175554f68d3SGreg Clayton     ENTRY_STRING ("blue"    , ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_BLUE)       ANSI_ESC_END),
176554f68d3SGreg Clayton     ENTRY_STRING ("purple"  , ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_PURPLE)     ANSI_ESC_END),
177554f68d3SGreg Clayton     ENTRY_STRING ("cyan"    , ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_CYAN)       ANSI_ESC_END),
178554f68d3SGreg Clayton     ENTRY_STRING ("white"   , ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_WHITE)      ANSI_ESC_END),
179554f68d3SGreg Clayton };
180554f68d3SGreg Clayton 
181554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_ansi_entries[] =
182554f68d3SGreg Clayton {
183554f68d3SGreg Clayton     ENTRY_CHILDREN  ( "fg"          , Invalid  , None      , g_ansi_fg_entries),
184554f68d3SGreg Clayton     ENTRY_CHILDREN  ( "bg"          , Invalid  , None      , g_ansi_bg_entries),
185554f68d3SGreg Clayton     ENTRY_STRING    ( "normal"      , ANSI_ESC_START _TO_STR(ANSI_CTRL_NORMAL)         ANSI_ESC_END),
186554f68d3SGreg Clayton     ENTRY_STRING    ( "bold"        , ANSI_ESC_START _TO_STR(ANSI_CTRL_BOLD)           ANSI_ESC_END),
187554f68d3SGreg Clayton     ENTRY_STRING    ( "faint"       , ANSI_ESC_START _TO_STR(ANSI_CTRL_FAINT)          ANSI_ESC_END),
188554f68d3SGreg Clayton     ENTRY_STRING    ( "italic"      , ANSI_ESC_START _TO_STR(ANSI_CTRL_ITALIC)         ANSI_ESC_END),
189554f68d3SGreg Clayton     ENTRY_STRING    ( "underline"   , ANSI_ESC_START _TO_STR(ANSI_CTRL_UNDERLINE)      ANSI_ESC_END),
190554f68d3SGreg Clayton     ENTRY_STRING    ( "slow-blink"  , ANSI_ESC_START _TO_STR(ANSI_CTRL_SLOW_BLINK)     ANSI_ESC_END),
191554f68d3SGreg Clayton     ENTRY_STRING    ( "fast-blink"  , ANSI_ESC_START _TO_STR(ANSI_CTRL_FAST_BLINK)     ANSI_ESC_END),
192554f68d3SGreg Clayton     ENTRY_STRING    ( "negative"    , ANSI_ESC_START _TO_STR(ANSI_CTRL_IMAGE_NEGATIVE) ANSI_ESC_END),
193554f68d3SGreg Clayton     ENTRY_STRING    ( "conceal"     , ANSI_ESC_START _TO_STR(ANSI_CTRL_CONCEAL)        ANSI_ESC_END),
194554f68d3SGreg Clayton     ENTRY_STRING    ( "crossed-out" , ANSI_ESC_START _TO_STR(ANSI_CTRL_CROSSED_OUT)    ANSI_ESC_END),
195554f68d3SGreg Clayton 
196554f68d3SGreg Clayton };
197554f68d3SGreg Clayton 
198554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_script_child_entries[] =
199554f68d3SGreg Clayton {
200554f68d3SGreg Clayton     ENTRY  ( "frame"   , ScriptFrame               , None),
201554f68d3SGreg Clayton     ENTRY  ( "process" , ScriptProcess             , None),
202554f68d3SGreg Clayton     ENTRY  ( "target"  , ScriptTarget              , None),
203554f68d3SGreg Clayton     ENTRY  ( "thread"  , ScriptThread              , None),
204554f68d3SGreg Clayton     ENTRY  ( "var"     , ScriptVariable            , None),
205554f68d3SGreg Clayton     ENTRY  ( "svar"    , ScriptVariableSynthetic   , None),
206554f68d3SGreg Clayton     ENTRY  ( "thread"  , ScriptThread              , None),
207554f68d3SGreg Clayton };
208554f68d3SGreg Clayton 
209554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_top_level_entries[] =
210554f68d3SGreg Clayton {
211554f68d3SGreg Clayton     ENTRY_CHILDREN          ("addr"                , AddressLoadOrFile      , UInt64    , g_addr_entries),
212554f68d3SGreg Clayton     ENTRY                   ("addr-file-or-load"   , AddressLoadOrFile      , UInt64    ),
213554f68d3SGreg Clayton     ENTRY_CHILDREN          ("ansi"                , Invalid                , None      , g_ansi_entries),
214554f68d3SGreg Clayton     ENTRY                   ("current-pc-arrow"    , CurrentPCArrow         , CString   ),
215554f68d3SGreg Clayton     ENTRY_CHILDREN          ("file"                , File                   , CString   , g_file_child_entries),
216dc975670SDawn Perchik     ENTRY                   ("language"            , Lang                   , CString),
217554f68d3SGreg Clayton     ENTRY_CHILDREN          ("frame"               , Invalid                , None      , g_frame_child_entries),
218554f68d3SGreg Clayton     ENTRY_CHILDREN          ("function"            , Invalid                , None      , g_function_child_entries),
219554f68d3SGreg Clayton     ENTRY_CHILDREN          ("line"                , Invalid                , None      , g_line_child_entries),
220554f68d3SGreg Clayton     ENTRY_CHILDREN          ("module"              , Invalid                , None      , g_module_child_entries),
221554f68d3SGreg Clayton     ENTRY_CHILDREN          ("process"             , Invalid                , None      , g_process_child_entries),
222554f68d3SGreg Clayton     ENTRY_CHILDREN          ("script"              , Invalid                , None      , g_script_child_entries),
223554f68d3SGreg Clayton     ENTRY_CHILDREN_KEEP_SEP ("svar"                , VariableSynthetic      , None      , g_svar_child_entries),
224554f68d3SGreg Clayton     ENTRY_CHILDREN          ("thread"              , Invalid                , None      , g_thread_child_entries),
225554f68d3SGreg Clayton     ENTRY_CHILDREN          ("target"              , Invalid                , None      , g_target_child_entries),
226554f68d3SGreg Clayton     ENTRY_CHILDREN_KEEP_SEP ("var"                 , Variable               , None      , g_var_child_entries),
227554f68d3SGreg Clayton };
228554f68d3SGreg Clayton 
229554f68d3SGreg Clayton static FormatEntity::Entry::Definition g_root = ENTRY_CHILDREN ("<root>", Root, None, g_top_level_entries);
230554f68d3SGreg Clayton 
231554f68d3SGreg Clayton 
232554f68d3SGreg Clayton FormatEntity::Entry::Entry (llvm::StringRef s) :
233554f68d3SGreg Clayton     string (s.data(), s.size()),
234554f68d3SGreg Clayton     printf_format (),
235554f68d3SGreg Clayton     children (),
236554f68d3SGreg Clayton     definition (NULL),
237554f68d3SGreg Clayton     type (Type::String),
238554f68d3SGreg Clayton     fmt (lldb::eFormatDefault),
239554f68d3SGreg Clayton     number (0),
240554f68d3SGreg Clayton     deref (false)
241554f68d3SGreg Clayton {
242554f68d3SGreg Clayton }
243554f68d3SGreg Clayton 
244554f68d3SGreg Clayton FormatEntity::Entry::Entry (char ch) :
245554f68d3SGreg Clayton     string (1, ch),
246554f68d3SGreg Clayton     printf_format (),
247554f68d3SGreg Clayton     children (),
248554f68d3SGreg Clayton     definition (NULL),
249554f68d3SGreg Clayton     type (Type::String),
250554f68d3SGreg Clayton     fmt (lldb::eFormatDefault),
251554f68d3SGreg Clayton     number (0),
252554f68d3SGreg Clayton     deref (false)
253554f68d3SGreg Clayton {
254554f68d3SGreg Clayton }
255554f68d3SGreg Clayton 
256554f68d3SGreg Clayton void
257554f68d3SGreg Clayton FormatEntity::Entry::AppendChar (char ch)
258554f68d3SGreg Clayton {
259554f68d3SGreg Clayton     if (children.empty() || children.back().type != Entry::Type::String)
260554f68d3SGreg Clayton         children.push_back(Entry(ch));
261554f68d3SGreg Clayton     else
262554f68d3SGreg Clayton         children.back().string.append(1, ch);
263554f68d3SGreg Clayton }
264554f68d3SGreg Clayton 
265554f68d3SGreg Clayton void
266554f68d3SGreg Clayton FormatEntity::Entry::AppendText (const llvm::StringRef &s)
267554f68d3SGreg Clayton {
268554f68d3SGreg Clayton     if (children.empty() || children.back().type != Entry::Type::String)
269554f68d3SGreg Clayton         children.push_back(Entry(s));
270554f68d3SGreg Clayton     else
271554f68d3SGreg Clayton         children.back().string.append(s.data(), s.size());
272554f68d3SGreg Clayton }
273554f68d3SGreg Clayton 
274554f68d3SGreg Clayton void
275554f68d3SGreg Clayton FormatEntity::Entry::AppendText (const char *cstr)
276554f68d3SGreg Clayton {
277554f68d3SGreg Clayton     return AppendText (llvm::StringRef(cstr));
278554f68d3SGreg Clayton }
279554f68d3SGreg Clayton 
280554f68d3SGreg Clayton 
281554f68d3SGreg Clayton Error
282554f68d3SGreg Clayton FormatEntity::Parse (const llvm::StringRef &format_str, Entry &entry)
283554f68d3SGreg Clayton {
284554f68d3SGreg Clayton     entry.Clear();
285554f68d3SGreg Clayton     entry.type = Entry::Type::Root;
286554f68d3SGreg Clayton     llvm::StringRef modifiable_format (format_str);
287554f68d3SGreg Clayton     return ParseInternal (modifiable_format, entry, 0);
288554f68d3SGreg Clayton }
289554f68d3SGreg Clayton 
290554f68d3SGreg Clayton #define ENUM_TO_CSTR(eee) case FormatEntity::Entry::Type::eee: return #eee
291554f68d3SGreg Clayton 
292554f68d3SGreg Clayton const char *
293554f68d3SGreg Clayton FormatEntity::Entry::TypeToCString (Type t)
294554f68d3SGreg Clayton {
295554f68d3SGreg Clayton     switch (t)
296554f68d3SGreg Clayton     {
297554f68d3SGreg Clayton     ENUM_TO_CSTR(Invalid);
298554f68d3SGreg Clayton     ENUM_TO_CSTR(ParentNumber);
299554f68d3SGreg Clayton     ENUM_TO_CSTR(ParentString);
300554f68d3SGreg Clayton     ENUM_TO_CSTR(InsertString);
301554f68d3SGreg Clayton     ENUM_TO_CSTR(Root);
302554f68d3SGreg Clayton     ENUM_TO_CSTR(String);
303554f68d3SGreg Clayton     ENUM_TO_CSTR(Scope);
304554f68d3SGreg Clayton     ENUM_TO_CSTR(Variable);
305554f68d3SGreg Clayton     ENUM_TO_CSTR(VariableSynthetic);
306554f68d3SGreg Clayton     ENUM_TO_CSTR(ScriptVariable);
307554f68d3SGreg Clayton     ENUM_TO_CSTR(ScriptVariableSynthetic);
308554f68d3SGreg Clayton     ENUM_TO_CSTR(AddressLoad);
309554f68d3SGreg Clayton     ENUM_TO_CSTR(AddressFile);
310554f68d3SGreg Clayton     ENUM_TO_CSTR(AddressLoadOrFile);
311554f68d3SGreg Clayton     ENUM_TO_CSTR(ProcessID);
312554f68d3SGreg Clayton     ENUM_TO_CSTR(ProcessFile);
313554f68d3SGreg Clayton     ENUM_TO_CSTR(ScriptProcess);
314554f68d3SGreg Clayton     ENUM_TO_CSTR(ThreadID);
315554f68d3SGreg Clayton     ENUM_TO_CSTR(ThreadProtocolID);
316554f68d3SGreg Clayton     ENUM_TO_CSTR(ThreadIndexID);
317554f68d3SGreg Clayton     ENUM_TO_CSTR(ThreadName);
318554f68d3SGreg Clayton     ENUM_TO_CSTR(ThreadQueue);
319554f68d3SGreg Clayton     ENUM_TO_CSTR(ThreadStopReason);
320554f68d3SGreg Clayton     ENUM_TO_CSTR(ThreadReturnValue);
321554f68d3SGreg Clayton     ENUM_TO_CSTR(ThreadCompletedExpression);
322554f68d3SGreg Clayton     ENUM_TO_CSTR(ScriptThread);
323554f68d3SGreg Clayton     ENUM_TO_CSTR(ThreadInfo);
324554f68d3SGreg Clayton     ENUM_TO_CSTR(TargetArch);
325554f68d3SGreg Clayton     ENUM_TO_CSTR(ScriptTarget);
326554f68d3SGreg Clayton     ENUM_TO_CSTR(ModuleFile);
327554f68d3SGreg Clayton     ENUM_TO_CSTR(File);
328dc975670SDawn Perchik     ENUM_TO_CSTR(Lang);
329554f68d3SGreg Clayton     ENUM_TO_CSTR(FrameIndex);
330554f68d3SGreg Clayton     ENUM_TO_CSTR(FrameRegisterPC);
331554f68d3SGreg Clayton     ENUM_TO_CSTR(FrameRegisterSP);
332554f68d3SGreg Clayton     ENUM_TO_CSTR(FrameRegisterFP);
333554f68d3SGreg Clayton     ENUM_TO_CSTR(FrameRegisterFlags);
334554f68d3SGreg Clayton     ENUM_TO_CSTR(FrameRegisterByName);
335554f68d3SGreg Clayton     ENUM_TO_CSTR(ScriptFrame);
336554f68d3SGreg Clayton     ENUM_TO_CSTR(FunctionID);
337554f68d3SGreg Clayton     ENUM_TO_CSTR(FunctionDidChange);
338554f68d3SGreg Clayton     ENUM_TO_CSTR(FunctionInitialFunction);
339554f68d3SGreg Clayton     ENUM_TO_CSTR(FunctionName);
340554f68d3SGreg Clayton     ENUM_TO_CSTR(FunctionNameWithArgs);
341554f68d3SGreg Clayton     ENUM_TO_CSTR(FunctionNameNoArgs);
342554f68d3SGreg Clayton     ENUM_TO_CSTR(FunctionAddrOffset);
343554f68d3SGreg Clayton     ENUM_TO_CSTR(FunctionAddrOffsetConcrete);
344554f68d3SGreg Clayton     ENUM_TO_CSTR(FunctionLineOffset);
345554f68d3SGreg Clayton     ENUM_TO_CSTR(FunctionPCOffset);
346c980fa92SJason Molenda     ENUM_TO_CSTR(FunctionInitial);
347c980fa92SJason Molenda     ENUM_TO_CSTR(FunctionChanged);
3486ab659a9SJason Molenda     ENUM_TO_CSTR(FunctionIsOptimized);
349554f68d3SGreg Clayton     ENUM_TO_CSTR(LineEntryFile);
350554f68d3SGreg Clayton     ENUM_TO_CSTR(LineEntryLineNumber);
351554f68d3SGreg Clayton     ENUM_TO_CSTR(LineEntryStartAddress);
352554f68d3SGreg Clayton     ENUM_TO_CSTR(LineEntryEndAddress);
353554f68d3SGreg Clayton     ENUM_TO_CSTR(CurrentPCArrow);
354554f68d3SGreg Clayton     }
355554f68d3SGreg Clayton     return "???";
356554f68d3SGreg Clayton }
357554f68d3SGreg Clayton 
358554f68d3SGreg Clayton #undef ENUM_TO_CSTR
359554f68d3SGreg Clayton 
360554f68d3SGreg Clayton void
361554f68d3SGreg Clayton FormatEntity::Entry::Dump (Stream &s, int depth) const
362554f68d3SGreg Clayton {
363554f68d3SGreg Clayton     s.Printf ("%*.*s%-20s: ", depth * 2, depth * 2, "", TypeToCString(type));
364554f68d3SGreg Clayton     if (fmt != eFormatDefault)
365554f68d3SGreg Clayton         s.Printf ("lldb-format = %s, ", FormatManager::GetFormatAsCString (fmt));
366554f68d3SGreg Clayton     if (!string.empty())
367554f68d3SGreg Clayton         s.Printf ("string = \"%s\"", string.c_str());
368554f68d3SGreg Clayton     if (!printf_format.empty())
369554f68d3SGreg Clayton         s.Printf ("printf_format = \"%s\"", printf_format.c_str());
370554f68d3SGreg Clayton     if (number != 0)
371554f68d3SGreg Clayton         s.Printf ("number = %" PRIu64 " (0x%" PRIx64 "), ", number, number);
372554f68d3SGreg Clayton     if (deref)
373554f68d3SGreg Clayton         s.Printf ("deref = true, ");
374554f68d3SGreg Clayton     s.EOL();
375554f68d3SGreg Clayton     for (const auto &child : children)
376554f68d3SGreg Clayton     {
377554f68d3SGreg Clayton         child.Dump(s, depth + 1);
378554f68d3SGreg Clayton     }
379554f68d3SGreg Clayton }
380554f68d3SGreg Clayton 
381554f68d3SGreg Clayton 
382554f68d3SGreg Clayton template <typename T>
383554f68d3SGreg Clayton static bool RunScriptFormatKeyword(Stream &s,
384554f68d3SGreg Clayton                                    const SymbolContext *sc,
385554f68d3SGreg Clayton                                    const ExecutionContext *exe_ctx,
386554f68d3SGreg Clayton                                    T t,
387554f68d3SGreg Clayton                                    const char *script_function_name)
388554f68d3SGreg Clayton {
389554f68d3SGreg Clayton     Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
390554f68d3SGreg Clayton 
391554f68d3SGreg Clayton     if (target)
392554f68d3SGreg Clayton     {
393554f68d3SGreg Clayton         ScriptInterpreter *script_interpreter = target->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
394554f68d3SGreg Clayton         if (script_interpreter)
395554f68d3SGreg Clayton         {
396554f68d3SGreg Clayton             Error error;
397554f68d3SGreg Clayton             std::string script_output;
398554f68d3SGreg Clayton 
399554f68d3SGreg Clayton             if (script_interpreter->RunScriptFormatKeyword(script_function_name, t, script_output, error) && error.Success())
400554f68d3SGreg Clayton             {
401554f68d3SGreg Clayton                 s.Printf("%s", script_output.c_str());
402554f68d3SGreg Clayton                 return true;
403554f68d3SGreg Clayton             }
404554f68d3SGreg Clayton             else
405554f68d3SGreg Clayton             {
406554f68d3SGreg Clayton                 s.Printf("<error: %s>",error.AsCString());
407554f68d3SGreg Clayton             }
408554f68d3SGreg Clayton         }
409554f68d3SGreg Clayton     }
410554f68d3SGreg Clayton     return false;
411554f68d3SGreg Clayton }
412554f68d3SGreg Clayton 
413554f68d3SGreg Clayton static bool
414554f68d3SGreg Clayton DumpAddress (Stream &s,
415554f68d3SGreg Clayton              const SymbolContext *sc,
416554f68d3SGreg Clayton              const ExecutionContext *exe_ctx,
417554f68d3SGreg Clayton              const Address &addr,
418554f68d3SGreg Clayton              bool print_file_addr_or_load_addr)
419554f68d3SGreg Clayton {
420554f68d3SGreg Clayton     Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
421554f68d3SGreg Clayton     addr_t vaddr = LLDB_INVALID_ADDRESS;
422554f68d3SGreg Clayton     if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
423554f68d3SGreg Clayton         vaddr = addr.GetLoadAddress (target);
424554f68d3SGreg Clayton     if (vaddr == LLDB_INVALID_ADDRESS)
425554f68d3SGreg Clayton         vaddr = addr.GetFileAddress ();
426554f68d3SGreg Clayton 
427554f68d3SGreg Clayton     if (vaddr != LLDB_INVALID_ADDRESS)
428554f68d3SGreg Clayton     {
429554f68d3SGreg Clayton         int addr_width = 0;
430554f68d3SGreg Clayton         if (exe_ctx && target)
431554f68d3SGreg Clayton         {
432554f68d3SGreg Clayton             addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
433554f68d3SGreg Clayton         }
434554f68d3SGreg Clayton         if (addr_width == 0)
435554f68d3SGreg Clayton             addr_width = 16;
436554f68d3SGreg Clayton         if (print_file_addr_or_load_addr)
437554f68d3SGreg Clayton         {
438554f68d3SGreg Clayton             ExecutionContextScope *exe_scope = NULL;
439554f68d3SGreg Clayton             if (exe_ctx)
440554f68d3SGreg Clayton                 exe_scope = exe_ctx->GetBestExecutionContextScope();
441554f68d3SGreg Clayton             addr.Dump (&s, exe_scope, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress, 0);
442554f68d3SGreg Clayton         }
443554f68d3SGreg Clayton         else
444554f68d3SGreg Clayton         {
445554f68d3SGreg Clayton             s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
446554f68d3SGreg Clayton         }
447554f68d3SGreg Clayton         return true;
448554f68d3SGreg Clayton     }
449554f68d3SGreg Clayton     return false;
450554f68d3SGreg Clayton }
451554f68d3SGreg Clayton 
452554f68d3SGreg Clayton static bool
453554f68d3SGreg Clayton DumpAddressOffsetFromFunction (Stream &s,
454554f68d3SGreg Clayton                                const SymbolContext *sc,
455554f68d3SGreg Clayton                                const ExecutionContext *exe_ctx,
456554f68d3SGreg Clayton                                const Address &format_addr,
457554f68d3SGreg Clayton                                bool concrete_only,
458c980fa92SJason Molenda                                bool no_padding,
459c980fa92SJason Molenda                                bool print_zero_offsets)
460554f68d3SGreg Clayton {
461554f68d3SGreg Clayton     if (format_addr.IsValid())
462554f68d3SGreg Clayton     {
463554f68d3SGreg Clayton         Address func_addr;
464554f68d3SGreg Clayton 
465554f68d3SGreg Clayton         if (sc)
466554f68d3SGreg Clayton         {
467554f68d3SGreg Clayton             if (sc->function)
468554f68d3SGreg Clayton             {
469554f68d3SGreg Clayton                 func_addr = sc->function->GetAddressRange().GetBaseAddress();
470554f68d3SGreg Clayton                 if (sc->block && !concrete_only)
471554f68d3SGreg Clayton                 {
472554f68d3SGreg Clayton                     // Check to make sure we aren't in an inline
473554f68d3SGreg Clayton                     // function. If we are, use the inline block
474554f68d3SGreg Clayton                     // range that contains "format_addr" since
475554f68d3SGreg Clayton                     // blocks can be discontiguous.
476554f68d3SGreg Clayton                     Block *inline_block = sc->block->GetContainingInlinedBlock ();
477554f68d3SGreg Clayton                     AddressRange inline_range;
478554f68d3SGreg Clayton                     if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
479554f68d3SGreg Clayton                         func_addr = inline_range.GetBaseAddress();
480554f68d3SGreg Clayton                 }
481554f68d3SGreg Clayton             }
482554f68d3SGreg Clayton             else if (sc->symbol && sc->symbol->ValueIsAddress())
483358cf1eaSGreg Clayton                 func_addr = sc->symbol->GetAddressRef();
484554f68d3SGreg Clayton         }
485554f68d3SGreg Clayton 
486554f68d3SGreg Clayton         if (func_addr.IsValid())
487554f68d3SGreg Clayton         {
488554f68d3SGreg Clayton             const char *addr_offset_padding = no_padding ? "" : " ";
489554f68d3SGreg Clayton 
490554f68d3SGreg Clayton             if (func_addr.GetSection() == format_addr.GetSection())
491554f68d3SGreg Clayton             {
492554f68d3SGreg Clayton                 addr_t func_file_addr = func_addr.GetFileAddress();
493554f68d3SGreg Clayton                 addr_t addr_file_addr = format_addr.GetFileAddress();
494c980fa92SJason Molenda                 if (addr_file_addr > func_file_addr
495c980fa92SJason Molenda                     || (addr_file_addr == func_file_addr && print_zero_offsets))
496c980fa92SJason Molenda                 {
497554f68d3SGreg Clayton                     s.Printf("%s+%s%" PRIu64, addr_offset_padding, addr_offset_padding, addr_file_addr - func_file_addr);
498c980fa92SJason Molenda                 }
499554f68d3SGreg Clayton                 else if (addr_file_addr < func_file_addr)
500c980fa92SJason Molenda                 {
501554f68d3SGreg Clayton                     s.Printf("%s-%s%" PRIu64, addr_offset_padding, addr_offset_padding, func_file_addr - addr_file_addr);
502c980fa92SJason Molenda                 }
503554f68d3SGreg Clayton                 return true;
504554f68d3SGreg Clayton             }
505554f68d3SGreg Clayton             else
506554f68d3SGreg Clayton             {
507554f68d3SGreg Clayton                 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
508554f68d3SGreg Clayton                 if (target)
509554f68d3SGreg Clayton                 {
510554f68d3SGreg Clayton                     addr_t func_load_addr = func_addr.GetLoadAddress (target);
511554f68d3SGreg Clayton                     addr_t addr_load_addr = format_addr.GetLoadAddress (target);
512c980fa92SJason Molenda                     if (addr_load_addr > func_load_addr
513c980fa92SJason Molenda                         || (addr_load_addr == func_load_addr && print_zero_offsets))
514c980fa92SJason Molenda                     {
515554f68d3SGreg Clayton                         s.Printf("%s+%s%" PRIu64, addr_offset_padding, addr_offset_padding, addr_load_addr - func_load_addr);
516c980fa92SJason Molenda                     }
517554f68d3SGreg Clayton                     else if (addr_load_addr < func_load_addr)
518c980fa92SJason Molenda                     {
519554f68d3SGreg Clayton                         s.Printf("%s-%s%" PRIu64, addr_offset_padding, addr_offset_padding, func_load_addr - addr_load_addr);
520c980fa92SJason Molenda                     }
521554f68d3SGreg Clayton                     return true;
522554f68d3SGreg Clayton                 }
523554f68d3SGreg Clayton             }
524554f68d3SGreg Clayton         }
525554f68d3SGreg Clayton     }
526554f68d3SGreg Clayton     return false;
527554f68d3SGreg Clayton }
528554f68d3SGreg Clayton 
529554f68d3SGreg Clayton static bool
530554f68d3SGreg Clayton ScanBracketedRange (llvm::StringRef subpath,
531554f68d3SGreg Clayton                     size_t& close_bracket_index,
532554f68d3SGreg Clayton                     const char*& var_name_final_if_array_range,
533554f68d3SGreg Clayton                     int64_t& index_lower,
534554f68d3SGreg Clayton                     int64_t& index_higher)
535554f68d3SGreg Clayton {
536*4edfef45SEnrico Granata     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_DATAFORMATTERS));
537554f68d3SGreg Clayton     close_bracket_index = llvm::StringRef::npos;
538554f68d3SGreg Clayton     const size_t open_bracket_index = subpath.find('[');
539554f68d3SGreg Clayton     if (open_bracket_index == llvm::StringRef::npos)
540554f68d3SGreg Clayton     {
541554f68d3SGreg Clayton         if (log)
542554f68d3SGreg Clayton             log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
543554f68d3SGreg Clayton         return false;
544554f68d3SGreg Clayton     }
545554f68d3SGreg Clayton 
546554f68d3SGreg Clayton     close_bracket_index = subpath.find(']', open_bracket_index + 1);
547554f68d3SGreg Clayton 
548554f68d3SGreg Clayton     if (close_bracket_index == llvm::StringRef::npos)
549554f68d3SGreg Clayton     {
550554f68d3SGreg Clayton         if (log)
551554f68d3SGreg Clayton             log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
552554f68d3SGreg Clayton         return false;
553554f68d3SGreg Clayton     }
554554f68d3SGreg Clayton     else
555554f68d3SGreg Clayton     {
556554f68d3SGreg Clayton         var_name_final_if_array_range = subpath.data() + open_bracket_index;
557554f68d3SGreg Clayton 
558554f68d3SGreg Clayton         if (close_bracket_index - open_bracket_index == 1)
559554f68d3SGreg Clayton         {
560554f68d3SGreg Clayton             if (log)
561554f68d3SGreg Clayton                 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
562554f68d3SGreg Clayton             index_lower = 0;
563554f68d3SGreg Clayton         }
564554f68d3SGreg Clayton         else
565554f68d3SGreg Clayton         {
566554f68d3SGreg Clayton             const size_t separator_index = subpath.find('-', open_bracket_index + 1);
567554f68d3SGreg Clayton 
568554f68d3SGreg Clayton             if (separator_index == llvm::StringRef::npos)
569554f68d3SGreg Clayton             {
570554f68d3SGreg Clayton                 const char *index_lower_cstr = subpath.data() + open_bracket_index + 1;
571554f68d3SGreg Clayton                 index_lower = ::strtoul (index_lower_cstr, NULL, 0);
572554f68d3SGreg Clayton                 index_higher = index_lower;
573554f68d3SGreg Clayton                 if (log)
574554f68d3SGreg Clayton                     log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", index_lower);
575554f68d3SGreg Clayton             }
576554f68d3SGreg Clayton             else
577554f68d3SGreg Clayton             {
578554f68d3SGreg Clayton                 const char *index_lower_cstr = subpath.data() + open_bracket_index + 1;
579554f68d3SGreg Clayton                 const char *index_higher_cstr = subpath.data() + separator_index + 1;
580554f68d3SGreg Clayton                 index_lower = ::strtoul (index_lower_cstr, NULL, 0);
581554f68d3SGreg Clayton                 index_higher = ::strtoul (index_higher_cstr, NULL, 0);
582554f68d3SGreg Clayton                 if (log)
583554f68d3SGreg Clayton                     log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", index_lower, index_higher);
584554f68d3SGreg Clayton             }
585554f68d3SGreg Clayton             if (index_lower > index_higher && index_higher > 0)
586554f68d3SGreg Clayton             {
587554f68d3SGreg Clayton                 if (log)
588554f68d3SGreg Clayton                     log->Printf("[ScanBracketedRange] swapping indices");
589554f68d3SGreg Clayton                 const int64_t temp = index_lower;
590554f68d3SGreg Clayton                 index_lower = index_higher;
591554f68d3SGreg Clayton                 index_higher = temp;
592554f68d3SGreg Clayton             }
593554f68d3SGreg Clayton         }
594554f68d3SGreg Clayton     }
595554f68d3SGreg Clayton     return true;
596554f68d3SGreg Clayton }
597554f68d3SGreg Clayton 
598554f68d3SGreg Clayton static bool
599554f68d3SGreg Clayton DumpFile (Stream &s, const FileSpec &file, FileKind file_kind)
600554f68d3SGreg Clayton {
601554f68d3SGreg Clayton     switch (file_kind)
602554f68d3SGreg Clayton     {
603554f68d3SGreg Clayton     case FileKind::FileError:
604554f68d3SGreg Clayton         break;
605554f68d3SGreg Clayton 
606554f68d3SGreg Clayton     case FileKind::Basename:
607554f68d3SGreg Clayton         if (file.GetFilename())
608554f68d3SGreg Clayton         {
609554f68d3SGreg Clayton             s << file.GetFilename();
610554f68d3SGreg Clayton             return true;
611554f68d3SGreg Clayton         }
612554f68d3SGreg Clayton         break;
613554f68d3SGreg Clayton 
614554f68d3SGreg Clayton     case FileKind::Dirname:
615554f68d3SGreg Clayton         if (file.GetDirectory())
616554f68d3SGreg Clayton         {
617554f68d3SGreg Clayton             s << file.GetDirectory();
618554f68d3SGreg Clayton             return true;
619554f68d3SGreg Clayton         }
620554f68d3SGreg Clayton         break;
621554f68d3SGreg Clayton 
622554f68d3SGreg Clayton     case FileKind::Fullpath:
623554f68d3SGreg Clayton         if (file)
624554f68d3SGreg Clayton         {
625554f68d3SGreg Clayton             s << file;
626554f68d3SGreg Clayton             return true;
627554f68d3SGreg Clayton         }
628554f68d3SGreg Clayton         break;
629554f68d3SGreg Clayton     }
630554f68d3SGreg Clayton     return false;
631554f68d3SGreg Clayton }
632554f68d3SGreg Clayton 
633554f68d3SGreg Clayton static bool
634554f68d3SGreg Clayton DumpRegister (Stream &s,
635554f68d3SGreg Clayton               StackFrame *frame,
636554f68d3SGreg Clayton               RegisterKind reg_kind,
637554f68d3SGreg Clayton               uint32_t reg_num,
638554f68d3SGreg Clayton               Format format)
639554f68d3SGreg Clayton 
640554f68d3SGreg Clayton {
641554f68d3SGreg Clayton     if (frame)
642554f68d3SGreg Clayton     {
643554f68d3SGreg Clayton         RegisterContext *reg_ctx = frame->GetRegisterContext().get();
644554f68d3SGreg Clayton 
645554f68d3SGreg Clayton         if (reg_ctx)
646554f68d3SGreg Clayton         {
647554f68d3SGreg Clayton             const uint32_t lldb_reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
648554f68d3SGreg Clayton             if (lldb_reg_num != LLDB_INVALID_REGNUM)
649554f68d3SGreg Clayton             {
650554f68d3SGreg Clayton                 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex (lldb_reg_num);
651554f68d3SGreg Clayton                 if (reg_info)
652554f68d3SGreg Clayton                 {
653554f68d3SGreg Clayton                     RegisterValue reg_value;
654554f68d3SGreg Clayton                     if (reg_ctx->ReadRegister (reg_info, reg_value))
655554f68d3SGreg Clayton                     {
656554f68d3SGreg Clayton                         reg_value.Dump(&s, reg_info, false, false, format);
657554f68d3SGreg Clayton                         return true;
658554f68d3SGreg Clayton                     }
659554f68d3SGreg Clayton                 }
660554f68d3SGreg Clayton             }
661554f68d3SGreg Clayton         }
662554f68d3SGreg Clayton     }
663554f68d3SGreg Clayton     return false;
664554f68d3SGreg Clayton }
665554f68d3SGreg Clayton 
666554f68d3SGreg Clayton 
667554f68d3SGreg Clayton static ValueObjectSP
668554f68d3SGreg Clayton ExpandIndexedExpression (ValueObject* valobj,
669554f68d3SGreg Clayton                          size_t index,
670554f68d3SGreg Clayton                          StackFrame* frame,
671554f68d3SGreg Clayton                          bool deref_pointer)
672554f68d3SGreg Clayton {
673*4edfef45SEnrico Granata     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_DATAFORMATTERS));
674554f68d3SGreg Clayton     const char* ptr_deref_format = "[%d]";
675554f68d3SGreg Clayton     std::string ptr_deref_buffer(10,0);
676554f68d3SGreg Clayton     ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
677554f68d3SGreg Clayton     if (log)
678554f68d3SGreg Clayton         log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
679554f68d3SGreg Clayton     const char* first_unparsed;
680554f68d3SGreg Clayton     ValueObject::GetValueForExpressionPathOptions options;
681554f68d3SGreg Clayton     ValueObject::ExpressionPathEndResultType final_value_type;
682554f68d3SGreg Clayton     ValueObject::ExpressionPathScanEndReason reason_to_stop;
683554f68d3SGreg Clayton     ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
684554f68d3SGreg Clayton     ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
685554f68d3SGreg Clayton                                                             &first_unparsed,
686554f68d3SGreg Clayton                                                             &reason_to_stop,
687554f68d3SGreg Clayton                                                             &final_value_type,
688554f68d3SGreg Clayton                                                             options,
689554f68d3SGreg Clayton                                                             &what_next);
690554f68d3SGreg Clayton     if (!item)
691554f68d3SGreg Clayton     {
692554f68d3SGreg Clayton         if (log)
693554f68d3SGreg Clayton             log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
694554f68d3SGreg Clayton                         " final_value_type %d",
695554f68d3SGreg Clayton                         first_unparsed, reason_to_stop, final_value_type);
696554f68d3SGreg Clayton     }
697554f68d3SGreg Clayton     else
698554f68d3SGreg Clayton     {
699554f68d3SGreg Clayton         if (log)
700554f68d3SGreg Clayton             log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
701554f68d3SGreg Clayton                         " final_value_type %d",
702554f68d3SGreg Clayton                         first_unparsed, reason_to_stop, final_value_type);
703554f68d3SGreg Clayton     }
704554f68d3SGreg Clayton     return item;
705554f68d3SGreg Clayton }
706554f68d3SGreg Clayton 
707554f68d3SGreg Clayton static char
708554f68d3SGreg Clayton ConvertValueObjectStyleToChar(ValueObject::ValueObjectRepresentationStyle style)
709554f68d3SGreg Clayton {
710554f68d3SGreg Clayton     switch (style)
711554f68d3SGreg Clayton     {
712554f68d3SGreg Clayton         case ValueObject::eValueObjectRepresentationStyleLanguageSpecific:  return '@';
713554f68d3SGreg Clayton         case ValueObject::eValueObjectRepresentationStyleValue:             return 'V';
714554f68d3SGreg Clayton         case ValueObject::eValueObjectRepresentationStyleLocation:          return 'L';
715554f68d3SGreg Clayton         case ValueObject::eValueObjectRepresentationStyleSummary:           return 'S';
716554f68d3SGreg Clayton         case ValueObject::eValueObjectRepresentationStyleChildrenCount:     return '#';
717554f68d3SGreg Clayton         case ValueObject::eValueObjectRepresentationStyleType:              return 'T';
718554f68d3SGreg Clayton         case ValueObject::eValueObjectRepresentationStyleName:              return 'N';
719554f68d3SGreg Clayton         case ValueObject::eValueObjectRepresentationStyleExpressionPath:    return '>';
720554f68d3SGreg Clayton     }
721554f68d3SGreg Clayton     return '\0';
722554f68d3SGreg Clayton }
723554f68d3SGreg Clayton 
724554f68d3SGreg Clayton static bool
725554f68d3SGreg Clayton DumpValue (Stream &s,
726554f68d3SGreg Clayton            const SymbolContext *sc,
727554f68d3SGreg Clayton            const ExecutionContext *exe_ctx,
728554f68d3SGreg Clayton            const FormatEntity::Entry &entry,
729554f68d3SGreg Clayton            ValueObject *valobj)
730554f68d3SGreg Clayton {
731554f68d3SGreg Clayton     if (valobj == NULL)
732554f68d3SGreg Clayton         return false;
733554f68d3SGreg Clayton 
734*4edfef45SEnrico Granata     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_DATAFORMATTERS));
735554f68d3SGreg Clayton     Format custom_format = eFormatInvalid;
736554f68d3SGreg Clayton     ValueObject::ValueObjectRepresentationStyle val_obj_display = entry.string.empty() ? ValueObject::eValueObjectRepresentationStyleValue : ValueObject::eValueObjectRepresentationStyleSummary;
737554f68d3SGreg Clayton 
738554f68d3SGreg Clayton     bool do_deref_pointer = entry.deref;
739554f68d3SGreg Clayton     bool is_script = false;
740554f68d3SGreg Clayton     switch (entry.type)
741554f68d3SGreg Clayton     {
742554f68d3SGreg Clayton         case FormatEntity::Entry::Type::ScriptVariable:
743554f68d3SGreg Clayton             is_script = true;
744554f68d3SGreg Clayton             break;
745554f68d3SGreg Clayton 
746554f68d3SGreg Clayton         case FormatEntity::Entry::Type::Variable:
747554f68d3SGreg Clayton             custom_format = entry.fmt;
748554f68d3SGreg Clayton             val_obj_display = (ValueObject::ValueObjectRepresentationStyle)entry.number;
749554f68d3SGreg Clayton             break;
750554f68d3SGreg Clayton 
751554f68d3SGreg Clayton         case FormatEntity::Entry::Type::ScriptVariableSynthetic:
752554f68d3SGreg Clayton             is_script = true;
753554f68d3SGreg Clayton             // Fall through
754554f68d3SGreg Clayton         case FormatEntity::Entry::Type::VariableSynthetic:
755554f68d3SGreg Clayton             custom_format = entry.fmt;
756554f68d3SGreg Clayton             val_obj_display = (ValueObject::ValueObjectRepresentationStyle)entry.number;
757554f68d3SGreg Clayton             if (!valobj->IsSynthetic())
758554f68d3SGreg Clayton             {
759554f68d3SGreg Clayton                 valobj = valobj->GetSyntheticValue().get();
760554f68d3SGreg Clayton                 if (valobj == nullptr)
761554f68d3SGreg Clayton                     return false;
762554f68d3SGreg Clayton             }
763554f68d3SGreg Clayton             break;
764554f68d3SGreg Clayton 
765554f68d3SGreg Clayton         default:
766554f68d3SGreg Clayton             return false;
767554f68d3SGreg Clayton     }
768554f68d3SGreg Clayton 
769554f68d3SGreg Clayton     if (valobj == NULL)
770554f68d3SGreg Clayton         return false;
771554f68d3SGreg Clayton 
772554f68d3SGreg Clayton     ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
773554f68d3SGreg Clayton                                                       ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
774554f68d3SGreg Clayton     ValueObject::GetValueForExpressionPathOptions options;
775495dccd6SEnrico Granata     options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().SetSyntheticChildrenTraversal(ValueObject::GetValueForExpressionPathOptions::SyntheticChildrenTraversal::Both);
776554f68d3SGreg Clayton     ValueObject* target = NULL;
777554f68d3SGreg Clayton     const char* var_name_final_if_array_range = NULL;
778554f68d3SGreg Clayton     size_t close_bracket_index = llvm::StringRef::npos;
779554f68d3SGreg Clayton     int64_t index_lower = -1;
780554f68d3SGreg Clayton     int64_t index_higher = -1;
781554f68d3SGreg Clayton     bool is_array_range = false;
782554f68d3SGreg Clayton     const char* first_unparsed;
783554f68d3SGreg Clayton     bool was_plain_var = false;
784554f68d3SGreg Clayton     bool was_var_format = false;
785554f68d3SGreg Clayton     bool was_var_indexed = false;
786554f68d3SGreg Clayton     ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
787554f68d3SGreg Clayton     ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
788554f68d3SGreg Clayton 
789554f68d3SGreg Clayton     if (is_script)
790554f68d3SGreg Clayton     {
791554f68d3SGreg Clayton         return RunScriptFormatKeyword (s, sc, exe_ctx, valobj, entry.string.c_str());
792554f68d3SGreg Clayton     }
793554f68d3SGreg Clayton 
794554f68d3SGreg Clayton     llvm::StringRef subpath (entry.string);
795554f68d3SGreg Clayton     // simplest case ${var}, just print valobj's value
796554f68d3SGreg Clayton     if (entry.string.empty())
797554f68d3SGreg Clayton     {
798554f68d3SGreg Clayton         if (entry.printf_format.empty() && entry.fmt == eFormatDefault && entry.number == ValueObject::eValueObjectRepresentationStyleValue)
799554f68d3SGreg Clayton             was_plain_var = true;
800554f68d3SGreg Clayton         else
801554f68d3SGreg Clayton             was_var_format = true;
802554f68d3SGreg Clayton         target = valobj;
803554f68d3SGreg Clayton     }
804554f68d3SGreg Clayton     else    // this is ${var.something} or multiple .something nested
805554f68d3SGreg Clayton     {
806554f68d3SGreg Clayton         if (entry.string[0] == '[')
807554f68d3SGreg Clayton             was_var_indexed = true;
808554f68d3SGreg Clayton         ScanBracketedRange (subpath,
809554f68d3SGreg Clayton                             close_bracket_index,
810554f68d3SGreg Clayton                             var_name_final_if_array_range,
811554f68d3SGreg Clayton                             index_lower,
812554f68d3SGreg Clayton                             index_higher);
813554f68d3SGreg Clayton 
814554f68d3SGreg Clayton         Error error;
815554f68d3SGreg Clayton 
816554f68d3SGreg Clayton         const std::string &expr_path = entry.string;
817554f68d3SGreg Clayton 
818554f68d3SGreg Clayton         if (log)
819554f68d3SGreg Clayton             log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
820554f68d3SGreg Clayton 
821554f68d3SGreg Clayton         target = valobj->GetValueForExpressionPath(expr_path.c_str(),
822554f68d3SGreg Clayton                                                    &first_unparsed,
823554f68d3SGreg Clayton                                                    &reason_to_stop,
824554f68d3SGreg Clayton                                                    &final_value_type,
825554f68d3SGreg Clayton                                                    options,
826554f68d3SGreg Clayton                                                    &what_next).get();
827554f68d3SGreg Clayton 
828554f68d3SGreg Clayton         if (!target)
829554f68d3SGreg Clayton         {
830554f68d3SGreg Clayton             if (log)
831554f68d3SGreg Clayton                 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
832554f68d3SGreg Clayton                             " final_value_type %d",
833554f68d3SGreg Clayton                             first_unparsed, reason_to_stop, final_value_type);
834554f68d3SGreg Clayton             return false;
835554f68d3SGreg Clayton         }
836554f68d3SGreg Clayton         else
837554f68d3SGreg Clayton         {
838554f68d3SGreg Clayton             if (log)
839554f68d3SGreg Clayton                 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
840554f68d3SGreg Clayton                             " final_value_type %d",
841554f68d3SGreg Clayton                             first_unparsed, reason_to_stop, final_value_type);
842554f68d3SGreg Clayton             target = target->GetQualifiedRepresentationIfAvailable(target->GetDynamicValueType(), true).get();
843554f68d3SGreg Clayton         }
844554f68d3SGreg Clayton     }
845554f68d3SGreg Clayton 
846554f68d3SGreg Clayton 
847554f68d3SGreg Clayton     is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
848554f68d3SGreg Clayton                       final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
849554f68d3SGreg Clayton 
850554f68d3SGreg Clayton     do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
851554f68d3SGreg Clayton 
852554f68d3SGreg Clayton     if (do_deref_pointer && !is_array_range)
853554f68d3SGreg Clayton     {
854554f68d3SGreg Clayton         // I have not deref-ed yet, let's do it
855554f68d3SGreg Clayton         // this happens when we are not going through GetValueForVariableExpressionPath
856554f68d3SGreg Clayton         // to get to the target ValueObject
857554f68d3SGreg Clayton         Error error;
858554f68d3SGreg Clayton         target = target->Dereference(error).get();
859554f68d3SGreg Clayton         if (error.Fail())
860554f68d3SGreg Clayton         {
861554f68d3SGreg Clayton             if (log)
862554f68d3SGreg Clayton                 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
863554f68d3SGreg Clayton             return false;
864554f68d3SGreg Clayton         }
865554f68d3SGreg Clayton         do_deref_pointer = false;
866554f68d3SGreg Clayton     }
867554f68d3SGreg Clayton 
868554f68d3SGreg Clayton     if (!target)
869554f68d3SGreg Clayton     {
870554f68d3SGreg Clayton         if (log)
871554f68d3SGreg Clayton             log->Printf("[Debugger::FormatPrompt] could not calculate target for prompt expression");
872554f68d3SGreg Clayton         return false;
873554f68d3SGreg Clayton     }
874554f68d3SGreg Clayton 
875554f68d3SGreg Clayton     // we do not want to use the summary for a bitfield of type T:n
876554f68d3SGreg Clayton     // if we were originally dealing with just a T - that would get
877554f68d3SGreg Clayton     // us into an endless recursion
878554f68d3SGreg Clayton     if (target->IsBitfield() && was_var_indexed)
879554f68d3SGreg Clayton     {
880554f68d3SGreg Clayton         // TODO: check for a (T:n)-specific summary - we should still obey that
881554f68d3SGreg Clayton         StreamString bitfield_name;
882554f68d3SGreg Clayton         bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
883554f68d3SGreg Clayton         lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
884554f68d3SGreg Clayton         if (val_obj_display == ValueObject::eValueObjectRepresentationStyleSummary && !DataVisualization::GetSummaryForType(type_sp))
885554f68d3SGreg Clayton             val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
886554f68d3SGreg Clayton     }
887554f68d3SGreg Clayton 
888554f68d3SGreg Clayton     // TODO use flags for these
88999558cc4SGreg Clayton     const uint32_t type_info_flags = target->GetCompilerType().GetTypeInfo(NULL);
890554f68d3SGreg Clayton     bool is_array = (type_info_flags & eTypeIsArray) != 0;
891554f68d3SGreg Clayton     bool is_pointer = (type_info_flags & eTypeIsPointer) != 0;
89299558cc4SGreg Clayton     bool is_aggregate = target->GetCompilerType().IsAggregateType();
893554f68d3SGreg Clayton 
894554f68d3SGreg Clayton     if ((is_array || is_pointer) && (!is_array_range) && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) // this should be wrong, but there are some exceptions
895554f68d3SGreg Clayton     {
896554f68d3SGreg Clayton         StreamString str_temp;
897554f68d3SGreg Clayton         if (log)
898554f68d3SGreg Clayton             log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
899554f68d3SGreg Clayton 
900554f68d3SGreg Clayton         if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format))
901554f68d3SGreg Clayton         {
902554f68d3SGreg Clayton             // try to use the special cases
903554f68d3SGreg Clayton             bool success = target->DumpPrintableRepresentation(str_temp,
904554f68d3SGreg Clayton                                                                val_obj_display,
905554f68d3SGreg Clayton                                                                custom_format);
906554f68d3SGreg Clayton             if (log)
907554f68d3SGreg Clayton                 log->Printf("[Debugger::FormatPrompt] special cases did%s match", success ? "" : "n't");
908554f68d3SGreg Clayton 
909554f68d3SGreg Clayton             // should not happen
910554f68d3SGreg Clayton             if (success)
911554f68d3SGreg Clayton                 s << str_temp.GetData();
912554f68d3SGreg Clayton             return true;
913554f68d3SGreg Clayton         }
914554f68d3SGreg Clayton         else
915554f68d3SGreg Clayton         {
916554f68d3SGreg Clayton             if (was_plain_var) // if ${var}
917554f68d3SGreg Clayton             {
918554f68d3SGreg Clayton                 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
919554f68d3SGreg Clayton             }
920554f68d3SGreg Clayton             else if (is_pointer) // if pointer, value is the address stored
921554f68d3SGreg Clayton             {
922554f68d3SGreg Clayton                 target->DumpPrintableRepresentation (s,
923554f68d3SGreg Clayton                                                      val_obj_display,
924554f68d3SGreg Clayton                                                      custom_format,
925554f68d3SGreg Clayton                                                      ValueObject::ePrintableRepresentationSpecialCasesDisable);
926554f68d3SGreg Clayton             }
927554f68d3SGreg Clayton             return true;
928554f68d3SGreg Clayton         }
929554f68d3SGreg Clayton     }
930554f68d3SGreg Clayton 
931554f68d3SGreg Clayton     // if directly trying to print ${var}, and this is an aggregate, display a nice
932554f68d3SGreg Clayton     // type @ location message
933554f68d3SGreg Clayton     if (is_aggregate && was_plain_var)
934554f68d3SGreg Clayton     {
935554f68d3SGreg Clayton         s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
936554f68d3SGreg Clayton         return true;
937554f68d3SGreg Clayton     }
938554f68d3SGreg Clayton 
939554f68d3SGreg Clayton     // if directly trying to print ${var%V}, and this is an aggregate, do not let the user do it
940554f68d3SGreg Clayton     if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
941554f68d3SGreg Clayton     {
942554f68d3SGreg Clayton         s << "<invalid use of aggregate type>";
943554f68d3SGreg Clayton         return true;
944554f68d3SGreg Clayton     }
945554f68d3SGreg Clayton 
946554f68d3SGreg Clayton     if (!is_array_range)
947554f68d3SGreg Clayton     {
948554f68d3SGreg Clayton         if (log)
949554f68d3SGreg Clayton             log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
950554f68d3SGreg Clayton         return target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
951554f68d3SGreg Clayton     }
952554f68d3SGreg Clayton     else
953554f68d3SGreg Clayton     {
954554f68d3SGreg Clayton         if (log)
955554f68d3SGreg Clayton             log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
956554f68d3SGreg Clayton         if (!is_array && !is_pointer)
957554f68d3SGreg Clayton             return false;
958554f68d3SGreg Clayton         if (log)
959554f68d3SGreg Clayton             log->Printf("[Debugger::FormatPrompt] handle as array");
960e8376261SVince Harron         StreamString special_directions_stream;
961554f68d3SGreg Clayton         llvm::StringRef special_directions;
962554f68d3SGreg Clayton         if (close_bracket_index != llvm::StringRef::npos && subpath.size() > close_bracket_index)
963554f68d3SGreg Clayton         {
964554f68d3SGreg Clayton             ConstString additional_data (subpath.drop_front(close_bracket_index+1));
965554f68d3SGreg Clayton             special_directions_stream.Printf("${%svar%s",
966554f68d3SGreg Clayton                                              do_deref_pointer ? "*" : "",
967554f68d3SGreg Clayton                                              additional_data.GetCString());
968554f68d3SGreg Clayton 
969554f68d3SGreg Clayton             if (entry.fmt != eFormatDefault)
970554f68d3SGreg Clayton             {
971554f68d3SGreg Clayton                 const char format_char = FormatManager::GetFormatAsFormatChar(entry.fmt);
972554f68d3SGreg Clayton                 if (format_char != '\0')
973554f68d3SGreg Clayton                     special_directions_stream.Printf("%%%c", format_char);
974554f68d3SGreg Clayton                 else
975554f68d3SGreg Clayton                 {
976554f68d3SGreg Clayton                     const char *format_cstr = FormatManager::GetFormatAsCString(entry.fmt);
977554f68d3SGreg Clayton                     special_directions_stream.Printf("%%%s", format_cstr);
978554f68d3SGreg Clayton                 }
979554f68d3SGreg Clayton             }
980554f68d3SGreg Clayton             else if (entry.number != 0)
981554f68d3SGreg Clayton             {
982554f68d3SGreg Clayton                 const char style_char = ConvertValueObjectStyleToChar ((ValueObject::ValueObjectRepresentationStyle)entry.number);
983554f68d3SGreg Clayton                 if (style_char)
984554f68d3SGreg Clayton                     special_directions_stream.Printf("%%%c", style_char);
985554f68d3SGreg Clayton             }
986554f68d3SGreg Clayton             special_directions_stream.PutChar('}');
987554f68d3SGreg Clayton             special_directions = llvm::StringRef(special_directions_stream.GetString());
988554f68d3SGreg Clayton         }
989554f68d3SGreg Clayton 
990554f68d3SGreg Clayton         // let us display items index_lower thru index_higher of this array
991554f68d3SGreg Clayton         s.PutChar('[');
992554f68d3SGreg Clayton 
993554f68d3SGreg Clayton         if (index_higher < 0)
994554f68d3SGreg Clayton             index_higher = valobj->GetNumChildren() - 1;
995554f68d3SGreg Clayton 
996554f68d3SGreg Clayton         uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
997554f68d3SGreg Clayton 
998554f68d3SGreg Clayton         bool success = true;
999554f68d3SGreg Clayton         for (int64_t index = index_lower;index<=index_higher; ++index)
1000554f68d3SGreg Clayton         {
1001554f68d3SGreg Clayton             ValueObject* item = ExpandIndexedExpression (target,
1002554f68d3SGreg Clayton                                                          index,
1003554f68d3SGreg Clayton                                                          exe_ctx->GetFramePtr(),
1004554f68d3SGreg Clayton                                                          false).get();
1005554f68d3SGreg Clayton 
1006554f68d3SGreg Clayton             if (!item)
1007554f68d3SGreg Clayton             {
1008554f68d3SGreg Clayton                 if (log)
1009554f68d3SGreg Clayton                     log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index);
1010554f68d3SGreg Clayton             }
1011554f68d3SGreg Clayton             else
1012554f68d3SGreg Clayton             {
1013554f68d3SGreg Clayton                 if (log)
1014554f68d3SGreg Clayton                     log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions.data() ? special_directions.data() : "");
1015554f68d3SGreg Clayton             }
1016554f68d3SGreg Clayton 
1017554f68d3SGreg Clayton             if (special_directions.empty())
1018554f68d3SGreg Clayton             {
1019554f68d3SGreg Clayton                 success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1020554f68d3SGreg Clayton             }
1021554f68d3SGreg Clayton             else
1022554f68d3SGreg Clayton             {
1023554f68d3SGreg Clayton                 success &= FormatEntity::FormatStringRef(special_directions, s, sc, exe_ctx, NULL, item, false, false);
1024554f68d3SGreg Clayton             }
1025554f68d3SGreg Clayton 
1026554f68d3SGreg Clayton             if (--max_num_children == 0)
1027554f68d3SGreg Clayton             {
1028554f68d3SGreg Clayton                 s.PutCString(", ...");
1029554f68d3SGreg Clayton                 break;
1030554f68d3SGreg Clayton             }
1031554f68d3SGreg Clayton 
1032554f68d3SGreg Clayton             if (index < index_higher)
1033554f68d3SGreg Clayton                 s.PutChar(',');
1034554f68d3SGreg Clayton         }
1035554f68d3SGreg Clayton         s.PutChar(']');
1036554f68d3SGreg Clayton         return success;
1037554f68d3SGreg Clayton     }
1038554f68d3SGreg Clayton 
1039554f68d3SGreg Clayton }
1040554f68d3SGreg Clayton 
1041554f68d3SGreg Clayton static bool
1042554f68d3SGreg Clayton DumpRegister (Stream &s,
1043554f68d3SGreg Clayton               StackFrame *frame,
1044554f68d3SGreg Clayton               const char *reg_name,
1045554f68d3SGreg Clayton               Format format)
1046554f68d3SGreg Clayton 
1047554f68d3SGreg Clayton {
1048554f68d3SGreg Clayton     if (frame)
1049554f68d3SGreg Clayton     {
1050554f68d3SGreg Clayton         RegisterContext *reg_ctx = frame->GetRegisterContext().get();
1051554f68d3SGreg Clayton 
1052554f68d3SGreg Clayton         if (reg_ctx)
1053554f68d3SGreg Clayton         {
1054554f68d3SGreg Clayton             const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(reg_name);
1055554f68d3SGreg Clayton             if (reg_info)
1056554f68d3SGreg Clayton             {
1057554f68d3SGreg Clayton                 RegisterValue reg_value;
1058554f68d3SGreg Clayton                 if (reg_ctx->ReadRegister (reg_info, reg_value))
1059554f68d3SGreg Clayton                 {
1060554f68d3SGreg Clayton                     reg_value.Dump(&s, reg_info, false, false, format);
1061554f68d3SGreg Clayton                     return true;
1062554f68d3SGreg Clayton                 }
1063554f68d3SGreg Clayton             }
1064554f68d3SGreg Clayton         }
1065554f68d3SGreg Clayton     }
1066554f68d3SGreg Clayton     return false;
1067554f68d3SGreg Clayton }
1068554f68d3SGreg Clayton 
1069554f68d3SGreg Clayton static bool
1070554f68d3SGreg Clayton FormatThreadExtendedInfoRecurse(const FormatEntity::Entry &entry,
1071554f68d3SGreg Clayton                                 const StructuredData::ObjectSP &thread_info_dictionary,
1072554f68d3SGreg Clayton                                 const SymbolContext *sc,
1073554f68d3SGreg Clayton                                 const ExecutionContext *exe_ctx,
1074554f68d3SGreg Clayton                                 Stream &s)
1075554f68d3SGreg Clayton {
1076554f68d3SGreg Clayton     llvm::StringRef path(entry.string);
1077554f68d3SGreg Clayton 
1078554f68d3SGreg Clayton     StructuredData::ObjectSP value = thread_info_dictionary->GetObjectForDotSeparatedPath (path);
1079554f68d3SGreg Clayton 
1080554f68d3SGreg Clayton     if (value.get())
1081554f68d3SGreg Clayton     {
1082554f68d3SGreg Clayton         if (value->GetType() == StructuredData::Type::eTypeInteger)
1083554f68d3SGreg Clayton         {
1084554f68d3SGreg Clayton             const char *token_format = "0x%4.4" PRIx64;
1085554f68d3SGreg Clayton             if (!entry.printf_format.empty())
1086554f68d3SGreg Clayton                 token_format = entry.printf_format.c_str();
1087554f68d3SGreg Clayton             s.Printf(token_format, value->GetAsInteger()->GetValue());
1088554f68d3SGreg Clayton             return true;
1089554f68d3SGreg Clayton         }
1090554f68d3SGreg Clayton         else if (value->GetType() == StructuredData::Type::eTypeFloat)
1091554f68d3SGreg Clayton         {
1092554f68d3SGreg Clayton             s.Printf ("%f", value->GetAsFloat()->GetValue());
1093554f68d3SGreg Clayton             return true;
1094554f68d3SGreg Clayton         }
1095554f68d3SGreg Clayton         else if (value->GetType() == StructuredData::Type::eTypeString)
1096554f68d3SGreg Clayton         {
1097554f68d3SGreg Clayton             s.Printf("%s", value->GetAsString()->GetValue().c_str());
1098554f68d3SGreg Clayton             return true;
1099554f68d3SGreg Clayton         }
1100554f68d3SGreg Clayton         else if (value->GetType() == StructuredData::Type::eTypeArray)
1101554f68d3SGreg Clayton         {
1102554f68d3SGreg Clayton             if (value->GetAsArray()->GetSize() > 0)
1103554f68d3SGreg Clayton             {
1104554f68d3SGreg Clayton                 s.Printf ("%zu", value->GetAsArray()->GetSize());
1105554f68d3SGreg Clayton                 return true;
1106554f68d3SGreg Clayton             }
1107554f68d3SGreg Clayton         }
1108554f68d3SGreg Clayton         else if (value->GetType() == StructuredData::Type::eTypeDictionary)
1109554f68d3SGreg Clayton         {
1110554f68d3SGreg Clayton             s.Printf ("%zu", value->GetAsDictionary()->GetKeys()->GetAsArray()->GetSize());
1111554f68d3SGreg Clayton             return true;
1112554f68d3SGreg Clayton         }
1113554f68d3SGreg Clayton     }
1114554f68d3SGreg Clayton 
1115554f68d3SGreg Clayton     return false;
1116554f68d3SGreg Clayton }
1117554f68d3SGreg Clayton 
1118554f68d3SGreg Clayton 
1119554f68d3SGreg Clayton static inline bool
1120554f68d3SGreg Clayton IsToken(const char *var_name_begin, const char *var)
1121554f68d3SGreg Clayton {
1122554f68d3SGreg Clayton     return (::strncmp (var_name_begin, var, strlen(var)) == 0);
1123554f68d3SGreg Clayton }
1124554f68d3SGreg Clayton 
1125554f68d3SGreg Clayton bool
1126554f68d3SGreg Clayton FormatEntity::FormatStringRef (const llvm::StringRef &format_str,
1127554f68d3SGreg Clayton                                Stream &s,
1128554f68d3SGreg Clayton                                const SymbolContext *sc,
1129554f68d3SGreg Clayton                                const ExecutionContext *exe_ctx,
1130554f68d3SGreg Clayton                                const Address *addr,
1131554f68d3SGreg Clayton                                ValueObject* valobj,
1132554f68d3SGreg Clayton                                bool function_changed,
1133554f68d3SGreg Clayton                                bool initial_function)
1134554f68d3SGreg Clayton {
1135554f68d3SGreg Clayton     if (!format_str.empty())
1136554f68d3SGreg Clayton     {
1137554f68d3SGreg Clayton         FormatEntity::Entry root;
1138554f68d3SGreg Clayton         Error error = FormatEntity::Parse(format_str, root);
1139554f68d3SGreg Clayton         if (error.Success())
1140554f68d3SGreg Clayton         {
1141554f68d3SGreg Clayton             return FormatEntity::Format (root,
1142554f68d3SGreg Clayton                                          s,
1143554f68d3SGreg Clayton                                          sc,
1144554f68d3SGreg Clayton                                          exe_ctx,
1145554f68d3SGreg Clayton                                          addr,
1146554f68d3SGreg Clayton                                          valobj,
1147554f68d3SGreg Clayton                                          function_changed,
1148554f68d3SGreg Clayton                                          initial_function);
1149554f68d3SGreg Clayton         }
1150554f68d3SGreg Clayton     }
1151554f68d3SGreg Clayton     return false;
1152554f68d3SGreg Clayton 
1153554f68d3SGreg Clayton }
1154554f68d3SGreg Clayton bool
1155554f68d3SGreg Clayton FormatEntity::FormatCString (const char *format,
1156554f68d3SGreg Clayton                              Stream &s,
1157554f68d3SGreg Clayton                              const SymbolContext *sc,
1158554f68d3SGreg Clayton                              const ExecutionContext *exe_ctx,
1159554f68d3SGreg Clayton                              const Address *addr,
1160554f68d3SGreg Clayton                              ValueObject* valobj,
1161554f68d3SGreg Clayton                              bool function_changed,
1162554f68d3SGreg Clayton                              bool initial_function)
1163554f68d3SGreg Clayton {
1164554f68d3SGreg Clayton     if (format && format[0])
1165554f68d3SGreg Clayton     {
1166554f68d3SGreg Clayton         FormatEntity::Entry root;
1167554f68d3SGreg Clayton         llvm::StringRef format_str(format);
1168554f68d3SGreg Clayton         Error error = FormatEntity::Parse(format_str, root);
1169554f68d3SGreg Clayton         if (error.Success())
1170554f68d3SGreg Clayton         {
1171554f68d3SGreg Clayton             return FormatEntity::Format (root,
1172554f68d3SGreg Clayton                                          s,
1173554f68d3SGreg Clayton                                          sc,
1174554f68d3SGreg Clayton                                          exe_ctx,
1175554f68d3SGreg Clayton                                          addr,
1176554f68d3SGreg Clayton                                          valobj,
1177554f68d3SGreg Clayton                                          function_changed,
1178554f68d3SGreg Clayton                                          initial_function);
1179554f68d3SGreg Clayton         }
1180554f68d3SGreg Clayton     }
1181554f68d3SGreg Clayton     return false;
1182554f68d3SGreg Clayton }
1183554f68d3SGreg Clayton 
1184554f68d3SGreg Clayton bool
1185554f68d3SGreg Clayton FormatEntity::Format (const Entry &entry,
1186554f68d3SGreg Clayton                       Stream &s,
1187554f68d3SGreg Clayton                       const SymbolContext *sc,
1188554f68d3SGreg Clayton                       const ExecutionContext *exe_ctx,
1189554f68d3SGreg Clayton                       const Address *addr,
1190554f68d3SGreg Clayton                       ValueObject* valobj,
1191554f68d3SGreg Clayton                       bool function_changed,
1192554f68d3SGreg Clayton                       bool initial_function)
1193554f68d3SGreg Clayton {
1194554f68d3SGreg Clayton     switch (entry.type)
1195554f68d3SGreg Clayton     {
1196554f68d3SGreg Clayton         case Entry::Type::Invalid:
1197554f68d3SGreg Clayton         case Entry::Type::ParentNumber: // Only used for FormatEntity::Entry::Definition encoding
1198554f68d3SGreg Clayton         case Entry::Type::ParentString: // Only used for FormatEntity::Entry::Definition encoding
1199554f68d3SGreg Clayton         case Entry::Type::InsertString: // Only used for FormatEntity::Entry::Definition encoding
1200554f68d3SGreg Clayton             return false;
1201554f68d3SGreg Clayton 
1202554f68d3SGreg Clayton         case Entry::Type::Root:
1203554f68d3SGreg Clayton             for (const auto &child : entry.children)
1204554f68d3SGreg Clayton             {
1205554f68d3SGreg Clayton                 if (Format (child,
1206554f68d3SGreg Clayton                             s,
1207554f68d3SGreg Clayton                             sc,
1208554f68d3SGreg Clayton                             exe_ctx,
1209554f68d3SGreg Clayton                             addr,
1210554f68d3SGreg Clayton                             valobj,
1211554f68d3SGreg Clayton                             function_changed,
1212554f68d3SGreg Clayton                             initial_function) == false)
1213554f68d3SGreg Clayton                 {
1214554f68d3SGreg Clayton                     return false; // If any item of root fails, then the formatting fails
1215554f68d3SGreg Clayton                 }
1216554f68d3SGreg Clayton             }
1217554f68d3SGreg Clayton             return true; // Only return true if all items succeeded
1218554f68d3SGreg Clayton 
1219554f68d3SGreg Clayton         case Entry::Type::String:
1220554f68d3SGreg Clayton             s.PutCString(entry.string.c_str());
1221554f68d3SGreg Clayton             return true;
1222554f68d3SGreg Clayton 
1223554f68d3SGreg Clayton         case Entry::Type::Scope:
1224554f68d3SGreg Clayton             {
1225554f68d3SGreg Clayton                 StreamString scope_stream;
1226554f68d3SGreg Clayton                 bool success = false;
1227554f68d3SGreg Clayton                 for (const auto &child : entry.children)
1228554f68d3SGreg Clayton                 {
1229554f68d3SGreg Clayton                     success = Format (child, scope_stream, sc, exe_ctx, addr, valobj, function_changed, initial_function);
1230554f68d3SGreg Clayton                     if (!success)
1231554f68d3SGreg Clayton                         break;
1232554f68d3SGreg Clayton                 }
1233554f68d3SGreg Clayton                 // Only if all items in a scope succeed, then do we
1234554f68d3SGreg Clayton                 // print the output into the main stream
1235554f68d3SGreg Clayton                 if (success)
1236554f68d3SGreg Clayton                     s.Write(scope_stream.GetString().data(), scope_stream.GetString().size());
1237554f68d3SGreg Clayton             }
1238554f68d3SGreg Clayton             return true; // Scopes always successfully print themselves
1239554f68d3SGreg Clayton 
1240554f68d3SGreg Clayton         case Entry::Type::Variable:
1241554f68d3SGreg Clayton         case Entry::Type::VariableSynthetic:
1242554f68d3SGreg Clayton         case Entry::Type::ScriptVariable:
1243554f68d3SGreg Clayton         case Entry::Type::ScriptVariableSynthetic:
1244554f68d3SGreg Clayton             if (DumpValue(s, sc, exe_ctx, entry, valobj))
1245554f68d3SGreg Clayton                 return true;
1246554f68d3SGreg Clayton             return false;
1247554f68d3SGreg Clayton 
1248554f68d3SGreg Clayton         case Entry::Type::AddressFile:
1249554f68d3SGreg Clayton         case Entry::Type::AddressLoad:
1250554f68d3SGreg Clayton         case Entry::Type::AddressLoadOrFile:
1251554f68d3SGreg Clayton             if (addr && addr->IsValid() && DumpAddress(s, sc, exe_ctx, *addr, entry.type == Entry::Type::AddressLoadOrFile))
1252554f68d3SGreg Clayton                 return true;
1253554f68d3SGreg Clayton             return false;
1254554f68d3SGreg Clayton 
1255554f68d3SGreg Clayton         case Entry::Type::ProcessID:
1256554f68d3SGreg Clayton             if (exe_ctx)
1257554f68d3SGreg Clayton             {
1258554f68d3SGreg Clayton                 Process *process = exe_ctx->GetProcessPtr();
1259554f68d3SGreg Clayton                 if (process)
1260554f68d3SGreg Clayton                 {
1261554f68d3SGreg Clayton                     const char *format = "%" PRIu64;
1262554f68d3SGreg Clayton                     if (!entry.printf_format.empty())
1263554f68d3SGreg Clayton                         format = entry.printf_format.c_str();
1264554f68d3SGreg Clayton                     s.Printf(format, process->GetID());
1265554f68d3SGreg Clayton                     return true;
1266554f68d3SGreg Clayton                 }
1267554f68d3SGreg Clayton             }
1268554f68d3SGreg Clayton             return false;
1269554f68d3SGreg Clayton 
1270554f68d3SGreg Clayton         case Entry::Type::ProcessFile:
1271554f68d3SGreg Clayton             if (exe_ctx)
1272554f68d3SGreg Clayton             {
1273554f68d3SGreg Clayton                 Process *process = exe_ctx->GetProcessPtr();
1274554f68d3SGreg Clayton                 if (process)
1275554f68d3SGreg Clayton                 {
1276554f68d3SGreg Clayton                     Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1277554f68d3SGreg Clayton                     if (exe_module)
1278554f68d3SGreg Clayton                     {
1279554f68d3SGreg Clayton                         if (DumpFile(s, exe_module->GetFileSpec(), (FileKind)entry.number))
1280554f68d3SGreg Clayton                             return true;
1281554f68d3SGreg Clayton                     }
1282554f68d3SGreg Clayton                 }
1283554f68d3SGreg Clayton             }
1284554f68d3SGreg Clayton             return false;
1285554f68d3SGreg Clayton 
1286554f68d3SGreg Clayton         case Entry::Type::ScriptProcess:
1287554f68d3SGreg Clayton             if (exe_ctx)
1288554f68d3SGreg Clayton             {
1289554f68d3SGreg Clayton                 Process *process = exe_ctx->GetProcessPtr();
1290554f68d3SGreg Clayton                 if (process)
1291554f68d3SGreg Clayton                     return RunScriptFormatKeyword (s, sc, exe_ctx, process, entry.string.c_str());
1292554f68d3SGreg Clayton             }
1293554f68d3SGreg Clayton             return false;
1294554f68d3SGreg Clayton 
1295554f68d3SGreg Clayton 
1296554f68d3SGreg Clayton         case Entry::Type::ThreadID:
1297554f68d3SGreg Clayton             if (exe_ctx)
1298554f68d3SGreg Clayton             {
1299554f68d3SGreg Clayton                 Thread *thread = exe_ctx->GetThreadPtr();
1300554f68d3SGreg Clayton                 if (thread)
1301554f68d3SGreg Clayton                 {
1302554f68d3SGreg Clayton                     const char *format = "0x%4.4" PRIx64;
1303554f68d3SGreg Clayton                     if (!entry.printf_format.empty())
1304554f68d3SGreg Clayton                     {
1305554f68d3SGreg Clayton                         // Watch for the special "tid" format...
1306554f68d3SGreg Clayton                         if (entry.printf_format == "tid")
1307554f68d3SGreg Clayton                         {
1308913f776fSZachary Turner                             // TODO(zturner): Rather than hardcoding this to be platform specific, it should be controlled by a
1309913f776fSZachary Turner                             // setting and the default value of the setting can be different depending on the platform.
1310554f68d3SGreg Clayton                             Target &target = thread->GetProcess()->GetTarget();
1311554f68d3SGreg Clayton                             ArchSpec arch (target.GetArchitecture ());
1312554f68d3SGreg Clayton                             llvm::Triple::OSType ostype = arch.IsValid() ? arch.GetTriple().getOS() : llvm::Triple::UnknownOS;
1313554f68d3SGreg Clayton                             if ((ostype == llvm::Triple::FreeBSD) || (ostype == llvm::Triple::Linux))
1314554f68d3SGreg Clayton                             {
1315554f68d3SGreg Clayton                                 format = "%" PRIu64;
1316554f68d3SGreg Clayton                             }
1317554f68d3SGreg Clayton                         }
1318554f68d3SGreg Clayton                         else
1319554f68d3SGreg Clayton                         {
1320554f68d3SGreg Clayton                             format = entry.printf_format.c_str();
1321554f68d3SGreg Clayton                         }
1322554f68d3SGreg Clayton                     }
1323554f68d3SGreg Clayton                     s.Printf(format, thread->GetID());
1324554f68d3SGreg Clayton                     return true;
1325554f68d3SGreg Clayton                 }
1326554f68d3SGreg Clayton             }
1327554f68d3SGreg Clayton             return false;
1328554f68d3SGreg Clayton 
1329554f68d3SGreg Clayton         case Entry::Type::ThreadProtocolID:
1330554f68d3SGreg Clayton             if (exe_ctx)
1331554f68d3SGreg Clayton             {
1332554f68d3SGreg Clayton                 Thread *thread = exe_ctx->GetThreadPtr();
1333554f68d3SGreg Clayton                 if (thread)
1334554f68d3SGreg Clayton                 {
1335554f68d3SGreg Clayton                     const char *format = "0x%4.4" PRIx64;
1336554f68d3SGreg Clayton                     if (!entry.printf_format.empty())
1337554f68d3SGreg Clayton                         format = entry.printf_format.c_str();
1338554f68d3SGreg Clayton                     s.Printf(format, thread->GetProtocolID());
1339554f68d3SGreg Clayton                     return true;
1340554f68d3SGreg Clayton                 }
1341554f68d3SGreg Clayton             }
1342554f68d3SGreg Clayton             return false;
1343554f68d3SGreg Clayton 
1344554f68d3SGreg Clayton         case Entry::Type::ThreadIndexID:
1345554f68d3SGreg Clayton             if (exe_ctx)
1346554f68d3SGreg Clayton             {
1347554f68d3SGreg Clayton                 Thread *thread = exe_ctx->GetThreadPtr();
1348554f68d3SGreg Clayton                 if (thread)
1349554f68d3SGreg Clayton                 {
1350554f68d3SGreg Clayton                     const char *format = "%" PRIu32;
1351554f68d3SGreg Clayton                     if (!entry.printf_format.empty())
1352554f68d3SGreg Clayton                         format = entry.printf_format.c_str();
1353554f68d3SGreg Clayton                     s.Printf(format, thread->GetIndexID());
1354554f68d3SGreg Clayton                     return true;
1355554f68d3SGreg Clayton                 }
1356554f68d3SGreg Clayton             }
1357554f68d3SGreg Clayton             return false;
1358554f68d3SGreg Clayton 
1359554f68d3SGreg Clayton         case Entry::Type::ThreadName:
1360554f68d3SGreg Clayton             if (exe_ctx)
1361554f68d3SGreg Clayton             {
1362554f68d3SGreg Clayton                 Thread *thread = exe_ctx->GetThreadPtr();
1363554f68d3SGreg Clayton                 if (thread)
1364554f68d3SGreg Clayton                 {
1365554f68d3SGreg Clayton                     const char *cstr = thread->GetName();
1366554f68d3SGreg Clayton                     if (cstr && cstr[0])
1367554f68d3SGreg Clayton                     {
1368554f68d3SGreg Clayton                         s.PutCString(cstr);
1369554f68d3SGreg Clayton                         return true;
1370554f68d3SGreg Clayton                     }
1371554f68d3SGreg Clayton                 }
1372554f68d3SGreg Clayton             }
1373554f68d3SGreg Clayton             return false;
1374554f68d3SGreg Clayton 
1375554f68d3SGreg Clayton         case Entry::Type::ThreadQueue:
1376554f68d3SGreg Clayton             if (exe_ctx)
1377554f68d3SGreg Clayton             {
1378554f68d3SGreg Clayton                 Thread *thread = exe_ctx->GetThreadPtr();
1379554f68d3SGreg Clayton                 if (thread)
1380554f68d3SGreg Clayton                 {
1381554f68d3SGreg Clayton                     const char *cstr = thread->GetQueueName();
1382554f68d3SGreg Clayton                     if (cstr && cstr[0])
1383554f68d3SGreg Clayton                     {
1384554f68d3SGreg Clayton                         s.PutCString(cstr);
1385554f68d3SGreg Clayton                         return true;
1386554f68d3SGreg Clayton                     }
1387554f68d3SGreg Clayton                 }
1388554f68d3SGreg Clayton             }
1389554f68d3SGreg Clayton             return false;
1390554f68d3SGreg Clayton 
1391554f68d3SGreg Clayton         case Entry::Type::ThreadStopReason:
1392554f68d3SGreg Clayton             if (exe_ctx)
1393554f68d3SGreg Clayton             {
1394554f68d3SGreg Clayton                 Thread *thread = exe_ctx->GetThreadPtr();
1395554f68d3SGreg Clayton                 if (thread)
1396554f68d3SGreg Clayton                 {
1397554f68d3SGreg Clayton                     StopInfoSP stop_info_sp = thread->GetStopInfo ();
1398554f68d3SGreg Clayton                     if (stop_info_sp && stop_info_sp->IsValid())
1399554f68d3SGreg Clayton                     {
1400554f68d3SGreg Clayton                         const char *cstr = stop_info_sp->GetDescription();
1401554f68d3SGreg Clayton                         if (cstr && cstr[0])
1402554f68d3SGreg Clayton                         {
1403554f68d3SGreg Clayton                             s.PutCString(cstr);
1404554f68d3SGreg Clayton                             return true;
1405554f68d3SGreg Clayton                         }
1406554f68d3SGreg Clayton                     }
1407554f68d3SGreg Clayton                 }
1408554f68d3SGreg Clayton             }
1409554f68d3SGreg Clayton             return false;
1410554f68d3SGreg Clayton 
1411554f68d3SGreg Clayton         case Entry::Type::ThreadReturnValue:
1412554f68d3SGreg Clayton             if (exe_ctx)
1413554f68d3SGreg Clayton             {
1414554f68d3SGreg Clayton                 Thread *thread = exe_ctx->GetThreadPtr();
1415554f68d3SGreg Clayton                 if (thread)
1416554f68d3SGreg Clayton                 {
1417554f68d3SGreg Clayton                     StopInfoSP stop_info_sp = thread->GetStopInfo ();
1418554f68d3SGreg Clayton                     if (stop_info_sp && stop_info_sp->IsValid())
1419554f68d3SGreg Clayton                     {
1420554f68d3SGreg Clayton                         ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
1421554f68d3SGreg Clayton                         if (return_valobj_sp)
1422554f68d3SGreg Clayton                         {
1423554f68d3SGreg Clayton                             return_valobj_sp->Dump(s);
1424554f68d3SGreg Clayton                             return true;
1425554f68d3SGreg Clayton                         }
1426554f68d3SGreg Clayton                     }
1427554f68d3SGreg Clayton                 }
1428554f68d3SGreg Clayton             }
1429554f68d3SGreg Clayton             return false;
1430554f68d3SGreg Clayton 
1431554f68d3SGreg Clayton         case Entry::Type::ThreadCompletedExpression:
1432554f68d3SGreg Clayton             if (exe_ctx)
1433554f68d3SGreg Clayton             {
1434554f68d3SGreg Clayton                 Thread *thread = exe_ctx->GetThreadPtr();
1435554f68d3SGreg Clayton                 if (thread)
1436554f68d3SGreg Clayton                 {
1437554f68d3SGreg Clayton                     StopInfoSP stop_info_sp = thread->GetStopInfo ();
1438554f68d3SGreg Clayton                     if (stop_info_sp && stop_info_sp->IsValid())
1439554f68d3SGreg Clayton                     {
1440bc8ac34eSSean Callanan                         ExpressionVariableSP expression_var_sp = StopInfo::GetExpressionVariable (stop_info_sp);
1441554f68d3SGreg Clayton                         if (expression_var_sp && expression_var_sp->GetValueObject())
1442554f68d3SGreg Clayton                         {
1443554f68d3SGreg Clayton                             expression_var_sp->GetValueObject()->Dump(s);
1444554f68d3SGreg Clayton                             return true;
1445554f68d3SGreg Clayton                         }
1446554f68d3SGreg Clayton                     }
1447554f68d3SGreg Clayton                 }
1448554f68d3SGreg Clayton             }
1449554f68d3SGreg Clayton             return false;
1450554f68d3SGreg Clayton 
1451554f68d3SGreg Clayton         case Entry::Type::ScriptThread:
1452554f68d3SGreg Clayton             if (exe_ctx)
1453554f68d3SGreg Clayton             {
1454554f68d3SGreg Clayton                 Thread *thread = exe_ctx->GetThreadPtr();
1455554f68d3SGreg Clayton                 if (thread)
1456554f68d3SGreg Clayton                     return RunScriptFormatKeyword (s, sc, exe_ctx, thread, entry.string.c_str());
1457554f68d3SGreg Clayton             }
1458554f68d3SGreg Clayton             return false;
1459554f68d3SGreg Clayton 
1460554f68d3SGreg Clayton         case Entry::Type::ThreadInfo:
1461554f68d3SGreg Clayton             if (exe_ctx)
1462554f68d3SGreg Clayton             {
1463554f68d3SGreg Clayton                 Thread *thread = exe_ctx->GetThreadPtr();
1464554f68d3SGreg Clayton                 if (thread)
1465554f68d3SGreg Clayton                 {
1466554f68d3SGreg Clayton                     StructuredData::ObjectSP object_sp = thread->GetExtendedInfo();
1467554f68d3SGreg Clayton                     if (object_sp && object_sp->GetType() == StructuredData::Type::eTypeDictionary)
1468554f68d3SGreg Clayton                     {
1469554f68d3SGreg Clayton                         if (FormatThreadExtendedInfoRecurse (entry, object_sp, sc, exe_ctx, s))
1470554f68d3SGreg Clayton                             return true;
1471554f68d3SGreg Clayton                     }
1472554f68d3SGreg Clayton                 }
1473554f68d3SGreg Clayton             }
1474554f68d3SGreg Clayton             return false;
1475554f68d3SGreg Clayton 
1476554f68d3SGreg Clayton         case Entry::Type::TargetArch:
1477554f68d3SGreg Clayton             if (exe_ctx)
1478554f68d3SGreg Clayton             {
1479554f68d3SGreg Clayton                 Target *target = exe_ctx->GetTargetPtr();
1480554f68d3SGreg Clayton                 if (target)
1481554f68d3SGreg Clayton                 {
1482554f68d3SGreg Clayton                     const ArchSpec &arch = target->GetArchitecture ();
1483554f68d3SGreg Clayton                     if (arch.IsValid())
1484554f68d3SGreg Clayton                     {
1485554f68d3SGreg Clayton                         s.PutCString (arch.GetArchitectureName());
1486554f68d3SGreg Clayton                         return true;
1487554f68d3SGreg Clayton                     }
1488554f68d3SGreg Clayton                 }
1489554f68d3SGreg Clayton             }
1490554f68d3SGreg Clayton             return false;
1491554f68d3SGreg Clayton 
1492554f68d3SGreg Clayton         case Entry::Type::ScriptTarget:
1493554f68d3SGreg Clayton             if (exe_ctx)
1494554f68d3SGreg Clayton             {
1495554f68d3SGreg Clayton                 Target *target = exe_ctx->GetTargetPtr();
1496554f68d3SGreg Clayton                 if (target)
1497554f68d3SGreg Clayton                     return RunScriptFormatKeyword (s, sc, exe_ctx, target, entry.string.c_str());
1498554f68d3SGreg Clayton             }
1499554f68d3SGreg Clayton             return false;
1500554f68d3SGreg Clayton 
1501554f68d3SGreg Clayton         case Entry::Type::ModuleFile:
1502554f68d3SGreg Clayton             if (sc)
1503554f68d3SGreg Clayton             {
1504554f68d3SGreg Clayton                 Module *module = sc->module_sp.get();
1505554f68d3SGreg Clayton                 if (module)
1506554f68d3SGreg Clayton                 {
1507554f68d3SGreg Clayton                     if (DumpFile(s, module->GetFileSpec(), (FileKind)entry.number))
1508554f68d3SGreg Clayton                         return true;
1509554f68d3SGreg Clayton                 }
1510554f68d3SGreg Clayton             }
1511554f68d3SGreg Clayton             return false;
1512554f68d3SGreg Clayton 
1513554f68d3SGreg Clayton         case Entry::Type::File:
1514554f68d3SGreg Clayton             if (sc)
1515554f68d3SGreg Clayton             {
1516554f68d3SGreg Clayton                 CompileUnit *cu = sc->comp_unit;
1517554f68d3SGreg Clayton                 if (cu)
1518554f68d3SGreg Clayton                 {
1519554f68d3SGreg Clayton                     // CompileUnit is a FileSpec
1520554f68d3SGreg Clayton                     if (DumpFile(s, *cu, (FileKind)entry.number))
1521554f68d3SGreg Clayton                         return true;
1522554f68d3SGreg Clayton                 }
1523554f68d3SGreg Clayton             }
1524554f68d3SGreg Clayton             return false;
1525554f68d3SGreg Clayton 
1526dc975670SDawn Perchik         case Entry::Type::Lang:
1527dc975670SDawn Perchik             if (sc)
1528dc975670SDawn Perchik             {
1529dc975670SDawn Perchik                 CompileUnit *cu = sc->comp_unit;
1530dc975670SDawn Perchik                 if (cu)
1531dc975670SDawn Perchik                 {
15320e0984eeSJim Ingham                     const char *lang_name = Language::GetNameForLanguageType(cu->GetLanguage());
1533dc975670SDawn Perchik                     if (lang_name)
1534dc975670SDawn Perchik                     {
1535dc975670SDawn Perchik                         s.PutCString(lang_name);
1536dc975670SDawn Perchik                         return true;
1537dc975670SDawn Perchik                     }
1538dc975670SDawn Perchik                 }
1539dc975670SDawn Perchik             }
1540dc975670SDawn Perchik             return false;
1541dc975670SDawn Perchik 
1542554f68d3SGreg Clayton         case Entry::Type::FrameIndex:
1543554f68d3SGreg Clayton             if (exe_ctx)
1544554f68d3SGreg Clayton             {
1545554f68d3SGreg Clayton                 StackFrame *frame = exe_ctx->GetFramePtr();
1546554f68d3SGreg Clayton                 if (frame)
1547554f68d3SGreg Clayton                 {
1548554f68d3SGreg Clayton                     const char *format = "%" PRIu32;
1549554f68d3SGreg Clayton                     if (!entry.printf_format.empty())
1550554f68d3SGreg Clayton                         format = entry.printf_format.c_str();
1551554f68d3SGreg Clayton                     s.Printf(format, frame->GetFrameIndex());
1552554f68d3SGreg Clayton                     return true;
1553554f68d3SGreg Clayton                 }
1554554f68d3SGreg Clayton             }
1555554f68d3SGreg Clayton             return false;
1556554f68d3SGreg Clayton 
1557554f68d3SGreg Clayton         case Entry::Type::FrameRegisterPC:
1558554f68d3SGreg Clayton             if (exe_ctx)
1559554f68d3SGreg Clayton             {
1560554f68d3SGreg Clayton                 StackFrame *frame = exe_ctx->GetFramePtr();
1561554f68d3SGreg Clayton                 if (frame)
1562554f68d3SGreg Clayton                 {
1563554f68d3SGreg Clayton                     const Address &pc_addr = frame->GetFrameCodeAddress();
1564554f68d3SGreg Clayton                     if (pc_addr.IsValid())
1565554f68d3SGreg Clayton                     {
1566554f68d3SGreg Clayton                         if (DumpAddress(s, sc, exe_ctx, pc_addr, false))
1567554f68d3SGreg Clayton                             return true;
1568554f68d3SGreg Clayton                     }
1569554f68d3SGreg Clayton                 }
1570554f68d3SGreg Clayton             }
1571554f68d3SGreg Clayton             return false;
1572554f68d3SGreg Clayton 
1573554f68d3SGreg Clayton         case Entry::Type::FrameRegisterSP:
1574554f68d3SGreg Clayton             if (exe_ctx)
1575554f68d3SGreg Clayton             {
1576554f68d3SGreg Clayton                 StackFrame *frame = exe_ctx->GetFramePtr();
1577554f68d3SGreg Clayton                 if (frame)
1578554f68d3SGreg Clayton                 {
1579554f68d3SGreg Clayton                     if (DumpRegister (s, frame, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, (lldb::Format)entry.number))
1580554f68d3SGreg Clayton                         return true;
1581554f68d3SGreg Clayton                 }
1582554f68d3SGreg Clayton             }
1583554f68d3SGreg Clayton             return false;
1584554f68d3SGreg Clayton 
1585554f68d3SGreg Clayton         case Entry::Type::FrameRegisterFP:
1586554f68d3SGreg Clayton             if (exe_ctx)
1587554f68d3SGreg Clayton             {
1588554f68d3SGreg Clayton                 StackFrame *frame = exe_ctx->GetFramePtr();
1589554f68d3SGreg Clayton                 if (frame)
1590554f68d3SGreg Clayton                 {
1591554f68d3SGreg Clayton                     if (DumpRegister (s, frame, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP, (lldb::Format)entry.number))
1592554f68d3SGreg Clayton                         return true;
1593554f68d3SGreg Clayton                 }
1594554f68d3SGreg Clayton             }
1595554f68d3SGreg Clayton             return false;
1596554f68d3SGreg Clayton 
1597554f68d3SGreg Clayton         case Entry::Type::FrameRegisterFlags:
1598554f68d3SGreg Clayton             if (exe_ctx)
1599554f68d3SGreg Clayton             {
1600554f68d3SGreg Clayton                 StackFrame *frame = exe_ctx->GetFramePtr();
1601554f68d3SGreg Clayton                 if (frame)
1602554f68d3SGreg Clayton                 {
1603554f68d3SGreg Clayton                     if (DumpRegister (s, frame, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, (lldb::Format)entry.number))
1604554f68d3SGreg Clayton                         return true;
1605554f68d3SGreg Clayton                 }
1606554f68d3SGreg Clayton             }
1607554f68d3SGreg Clayton             return false;
1608554f68d3SGreg Clayton 
1609554f68d3SGreg Clayton 
1610554f68d3SGreg Clayton         case Entry::Type::FrameRegisterByName:
1611554f68d3SGreg Clayton             if (exe_ctx)
1612554f68d3SGreg Clayton             {
1613554f68d3SGreg Clayton                 StackFrame *frame = exe_ctx->GetFramePtr();
1614554f68d3SGreg Clayton                 if (frame)
1615554f68d3SGreg Clayton                 {
1616554f68d3SGreg Clayton                     if (DumpRegister (s, frame, entry.string.c_str(), (lldb::Format)entry.number))
1617554f68d3SGreg Clayton                         return true;
1618554f68d3SGreg Clayton                 }
1619554f68d3SGreg Clayton             }
1620554f68d3SGreg Clayton             return false;
1621554f68d3SGreg Clayton 
1622554f68d3SGreg Clayton         case Entry::Type::ScriptFrame:
1623554f68d3SGreg Clayton             if (exe_ctx)
1624554f68d3SGreg Clayton             {
1625554f68d3SGreg Clayton                 StackFrame *frame = exe_ctx->GetFramePtr();
1626554f68d3SGreg Clayton                 if (frame)
1627554f68d3SGreg Clayton                     return RunScriptFormatKeyword (s, sc, exe_ctx, frame, entry.string.c_str());
1628554f68d3SGreg Clayton             }
1629554f68d3SGreg Clayton             return false;
1630554f68d3SGreg Clayton 
1631554f68d3SGreg Clayton         case Entry::Type::FunctionID:
1632554f68d3SGreg Clayton             if (sc)
1633554f68d3SGreg Clayton             {
1634554f68d3SGreg Clayton                 if (sc->function)
1635554f68d3SGreg Clayton                 {
1636554f68d3SGreg Clayton                     s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
1637554f68d3SGreg Clayton                     return true;
1638554f68d3SGreg Clayton                 }
1639554f68d3SGreg Clayton                 else if (sc->symbol)
1640554f68d3SGreg Clayton                 {
1641554f68d3SGreg Clayton                     s.Printf("symbol[%u]", sc->symbol->GetID());
1642554f68d3SGreg Clayton                     return true;
1643554f68d3SGreg Clayton                 }
1644554f68d3SGreg Clayton             }
1645554f68d3SGreg Clayton             return false;
1646554f68d3SGreg Clayton 
1647554f68d3SGreg Clayton         case Entry::Type::FunctionDidChange:
1648554f68d3SGreg Clayton             return function_changed;
1649554f68d3SGreg Clayton 
1650554f68d3SGreg Clayton         case Entry::Type::FunctionInitialFunction:
1651554f68d3SGreg Clayton             return initial_function;
1652554f68d3SGreg Clayton 
1653554f68d3SGreg Clayton         case Entry::Type::FunctionName:
1654554f68d3SGreg Clayton             {
1655554f68d3SGreg Clayton                 const char *name = NULL;
1656554f68d3SGreg Clayton                 if (sc->function)
1657554f68d3SGreg Clayton                     name = sc->function->GetName().AsCString (NULL);
1658554f68d3SGreg Clayton                 else if (sc->symbol)
1659554f68d3SGreg Clayton                     name = sc->symbol->GetName().AsCString (NULL);
1660554f68d3SGreg Clayton                 if (name)
1661554f68d3SGreg Clayton                 {
1662554f68d3SGreg Clayton                     s.PutCString(name);
1663554f68d3SGreg Clayton 
1664554f68d3SGreg Clayton                     if (sc->block)
1665554f68d3SGreg Clayton                     {
1666554f68d3SGreg Clayton                         Block *inline_block = sc->block->GetContainingInlinedBlock ();
1667554f68d3SGreg Clayton                         if (inline_block)
1668554f68d3SGreg Clayton                         {
1669554f68d3SGreg Clayton                             const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
1670554f68d3SGreg Clayton                             if (inline_info)
1671554f68d3SGreg Clayton                             {
1672554f68d3SGreg Clayton                                 s.PutCString(" [inlined] ");
1673ddaf6a72SGreg Clayton                                 inline_info->GetName(sc->function->GetLanguage()).Dump(&s);
1674554f68d3SGreg Clayton                             }
1675554f68d3SGreg Clayton                         }
1676554f68d3SGreg Clayton                     }
1677554f68d3SGreg Clayton                     return true;
1678554f68d3SGreg Clayton                 }
1679554f68d3SGreg Clayton             }
1680554f68d3SGreg Clayton             return false;
1681554f68d3SGreg Clayton 
1682554f68d3SGreg Clayton         case Entry::Type::FunctionNameNoArgs:
1683554f68d3SGreg Clayton             {
1684554f68d3SGreg Clayton                 ConstString name;
1685554f68d3SGreg Clayton                 if (sc->function)
1686ddaf6a72SGreg Clayton                     name = sc->function->GetNameNoArguments();
1687554f68d3SGreg Clayton                 else if (sc->symbol)
1688ddaf6a72SGreg Clayton                     name = sc->symbol->GetNameNoArguments();
1689554f68d3SGreg Clayton                 if (name)
1690554f68d3SGreg Clayton                 {
1691554f68d3SGreg Clayton                     s.PutCString(name.GetCString());
1692554f68d3SGreg Clayton                     return true;
1693554f68d3SGreg Clayton                 }
1694554f68d3SGreg Clayton             }
1695554f68d3SGreg Clayton             return false;
1696554f68d3SGreg Clayton 
1697554f68d3SGreg Clayton         case Entry::Type::FunctionNameWithArgs:
1698554f68d3SGreg Clayton             {
1699554f68d3SGreg Clayton                 // Print the function name with arguments in it
1700554f68d3SGreg Clayton                 if (sc->function)
1701554f68d3SGreg Clayton                 {
1702554f68d3SGreg Clayton                     ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
1703554f68d3SGreg Clayton                     const char *cstr = sc->function->GetName().AsCString (NULL);
1704554f68d3SGreg Clayton                     if (cstr)
1705554f68d3SGreg Clayton                     {
1706554f68d3SGreg Clayton                         const InlineFunctionInfo *inline_info = NULL;
1707554f68d3SGreg Clayton                         VariableListSP variable_list_sp;
1708554f68d3SGreg Clayton                         bool get_function_vars = true;
1709554f68d3SGreg Clayton                         if (sc->block)
1710554f68d3SGreg Clayton                         {
1711554f68d3SGreg Clayton                             Block *inline_block = sc->block->GetContainingInlinedBlock ();
1712554f68d3SGreg Clayton 
1713554f68d3SGreg Clayton                             if (inline_block)
1714554f68d3SGreg Clayton                             {
1715554f68d3SGreg Clayton                                 get_function_vars = false;
1716554f68d3SGreg Clayton                                 inline_info = sc->block->GetInlinedFunctionInfo();
1717554f68d3SGreg Clayton                                 if (inline_info)
1718554f68d3SGreg Clayton                                     variable_list_sp = inline_block->GetBlockVariableList (true);
1719554f68d3SGreg Clayton                             }
1720554f68d3SGreg Clayton                         }
1721554f68d3SGreg Clayton 
1722554f68d3SGreg Clayton                         if (get_function_vars)
1723554f68d3SGreg Clayton                         {
1724554f68d3SGreg Clayton                             variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
1725554f68d3SGreg Clayton                         }
1726554f68d3SGreg Clayton 
1727554f68d3SGreg Clayton                         if (inline_info)
1728554f68d3SGreg Clayton                         {
1729554f68d3SGreg Clayton                             s.PutCString (cstr);
1730554f68d3SGreg Clayton                             s.PutCString (" [inlined] ");
1731ddaf6a72SGreg Clayton                             cstr = inline_info->GetName(sc->function->GetLanguage()).GetCString();
1732554f68d3SGreg Clayton                         }
1733554f68d3SGreg Clayton 
1734554f68d3SGreg Clayton                         VariableList args;
1735554f68d3SGreg Clayton                         if (variable_list_sp)
1736554f68d3SGreg Clayton                             variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument, args);
1737554f68d3SGreg Clayton                         if (args.GetSize() > 0)
1738554f68d3SGreg Clayton                         {
1739554f68d3SGreg Clayton                             const char *open_paren = strchr (cstr, '(');
1740554f68d3SGreg Clayton                             const char *close_paren = nullptr;
1741554f68d3SGreg Clayton                             const char *generic = strchr(cstr, '<');
1742554f68d3SGreg Clayton                             // if before the arguments list begins there is a template sign
1743554f68d3SGreg Clayton                             // then scan to the end of the generic args before you try to find
1744554f68d3SGreg Clayton                             // the arguments list
1745554f68d3SGreg Clayton                             if (generic && open_paren && generic < open_paren)
1746554f68d3SGreg Clayton                             {
1747554f68d3SGreg Clayton                                 int generic_depth = 1;
1748554f68d3SGreg Clayton                                 ++generic;
1749554f68d3SGreg Clayton                                 for (;
1750554f68d3SGreg Clayton                                      *generic && generic_depth > 0;
1751554f68d3SGreg Clayton                                      generic++)
1752554f68d3SGreg Clayton                                 {
1753554f68d3SGreg Clayton                                     if (*generic == '<')
1754554f68d3SGreg Clayton                                         generic_depth++;
1755554f68d3SGreg Clayton                                     if (*generic == '>')
1756554f68d3SGreg Clayton                                         generic_depth--;
1757554f68d3SGreg Clayton                                 }
1758554f68d3SGreg Clayton                                 if (*generic)
1759554f68d3SGreg Clayton                                     open_paren = strchr(generic, '(');
1760554f68d3SGreg Clayton                                 else
1761554f68d3SGreg Clayton                                     open_paren = nullptr;
1762554f68d3SGreg Clayton                             }
1763554f68d3SGreg Clayton                             if (open_paren)
1764554f68d3SGreg Clayton                             {
1765554f68d3SGreg Clayton                                 if (IsToken (open_paren, "(anonymous namespace)"))
1766554f68d3SGreg Clayton                                 {
1767554f68d3SGreg Clayton                                     open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '(');
1768554f68d3SGreg Clayton                                     if (open_paren)
1769554f68d3SGreg Clayton                                         close_paren = strchr (open_paren, ')');
1770554f68d3SGreg Clayton                                 }
1771554f68d3SGreg Clayton                                 else
1772554f68d3SGreg Clayton                                     close_paren = strchr (open_paren, ')');
1773554f68d3SGreg Clayton                             }
1774554f68d3SGreg Clayton 
1775554f68d3SGreg Clayton                             if (open_paren)
1776554f68d3SGreg Clayton                                 s.Write(cstr, open_paren - cstr + 1);
1777554f68d3SGreg Clayton                             else
1778554f68d3SGreg Clayton                             {
1779554f68d3SGreg Clayton                                 s.PutCString (cstr);
1780554f68d3SGreg Clayton                                 s.PutChar ('(');
1781554f68d3SGreg Clayton                             }
1782554f68d3SGreg Clayton                             const size_t num_args = args.GetSize();
1783554f68d3SGreg Clayton                             for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
1784554f68d3SGreg Clayton                             {
1785554f68d3SGreg Clayton                                 std::string buffer;
1786554f68d3SGreg Clayton 
1787554f68d3SGreg Clayton                                 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
1788554f68d3SGreg Clayton                                 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
1789ad91c7ceSEnrico Granata                                 StreamString ss;
1790554f68d3SGreg Clayton                                 const char *var_representation = nullptr;
1791554f68d3SGreg Clayton                                 const char *var_name = var_value_sp->GetName().GetCString();
1792ad91c7ceSEnrico Granata                                 if (var_value_sp->GetCompilerType().IsValid())
1793ad91c7ceSEnrico Granata                                 {
1794ad91c7ceSEnrico Granata                                     if (var_value_sp && exe_scope->CalculateTarget())
1795ad91c7ceSEnrico Granata                                         var_value_sp = var_value_sp->GetQualifiedRepresentationIfAvailable(exe_scope->CalculateTarget()->TargetProperties::GetPreferDynamicValue(),
1796ad91c7ceSEnrico Granata                                                                                                            exe_scope->CalculateTarget()->TargetProperties::GetEnableSyntheticValue());
179799558cc4SGreg Clayton                                     if (var_value_sp->GetCompilerType().IsAggregateType() &&
1798554f68d3SGreg Clayton                                         DataVisualization::ShouldPrintAsOneLiner(*var_value_sp.get()))
1799554f68d3SGreg Clayton                                     {
1800554f68d3SGreg Clayton                                         static StringSummaryFormat format(TypeSummaryImpl::Flags()
1801554f68d3SGreg Clayton                                                                           .SetHideItemNames(false)
1802554f68d3SGreg Clayton                                                                           .SetShowMembersOneLiner(true),
1803554f68d3SGreg Clayton                                                                           "");
1804554f68d3SGreg Clayton                                         format.FormatObject(var_value_sp.get(), buffer, TypeSummaryOptions());
1805554f68d3SGreg Clayton                                         var_representation = buffer.c_str();
1806554f68d3SGreg Clayton                                     }
1807554f68d3SGreg Clayton                                     else
1808ad91c7ceSEnrico Granata                                         var_value_sp->DumpPrintableRepresentation(ss,
1809ad91c7ceSEnrico Granata                                                                                   ValueObject::ValueObjectRepresentationStyle::eValueObjectRepresentationStyleSummary,
1810ad91c7ceSEnrico Granata                                                                                   eFormatDefault,
1811ad91c7ceSEnrico Granata                                                                                   ValueObject::PrintableRepresentationSpecialCases::ePrintableRepresentationSpecialCasesAllow,
1812ad91c7ceSEnrico Granata                                                                                   false);
1813ad91c7ceSEnrico Granata                                 }
1814ad91c7ceSEnrico Granata 
1815ad91c7ceSEnrico Granata                                 if (ss.GetData() && ss.GetSize())
1816ad91c7ceSEnrico Granata                                     var_representation = ss.GetData();
1817554f68d3SGreg Clayton                                 if (arg_idx > 0)
1818554f68d3SGreg Clayton                                     s.PutCString (", ");
1819554f68d3SGreg Clayton                                 if (var_value_sp->GetError().Success())
1820554f68d3SGreg Clayton                                 {
1821554f68d3SGreg Clayton                                     if (var_representation)
1822554f68d3SGreg Clayton                                         s.Printf ("%s=%s", var_name, var_representation);
1823554f68d3SGreg Clayton                                     else
1824554f68d3SGreg Clayton                                         s.Printf ("%s=%s at %s", var_name, var_value_sp->GetTypeName().GetCString(), var_value_sp->GetLocationAsCString());
1825554f68d3SGreg Clayton                                 }
1826554f68d3SGreg Clayton                                 else
1827554f68d3SGreg Clayton                                     s.Printf ("%s=<unavailable>", var_name);
1828554f68d3SGreg Clayton                             }
1829554f68d3SGreg Clayton 
1830554f68d3SGreg Clayton                             if (close_paren)
1831554f68d3SGreg Clayton                                 s.PutCString (close_paren);
1832554f68d3SGreg Clayton                             else
1833554f68d3SGreg Clayton                                 s.PutChar(')');
1834554f68d3SGreg Clayton 
1835554f68d3SGreg Clayton                         }
1836554f68d3SGreg Clayton                         else
1837554f68d3SGreg Clayton                         {
1838554f68d3SGreg Clayton                             s.PutCString(cstr);
1839554f68d3SGreg Clayton                         }
1840554f68d3SGreg Clayton                         return true;
1841554f68d3SGreg Clayton                     }
1842554f68d3SGreg Clayton                 }
1843554f68d3SGreg Clayton                 else if (sc->symbol)
1844554f68d3SGreg Clayton                 {
1845554f68d3SGreg Clayton                     const char *cstr = sc->symbol->GetName().AsCString (NULL);
1846554f68d3SGreg Clayton                     if (cstr)
1847554f68d3SGreg Clayton                     {
1848554f68d3SGreg Clayton                         s.PutCString(cstr);
1849554f68d3SGreg Clayton                         return true;
1850554f68d3SGreg Clayton                     }
1851554f68d3SGreg Clayton                 }
1852554f68d3SGreg Clayton             }
1853554f68d3SGreg Clayton             return false;
1854554f68d3SGreg Clayton 
1855554f68d3SGreg Clayton         case Entry::Type::FunctionAddrOffset:
1856554f68d3SGreg Clayton             if (addr)
1857554f68d3SGreg Clayton             {
1858c980fa92SJason Molenda                 if (DumpAddressOffsetFromFunction (s, sc, exe_ctx, *addr, false, false, false))
1859554f68d3SGreg Clayton                     return true;
1860554f68d3SGreg Clayton             }
1861554f68d3SGreg Clayton             return false;
1862554f68d3SGreg Clayton 
1863554f68d3SGreg Clayton         case Entry::Type::FunctionAddrOffsetConcrete:
1864554f68d3SGreg Clayton             if (addr)
1865554f68d3SGreg Clayton             {
1866c980fa92SJason Molenda                 if (DumpAddressOffsetFromFunction (s, sc, exe_ctx, *addr, true, true, true))
1867554f68d3SGreg Clayton                     return true;
1868554f68d3SGreg Clayton             }
1869554f68d3SGreg Clayton             return false;
1870554f68d3SGreg Clayton 
1871554f68d3SGreg Clayton         case Entry::Type::FunctionLineOffset:
1872c980fa92SJason Molenda             if (DumpAddressOffsetFromFunction (s, sc, exe_ctx, sc->line_entry.range.GetBaseAddress(), false, false, false))
1873554f68d3SGreg Clayton                 return true;
1874554f68d3SGreg Clayton             return false;
1875554f68d3SGreg Clayton 
1876554f68d3SGreg Clayton         case Entry::Type::FunctionPCOffset:
1877554f68d3SGreg Clayton             if (exe_ctx)
1878554f68d3SGreg Clayton             {
1879554f68d3SGreg Clayton                 StackFrame *frame = exe_ctx->GetFramePtr();
1880554f68d3SGreg Clayton                 if (frame)
1881554f68d3SGreg Clayton                 {
1882c980fa92SJason Molenda                     if (DumpAddressOffsetFromFunction (s, sc, exe_ctx, frame->GetFrameCodeAddress(), false, false, false))
1883554f68d3SGreg Clayton                         return true;
1884554f68d3SGreg Clayton                 }
1885554f68d3SGreg Clayton             }
1886554f68d3SGreg Clayton             return false;
1887554f68d3SGreg Clayton 
1888c980fa92SJason Molenda         case Entry::Type::FunctionChanged:
1889c980fa92SJason Molenda             return function_changed == true;
1890c980fa92SJason Molenda 
18916ab659a9SJason Molenda         case Entry::Type::FunctionIsOptimized:
18926ab659a9SJason Molenda             {
18936ab659a9SJason Molenda                 bool is_optimized = false;
18946ab659a9SJason Molenda                 if (sc->function && sc->function->GetIsOptimized())
18956ab659a9SJason Molenda                 {
18966ab659a9SJason Molenda                     is_optimized = true;
18976ab659a9SJason Molenda                 }
18986ab659a9SJason Molenda                 return is_optimized;
18996ab659a9SJason Molenda             }
19006ab659a9SJason Molenda 
1901c980fa92SJason Molenda         case Entry::Type::FunctionInitial:
1902c980fa92SJason Molenda             return initial_function == true;
1903c980fa92SJason Molenda 
1904554f68d3SGreg Clayton         case Entry::Type::LineEntryFile:
1905554f68d3SGreg Clayton             if (sc && sc->line_entry.IsValid())
1906554f68d3SGreg Clayton             {
1907554f68d3SGreg Clayton                 Module *module = sc->module_sp.get();
1908554f68d3SGreg Clayton                 if (module)
1909554f68d3SGreg Clayton                 {
1910554f68d3SGreg Clayton                     if (DumpFile(s, sc->line_entry.file, (FileKind)entry.number))
1911554f68d3SGreg Clayton                         return true;
1912554f68d3SGreg Clayton                 }
1913554f68d3SGreg Clayton             }
1914554f68d3SGreg Clayton             return false;
1915554f68d3SGreg Clayton 
1916554f68d3SGreg Clayton         case Entry::Type::LineEntryLineNumber:
1917554f68d3SGreg Clayton             if (sc && sc->line_entry.IsValid())
1918554f68d3SGreg Clayton             {
1919554f68d3SGreg Clayton                 const char *format = "%" PRIu32;
1920554f68d3SGreg Clayton                 if (!entry.printf_format.empty())
1921554f68d3SGreg Clayton                     format = entry.printf_format.c_str();
1922554f68d3SGreg Clayton                 s.Printf(format, sc->line_entry.line);
1923554f68d3SGreg Clayton                 return true;
1924554f68d3SGreg Clayton             }
1925554f68d3SGreg Clayton             return false;
1926554f68d3SGreg Clayton 
1927554f68d3SGreg Clayton         case Entry::Type::LineEntryStartAddress:
1928554f68d3SGreg Clayton         case Entry::Type::LineEntryEndAddress:
1929554f68d3SGreg Clayton             if (sc && sc->line_entry.range.GetBaseAddress().IsValid())
1930554f68d3SGreg Clayton             {
1931554f68d3SGreg Clayton                 Address addr = sc->line_entry.range.GetBaseAddress();
1932554f68d3SGreg Clayton 
1933554f68d3SGreg Clayton                 if (entry.type == Entry::Type::LineEntryEndAddress)
1934554f68d3SGreg Clayton                     addr.Slide(sc->line_entry.range.GetByteSize());
1935554f68d3SGreg Clayton                 if (DumpAddress(s, sc, exe_ctx, addr, false))
1936554f68d3SGreg Clayton                     return true;
1937554f68d3SGreg Clayton             }
1938554f68d3SGreg Clayton             return false;
1939554f68d3SGreg Clayton 
1940554f68d3SGreg Clayton         case Entry::Type::CurrentPCArrow:
1941554f68d3SGreg Clayton             if (addr && exe_ctx && exe_ctx->GetFramePtr())
1942554f68d3SGreg Clayton             {
1943554f68d3SGreg Clayton                 RegisterContextSP reg_ctx = exe_ctx->GetFramePtr()->GetRegisterContextSP();
1944554f68d3SGreg Clayton                 if (reg_ctx.get())
1945554f68d3SGreg Clayton                 {
1946554f68d3SGreg Clayton                     addr_t pc_loadaddr = reg_ctx->GetPC();
1947554f68d3SGreg Clayton                     if (pc_loadaddr != LLDB_INVALID_ADDRESS)
1948554f68d3SGreg Clayton                     {
1949554f68d3SGreg Clayton                         Address pc;
1950554f68d3SGreg Clayton                         pc.SetLoadAddress (pc_loadaddr, exe_ctx->GetTargetPtr());
1951554f68d3SGreg Clayton                         if (pc == *addr)
1952554f68d3SGreg Clayton                         {
1953554f68d3SGreg Clayton                             s.Printf ("-> ");
1954554f68d3SGreg Clayton                             return true;
1955554f68d3SGreg Clayton                         }
1956554f68d3SGreg Clayton                     }
1957554f68d3SGreg Clayton                 }
1958554f68d3SGreg Clayton                 s.Printf("   ");
1959554f68d3SGreg Clayton                 return true;
1960554f68d3SGreg Clayton             }
1961554f68d3SGreg Clayton             return false;
1962554f68d3SGreg Clayton     }
1963554f68d3SGreg Clayton     return false;
1964554f68d3SGreg Clayton }
1965554f68d3SGreg Clayton 
1966554f68d3SGreg Clayton static bool
1967554f68d3SGreg Clayton DumpCommaSeparatedChildEntryNames (Stream &s, const FormatEntity::Entry::Definition *parent)
1968554f68d3SGreg Clayton {
1969554f68d3SGreg Clayton     if (parent->children)
1970554f68d3SGreg Clayton     {
1971554f68d3SGreg Clayton         const size_t n = parent->num_children;
1972554f68d3SGreg Clayton         for (size_t i=0; i<n; ++i)
1973554f68d3SGreg Clayton         {
1974554f68d3SGreg Clayton             if (i > 0)
1975554f68d3SGreg Clayton                 s.PutCString(", ");
1976554f68d3SGreg Clayton             s.Printf ("\"%s\"", parent->children[i].name);
1977554f68d3SGreg Clayton         }
1978554f68d3SGreg Clayton         return true;
1979554f68d3SGreg Clayton     }
1980554f68d3SGreg Clayton     return false;
1981554f68d3SGreg Clayton }
1982554f68d3SGreg Clayton 
1983554f68d3SGreg Clayton 
1984554f68d3SGreg Clayton static Error
1985554f68d3SGreg Clayton ParseEntry (const llvm::StringRef &format_str,
1986554f68d3SGreg Clayton             const FormatEntity::Entry::Definition *parent,
1987554f68d3SGreg Clayton             FormatEntity::Entry &entry)
1988554f68d3SGreg Clayton {
1989554f68d3SGreg Clayton     Error error;
1990554f68d3SGreg Clayton 
1991554f68d3SGreg Clayton     const size_t sep_pos = format_str.find_first_of(".[:");
1992554f68d3SGreg Clayton     const char sep_char = (sep_pos == llvm::StringRef::npos) ? '\0' : format_str[sep_pos];
1993554f68d3SGreg Clayton     llvm::StringRef key = format_str.substr(0, sep_pos);
1994554f68d3SGreg Clayton 
1995554f68d3SGreg Clayton     const size_t n = parent->num_children;
1996554f68d3SGreg Clayton     for (size_t i=0; i<n; ++i)
1997554f68d3SGreg Clayton     {
1998554f68d3SGreg Clayton         const FormatEntity::Entry::Definition *entry_def = parent->children + i;
1999554f68d3SGreg Clayton         if (key.equals(entry_def->name) || entry_def->name[0] == '*')
2000554f68d3SGreg Clayton         {
2001554f68d3SGreg Clayton             llvm::StringRef value;
2002554f68d3SGreg Clayton             if (sep_char)
2003554f68d3SGreg Clayton                 value = format_str.substr(sep_pos + (entry_def->keep_separator ? 0 : 1));
2004554f68d3SGreg Clayton             switch (entry_def->type)
2005554f68d3SGreg Clayton             {
2006554f68d3SGreg Clayton                 case FormatEntity::Entry::Type::ParentString:
20073a29f8b9SPavel Labath                     entry.string = format_str.str();
2008554f68d3SGreg Clayton                     return error; // Success
2009554f68d3SGreg Clayton 
2010554f68d3SGreg Clayton                 case FormatEntity::Entry::Type::ParentNumber:
2011554f68d3SGreg Clayton                     entry.number = entry_def->data;
2012554f68d3SGreg Clayton                     return error; // Success
2013554f68d3SGreg Clayton 
2014554f68d3SGreg Clayton                 case FormatEntity::Entry::Type::InsertString:
2015554f68d3SGreg Clayton                     entry.type = entry_def->type;
2016554f68d3SGreg Clayton                     entry.string = entry_def->string;
2017554f68d3SGreg Clayton                     return error; // Success
2018554f68d3SGreg Clayton 
2019554f68d3SGreg Clayton                 default:
2020554f68d3SGreg Clayton                     entry.type = entry_def->type;
2021554f68d3SGreg Clayton                     break;
2022554f68d3SGreg Clayton             }
2023554f68d3SGreg Clayton 
2024554f68d3SGreg Clayton             if (value.empty())
2025554f68d3SGreg Clayton             {
2026554f68d3SGreg Clayton                 if (entry_def->type == FormatEntity::Entry::Type::Invalid)
2027554f68d3SGreg Clayton                 {
2028554f68d3SGreg Clayton                     if (entry_def->children)
2029554f68d3SGreg Clayton                     {
2030554f68d3SGreg Clayton                         StreamString error_strm;
2031554f68d3SGreg Clayton                         error_strm.Printf("'%s' can't be specified on its own, you must access one of its children: ", entry_def->name);
2032554f68d3SGreg Clayton                         DumpCommaSeparatedChildEntryNames (error_strm, entry_def);
2033554f68d3SGreg Clayton                         error.SetErrorStringWithFormat("%s", error_strm.GetString().c_str());
2034554f68d3SGreg Clayton                     }
2035554f68d3SGreg Clayton                     else if (sep_char == ':')
2036554f68d3SGreg Clayton                     {
2037554f68d3SGreg Clayton                         // Any value whose separator is a with a ':' means this value has a string argument
2038554f68d3SGreg Clayton                         // that needs to be stored in the entry (like "${script.var:}").
2039554f68d3SGreg Clayton                         // In this case the string value is the empty string which is ok.
2040554f68d3SGreg Clayton                     }
2041554f68d3SGreg Clayton                     else
2042554f68d3SGreg Clayton                     {
2043554f68d3SGreg Clayton                         error.SetErrorStringWithFormat("%s", "invalid entry definitions");
2044554f68d3SGreg Clayton                     }
2045554f68d3SGreg Clayton                 }
2046554f68d3SGreg Clayton             }
2047554f68d3SGreg Clayton             else
2048554f68d3SGreg Clayton             {
2049554f68d3SGreg Clayton                 if (entry_def->children)
2050554f68d3SGreg Clayton                 {
2051554f68d3SGreg Clayton                     error = ParseEntry (value, entry_def, entry);
2052554f68d3SGreg Clayton                 }
2053554f68d3SGreg Clayton                 else if (sep_char == ':')
2054554f68d3SGreg Clayton                 {
2055554f68d3SGreg Clayton                     // Any value whose separator is a with a ':' means this value has a string argument
2056554f68d3SGreg Clayton                     // that needs to be stored in the entry (like "${script.var:modulename.function}")
20573a29f8b9SPavel Labath                     entry.string = value.str();
2058554f68d3SGreg Clayton                 }
2059554f68d3SGreg Clayton                 else
2060554f68d3SGreg Clayton                 {
2061554f68d3SGreg Clayton                     error.SetErrorStringWithFormat("'%s' followed by '%s' but it has no children",
2062554f68d3SGreg Clayton                                                    key.str().c_str(),
2063554f68d3SGreg Clayton                                                    value.str().c_str());
2064554f68d3SGreg Clayton                 }
2065554f68d3SGreg Clayton             }
2066554f68d3SGreg Clayton             return error;
2067554f68d3SGreg Clayton         }
2068554f68d3SGreg Clayton     }
2069554f68d3SGreg Clayton     StreamString error_strm;
2070554f68d3SGreg Clayton     if (parent->type == FormatEntity::Entry::Type::Root)
2071554f68d3SGreg Clayton         error_strm.Printf("invalid top level item '%s'. Valid top level items are: ", key.str().c_str());
2072554f68d3SGreg Clayton     else
2073554f68d3SGreg Clayton         error_strm.Printf("invalid member '%s' in '%s'. Valid members are: ", key.str().c_str(), parent->name);
2074554f68d3SGreg Clayton     DumpCommaSeparatedChildEntryNames (error_strm, parent);
2075554f68d3SGreg Clayton     error.SetErrorStringWithFormat("%s", error_strm.GetString().c_str());
2076554f68d3SGreg Clayton     return error;
2077554f68d3SGreg Clayton }
2078554f68d3SGreg Clayton 
2079554f68d3SGreg Clayton 
2080554f68d3SGreg Clayton static const FormatEntity::Entry::Definition *
2081554f68d3SGreg Clayton FindEntry (const llvm::StringRef &format_str, const FormatEntity::Entry::Definition *parent, llvm::StringRef &remainder)
2082554f68d3SGreg Clayton {
2083554f68d3SGreg Clayton     Error error;
2084554f68d3SGreg Clayton 
2085554f68d3SGreg Clayton     std::pair<llvm::StringRef, llvm::StringRef> p = format_str.split('.');
2086554f68d3SGreg Clayton     const size_t n = parent->num_children;
2087554f68d3SGreg Clayton     for (size_t i=0; i<n; ++i)
2088554f68d3SGreg Clayton     {
2089554f68d3SGreg Clayton         const FormatEntity::Entry::Definition *entry_def = parent->children + i;
2090554f68d3SGreg Clayton         if (p.first.equals(entry_def->name) || entry_def->name[0] == '*')
2091554f68d3SGreg Clayton         {
2092554f68d3SGreg Clayton             if (p.second.empty())
2093554f68d3SGreg Clayton             {
2094554f68d3SGreg Clayton                 if (format_str.back() == '.')
2095554f68d3SGreg Clayton                     remainder = format_str.drop_front(format_str.size() - 1);
2096554f68d3SGreg Clayton                 else
2097554f68d3SGreg Clayton                     remainder = llvm::StringRef(); // Exact match
2098554f68d3SGreg Clayton                 return entry_def;
2099554f68d3SGreg Clayton             }
2100554f68d3SGreg Clayton             else
2101554f68d3SGreg Clayton             {
2102554f68d3SGreg Clayton                 if (entry_def->children)
2103554f68d3SGreg Clayton                 {
2104554f68d3SGreg Clayton                     return FindEntry (p.second, entry_def, remainder);
2105554f68d3SGreg Clayton                 }
2106554f68d3SGreg Clayton                 else
2107554f68d3SGreg Clayton                 {
2108554f68d3SGreg Clayton                     remainder = p.second;
2109554f68d3SGreg Clayton                     return entry_def;
2110554f68d3SGreg Clayton                 }
2111554f68d3SGreg Clayton             }
2112554f68d3SGreg Clayton         }
2113554f68d3SGreg Clayton     }
2114554f68d3SGreg Clayton     remainder = format_str;
2115554f68d3SGreg Clayton     return parent;
2116554f68d3SGreg Clayton }
2117554f68d3SGreg Clayton 
2118554f68d3SGreg Clayton Error
2119554f68d3SGreg Clayton FormatEntity::ParseInternal (llvm::StringRef &format, Entry &parent_entry, uint32_t depth)
2120554f68d3SGreg Clayton {
2121554f68d3SGreg Clayton     Error error;
2122554f68d3SGreg Clayton     while (!format.empty() && error.Success())
2123554f68d3SGreg Clayton     {
2124554f68d3SGreg Clayton         const size_t non_special_chars = format.find_first_of("${}\\");
2125554f68d3SGreg Clayton 
2126554f68d3SGreg Clayton         if (non_special_chars == llvm::StringRef::npos)
2127554f68d3SGreg Clayton         {
2128554f68d3SGreg Clayton             // No special characters, just string bytes so add them and we are done
2129554f68d3SGreg Clayton             parent_entry.AppendText(format);
2130554f68d3SGreg Clayton             return error;
2131554f68d3SGreg Clayton         }
2132554f68d3SGreg Clayton 
2133554f68d3SGreg Clayton         if (non_special_chars > 0)
2134554f68d3SGreg Clayton         {
2135554f68d3SGreg Clayton             // We have a special character, so add all characters before these as a plain string
2136554f68d3SGreg Clayton             parent_entry.AppendText(format.substr(0,non_special_chars));
2137554f68d3SGreg Clayton             format = format.drop_front(non_special_chars);
2138554f68d3SGreg Clayton         }
2139554f68d3SGreg Clayton 
2140554f68d3SGreg Clayton         switch (format[0])
2141554f68d3SGreg Clayton         {
2142554f68d3SGreg Clayton             case '\0':
2143554f68d3SGreg Clayton                 return error;
2144554f68d3SGreg Clayton 
2145554f68d3SGreg Clayton             case '{':
2146554f68d3SGreg Clayton                 {
2147554f68d3SGreg Clayton                     format = format.drop_front(); // Skip the '{'
2148554f68d3SGreg Clayton                     Entry scope_entry(Entry::Type::Scope);
2149554f68d3SGreg Clayton                     error = FormatEntity::ParseInternal (format, scope_entry, depth+1);
2150554f68d3SGreg Clayton                     if (error.Fail())
2151554f68d3SGreg Clayton                         return error;
2152554f68d3SGreg Clayton                     parent_entry.AppendEntry(std::move(scope_entry));
2153554f68d3SGreg Clayton                 }
2154554f68d3SGreg Clayton                 break;
2155554f68d3SGreg Clayton 
2156554f68d3SGreg Clayton             case '}':
2157554f68d3SGreg Clayton                 if (depth == 0)
2158554f68d3SGreg Clayton                     error.SetErrorString("unmatched '}' character");
2159554f68d3SGreg Clayton                 else
2160554f68d3SGreg Clayton                     format = format.drop_front(); // Skip the '}' as we are at the end of the scope
2161554f68d3SGreg Clayton                 return error;
2162554f68d3SGreg Clayton 
2163554f68d3SGreg Clayton             case '\\':
2164554f68d3SGreg Clayton                 {
2165554f68d3SGreg Clayton                     format = format.drop_front(); // Skip the '\' character
2166554f68d3SGreg Clayton                     if (format.empty())
2167554f68d3SGreg Clayton                     {
2168554f68d3SGreg Clayton                         error.SetErrorString("'\\' character was not followed by another character");
2169554f68d3SGreg Clayton                         return error;
2170554f68d3SGreg Clayton                     }
2171554f68d3SGreg Clayton 
2172554f68d3SGreg Clayton                     const char desens_char = format[0];
2173554f68d3SGreg Clayton                     format = format.drop_front(); // Skip the desensitized char character
2174554f68d3SGreg Clayton                     switch (desens_char)
2175554f68d3SGreg Clayton                     {
2176554f68d3SGreg Clayton                         case 'a': parent_entry.AppendChar('\a'); break;
2177554f68d3SGreg Clayton                         case 'b': parent_entry.AppendChar('\b'); break;
2178554f68d3SGreg Clayton                         case 'f': parent_entry.AppendChar('\f'); break;
2179554f68d3SGreg Clayton                         case 'n': parent_entry.AppendChar('\n'); break;
2180554f68d3SGreg Clayton                         case 'r': parent_entry.AppendChar('\r'); break;
2181554f68d3SGreg Clayton                         case 't': parent_entry.AppendChar('\t'); break;
2182554f68d3SGreg Clayton                         case 'v': parent_entry.AppendChar('\v'); break;
2183554f68d3SGreg Clayton                         case '\'': parent_entry.AppendChar('\''); break;
2184554f68d3SGreg Clayton                         case '\\': parent_entry.AppendChar('\\'); break;
2185554f68d3SGreg Clayton                         case '0':
2186554f68d3SGreg Clayton                             // 1 to 3 octal chars
2187554f68d3SGreg Clayton                         {
2188554f68d3SGreg Clayton                             // Make a string that can hold onto the initial zero char,
2189554f68d3SGreg Clayton                             // up to 3 octal digits, and a terminating NULL.
2190554f68d3SGreg Clayton                             char oct_str[5] = { 0, 0, 0, 0, 0 };
2191554f68d3SGreg Clayton 
2192554f68d3SGreg Clayton                             int i;
2193554f68d3SGreg Clayton                             for (i=0; (format[i] >= '0' && format[i] <= '7') && i<4; ++i)
2194554f68d3SGreg Clayton                                 oct_str[i] = format[i];
2195554f68d3SGreg Clayton 
2196554f68d3SGreg Clayton                             // We don't want to consume the last octal character since
2197554f68d3SGreg Clayton                             // the main for loop will do this for us, so we advance p by
2198554f68d3SGreg Clayton                             // one less than i (even if i is zero)
2199554f68d3SGreg Clayton                             format = format.drop_front(i);
2200554f68d3SGreg Clayton                             unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2201554f68d3SGreg Clayton                             if (octal_value <= UINT8_MAX)
2202554f68d3SGreg Clayton                             {
2203554f68d3SGreg Clayton                                 parent_entry.AppendChar((char)octal_value);
2204554f68d3SGreg Clayton                             }
2205554f68d3SGreg Clayton                             else
2206554f68d3SGreg Clayton                             {
2207554f68d3SGreg Clayton                                 error.SetErrorString("octal number is larger than a single byte");
2208554f68d3SGreg Clayton                                 return error;
2209554f68d3SGreg Clayton                             }
2210554f68d3SGreg Clayton                         }
2211554f68d3SGreg Clayton                             break;
2212554f68d3SGreg Clayton 
2213554f68d3SGreg Clayton                         case 'x':
2214554f68d3SGreg Clayton                             // hex number in the format
2215554f68d3SGreg Clayton                             if (isxdigit(format[0]))
2216554f68d3SGreg Clayton                             {
2217554f68d3SGreg Clayton                                 // Make a string that can hold onto two hex chars plus a
2218554f68d3SGreg Clayton                                 // NULL terminator
2219554f68d3SGreg Clayton                                 char hex_str[3] = { 0,0,0 };
2220554f68d3SGreg Clayton                                 hex_str[0] = format[0];
2221554f68d3SGreg Clayton 
2222554f68d3SGreg Clayton                                 format = format.drop_front();
2223554f68d3SGreg Clayton 
2224554f68d3SGreg Clayton                                 if (isxdigit(format[0]))
2225554f68d3SGreg Clayton                                 {
2226554f68d3SGreg Clayton                                     hex_str[1] = format[0];
2227554f68d3SGreg Clayton                                     format = format.drop_front();
2228554f68d3SGreg Clayton                                 }
2229554f68d3SGreg Clayton 
2230554f68d3SGreg Clayton                                 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2231554f68d3SGreg Clayton                                 if (hex_value <= UINT8_MAX)
2232554f68d3SGreg Clayton                                 {
2233554f68d3SGreg Clayton                                     parent_entry.AppendChar((char)hex_value);
2234554f68d3SGreg Clayton                                 }
2235554f68d3SGreg Clayton                                 else
2236554f68d3SGreg Clayton                                 {
2237554f68d3SGreg Clayton                                     error.SetErrorString("hex number is larger than a single byte");
2238554f68d3SGreg Clayton                                     return error;
2239554f68d3SGreg Clayton                                 }
2240554f68d3SGreg Clayton                             }
2241554f68d3SGreg Clayton                             else
2242554f68d3SGreg Clayton                             {
2243554f68d3SGreg Clayton                                 parent_entry.AppendChar(desens_char);
2244554f68d3SGreg Clayton                             }
2245554f68d3SGreg Clayton                             break;
2246554f68d3SGreg Clayton 
2247554f68d3SGreg Clayton                         default:
2248554f68d3SGreg Clayton                             // Just desensitize any other character by just printing what
2249554f68d3SGreg Clayton                             // came after the '\'
2250554f68d3SGreg Clayton                             parent_entry.AppendChar(desens_char);
2251554f68d3SGreg Clayton                             break;
2252554f68d3SGreg Clayton                     }
2253554f68d3SGreg Clayton                 }
2254554f68d3SGreg Clayton                 break;
2255554f68d3SGreg Clayton 
2256554f68d3SGreg Clayton             case '$':
2257554f68d3SGreg Clayton                 if (format.size() == 1)
2258554f68d3SGreg Clayton                 {
2259554f68d3SGreg Clayton                     // '$' at the end of a format string, just print the '$'
2260554f68d3SGreg Clayton                     parent_entry.AppendText("$");
2261554f68d3SGreg Clayton                 }
2262554f68d3SGreg Clayton                 else
2263554f68d3SGreg Clayton                 {
2264554f68d3SGreg Clayton                     format = format.drop_front(); // Skip the '$'
2265554f68d3SGreg Clayton 
2266554f68d3SGreg Clayton                     if (format[0] == '{')
2267554f68d3SGreg Clayton                     {
2268554f68d3SGreg Clayton                         format = format.drop_front(); // Skip the '{'
2269554f68d3SGreg Clayton 
2270554f68d3SGreg Clayton                         llvm::StringRef variable, variable_format;
2271554f68d3SGreg Clayton                         error = FormatEntity::ExtractVariableInfo (format, variable, variable_format);
2272554f68d3SGreg Clayton                         if (error.Fail())
2273554f68d3SGreg Clayton                             return error;
2274554f68d3SGreg Clayton                         bool verify_is_thread_id = false;
2275554f68d3SGreg Clayton                         Entry entry;
2276554f68d3SGreg Clayton                         if (!variable_format.empty())
2277554f68d3SGreg Clayton                         {
22783a29f8b9SPavel Labath                             entry.printf_format = variable_format.str();
2279554f68d3SGreg Clayton 
2280554f68d3SGreg Clayton                             // If the format contains a '%' we are going to assume this is
2281554f68d3SGreg Clayton                             // a printf style format. So if you want to format your thread ID
2282554f68d3SGreg Clayton                             // using "0x%llx" you can use:
2283554f68d3SGreg Clayton                             // ${thread.id%0x%llx}
2284554f68d3SGreg Clayton                             //
2285554f68d3SGreg Clayton                             // If there is no '%' in the format, then it is assumed to be a
2286554f68d3SGreg Clayton                             // LLDB format name, or one of the extended formats specified in
2287554f68d3SGreg Clayton                             // the switch statement below.
2288554f68d3SGreg Clayton 
2289554f68d3SGreg Clayton                             if (entry.printf_format.find('%') == std::string::npos)
2290554f68d3SGreg Clayton                             {
2291554f68d3SGreg Clayton                                 bool clear_printf = false;
2292554f68d3SGreg Clayton 
2293554f68d3SGreg Clayton                                 if (FormatManager::GetFormatFromCString(entry.printf_format.c_str(),
2294554f68d3SGreg Clayton                                                                         false,
2295554f68d3SGreg Clayton                                                                         entry.fmt))
2296554f68d3SGreg Clayton                                 {
2297554f68d3SGreg Clayton                                     // We have an LLDB format, so clear the printf format
2298554f68d3SGreg Clayton                                     clear_printf = true;
2299554f68d3SGreg Clayton                                 }
2300554f68d3SGreg Clayton                                 else if (entry.printf_format.size() == 1)
2301554f68d3SGreg Clayton                                 {
2302554f68d3SGreg Clayton                                     switch (entry.printf_format[0])
2303554f68d3SGreg Clayton                                     {
2304554f68d3SGreg Clayton                                         case '@':             // if this is an @ sign, print ObjC description
2305554f68d3SGreg Clayton                                             entry.number = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
2306554f68d3SGreg Clayton                                             clear_printf = true;
2307554f68d3SGreg Clayton                                             break;
2308554f68d3SGreg Clayton                                         case 'V': // if this is a V, print the value using the default format
2309554f68d3SGreg Clayton                                             entry.number = ValueObject::eValueObjectRepresentationStyleValue;
2310554f68d3SGreg Clayton                                             clear_printf = true;
2311554f68d3SGreg Clayton                                             break;
2312554f68d3SGreg Clayton                                         case 'L': // if this is an L, print the location of the value
2313554f68d3SGreg Clayton                                             entry.number = ValueObject::eValueObjectRepresentationStyleLocation;
2314554f68d3SGreg Clayton                                             clear_printf = true;
2315554f68d3SGreg Clayton                                             break;
2316554f68d3SGreg Clayton                                         case 'S': // if this is an S, print the summary after all
2317554f68d3SGreg Clayton                                             entry.number = ValueObject::eValueObjectRepresentationStyleSummary;
2318554f68d3SGreg Clayton                                             clear_printf = true;
2319554f68d3SGreg Clayton                                             break;
2320554f68d3SGreg Clayton                                         case '#': // if this is a '#', print the number of children
2321554f68d3SGreg Clayton                                             entry.number = ValueObject::eValueObjectRepresentationStyleChildrenCount;
2322554f68d3SGreg Clayton                                             clear_printf = true;
2323554f68d3SGreg Clayton                                             break;
2324554f68d3SGreg Clayton                                         case 'T': // if this is a 'T', print the type
2325554f68d3SGreg Clayton                                             entry.number = ValueObject::eValueObjectRepresentationStyleType;
2326554f68d3SGreg Clayton                                             clear_printf = true;
2327554f68d3SGreg Clayton                                             break;
2328554f68d3SGreg Clayton                                         case 'N': // if this is a 'N', print the name
2329554f68d3SGreg Clayton                                             entry.number = ValueObject::eValueObjectRepresentationStyleName;
2330554f68d3SGreg Clayton                                             clear_printf = true;
2331554f68d3SGreg Clayton                                             break;
2332281b65bcSGreg Clayton                                         case '>': // if this is a '>', print the expression path
2333554f68d3SGreg Clayton                                             entry.number = ValueObject::eValueObjectRepresentationStyleExpressionPath;
2334554f68d3SGreg Clayton                                             clear_printf = true;
2335554f68d3SGreg Clayton                                             break;
2336554f68d3SGreg Clayton                                         default:
2337554f68d3SGreg Clayton                                             error.SetErrorStringWithFormat("invalid format: '%s'", entry.printf_format.c_str());
2338554f68d3SGreg Clayton                                             return error;
2339554f68d3SGreg Clayton                                     }
2340554f68d3SGreg Clayton                                 }
2341554f68d3SGreg Clayton                                 else if (FormatManager::GetFormatFromCString(entry.printf_format.c_str(),
2342554f68d3SGreg Clayton                                                                              true,
2343554f68d3SGreg Clayton                                                                              entry.fmt))
2344554f68d3SGreg Clayton                                 {
2345554f68d3SGreg Clayton                                     clear_printf = true;
2346554f68d3SGreg Clayton                                 }
2347554f68d3SGreg Clayton                                 else if (entry.printf_format == "tid")
2348554f68d3SGreg Clayton                                 {
2349554f68d3SGreg Clayton                                     verify_is_thread_id = true;
2350554f68d3SGreg Clayton                                 }
2351554f68d3SGreg Clayton                                 else
2352554f68d3SGreg Clayton                                 {
2353554f68d3SGreg Clayton                                     error.SetErrorStringWithFormat("invalid format: '%s'", entry.printf_format.c_str());
2354554f68d3SGreg Clayton                                     return error;
2355554f68d3SGreg Clayton                                 }
2356554f68d3SGreg Clayton 
2357554f68d3SGreg Clayton                                 // Our format string turned out to not be a printf style format
2358554f68d3SGreg Clayton                                 // so lets clear the string
2359554f68d3SGreg Clayton                                 if (clear_printf)
2360554f68d3SGreg Clayton                                     entry.printf_format.clear();
2361554f68d3SGreg Clayton                             }
2362554f68d3SGreg Clayton                         }
2363554f68d3SGreg Clayton 
2364554f68d3SGreg Clayton                         // Check for dereferences
2365554f68d3SGreg Clayton                         if (variable[0] == '*')
2366554f68d3SGreg Clayton                         {
2367554f68d3SGreg Clayton                             entry.deref = true;
2368554f68d3SGreg Clayton                             variable = variable.drop_front();
2369554f68d3SGreg Clayton                         }
2370554f68d3SGreg Clayton 
2371554f68d3SGreg Clayton                         error = ParseEntry (variable, &g_root, entry);
2372554f68d3SGreg Clayton                         if (error.Fail())
2373554f68d3SGreg Clayton                             return error;
2374554f68d3SGreg Clayton 
2375554f68d3SGreg Clayton                         if (verify_is_thread_id)
2376554f68d3SGreg Clayton                         {
2377554f68d3SGreg Clayton                             if (entry.type != Entry::Type::ThreadID &&
2378554f68d3SGreg Clayton                                 entry.type != Entry::Type::ThreadProtocolID)
2379554f68d3SGreg Clayton                             {
2380554f68d3SGreg Clayton                                 error.SetErrorString("the 'tid' format can only be used on ${thread.id} and ${thread.protocol_id}");
2381554f68d3SGreg Clayton                             }
2382554f68d3SGreg Clayton                         }
2383554f68d3SGreg Clayton 
2384554f68d3SGreg Clayton                         switch (entry.type)
2385554f68d3SGreg Clayton                         {
2386554f68d3SGreg Clayton                             case Entry::Type::Variable:
2387554f68d3SGreg Clayton                             case Entry::Type::VariableSynthetic:
2388554f68d3SGreg Clayton                                 if (entry.number == 0)
2389554f68d3SGreg Clayton                                 {
2390554f68d3SGreg Clayton                                     if (entry.string.empty())
2391554f68d3SGreg Clayton                                         entry.number = ValueObject::eValueObjectRepresentationStyleValue;
2392554f68d3SGreg Clayton                                     else
2393554f68d3SGreg Clayton                                         entry.number = ValueObject::eValueObjectRepresentationStyleSummary;
2394554f68d3SGreg Clayton                                 }
2395554f68d3SGreg Clayton                                 break;
2396554f68d3SGreg Clayton                             default:
2397554f68d3SGreg Clayton                                 // Make sure someone didn't try to dereference anything but ${var} or ${svar}
2398554f68d3SGreg Clayton                                 if (entry.deref)
2399554f68d3SGreg Clayton                                 {
2400554f68d3SGreg Clayton                                     error.SetErrorStringWithFormat("${%s} can't be dereferenced, only ${var} and ${svar} can.", variable.str().c_str());
2401554f68d3SGreg Clayton                                     return error;
2402554f68d3SGreg Clayton                                 }
2403554f68d3SGreg Clayton                         }
2404554f68d3SGreg Clayton                         // Check if this entry just wants to insert a constant string
2405554f68d3SGreg Clayton                         // value into the parent_entry, if so, insert the string with
2406554f68d3SGreg Clayton                         // AppendText, else append the entry to the parent_entry.
2407554f68d3SGreg Clayton                         if (entry.type == Entry::Type::InsertString)
2408554f68d3SGreg Clayton                             parent_entry.AppendText(entry.string.c_str());
2409554f68d3SGreg Clayton                         else
2410554f68d3SGreg Clayton                             parent_entry.AppendEntry(std::move(entry));
2411554f68d3SGreg Clayton                     }
2412554f68d3SGreg Clayton                 }
2413554f68d3SGreg Clayton                 break;
2414554f68d3SGreg Clayton         }
2415554f68d3SGreg Clayton     }
2416554f68d3SGreg Clayton     return error;
2417554f68d3SGreg Clayton }
2418554f68d3SGreg Clayton 
2419554f68d3SGreg Clayton 
2420554f68d3SGreg Clayton Error
2421554f68d3SGreg Clayton FormatEntity::ExtractVariableInfo (llvm::StringRef &format_str, llvm::StringRef &variable_name, llvm::StringRef &variable_format)
2422554f68d3SGreg Clayton {
2423554f68d3SGreg Clayton     Error error;
2424554f68d3SGreg Clayton     variable_name = llvm::StringRef();
2425554f68d3SGreg Clayton     variable_format = llvm::StringRef();
2426554f68d3SGreg Clayton 
2427e8433cc1SBruce Mitchener     const size_t paren_pos = format_str.find('}');
2428554f68d3SGreg Clayton     if (paren_pos != llvm::StringRef::npos)
2429554f68d3SGreg Clayton     {
2430e8433cc1SBruce Mitchener         const size_t percent_pos = format_str.find('%');
2431554f68d3SGreg Clayton         if (percent_pos < paren_pos)
2432554f68d3SGreg Clayton         {
2433554f68d3SGreg Clayton             if (percent_pos > 0)
2434554f68d3SGreg Clayton             {
2435554f68d3SGreg Clayton                 if (percent_pos > 1)
2436554f68d3SGreg Clayton                     variable_name = format_str.substr(0, percent_pos);
2437554f68d3SGreg Clayton                 variable_format = format_str.substr(percent_pos + 1, paren_pos - (percent_pos + 1));
2438554f68d3SGreg Clayton             }
2439554f68d3SGreg Clayton         }
2440554f68d3SGreg Clayton         else
2441554f68d3SGreg Clayton         {
2442554f68d3SGreg Clayton             variable_name = format_str.substr(0, paren_pos);
2443554f68d3SGreg Clayton         }
2444554f68d3SGreg Clayton         // Strip off elements and the formatting and the trailing '}'
2445554f68d3SGreg Clayton         format_str = format_str.substr(paren_pos + 1);
2446554f68d3SGreg Clayton     }
2447554f68d3SGreg Clayton     else
2448554f68d3SGreg Clayton     {
2449554f68d3SGreg Clayton         error.SetErrorStringWithFormat("missing terminating '}' character for '${%s'", format_str.str().c_str());
2450554f68d3SGreg Clayton     }
2451554f68d3SGreg Clayton     return error;
2452554f68d3SGreg Clayton }
2453554f68d3SGreg Clayton 
2454554f68d3SGreg Clayton bool
2455554f68d3SGreg Clayton FormatEntity::FormatFileSpec (const FileSpec &file_spec, Stream &s, llvm::StringRef variable_name, llvm::StringRef variable_format)
2456554f68d3SGreg Clayton {
2457554f68d3SGreg Clayton     if (variable_name.empty() || variable_name.equals(".fullpath"))
2458554f68d3SGreg Clayton     {
2459554f68d3SGreg Clayton         file_spec.Dump(&s);
2460554f68d3SGreg Clayton         return true;
2461554f68d3SGreg Clayton     }
2462554f68d3SGreg Clayton     else if (variable_name.equals(".basename"))
2463554f68d3SGreg Clayton     {
2464554f68d3SGreg Clayton         s.PutCString(file_spec.GetFilename().AsCString(""));
2465554f68d3SGreg Clayton         return true;
2466554f68d3SGreg Clayton     }
2467554f68d3SGreg Clayton     else if (variable_name.equals(".dirname"))
2468554f68d3SGreg Clayton     {
2469554f68d3SGreg Clayton         s.PutCString(file_spec.GetFilename().AsCString(""));
2470554f68d3SGreg Clayton         return true;
2471554f68d3SGreg Clayton     }
2472554f68d3SGreg Clayton     return false;
2473554f68d3SGreg Clayton }
2474554f68d3SGreg Clayton 
2475554f68d3SGreg Clayton static std::string
2476554f68d3SGreg Clayton MakeMatch (const llvm::StringRef &prefix, const char *suffix)
2477554f68d3SGreg Clayton {
2478554f68d3SGreg Clayton     std::string match(prefix.str());
2479554f68d3SGreg Clayton     match.append(suffix);
24803a29f8b9SPavel Labath     return match;
2481554f68d3SGreg Clayton }
2482554f68d3SGreg Clayton 
2483554f68d3SGreg Clayton static void
2484554f68d3SGreg Clayton AddMatches (const FormatEntity::Entry::Definition *def,
2485554f68d3SGreg Clayton             const llvm::StringRef &prefix,
2486554f68d3SGreg Clayton             const llvm::StringRef &match_prefix,
2487554f68d3SGreg Clayton             StringList &matches)
2488554f68d3SGreg Clayton {
2489554f68d3SGreg Clayton     const size_t n = def->num_children;
2490554f68d3SGreg Clayton     if (n > 0)
2491554f68d3SGreg Clayton     {
2492554f68d3SGreg Clayton         for (size_t i=0; i<n; ++i)
2493554f68d3SGreg Clayton         {
24943a29f8b9SPavel Labath             std::string match = prefix.str();
2495554f68d3SGreg Clayton             if (match_prefix.empty())
2496554f68d3SGreg Clayton                 matches.AppendString(MakeMatch (prefix, def->children[i].name));
2497554f68d3SGreg Clayton             else if (strncmp(def->children[i].name, match_prefix.data(), match_prefix.size()) == 0)
2498554f68d3SGreg Clayton                 matches.AppendString(MakeMatch (prefix, def->children[i].name + match_prefix.size()));
2499554f68d3SGreg Clayton         }
2500554f68d3SGreg Clayton     }
2501554f68d3SGreg Clayton }
2502554f68d3SGreg Clayton size_t
2503554f68d3SGreg Clayton FormatEntity::AutoComplete (const char *s,
2504554f68d3SGreg Clayton                             int match_start_point,
2505554f68d3SGreg Clayton                             int max_return_elements,
2506554f68d3SGreg Clayton                             bool &word_complete,
2507554f68d3SGreg Clayton                             StringList &matches)
2508554f68d3SGreg Clayton {
2509554f68d3SGreg Clayton     word_complete = false;
2510554f68d3SGreg Clayton     llvm::StringRef str(s + match_start_point);
2511554f68d3SGreg Clayton     matches.Clear();
2512554f68d3SGreg Clayton 
2513554f68d3SGreg Clayton     const size_t dollar_pos = str.rfind('$');
2514554f68d3SGreg Clayton     if (dollar_pos != llvm::StringRef::npos)
2515554f68d3SGreg Clayton     {
2516554f68d3SGreg Clayton         // Hitting TAB after $ at the end of the string add a "{"
2517554f68d3SGreg Clayton         if (dollar_pos == str.size() - 1)
2518554f68d3SGreg Clayton         {
25193a29f8b9SPavel Labath             std::string match = str.str();
2520554f68d3SGreg Clayton             match.append("{");
2521554f68d3SGreg Clayton             matches.AppendString(std::move(match));
2522554f68d3SGreg Clayton         }
2523554f68d3SGreg Clayton         else if (str[dollar_pos + 1] == '{')
2524554f68d3SGreg Clayton         {
2525554f68d3SGreg Clayton             const size_t close_pos = str.find('}', dollar_pos + 2);
2526554f68d3SGreg Clayton             if (close_pos == llvm::StringRef::npos)
2527554f68d3SGreg Clayton             {
2528554f68d3SGreg Clayton                 const size_t format_pos = str.find('%', dollar_pos + 2);
2529554f68d3SGreg Clayton                 if (format_pos == llvm::StringRef::npos)
2530554f68d3SGreg Clayton                 {
2531554f68d3SGreg Clayton                     llvm::StringRef partial_variable (str.substr(dollar_pos + 2));
2532554f68d3SGreg Clayton                     if (partial_variable.empty())
2533554f68d3SGreg Clayton                     {
2534554f68d3SGreg Clayton                         // Suggest all top level entites as we are just past "${"
2535554f68d3SGreg Clayton                         AddMatches(&g_root, str, llvm::StringRef(), matches);
2536554f68d3SGreg Clayton                     }
2537554f68d3SGreg Clayton                     else
2538554f68d3SGreg Clayton                     {
2539554f68d3SGreg Clayton                         // We have a partially specified variable, find it
2540554f68d3SGreg Clayton                         llvm::StringRef remainder;
2541554f68d3SGreg Clayton                         const FormatEntity::Entry::Definition* entry_def = FindEntry (partial_variable, &g_root, remainder);
2542554f68d3SGreg Clayton                         if (entry_def)
2543554f68d3SGreg Clayton                         {
2544554f68d3SGreg Clayton                             const size_t n = entry_def->num_children;
2545554f68d3SGreg Clayton 
2546554f68d3SGreg Clayton                             if (remainder.empty())
2547554f68d3SGreg Clayton                             {
2548554f68d3SGreg Clayton                                 // Exact match
2549554f68d3SGreg Clayton                                 if (n > 0)
2550554f68d3SGreg Clayton                                 {
2551554f68d3SGreg Clayton                                     // "${thread.info" <TAB>
25523a29f8b9SPavel Labath                                     matches.AppendString(MakeMatch(str, "."));
2553554f68d3SGreg Clayton                                 }
2554554f68d3SGreg Clayton                                 else
2555554f68d3SGreg Clayton                                 {
2556554f68d3SGreg Clayton                                     // "${thread.id" <TAB>
25573a29f8b9SPavel Labath                                     matches.AppendString(MakeMatch (str, "}"));
2558554f68d3SGreg Clayton                                     word_complete = true;
2559554f68d3SGreg Clayton                                 }
2560554f68d3SGreg Clayton                             }
2561554f68d3SGreg Clayton                             else if (remainder.equals("."))
2562554f68d3SGreg Clayton                             {
2563554f68d3SGreg Clayton                                 // "${thread." <TAB>
2564554f68d3SGreg Clayton                                 AddMatches(entry_def, str, llvm::StringRef(), matches);
2565554f68d3SGreg Clayton                             }
2566554f68d3SGreg Clayton                             else
2567554f68d3SGreg Clayton                             {
2568554f68d3SGreg Clayton                                 // We have a partial match
2569554f68d3SGreg Clayton                                 // "${thre" <TAB>
2570554f68d3SGreg Clayton                                 AddMatches(entry_def, str, remainder, matches);
2571554f68d3SGreg Clayton                             }
2572554f68d3SGreg Clayton                         }
2573554f68d3SGreg Clayton                     }
2574554f68d3SGreg Clayton                 }
2575554f68d3SGreg Clayton             }
2576554f68d3SGreg Clayton         }
2577554f68d3SGreg Clayton     }
2578554f68d3SGreg Clayton     return matches.GetSize();
2579554f68d3SGreg Clayton }
2580