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