summaryrefslogtreecommitdiffstats
path: root/lldb/source/Commands/CommandObjectCall.cpp
blob: 58d0a0e9f110136be5dbbb4bad61b0ef12026536 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//===-- CommandObjectCall.cpp -----------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "CommandObjectCall.h"

// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Core/Args.h"
#include "lldb/Core/Value.h"
#include "lldb/Expression/ClangExpression.h"
#include "lldb/Expression/ClangExpressionVariable.h"
#include "lldb/Expression/ClangFunction.h"
#include "lldb/Host/Host.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandContext.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/Variable.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/StackFrame.h"

using namespace lldb;
using namespace lldb_private;

// This command is a toy.  I'm just using it to have a way to construct the arguments to
// calling functions.
//

CommandObjectCall::CommandOptions::CommandOptions () :
    Options()
{
    // Keep only one place to reset the values to their defaults
    ResetOptionValues();
}


CommandObjectCall::CommandOptions::~CommandOptions ()
{
}

Error
CommandObjectCall::CommandOptions::SetOptionValue (int option_idx, const char *option_arg)
{
    Error error;

    char short_option = (char) m_getopt_table[option_idx].val;

    switch (short_option)
    {
    case 'l':
        if (language.SetLanguageFromCString (option_arg) == false)
        {
            error.SetErrorStringWithFormat("Invalid language option argument '%s'.\n", option_arg);
        }
        break;

    case 'g':
        debug = true;
        break;

    case 'f':
        error = Args::StringToFormat(option_arg,format);
        break;

    case 'n':
        noexecute = true;
        break;
            
    case 'a':
        use_abi = true;
        break;
            
    default:
        error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
        break;
    }

    return error;
}

void
CommandObjectCall::CommandOptions::ResetOptionValues ()
{
    Options::ResetOptionValues();
    language.Clear();
    debug = false;
    format = eFormatDefault;
    show_types = true;
    show_summary = true;
    noexecute = false;
    use_abi = false;
}

const lldb::OptionDefinition*
CommandObjectCall::CommandOptions::GetDefinitions ()
{
    return g_option_table;
}

