1 //===-- OptionValueUUID.cpp ------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Interpreter/OptionValueUUID.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/Stream.h"
18 #include "lldb/Core/StringList.h"
19 #include "lldb/Interpreter/CommandInterpreter.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 void
25 OptionValueUUID::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
26 {
27     if (dump_mask & eDumpOptionType)
28         strm.Printf ("(%s)", GetTypeAsCString ());
29     if (dump_mask & eDumpOptionValue)
30     {
31         if (dump_mask & eDumpOptionType)
32             strm.PutCString (" = ");
33         m_uuid.Dump (&strm);
34     }
35 }
36 
37 Error
38 OptionValueUUID::SetValueFromString (llvm::StringRef value,
39                                       VarSetOperationType op)
40 {
41     Error error;
42     switch (op)
43     {
44         case eVarSetOperationClear:
45             Clear();
46             NotifyValueChanged();
47             break;
48 
49         case eVarSetOperationReplace:
50         case eVarSetOperationAssign:
51             {
52                 if (m_uuid.SetFromCString(value.str().c_str()) == 0)
53                     error.SetErrorStringWithFormat ("invalid uuid string value '%s'", value.str().c_str());
54                 else
55                 {
56                     m_value_was_set = true;
57                     NotifyValueChanged();
58                 }
59             }
60             break;
61 
62         case eVarSetOperationInsertBefore:
63         case eVarSetOperationInsertAfter:
64         case eVarSetOperationRemove:
65         case eVarSetOperationAppend:
66         case eVarSetOperationInvalid:
67             error = OptionValue::SetValueFromString (value, op);
68             break;
69     }
70     return error;
71 }
72 
73 lldb::OptionValueSP
74 OptionValueUUID::DeepCopy () const
75 {
76     return OptionValueSP(new OptionValueUUID(*this));
77 }
78 
79 size_t
80 OptionValueUUID::AutoComplete (CommandInterpreter &interpreter,
81                                const char *s,
82                                int match_start_point,
83                                int max_return_elements,
84                                bool &word_complete,
85                                StringList &matches)
86 {
87     word_complete = false;
88     matches.Clear();
89     ExecutionContext exe_ctx(interpreter.GetExecutionContext());
90     Target *target = exe_ctx.GetTargetPtr();
91     if (target)
92     {
93         const size_t num_modules = target->GetImages().GetSize();
94         if (num_modules > 0)
95         {
96             UUID::ValueType uuid_bytes;
97             const size_t num_bytes_decoded = UUID::DecodeUUIDBytesFromCString(s, uuid_bytes, nullptr);
98             for (size_t i=0; i<num_modules; ++i)
99             {
100                 ModuleSP module_sp (target->GetImages().GetModuleAtIndex(i));
101                 if (module_sp)
102                 {
103                     const UUID &module_uuid = module_sp->GetUUID();
104                     if (module_uuid.IsValid())
105                     {
106                         bool add_uuid = false;
107                         if (num_bytes_decoded == 0)
108                             add_uuid = true;
109                         else
110                             add_uuid = ::memcmp(module_uuid.GetBytes(), uuid_bytes, num_bytes_decoded) == 0;
111                         if (add_uuid)
112                         {
113                             std::string uuid_str;
114                             uuid_str = module_uuid.GetAsString();
115                             if (!uuid_str.empty())
116                                 matches.AppendString(uuid_str.c_str());
117                         }
118                     }
119                 }
120             }
121         }
122     }
123     return matches.GetSize();
124 }
125 
126