CommandObjectCall::CommandObjectCall () :
    CommandObject (
            "call",
            "Call a function.",
            "call <return_type> <function-name> [[<arg1-type> <arg1-value>] ... <argn-type> <argn-value>] [<cmd-options>]",
            eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
{
}

CommandObjectCall::~CommandObjectCall ()
{
}

Options *
CommandObjectCall::GetOptions ()
{
    return &m_options;
}

bool
CommandObjectCall::Execute
(
    Args &command,
    CommandContext *context,
    CommandInterpreter *interpreter,
    CommandReturnObject &result
)
{
    ConstString target_triple;
    int num_args = command.GetArgumentCount();

    Target *target = context->GetTarget ();
    if (target)
        target->GetTargetTriple(target_triple);

    if (!target_triple)
        target_triple = Host::GetTargetTriple ();

    ExecutionContext exe_ctx(context->GetExecutionContext());
    if (exe_ctx.thread == NULL || exe_ctx.frame == NULL)
    {
        result.AppendError ("No currently selected thread and frame.");
        result.SetStatus (eReturnStatusFailed);
        return false;
    }

    if (num_args < 2)
    {
        result.AppendErrorWithFormat ("Invalid usage, should be: %s.\n", GetSyntax());
        result.SetStatus (eReturnStatusFailed);
        return false;
    }

    if ((num_args - 2) %2 != 0)
    {
        result.AppendErrorWithFormat ("Invalid usage - unmatched args & types, should be: %s.\n", GetSyntax());
        result.SetStatus (eReturnStatusFailed);
        return false;
    }

    if (target_triple)
    {
        //const char *return_type = command.GetArgumentAtIndex(0);
        const char *function_name = command.GetArgumentAtIndex(1);
        // Look up the called function:

        Function *target_fn = exe_ctx.frame->GetSymbolContext(eSymbolContextEverything).FindFunctionByName (function_name);

        // FIXME: If target_fn is NULL, we should look up the name as a symbol and use it and the provided
        // return type.

        if (target_fn == NULL)
        {
            result.AppendErrorWithFormat ("Could not find function '%s'.\n", function_name);
            result.SetStatus (eReturnStatusFailed);
            return false;
        }

        ValueList value_list;
        // Okay, now parse arguments.  For now we only accept basic types.
        for (int i = 2; i < num_args; i+= 2)
        {
            const char *type_str = command.GetArgumentAtIndex(i);
            const char *value_str = command.GetArgumentAtIndex(i + 1);
            bool success;
            if (strcmp(type_str, "int") == 0
                || strcmp(type_str, "int32_t") == 0)
            {
                value_list.PushValue(Value(Args::StringToSInt32(value_str, 0, 0, &success)));
            }
            else if (strcmp (type_str, "int64_t") == 0)
            {
                value_list.PushValue(Value(Args::StringToSInt64(value_str, 0, 0, &success)));
            }
            else if (strcmp(type_str, "uint") == 0
                || strcmp(type_str, "uint32_t") == 0)
            {
                value_list.PushValue(Value(Args::StringToUInt32(value_str, 0, 0, &success)));
            }
            else if (strcmp (type_str, "uint64_t") == 0)
            {
                value_list.PushValue(Value(Args::StringToUInt64(value_str, 0, 0, &success)));
            }
            else if (strcmp (type_str, "cstr") == 0)
            {
                Value val ((intptr_t)value_str);
                val.SetValueType (Value::eValueTypeHostAddress);
                
                
                void *cstr_type = target->GetScratchClangASTContext()->GetCStringType(true);
                val.SetContext (Value::eContextTypeOpaqueClangQualType, cstr_type);
                value_list.PushValue(val);
                
                success = true;
            }

            if (!success)
            {
                result.AppendErrorWithFormat ("Could not convert value: '%s' to type '%s'.\n", value_str, type_str);
                result.SetStatus (eReturnStatusFailed);
                return false;
            }
        }
        // Okay, we have the function and the argument list and the return type.  Now make a ClangFunction object and
        // run it:

        StreamString errors;
        ClangFunction clang_fun (target_triple.GetCString(), *target_fn, target->GetScratchClangASTContext(), value_list);
        if (m_options.noexecute)
        {
            // Now write down the argument values for this call.
            lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
            if (!clang_fun.InsertFunction (exe_ctx, args_addr, errors))
            {
                result.AppendErrorWithFormat("Error inserting function: '%s'.\n", errors.GetData());
                result.SetStatus (eReturnStatusFailed);
                return false;
            }
            else
            {
                result.Succeeded();
                return true;
            }
        }
        
        ClangFunction::ExecutionResults return_status;
        Value return_value;
        
        if (m_options.use_abi)
        {
            return_status = clang_fun.ExecuteFunctionWithABI(exe_ctx, errors, return_value);
        }
        else 
        {
            bool stop_others = true;
            return_status = clang_fun.ExecuteFunction(exe_ctx, errors, stop_others, NULL, return_value);
        }

        // Now figure out what to do with the return value.
        if (return_status == ClangFunction::eExecutionSetupError)
        {
            result.AppendErrorWithFormat("Error setting up function execution: '%s'.\n", errors.GetData());
            result.SetStatus (eReturnStatusFailed);
            return false;
        }
        else if (return_status != ClangFunction::eExecutionCompleted)
        {
            result.AppendWarningWithFormat("Interrupted while calling function: '%s'.\n", errors.GetData());
            result.SetStatus(eReturnStatusSuccessFinishNoResult);
            return true;
        }
        else
        {
            // Now print out the result.
            result.GetOutputStream().Printf("Return value: ");
            return_value.Dump(&(result.GetOutputStream()));
            result.Succeeded();
        }

    }
    else
    {
        result.AppendError ("invalid target triple");
        result.SetStatus (eReturnStatusFailed);
    }
    return result.Succeeded();
}

lldb::OptionDefinition
CommandObjectCall::CommandOptions::g_option_table[] =
{
{ 0, true,  "language",   'l', required_argument, NULL, 0, "[c|c++|objc|objc++]",          "Sets the language to use when parsing the expression."},
{ 0, false, "format",     'f', required_argument, NULL, 0, "[ [bool|b] | [bin] | [char|c] | [oct|o] | [dec|i|d|u] | [hex|x] | [float|f] | [cstr|s] ]",  "Specify the format that the expression output should use."},
{ 0, false, "debug",      'g', no_argument,       NULL, 0, NULL,                           "Enable verbose debug logging of the expression parsing and evaluation."},
{ 0, false, "noexecute",  'n', no_argument,       NULL, 0, "no execute",                   "Only JIT and copy the wrapper & arguments, but don't execute."},
{ 0, false, "useabi",     'a', no_argument,       NULL, 0, NULL,                           "Use the ABI instead of the JIT to marshall arguments."},
{ 0, false, NULL, 0, 0, NULL, NULL, NULL, NULL }
};

OpenPOWER on IntegriCloud