CommandObjectMemory.cpp revision 42077d90baa272e1be560884a6b115d1264566be
124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//===-- CommandObjectMemory.cpp ---------------------------------*- C++ -*-===//
224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//
324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//                     The LLVM Compiler Infrastructure
424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//
524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// This file is distributed under the University of Illinois Open Source
624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// License. See LICENSE.TXT for details.
724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//
824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//===----------------------------------------------------------------------===//
924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
10d891f9b872103235cfd2ed452c6f14a4394d9b3aDaniel Malea#include "lldb/lldb-python.h"
11d891f9b872103235cfd2ed452c6f14a4394d9b3aDaniel Malea
1224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "CommandObjectMemory.h"
1324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
1424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// C Includes
1524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// C++ Includes
1624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// Other libraries and framework includes
1724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// Project includes
1824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Core/DataBufferHeap.h"
1924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Core/DataExtractor.h"
2063094e0bb161580564954dee512955c1c79d3476Greg Clayton#include "lldb/Core/Debugger.h"
2149ce8969d3154e1560106cfe530444c09410f217Greg Clayton#include "lldb/Core/Module.h"
2224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Core/StreamString.h"
2357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton#include "lldb/Core/ValueObjectMemory.h"
2463094e0bb161580564954dee512955c1c79d3476Greg Clayton#include "lldb/Interpreter/Args.h"
2524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Interpreter/CommandReturnObject.h"
2663094e0bb161580564954dee512955c1c79d3476Greg Clayton#include "lldb/Interpreter/CommandInterpreter.h"
2763094e0bb161580564954dee512955c1c79d3476Greg Clayton#include "lldb/Interpreter/Options.h"
2857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton#include "lldb/Interpreter/OptionGroupFormat.h"
2957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton#include "lldb/Interpreter/OptionGroupOutputFile.h"
3056bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton#include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
3173844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton#include "lldb/Interpreter/OptionValueString.h"
3249ce8969d3154e1560106cfe530444c09410f217Greg Clayton#include "lldb/Symbol/TypeList.h"
3324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Target/Process.h"
3457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton#include "lldb/Target/StackFrame.h"
3524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
3624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerusing namespace lldb;
3724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerusing namespace lldb_private;
3824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
3956bbdaf817cb19a2f133e8501473f499be447c2dGreg Claytonstatic OptionDefinition
4057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Claytong_option_table[] =
4124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
4257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    { LLDB_OPT_SET_1, false, "num-per-line" ,'l', required_argument, NULL, 0, eArgTypeNumberPerLine ,"The number of items per line to display."},
4357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    { LLDB_OPT_SET_2, false, "binary"       ,'b', no_argument      , NULL, 0, eArgTypeNone          ,"If true, memory will be saved as binary. If false, the memory is saved save as an ASCII dump that uses the format, size, count and number per line settings."},
4406dc17f8eb821d7256fd42e56f85c2779a29f689Sean Callanan    { LLDB_OPT_SET_3, true , "type"         ,'t', required_argument, NULL, 0, eArgTypeNone          ,"The name of a type to view memory as."},
45827baf6b629b389f5fdca096f70a249f4143f13bGreg Clayton    { LLDB_OPT_SET_1|
46827baf6b629b389f5fdca096f70a249f4143f13bGreg Clayton      LLDB_OPT_SET_2|
47827baf6b629b389f5fdca096f70a249f4143f13bGreg Clayton      LLDB_OPT_SET_3, false, "force"        ,'r', no_argument,       NULL, 0, eArgTypeNone          ,"Necessary if reading over 1024 bytes of memory."},
4857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton};
4924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
5024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
5124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
5257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Claytonclass OptionGroupReadMemory : public OptionGroup
5357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton{
5457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Claytonpublic:
5524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
5657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    OptionGroupReadMemory () :
5756bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton        m_num_per_line (1,1),
5857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        m_output_as_binary (false),
5957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        m_view_as_type()
6057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    {
6157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    }
6224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
6357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    virtual
6457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    ~OptionGroupReadMemory ()
6557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    {
6657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    }
6757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
6857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
6957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    virtual uint32_t
7057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    GetNumDefinitions ()
7157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    {
7257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        return sizeof (g_option_table) / sizeof (OptionDefinition);
7357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    }
7457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
7557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    virtual const OptionDefinition*
7657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    GetDefinitions ()
7757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    {
7857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        return g_option_table;
7957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    }
8057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
8157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    virtual Error
8257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    SetOptionValue (CommandInterpreter &interpreter,
8357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    uint32_t option_idx,
8457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    const char *option_arg)
8557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    {
8657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        Error error;
876475c42148a8ea1ca86e5db465db7eca742d897dGreg Clayton        const int short_option = g_option_table[option_idx].short_option;
8857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
8957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        switch (short_option)
9057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        {
9124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case 'l':
9257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                error = m_num_per_line.SetValueFromCString (option_arg);
9357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                if (m_num_per_line.GetCurrentValue() == 0)
949c236733d43e6250c8a5671a438f4a2afeb9c0b2Greg Clayton                    error.SetErrorStringWithFormat("invalid value for --num-per-line option '%s'", option_arg);
9524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                break;
96a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton
97e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            case 'b':
98e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                m_output_as_binary = true;
99e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                break;
10057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
10157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case 't':
10257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                error = m_view_as_type.SetValueFromCString (option_arg);
103e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                break;
1048a6f3e9fd6d9a12b4f0153357a7e1f5c66455586Sean Callanan
1058a6f3e9fd6d9a12b4f0153357a7e1f5c66455586Sean Callanan            case 'r':
1068a6f3e9fd6d9a12b4f0153357a7e1f5c66455586Sean Callanan                m_force = true;
1078a6f3e9fd6d9a12b4f0153357a7e1f5c66455586Sean Callanan                break;
10857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
10924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            default:
1109c236733d43e6250c8a5671a438f4a2afeb9c0b2Greg Clayton                error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
11124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                break;
11224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
11357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        return error;
11457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    }
11557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
11657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    virtual void
11757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    OptionParsingStarting (CommandInterpreter &interpreter)
11857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    {
11957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        m_num_per_line.Clear();
12057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        m_output_as_binary = false;
12157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        m_view_as_type.Clear();
12257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    }
12357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
12456bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton    Error
125a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton    FinalizeSettings (Target *target, OptionGroupFormat& format_options)
12657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    {
12756bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton        Error error;
128a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        OptionValueUInt64 &byte_size_value = format_options.GetByteSizeValue();
129a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        OptionValueUInt64 &count_value = format_options.GetCountValue();
1309c236733d43e6250c8a5671a438f4a2afeb9c0b2Greg Clayton        const bool byte_size_option_set = byte_size_value.OptionWasSet();
13156bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton        const bool num_per_line_option_set = m_num_per_line.OptionWasSet();
132a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        const bool count_option_set = format_options.GetCountValue().OptionWasSet();
13356bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton
13456bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton        switch (format_options.GetFormat())
13524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
13657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            default:
13757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                break;
13857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
13957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatBoolean:
14056bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!byte_size_option_set)
141a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    byte_size_value = 1;
14256bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!num_per_line_option_set)
14356bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                    m_num_per_line = 1;
14456bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!count_option_set)
145a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    format_options.GetCountValue() = 8;
14657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                break;
14757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
14857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatCString:
14957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                break;
15024a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton
15124a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton            case eFormatInstruction:
15224a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton                if (count_option_set)
153ab885839103d4b9a72de165092d3bec35094683bJim Ingham                    byte_size_value = target->GetArchitecture().GetMaximumOpcodeByteSize();
15424a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton                m_num_per_line = 1;
15524a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton                break;
15624a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton
15724a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton            case eFormatAddressInfo:
15824a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton                if (!byte_size_option_set)
15924a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton                    byte_size_value = target->GetArchitecture().GetAddressByteSize();
16024a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton                m_num_per_line = 1;
16124a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton                if (!count_option_set)
16224a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton                    format_options.GetCountValue() = 8;
16324a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton                break;
16424a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton
16557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatPointer:
166a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                byte_size_value = target->GetArchitecture().GetAddressByteSize();
16756bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!num_per_line_option_set)
16856bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                    m_num_per_line = 4;
16956bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!count_option_set)
170a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    format_options.GetCountValue() = 8;
17157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                break;
17257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
17357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatBinary:
17457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatFloat:
17557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatOctal:
17657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatDecimal:
17757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatEnum:
17857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatUnicode16:
17957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatUnicode32:
18057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatUnsigned:
18124a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton            case eFormatHexFloat:
18256bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!byte_size_option_set)
183a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    byte_size_value = 4;
18456bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!num_per_line_option_set)
18556bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                    m_num_per_line = 1;
18656bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!count_option_set)
187a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    format_options.GetCountValue() = 8;
18857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                break;
18924a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton
19057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatBytes:
19157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatBytesWithASCII:
1929c236733d43e6250c8a5671a438f4a2afeb9c0b2Greg Clayton                if (byte_size_option_set)
19356bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                {
194a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    if (byte_size_value > 1)
1955f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea                        error.SetErrorStringWithFormat ("display format (bytes/bytes with ascii) conflicts with the specified byte size %" PRIu64 "\n"
196bf9ba5979ccc1a0058ac76cd91dafa493c55854cJohnny Chen                                                        "\tconsider using a different display format or don't specify the byte size",
197bf9ba5979ccc1a0058ac76cd91dafa493c55854cJohnny Chen                                                        byte_size_value.GetCurrentValue());
19856bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                }
19956bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                else
200a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    byte_size_value = 1;
20156bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!num_per_line_option_set)
20256bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                    m_num_per_line = 16;
20356bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!count_option_set)
204a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    format_options.GetCountValue() = 32;
20557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                break;
206307fa07606d519d427c812802aff5f9727e7047cGreg Clayton            case eFormatCharArray:
20757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatChar:
20857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatCharPrintable:
20956bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!byte_size_option_set)
210a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    byte_size_value = 1;
21156bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!num_per_line_option_set)
21256bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                    m_num_per_line = 32;
21356bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!count_option_set)
214a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    format_options.GetCountValue() = 64;
21557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                break;
21657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatComplex:
21756bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!byte_size_option_set)
218a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    byte_size_value = 8;
21956bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!num_per_line_option_set)
22056bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                    m_num_per_line = 1;
22156bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!count_option_set)
222a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    format_options.GetCountValue() = 8;
22342077d90baa272e1be560884a6b115d1264566beJason Molenda                break;
22442077d90baa272e1be560884a6b115d1264566beJason Molenda            case eFormatComplexInteger:
22542077d90baa272e1be560884a6b115d1264566beJason Molenda                if (!byte_size_option_set)
22642077d90baa272e1be560884a6b115d1264566beJason Molenda                    byte_size_value = 8;
22742077d90baa272e1be560884a6b115d1264566beJason Molenda                if (!num_per_line_option_set)
22842077d90baa272e1be560884a6b115d1264566beJason Molenda                    m_num_per_line = 1;
22942077d90baa272e1be560884a6b115d1264566beJason Molenda                if (!count_option_set)
23042077d90baa272e1be560884a6b115d1264566beJason Molenda                    format_options.GetCountValue() = 8;
23157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                break;
23257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatHex:
23356bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!byte_size_option_set)
234a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    byte_size_value = 4;
23556bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!num_per_line_option_set)
23656bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                {
237a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    switch (byte_size_value)
23856bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                    {
23956bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                        case 1:
24056bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                        case 2:
24156bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                            m_num_per_line = 8;
24256bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                            break;
24356bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                        case 4:
24456bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                            m_num_per_line = 4;
24556bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                            break;
24656bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                        case 8:
24756bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                            m_num_per_line = 2;
24856bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                            break;
24956bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                        default:
25056bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                            m_num_per_line = 1;
25156bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                            break;
25256bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                    }
25356bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                }
25456bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!count_option_set)
255a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    count_value = 8;
25657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                break;
25757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
25857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatVectorOfChar:
25957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatVectorOfSInt8:
26057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatVectorOfUInt8:
26157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatVectorOfSInt16:
26257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatVectorOfUInt16:
26357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatVectorOfSInt32:
26457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatVectorOfUInt32:
26557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatVectorOfSInt64:
26657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatVectorOfUInt64:
26757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatVectorOfFloat32:
26857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatVectorOfFloat64:
26957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            case eFormatVectorOfUInt128:
27056bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!byte_size_option_set)
271a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    byte_size_value = 128;
27256bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!num_per_line_option_set)
27356bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                    m_num_per_line = 1;
27456bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                if (!count_option_set)
275a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    count_value = 4;
27657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                break;
27724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
27856bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton        return error;
27957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    }
28024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
281902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton    bool
282902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton    AnyOptionWasSet () const
283902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton    {
284902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        return m_num_per_line.OptionWasSet() ||
285902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton               m_output_as_binary ||
286902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton               m_view_as_type.OptionWasSet();
287902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton    }
288902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton
28957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    OptionValueUInt64 m_num_per_line;
29057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    bool m_output_as_binary;
29157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    OptionValueString m_view_as_type;
2928a6f3e9fd6d9a12b4f0153357a7e1f5c66455586Sean Callanan    bool m_force;
29357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton};
29424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
29524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
29624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
29757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton//----------------------------------------------------------------------
29857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton// Read memory from the inferior process
29957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton//----------------------------------------------------------------------
300da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectMemoryRead : public CommandObjectParsed
30157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton{
30257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Claytonpublic:
30357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
304238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    CommandObjectMemoryRead (CommandInterpreter &interpreter) :
305da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectParsed (interpreter,
306da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "memory read",
307da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "Read from the memory of the process being debugged.",
308da26bd203cbb104291b39891febf7481794f205fJim Ingham                             NULL,
309ea0bb4d45c14447a2fe9be9f4bd8e79a84513537Greg Clayton                             eFlagRequiresTarget | eFlagProcessMustBePaused),
31057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        m_option_group (interpreter),
311a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        m_format_options (eFormatBytesWithASCII, 1, 8),
31257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        m_memory_options (),
31356bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton        m_outfile_options (),
314902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        m_varobj_options(),
315902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        m_next_addr(LLDB_INVALID_ADDRESS),
316902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        m_prev_byte_size(0),
317902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        m_prev_format_options (eFormatBytesWithASCII, 1, 8),
318902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        m_prev_memory_options (),
319902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        m_prev_outfile_options (),
320902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        m_prev_varobj_options()
32124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
32243b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentEntry arg1;
32343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentEntry arg2;
32443b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentData start_addr_arg;
32543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentData end_addr_arg;
32643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
32743b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Define the first (and only) variant of this arg.
3284968ad817597891cc02c195d4b3e7c3cde1fc391Enrico Granata        start_addr_arg.arg_type = eArgTypeAddressOrExpression;
32943b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        start_addr_arg.arg_repetition = eArgRepeatPlain;
33043b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
33143b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // There is only one variant this argument could be; put it into the argument entry.
33243b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        arg1.push_back (start_addr_arg);
33343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
33443b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Define the first (and only) variant of this arg.
3354968ad817597891cc02c195d4b3e7c3cde1fc391Enrico Granata        end_addr_arg.arg_type = eArgTypeAddressOrExpression;
33643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        end_addr_arg.arg_repetition = eArgRepeatOptional;
33743b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
33843b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // There is only one variant this argument could be; put it into the argument entry.
33943b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        arg2.push_back (end_addr_arg);
34043b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
34143b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Push the data for the first argument into the m_arguments vector.
34243b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        m_arguments.push_back (arg1);
34343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        m_arguments.push_back (arg2);
34457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
345a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        // Add the "--format" and "--count" options to group 1 and 3
346a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        m_option_group.Append (&m_format_options,
347a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                               OptionGroupFormat::OPTION_GROUP_FORMAT | OptionGroupFormat::OPTION_GROUP_COUNT,
34831feaa8b346312b7935e9424e9ae22959bf46b02Greg Clayton                               LLDB_OPT_SET_1 | LLDB_OPT_SET_2 | LLDB_OPT_SET_3);
34924a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton        m_option_group.Append (&m_format_options,
35024a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton                               OptionGroupFormat::OPTION_GROUP_GDB_FMT,
35131feaa8b346312b7935e9424e9ae22959bf46b02Greg Clayton                               LLDB_OPT_SET_1 | LLDB_OPT_SET_3);
352a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        // Add the "--size" option to group 1 and 2
353a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        m_option_group.Append (&m_format_options,
354a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                               OptionGroupFormat::OPTION_GROUP_SIZE,
355a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                               LLDB_OPT_SET_1 | LLDB_OPT_SET_2);
35657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        m_option_group.Append (&m_memory_options);
35757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        m_option_group.Append (&m_outfile_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1 | LLDB_OPT_SET_2 | LLDB_OPT_SET_3);
35856bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton        m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_3);
35957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        m_option_group.Finalize();
36024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
36124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
36224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    virtual
36324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    ~CommandObjectMemoryRead ()
36424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
36524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
36624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
36724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    Options *
36824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    GetOptions ()
36924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
37057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        return &m_option_group;
37124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
37224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
373902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton    virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
374902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton    {
375902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        return m_cmd_name.c_str();
376902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton    }
377902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton
378da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
37924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    virtual bool
38049d888d8c3e4de1d2f38d541559f533f1c0bcab3Greg Clayton    DoExecute (Args& command, CommandReturnObject &result)
38124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
382ea0bb4d45c14447a2fe9be9f4bd8e79a84513537Greg Clayton        // No need to check "target" for validity as eFlagRequiresTarget ensures it is valid
383ea0bb4d45c14447a2fe9be9f4bd8e79a84513537Greg Clayton        Target *target = m_exe_ctx.GetTargetPtr();
384ea0bb4d45c14447a2fe9be9f4bd8e79a84513537Greg Clayton
38524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        const size_t argc = command.GetArgumentCount();
38624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
387902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        if ((argc == 0 && m_next_addr == LLDB_INVALID_ADDRESS) || argc > 2)
38824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
38949d888d8c3e4de1d2f38d541559f533f1c0bcab3Greg Clayton            result.AppendErrorWithFormat ("%s takes a start address expression with an optional end address expression.\n", m_cmd_name.c_str());
390bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton            result.AppendRawWarning("Expressions should be quoted if they contain spaces or other special characters.\n");
39124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            result.SetStatus(eReturnStatusFailed);
39224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            return false;
39324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
39424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
39556bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton        ClangASTType clang_ast_type;
39656bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton        Error error;
39756bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton
39857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        const char *view_as_type_cstr = m_memory_options.m_view_as_type.GetCurrentValue();
39957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        if (view_as_type_cstr && view_as_type_cstr[0])
40024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
40157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            // We are viewing memory as a type
402bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton
40357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            SymbolContext sc;
404dc0a38c5a727cae5362b218a3180d0f4265a619dGreg Clayton            const bool exact_match = false;
40557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            TypeList type_list;
40657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            uint32_t reference_count = 0;
40757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            uint32_t pointer_count = 0;
40857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            size_t idx;
409ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan
410ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan#define ALL_KEYWORDS        \
411ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan    KEYWORD("const")        \
412ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan    KEYWORD("volatile")     \
413ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan    KEYWORD("restrict")     \
414ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan    KEYWORD("struct")       \
415ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan    KEYWORD("class")        \
416ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan    KEYWORD("union")
417ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan
418ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan#define KEYWORD(s) s,
419ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan            static const char *g_keywords[] =
420ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan            {
421ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan                ALL_KEYWORDS
422ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan            };
423ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan#undef KEYWORD
424ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan
425ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan#define KEYWORD(s) (sizeof(s) - 1),
426ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan            static const int g_keyword_lengths[] =
427ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan            {
428ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan                ALL_KEYWORDS
429ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan            };
430ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan#undef KEYWORD
431ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan
432ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan#undef ALL_KEYWORDS
433ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan
434ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan            static size_t g_num_keywords = sizeof(g_keywords) / sizeof(const char *);
43557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            std::string type_str(view_as_type_cstr);
43657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
43757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            // Remove all instances of g_keywords that are followed by spaces
43857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            for (size_t i = 0; i < g_num_keywords; ++i)
43957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            {
44057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                const char *keyword = g_keywords[i];
441ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan                int keyword_len = g_keyword_lengths[i];
442ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan
443ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan                idx = 0;
444ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan                while ((idx = type_str.find (keyword, idx)) != std::string::npos)
44557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                {
44657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    if (type_str[idx + keyword_len] == ' ' || type_str[idx + keyword_len] == '\t')
447ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan                    {
44857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                        type_str.erase(idx, keyword_len+1);
449ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan                        idx = 0;
450ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan                    }
451ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan                    else
452ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan                    {
453ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan                        idx += keyword_len;
454ddb2ece9027de20442c691ca678449cfadb0bc86Sean Callanan                    }
45557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                }
45657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            }
45757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            bool done = type_str.empty();
45857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            //
45957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            idx = type_str.find_first_not_of (" \t");
46057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            if (idx > 0 && idx != std::string::npos)
46157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                type_str.erase (0, idx);
46257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            while (!done)
46357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            {
46457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                // Strip trailing spaces
46557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                if (type_str.empty())
46657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    done = true;
46757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                else
46857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                {
46957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    switch (type_str[type_str.size()-1])
47057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    {
47157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    case '*':
47257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                        ++pointer_count;
47357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                        // fall through...
47457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    case ' ':
47557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    case '\t':
47657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                        type_str.erase(type_str.size()-1);
47757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                        break;
47857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
47957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    case '&':
48057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                        if (reference_count == 0)
48157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                        {
48257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                            reference_count = 1;
48357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                            type_str.erase(type_str.size()-1);
48457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                        }
48557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                        else
48657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                        {
48757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                            result.AppendErrorWithFormat ("invalid type string: '%s'\n", view_as_type_cstr);
48857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                            result.SetStatus(eReturnStatusFailed);
48957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                            return false;
49057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                        }
49157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                        break;
49257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
49357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    default:
49457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                        done = true;
49557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                        break;
49657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    }
49757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                }
49857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            }
49957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
50057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            ConstString lookup_type_name(type_str.c_str());
501ea0bb4d45c14447a2fe9be9f4bd8e79a84513537Greg Clayton            StackFrame *frame = m_exe_ctx.GetFramePtr();
502567e7f3ba16eb48cb9fd6a2f26f2f7269eb6983cGreg Clayton            if (frame)
50357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            {
504567e7f3ba16eb48cb9fd6a2f26f2f7269eb6983cGreg Clayton                sc = frame->GetSymbolContext (eSymbolContextModule);
50557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                if (sc.module_sp)
50657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                {
5073e80cd9c9e6ae50ff54537551e2fe3ed5319b9b4Sean Callanan                    sc.module_sp->FindTypes (sc,
5083e80cd9c9e6ae50ff54537551e2fe3ed5319b9b4Sean Callanan                                             lookup_type_name,
509dc0a38c5a727cae5362b218a3180d0f4265a619dGreg Clayton                                             exact_match,
51057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                                             1,
51157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                                             type_list);
51257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                }
51357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            }
51457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            if (type_list.GetSize() == 0)
51557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            {
5169f95fb63a492b53206d578f46e73899d60d70321Greg Clayton                target->GetImages().FindTypes (sc,
517567e7f3ba16eb48cb9fd6a2f26f2f7269eb6983cGreg Clayton                                               lookup_type_name,
518dc0a38c5a727cae5362b218a3180d0f4265a619dGreg Clayton                                               exact_match,
519567e7f3ba16eb48cb9fd6a2f26f2f7269eb6983cGreg Clayton                                               1,
520567e7f3ba16eb48cb9fd6a2f26f2f7269eb6983cGreg Clayton                                               type_list);
52157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            }
52257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
52357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            if (type_list.GetSize() == 0)
52457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            {
52557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                result.AppendErrorWithFormat ("unable to find any types that match the raw type '%s' for full type '%s'\n",
52657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                                              lookup_type_name.GetCString(),
52757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                                              view_as_type_cstr);
52857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                result.SetStatus(eReturnStatusFailed);
52957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                return false;
53057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            }
53157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
53257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            TypeSP type_sp (type_list.GetTypeAtIndex(0));
53357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            clang_ast_type.SetClangType (type_sp->GetClangAST(), type_sp->GetClangFullType());
53457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
53557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            while (pointer_count > 0)
53657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            {
53757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                clang_type_t pointer_type = ClangASTContext::CreatePointerType (clang_ast_type.GetASTContext(), clang_ast_type.GetOpaqueQualType());
53857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                if (pointer_type)
53957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    clang_ast_type.SetClangType (clang_ast_type.GetASTContext(), pointer_type);
54057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                else
54157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                {
54257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    result.AppendError ("unable make a pointer type\n");
54357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    result.SetStatus(eReturnStatusFailed);
54457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    return false;
54557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                }
54657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                --pointer_count;
54757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            }
54857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
549fe6dc6e241c52822710380cec0931351a1d7b2d3Greg Clayton            m_format_options.GetByteSizeValue() = clang_ast_type.GetClangTypeByteSize();
55057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
551a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton            if (m_format_options.GetByteSizeValue() == 0)
55257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            {
55357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                result.AppendErrorWithFormat ("unable to get the byte size of the type '%s'\n",
55457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                                              view_as_type_cstr);
55557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                result.SetStatus(eReturnStatusFailed);
55657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                return false;
55757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            }
55856bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton
559a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton            if (!m_format_options.GetCountValue().OptionWasSet())
560a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                m_format_options.GetCountValue() = 1;
56157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        }
56257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        else
56357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        {
564567e7f3ba16eb48cb9fd6a2f26f2f7269eb6983cGreg Clayton            error = m_memory_options.FinalizeSettings (target, m_format_options);
56524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
56624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
56756bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton        // Look for invalid combinations of settings
56856bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton        if (error.Fail())
56924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
57049d888d8c3e4de1d2f38d541559f533f1c0bcab3Greg Clayton            result.AppendError(error.AsCString());
57156bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton            result.SetStatus(eReturnStatusFailed);
57256bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton            return false;
57324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
57424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
575902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        lldb::addr_t addr;
576902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        size_t total_byte_size = 0;
577902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        if (argc == 0)
578902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        {
579902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton            // Use the last address and byte size and all options as they were
580902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton            // if no options have been set
581902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton            addr = m_next_addr;
582902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton            total_byte_size = m_prev_byte_size;
583bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton            clang_ast_type = m_prev_clang_ast_type;
584bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton            if (!m_format_options.AnyOptionWasSet() &&
585902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton                !m_memory_options.AnyOptionWasSet() &&
586902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton                !m_outfile_options.AnyOptionWasSet() &&
587902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton                !m_varobj_options.AnyOptionWasSet())
588902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton            {
589902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton                m_format_options = m_prev_format_options;
590902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton                m_memory_options = m_prev_memory_options;
591902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton                m_outfile_options = m_prev_outfile_options;
592902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton                m_varobj_options = m_prev_varobj_options;
593902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton            }
594902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        }
595902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton
596a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        size_t item_count = m_format_options.GetCountValue().GetCurrentValue();
5974535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata        size_t item_byte_size = m_format_options.GetByteSizeValue().GetCurrentValue();
59856bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton        const size_t num_per_line = m_memory_options.m_num_per_line.GetCurrentValue();
59956bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton
60024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        if (total_byte_size == 0)
601902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        {
602902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton            total_byte_size = item_count * item_byte_size;
603902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton            if (total_byte_size == 0)
604902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton                total_byte_size = 32;
605902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        }
60624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
607902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton        if (argc > 0)
608ea0bb4d45c14447a2fe9be9f4bd8e79a84513537Greg Clayton            addr = Args::StringToAddress(&m_exe_ctx, command.GetArgumentAtIndex(0), LLDB_INVALID_ADDRESS, &error);
60924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
61024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        if (addr == LLDB_INVALID_ADDRESS)
61124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
61249d888d8c3e4de1d2f38d541559f533f1c0bcab3Greg Clayton            result.AppendError("invalid start address expression.");
61349d888d8c3e4de1d2f38d541559f533f1c0bcab3Greg Clayton            result.AppendError(error.AsCString());
61424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            result.SetStatus(eReturnStatusFailed);
61524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            return false;
61624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
61724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
61824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        if (argc == 2)
61924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
620ea0bb4d45c14447a2fe9be9f4bd8e79a84513537Greg Clayton            lldb::addr_t end_addr = Args::StringToAddress(&m_exe_ctx, command.GetArgumentAtIndex(1), LLDB_INVALID_ADDRESS, 0);
62124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            if (end_addr == LLDB_INVALID_ADDRESS)
62224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            {
62349d888d8c3e4de1d2f38d541559f533f1c0bcab3Greg Clayton                result.AppendError("invalid end address expression.");
62449d888d8c3e4de1d2f38d541559f533f1c0bcab3Greg Clayton                result.AppendError(error.AsCString());
62524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                result.SetStatus(eReturnStatusFailed);
62624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                return false;
62724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            }
62824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            else if (end_addr <= addr)
62924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            {
6305f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea                result.AppendErrorWithFormat("end address (0x%" PRIx64 ") must be greater that the start address (0x%" PRIx64 ").\n", end_addr, addr);
63124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                result.SetStatus(eReturnStatusFailed);
63224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                return false;
63324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            }
634a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton            else if (m_format_options.GetCountValue().OptionWasSet())
63524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            {
6365f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea                result.AppendErrorWithFormat("specify either the end address (0x%" PRIx64 ") or the count (--count %lu), not both.\n", end_addr, item_count);
63724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                result.SetStatus(eReturnStatusFailed);
63824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                return false;
63924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            }
64024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
64124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            total_byte_size = end_addr - addr;
64224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            item_count = total_byte_size / item_byte_size;
64324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
64457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
6458a6f3e9fd6d9a12b4f0153357a7e1f5c66455586Sean Callanan        if (total_byte_size > 1024 && !m_memory_options.m_force)
6468a6f3e9fd6d9a12b4f0153357a7e1f5c66455586Sean Callanan        {
6478a6f3e9fd6d9a12b4f0153357a7e1f5c66455586Sean Callanan            result.AppendErrorWithFormat("Normally, \'memory read\' will not read over 1Kbyte of data.\n");
6488a6f3e9fd6d9a12b4f0153357a7e1f5c66455586Sean Callanan            result.AppendErrorWithFormat("Please use --force to override this restriction.\n");
6498a6f3e9fd6d9a12b4f0153357a7e1f5c66455586Sean Callanan            return false;
6508a6f3e9fd6d9a12b4f0153357a7e1f5c66455586Sean Callanan        }
6518a6f3e9fd6d9a12b4f0153357a7e1f5c66455586Sean Callanan
6524535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata
6534535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata
65457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        DataBufferSP data_sp;
65557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        size_t bytes_read = 0;
656bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton        if (clang_ast_type.GetOpaqueQualType())
657bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton        {
658bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton            // Make sure we don't display our type as ASCII bytes like the default memory read
659bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton            if (m_format_options.GetFormatValue().OptionWasSet() == false)
660bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton                m_format_options.GetFormatValue().SetCurrentValue(eFormatDefault);
661bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton
662bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton            bytes_read = clang_ast_type.GetTypeByteSize() * m_format_options.GetCountValue().GetCurrentValue();
663bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton        }
6644535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata        else if (m_format_options.GetFormatValue().GetCurrentValue() != eFormatCString)
66524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
66657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            data_sp.reset (new DataBufferHeap (total_byte_size, '\0'));
6673508c387c3f0c9ecc439d98048fd7694d41bab1bGreg Clayton            Address address(addr, NULL);
668567e7f3ba16eb48cb9fd6a2f26f2f7269eb6983cGreg Clayton            bytes_read = target->ReadMemory(address, false, data_sp->GetBytes (), data_sp->GetByteSize(), error);
66957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            if (bytes_read == 0)
67057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            {
67104e6ada409d714592c7ab18b5b5472a5229d63d2Greg Clayton                const char *error_cstr = error.AsCString();
67204e6ada409d714592c7ab18b5b5472a5229d63d2Greg Clayton                if (error_cstr && error_cstr[0])
67304e6ada409d714592c7ab18b5b5472a5229d63d2Greg Clayton                {
67404e6ada409d714592c7ab18b5b5472a5229d63d2Greg Clayton                    result.AppendError(error_cstr);
67504e6ada409d714592c7ab18b5b5472a5229d63d2Greg Clayton                }
67604e6ada409d714592c7ab18b5b5472a5229d63d2Greg Clayton                else
67704e6ada409d714592c7ab18b5b5472a5229d63d2Greg Clayton                {
6785f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea                    result.AppendErrorWithFormat("failed to read memory from 0x%" PRIx64 ".\n", addr);
67904e6ada409d714592c7ab18b5b5472a5229d63d2Greg Clayton                }
68057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                result.SetStatus(eReturnStatusFailed);
68157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                return false;
68257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            }
68357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
68457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            if (bytes_read < total_byte_size)
68540aa90314d979a60122ba13589a793f231a95dddJim Ingham                result.AppendWarningWithFormat("Not all bytes (%lu/%lu) were able to be read from 0x%" PRIx64 ".\n", bytes_read, total_byte_size, addr);
6864535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata        }
6874535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata        else
6884535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata        {
6894535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata            // we treat c-strings as a special case because they do not have a fixed size
6904535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata            if (m_format_options.GetByteSizeValue().OptionWasSet() && !m_format_options.HasGDBFormat())
6914535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                item_byte_size = m_format_options.GetByteSizeValue().GetCurrentValue();
6924535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata            else
6934535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                item_byte_size = target->GetMaximumSizeOfStringSummary();
6944535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata            if (!m_format_options.GetCountValue().OptionWasSet())
6954535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                item_count = 1;
6964535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata            data_sp.reset (new DataBufferHeap ((item_byte_size+1) * item_count, '\0')); // account for NULLs as necessary
6974535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata            uint8_t *data_ptr = data_sp->GetBytes();
6984535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata            auto data_addr = addr;
6994535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata            auto count = item_count;
7004535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata            item_count = 0;
7014535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata            while (item_count < count)
7024535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata            {
7034535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                std::string buffer;
7044535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                buffer.resize(item_byte_size+1,0);
7054535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                Error error;
7064535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                size_t read = target->ReadCStringFromMemory(data_addr, &buffer[0], item_byte_size+1, error);
7074535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                if (error.Fail())
7084535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                {
7094535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                    result.AppendErrorWithFormat("failed to read memory from 0x%" PRIx64 ".\n", addr);
7104535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                    result.SetStatus(eReturnStatusFailed);
7114535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                    return false;
7124535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                }
7134535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                if (item_byte_size == read)
7144535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                {
7154535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                    result.AppendWarningWithFormat("unable to find a NULL terminated string at 0x%" PRIx64 ".Consider increasing the maximum read length.\n", data_addr);
7164535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                    break;
7174535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                }
7184535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                read+=1; // account for final NULL byte
7194535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                memcpy(data_ptr, &buffer[0], read);
7204535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                data_ptr += read;
7214535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                data_addr += read;
7224535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                bytes_read += read;
7234535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata                item_count++; // if we break early we know we only read item_count strings
7244535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata            }
7254535891c699c40710e59d8ea966c4ad3ab19c077Enrico Granata            data_sp.reset(new DataBufferHeap(data_sp->GetBytes(),bytes_read+1));
72624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
72724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
728bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton        m_next_addr = addr + bytes_read;
729bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton        m_prev_byte_size = bytes_read;
730bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton        m_prev_format_options = m_format_options;
731bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton        m_prev_memory_options = m_memory_options;
732bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton        m_prev_outfile_options = m_outfile_options;
733bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton        m_prev_varobj_options = m_varobj_options;
734bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton        m_prev_clang_ast_type = clang_ast_type;
735bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton
736e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        StreamFile outfile_stream;
737e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        Stream *output_stream = NULL;
73857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        const FileSpec &outfile_spec = m_outfile_options.GetFile().GetCurrentValue();
73957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        if (outfile_spec)
740e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        {
741e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            char path[PATH_MAX];
74257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            outfile_spec.GetPath (path, sizeof(path));
74357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
74457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            uint32_t open_options = File::eOpenOptionWrite | File::eOpenOptionCanCreate;
74557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            const bool append = m_outfile_options.GetAppend().GetCurrentValue();
74657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            if (append)
74757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                open_options |= File::eOpenOptionAppend;
74857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
74957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            if (outfile_stream.GetFile ().Open (path, open_options).Success())
750e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            {
75157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                if (m_memory_options.m_output_as_binary)
752e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                {
75336da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton                    const size_t bytes_written = outfile_stream.Write (data_sp->GetBytes(), bytes_read);
754e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    if (bytes_written > 0)
755e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    {
75636da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton                        result.GetOutputStream().Printf ("%zi bytes %s to '%s'\n",
757e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                                                         bytes_written,
75857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                                                         append ? "appended" : "written",
759e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                                                         path);
760e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                        return true;
761e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    }
762e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    else
763e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    {
7645f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea                        result.AppendErrorWithFormat("Failed to write %" PRIu64 " bytes to '%s'.\n", (uint64_t)bytes_read, path);
765e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                        result.SetStatus(eReturnStatusFailed);
766e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                        return false;
767e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    }
768e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                }
769e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                else
770e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                {
771e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    // We are going to write ASCII to the file just point the
772e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    // output_stream to our outfile_stream...
773e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    output_stream = &outfile_stream;
774e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                }
775e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            }
776e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            else
777e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            {
77857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                result.AppendErrorWithFormat("Failed to open file '%s' for %s.\n", path, append ? "append" : "write");
779e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                result.SetStatus(eReturnStatusFailed);
780e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                return false;
781e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            }
782e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        }
783e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        else
784e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        {
785e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            output_stream = &result.GetOutputStream();
786e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        }
787e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton
78857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
789ea0bb4d45c14447a2fe9be9f4bd8e79a84513537Greg Clayton        ExecutionContextScope *exe_scope = m_exe_ctx.GetBestExecutionContextScope();
79057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        if (clang_ast_type.GetOpaqueQualType())
79157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        {
79257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            for (uint32_t i = 0; i<item_count; ++i)
79357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            {
79457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                addr_t item_addr = addr + (i * item_byte_size);
7953508c387c3f0c9ecc439d98048fd7694d41bab1bGreg Clayton                Address address (item_addr);
79657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                StreamString name_strm;
7975f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea                name_strm.Printf ("0x%" PRIx64, item_addr);
79824a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton                ValueObjectSP valobj_sp (ValueObjectMemory::Create (exe_scope,
79957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                                                                    name_strm.GetString().c_str(),
80057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                                                                    address,
80157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                                                                    clang_ast_type));
80257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                if (valobj_sp)
80357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                {
804bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton                    Format format = m_format_options.GetFormat();
80556bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                    if (format != eFormatDefault)
80656bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                        valobj_sp->SetFormat (format);
80756bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton
80856bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton                    bool scope_already_checked = true;
80957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
8103069c62fc7d3c0b857cd1e9269ff22011ed418fbEnrico Granata                    ValueObject::DumpValueObjectOptions options;
8113069c62fc7d3c0b857cd1e9269ff22011ed418fbEnrico Granata                    options.SetMaximumPointerDepth(m_varobj_options.ptr_depth)
8123069c62fc7d3c0b857cd1e9269ff22011ed418fbEnrico Granata                    .SetMaximumDepth(m_varobj_options.max_depth)
8133069c62fc7d3c0b857cd1e9269ff22011ed418fbEnrico Granata                    .SetShowLocation(m_varobj_options.show_location)
8143069c62fc7d3c0b857cd1e9269ff22011ed418fbEnrico Granata                    .SetShowTypes(m_varobj_options.show_types)
8153069c62fc7d3c0b857cd1e9269ff22011ed418fbEnrico Granata                    .SetUseObjectiveC(m_varobj_options.use_objc)
8163069c62fc7d3c0b857cd1e9269ff22011ed418fbEnrico Granata                    .SetScopeChecked(scope_already_checked)
8173069c62fc7d3c0b857cd1e9269ff22011ed418fbEnrico Granata                    .SetFlatOutput(m_varobj_options.flat_output)
818cf09f885c201becf51acc4a5cfac00b3df53f2a8Enrico Granata                    .SetUseSyntheticValue(m_varobj_options.be_raw ? false : m_varobj_options.use_synth)
8193069c62fc7d3c0b857cd1e9269ff22011ed418fbEnrico Granata                    .SetOmitSummaryDepth(m_varobj_options.be_raw ? UINT32_MAX : m_varobj_options.no_summary_depth)
8203069c62fc7d3c0b857cd1e9269ff22011ed418fbEnrico Granata                    .SetIgnoreCap(m_varobj_options.be_raw ? true : m_varobj_options.ignore_cap)
8213069c62fc7d3c0b857cd1e9269ff22011ed418fbEnrico Granata                    .SetFormat(format)
8223069c62fc7d3c0b857cd1e9269ff22011ed418fbEnrico Granata                    .SetSummary();
82357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    ValueObject::DumpValueObject (*output_stream,
82457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                                                  valobj_sp.get(),
8253069c62fc7d3c0b857cd1e9269ff22011ed418fbEnrico Granata                                                  options);
82657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                }
82757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                else
82857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                {
82957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    result.AppendErrorWithFormat ("failed to create a value object for: (%s) %s\n",
83057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                                                  view_as_type_cstr,
83157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                                                  name_strm.GetString().c_str());
83257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    result.SetStatus(eReturnStatusFailed);
83357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                    return false;
83457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton                }
83557b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            }
83657b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton            return true;
83757b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        }
83857b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
83957b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        result.SetStatus(eReturnStatusSuccessFinishResult);
84057b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton        DataExtractor data (data_sp,
841567e7f3ba16eb48cb9fd6a2f26f2f7269eb6983cGreg Clayton                            target->GetArchitecture().GetByteOrder(),
842567e7f3ba16eb48cb9fd6a2f26f2f7269eb6983cGreg Clayton                            target->GetArchitecture().GetAddressByteSize());
84357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
84457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton
845e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        assert (output_stream);
84636da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton        size_t bytes_dumped = data.Dump (output_stream,
84736da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton                                         0,
84836da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton                                         m_format_options.GetFormat(),
84936da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton                                         item_byte_size,
85036da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton                                         item_count,
85136da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton                                         num_per_line,
85236da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton                                         addr,
85336da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton                                         0,
85436da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton                                         0,
85536da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton                                         exe_scope);
856746979d1b0d500be02ad92550ab3e3c858ba517dGreg Clayton        m_next_addr = addr + bytes_dumped;
857e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        output_stream->EOL();
85824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        return true;
85924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
86024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
86157b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    OptionGroupOptions m_option_group;
86257b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    OptionGroupFormat m_format_options;
86357b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    OptionGroupReadMemory m_memory_options;
86457b3c6b12812b0a7a79f896855c787bd4d893ecbGreg Clayton    OptionGroupOutputFile m_outfile_options;
86556bbdaf817cb19a2f133e8501473f499be447c2dGreg Clayton    OptionGroupValueObjectDisplay m_varobj_options;
866902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton    lldb::addr_t m_next_addr;
867902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton    lldb::addr_t m_prev_byte_size;
868902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton    OptionGroupFormat m_prev_format_options;
869902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton    OptionGroupReadMemory m_prev_memory_options;
870902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton    OptionGroupOutputFile m_prev_outfile_options;
871902b5beee960a8861867f06588c12ac6eb55eb9dGreg Clayton    OptionGroupValueObjectDisplay m_prev_varobj_options;
872bad1c9e139932d5afa197f8c03e1262f4145da9eGreg Clayton    ClangASTType m_prev_clang_ast_type;
87324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner};
87424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
875a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton
876a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg ClaytonOptionDefinition
877a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Claytong_memory_write_option_table[] =
878a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton{
879a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton{ LLDB_OPT_SET_1, true,  "infile", 'i', required_argument, NULL, 0, eArgTypeFilename, "Write memory using the contents of a file."},
880a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton{ LLDB_OPT_SET_1, false, "offset", 'o', required_argument, NULL, 0, eArgTypeOffset,   "Start writng bytes from an offset within the input file."},
881a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton};
882a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton
883a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton
88424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//----------------------------------------------------------------------
88524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// Write memory to the inferior process
88624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//----------------------------------------------------------------------
887da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectMemoryWrite : public CommandObjectParsed
88824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
88924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerpublic:
89024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
891a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton    class OptionGroupWriteMemory : public OptionGroup
89224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
89324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    public:
894a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        OptionGroupWriteMemory () :
895a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton            OptionGroup()
89624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
89724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
89824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
89924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        virtual
900a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        ~OptionGroupWriteMemory ()
90124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
90224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
90324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
904a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        virtual uint32_t
905a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        GetNumDefinitions ()
906a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        {
907a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton            return sizeof (g_memory_write_option_table) / sizeof (OptionDefinition);
908a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        }
909a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton
910a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        virtual const OptionDefinition*
911a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        GetDefinitions ()
912a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        {
913a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton            return g_memory_write_option_table;
914a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        }
915a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton
91624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        virtual Error
917a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        SetOptionValue (CommandInterpreter &interpreter,
918a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                        uint32_t option_idx,
919a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                        const char *option_arg)
92024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
92124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            Error error;
9226475c42148a8ea1ca86e5db465db7eca742d897dGreg Clayton            const int short_option = g_memory_write_option_table[option_idx].short_option;
923a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton
92424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            switch (short_option)
92524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            {
926a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                case 'i':
927a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    m_infile.SetFile (option_arg, true);
928a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    if (!m_infile.Exists())
929e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    {
930a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                        m_infile.Clear();
9319c236733d43e6250c8a5671a438f4a2afeb9c0b2Greg Clayton                        error.SetErrorStringWithFormat("input file does not exist: '%s'", option_arg);
932e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    }
933a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    break;
934a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton
935a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                case 'o':
936a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    {
937a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                        bool success;
938a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                        m_infile_offset = Args::StringToUInt64(option_arg, 0, 0, &success);
939a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                        if (!success)
940a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                        {
9419c236733d43e6250c8a5671a438f4a2afeb9c0b2Greg Clayton                            error.SetErrorStringWithFormat("invalid offset string '%s'", option_arg);
942a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                        }
943a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    }
944a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    break;
945a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton
946a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                default:
9479c236733d43e6250c8a5671a438f4a2afeb9c0b2Greg Clayton                    error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
948a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    break;
94924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            }
95024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            return error;
95124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
952a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton
953a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        virtual void
954a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        OptionParsingStarting (CommandInterpreter &interpreter)
95524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
956e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            m_infile.Clear();
957e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            m_infile_offset = 0;
95824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
95924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
960e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        FileSpec m_infile;
961e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        off_t m_infile_offset;
96224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    };
96324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
964238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    CommandObjectMemoryWrite (CommandInterpreter &interpreter) :
965da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectParsed (interpreter,
966da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "memory write",
967da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "Write to the memory of the process being debugged.",
968da26bd203cbb104291b39891febf7481794f205fJim Ingham                             NULL,
969ea0bb4d45c14447a2fe9be9f4bd8e79a84513537Greg Clayton                             eFlagRequiresProcess | eFlagProcessMustBeLaunched),
970a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        m_option_group (interpreter),
971a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        m_format_options (eFormatBytes, 1, UINT64_MAX),
972a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        m_memory_options ()
97324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
97443b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentEntry arg1;
97543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentEntry arg2;
97643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentData addr_arg;
97743b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentData value_arg;
97843b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
97943b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Define the first (and only) variant of this arg.
98043b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        addr_arg.arg_type = eArgTypeAddress;
98143b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        addr_arg.arg_repetition = eArgRepeatPlain;
98243b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
98343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // There is only one variant this argument could be; put it into the argument entry.
98443b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        arg1.push_back (addr_arg);
98543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
98643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Define the first (and only) variant of this arg.
98743b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        value_arg.arg_type = eArgTypeValue;
98843b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        value_arg.arg_repetition = eArgRepeatPlus;
98943b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
99043b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // There is only one variant this argument could be; put it into the argument entry.
99143b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        arg2.push_back (value_arg);
99243b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
99343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Push the data for the first argument into the m_arguments vector.
99443b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        m_arguments.push_back (arg1);
99543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        m_arguments.push_back (arg2);
996a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton
997a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        m_option_group.Append (&m_format_options, OptionGroupFormat::OPTION_GROUP_FORMAT, LLDB_OPT_SET_1);
998a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        m_option_group.Append (&m_format_options, OptionGroupFormat::OPTION_GROUP_SIZE  , LLDB_OPT_SET_1|LLDB_OPT_SET_2);
999a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        m_option_group.Append (&m_memory_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_2);
1000a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        m_option_group.Finalize();
1001a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton
100224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
100324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
100424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    virtual
100524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    ~CommandObjectMemoryWrite ()
100624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
100724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
100824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
100924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    Options *
101024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    GetOptions ()
101124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
1012a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        return &m_option_group;
101324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
101424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
101524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    bool
101624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    UIntValueIsValidForSize (uint64_t uval64, size_t total_byte_size)
101724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
101824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        if (total_byte_size > 8)
101924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            return false;
102024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
102124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        if (total_byte_size == 8)
102224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            return true;
102324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
102424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        const uint64_t max = ((uint64_t)1 << (uint64_t)(total_byte_size * 8)) - 1;
102524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        return uval64 <= max;
102624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
102724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
102824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    bool
102924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    SIntValueIsValidForSize (int64_t sval64, size_t total_byte_size)
103024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
103124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        if (total_byte_size > 8)
103224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            return false;
103324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
103424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        if (total_byte_size == 8)
103524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            return true;
103624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
103724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        const int64_t max = ((int64_t)1 << (uint64_t)(total_byte_size * 8 - 1)) - 1;
103824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        const int64_t min = ~(max);
103924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        return min <= sval64 && sval64 <= max;
104024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
104124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
1042da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
104324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    virtual bool
1044da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute (Args& command, CommandReturnObject &result)
104524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
1046ea0bb4d45c14447a2fe9be9f4bd8e79a84513537Greg Clayton        // No need to check "process" for validity as eFlagRequiresProcess ensures it is valid
1047ea0bb4d45c14447a2fe9be9f4bd8e79a84513537Greg Clayton        Process *process = m_exe_ctx.GetProcessPtr();
104824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
104924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        const size_t argc = command.GetArgumentCount();
105024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
1051a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        if (m_memory_options.m_infile)
105224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
1053e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            if (argc < 1)
1054e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            {
1055e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                result.AppendErrorWithFormat ("%s takes a destination address when writing file contents.\n", m_cmd_name.c_str());
1056e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                result.SetStatus(eReturnStatusFailed);
1057e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                return false;
1058e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            }
1059e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        }
1060e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        else if (argc < 2)
1061e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        {
1062e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            result.AppendErrorWithFormat ("%s takes a destination address and at least one value.\n", m_cmd_name.c_str());
106324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            result.SetStatus(eReturnStatusFailed);
106424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            return false;
106524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
106624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
106724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        StreamString buffer (Stream::eBinary,
1068395fc33dc4b06c048ed35047ec461bc092ef2df3Greg Clayton                             process->GetTarget().GetArchitecture().GetAddressByteSize(),
1069395fc33dc4b06c048ed35047ec461bc092ef2df3Greg Clayton                             process->GetTarget().GetArchitecture().GetByteOrder());
107024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
1071a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        OptionValueUInt64 &byte_size_value = m_format_options.GetByteSizeValue();
1072a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        size_t item_byte_size = byte_size_value.GetCurrentValue();
107354e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton
107449d888d8c3e4de1d2f38d541559f533f1c0bcab3Greg Clayton        Error error;
1075ea0bb4d45c14447a2fe9be9f4bd8e79a84513537Greg Clayton        lldb::addr_t addr = Args::StringToAddress (&m_exe_ctx,
107649d888d8c3e4de1d2f38d541559f533f1c0bcab3Greg Clayton                                                   command.GetArgumentAtIndex(0),
107749d888d8c3e4de1d2f38d541559f533f1c0bcab3Greg Clayton                                                   LLDB_INVALID_ADDRESS,
107849d888d8c3e4de1d2f38d541559f533f1c0bcab3Greg Clayton                                                   &error);
107924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
108024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        if (addr == LLDB_INVALID_ADDRESS)
108124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
108249d888d8c3e4de1d2f38d541559f533f1c0bcab3Greg Clayton            result.AppendError("invalid address expression\n");
108349d888d8c3e4de1d2f38d541559f533f1c0bcab3Greg Clayton            result.AppendError(error.AsCString());
108424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            result.SetStatus(eReturnStatusFailed);
108524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            return false;
108624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
1087e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton
1088a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        if (m_memory_options.m_infile)
1089e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        {
1090e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            size_t length = SIZE_MAX;
1091a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton            if (item_byte_size > 0)
1092a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                length = item_byte_size;
1093a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton            lldb::DataBufferSP data_sp (m_memory_options.m_infile.ReadFileContents (m_memory_options.m_infile_offset, length));
1094e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            if (data_sp)
1095e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            {
1096e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                length = data_sp->GetByteSize();
1097e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                if (length > 0)
1098e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                {
1099e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    Error error;
1100e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    size_t bytes_written = process->WriteMemory (addr, data_sp->GetBytes(), length, error);
1101e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton
1102e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    if (bytes_written == length)
1103e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    {
1104e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                        // All bytes written
11055f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea                        result.GetOutputStream().Printf("%" PRIu64 " bytes were written to 0x%" PRIx64 "\n", (uint64_t)bytes_written, addr);
1106e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                        result.SetStatus(eReturnStatusSuccessFinishResult);
1107e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    }
1108e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    else if (bytes_written > 0)
1109e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    {
1110e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                        // Some byte written
11115f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea                        result.GetOutputStream().Printf("%" PRIu64 " bytes of %" PRIu64 " requested were written to 0x%" PRIx64 "\n", (uint64_t)bytes_written, (uint64_t)length, addr);
1112e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                        result.SetStatus(eReturnStatusSuccessFinishResult);
1113e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    }
1114e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    else
1115e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    {
11165f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea                        result.AppendErrorWithFormat ("Memory write to 0x%" PRIx64 " failed: %s.\n", addr, error.AsCString());
1117e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                        result.SetStatus(eReturnStatusFailed);
1118e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                    }
1119e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                }
1120e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            }
1121e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            else
1122e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            {
1123e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                result.AppendErrorWithFormat ("Unable to read contents of file.\n");
1124e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                result.SetStatus(eReturnStatusFailed);
1125e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            }
1126e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            return result.Succeeded();
1127e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        }
1128a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton        else if (item_byte_size == 0)
1129e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        {
1130a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton            if (m_format_options.GetFormat() == eFormatPointer)
1131e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                item_byte_size = buffer.GetAddressByteSize();
1132e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton            else
1133e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton                item_byte_size = 1;
1134e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton        }
1135e9f5fbdeb698fcc97d6d59d616f731ca4d8d4dceGreg Clayton
113624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        command.Shift(); // shift off the address argument
113724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        uint64_t uval64;
113824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        int64_t sval64;
113924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        bool success = false;
114036da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton        const size_t num_value_args = command.GetArgumentCount();
114136da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton        for (size_t i=0; i<num_value_args; ++i)
114224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
114324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            const char *value_str = command.GetArgumentAtIndex(i);
114424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
1145a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton            switch (m_format_options.GetFormat())
114624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            {
11473182effd150f2e0381d7c6867236737ac69ad846Greg Clayton            case kNumFormats:
114824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatFloat:  // TODO: add support for floats soon
114924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatCharPrintable:
115024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatBytesWithASCII:
115124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatComplex:
115224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatEnum:
115324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatUnicode16:
115424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatUnicode32:
115524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatVectorOfChar:
115624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatVectorOfSInt8:
115724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatVectorOfUInt8:
115824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatVectorOfSInt16:
115924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatVectorOfUInt16:
116024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatVectorOfSInt32:
116124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatVectorOfUInt32:
116224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatVectorOfSInt64:
116324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatVectorOfUInt64:
116424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatVectorOfFloat32:
116524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatVectorOfFloat64:
116624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatVectorOfUInt128:
11674fdf7602bedd8be648f3c549074cf13d90a05f03Greg Clayton            case eFormatOSType:
11684fdf7602bedd8be648f3c549074cf13d90a05f03Greg Clayton            case eFormatComplexInteger:
116924a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton            case eFormatAddressInfo:
117024a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton            case eFormatHexFloat:
117124a6bd9835ed1655984397b0cdf35127e47681e9Greg Clayton            case eFormatInstruction:
117296abc62dcb25159dbb532e477ba2cf6d85b81700Sean Callanan            case eFormatVoid:
117324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                result.AppendError("unsupported format for writing memory");
117424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                result.SetStatus(eReturnStatusFailed);
117524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                return false;
117624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
117724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatDefault:
117824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatBytes:
117924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatHex:
1180535543d6104ee45b4503db6bb6c175e66d0e093bEnrico Granata            case eFormatHexUppercase:
118154e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton            case eFormatPointer:
118254e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton
118324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                // Decode hex bytes
118424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                uval64 = Args::StringToUInt64(value_str, UINT64_MAX, 16, &success);
118524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                if (!success)
118624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
118724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    result.AppendErrorWithFormat ("'%s' is not a valid hex string value.\n", value_str);
118824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    result.SetStatus(eReturnStatusFailed);
118924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    return false;
119024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
119124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                else if (!UIntValueIsValidForSize (uval64, item_byte_size))
119224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
11935f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea                    result.AppendErrorWithFormat ("Value 0x%" PRIx64 " is too large to fit in a %lu byte unsigned integer value.\n", uval64, item_byte_size);
119424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    result.SetStatus(eReturnStatusFailed);
119524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    return false;
119624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
119724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                buffer.PutMaxHex64 (uval64, item_byte_size);
119824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                break;
119924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
120024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatBoolean:
120124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                uval64 = Args::StringToBoolean(value_str, false, &success);
120224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                if (!success)
120324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
120424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    result.AppendErrorWithFormat ("'%s' is not a valid boolean string value.\n", value_str);
120524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    result.SetStatus(eReturnStatusFailed);
120624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    return false;
120724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
120824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                buffer.PutMaxHex64 (uval64, item_byte_size);
120924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                break;
121024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
121124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatBinary:
121224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                uval64 = Args::StringToUInt64(value_str, UINT64_MAX, 2, &success);
121324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                if (!success)
121424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
121524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    result.AppendErrorWithFormat ("'%s' is not a valid binary string value.\n", value_str);
121624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    result.SetStatus(eReturnStatusFailed);
121724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    return false;
121824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
121924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                else if (!UIntValueIsValidForSize (uval64, item_byte_size))
122024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
12215f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea                    result.AppendErrorWithFormat ("Value 0x%" PRIx64 " is too large to fit in a %lu byte unsigned integer value.\n", uval64, item_byte_size);
122224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    result.SetStatus(eReturnStatusFailed);
122324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    return false;
122424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
122524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                buffer.PutMaxHex64 (uval64, item_byte_size);
122624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                break;
122724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
1228307fa07606d519d427c812802aff5f9727e7047cGreg Clayton            case eFormatCharArray:
122924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatChar:
123024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatCString:
123124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                if (value_str[0])
123224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
123324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    size_t len = strlen (value_str);
123424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    // Include the NULL for C strings...
1235a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton                    if (m_format_options.GetFormat() == eFormatCString)
123624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                        ++len;
123724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    Error error;
123824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    if (process->WriteMemory (addr, value_str, len, error) == len)
123924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    {
124024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                        addr += len;
124124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    }
124224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    else
124324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    {
12445f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea                        result.AppendErrorWithFormat ("Memory write to 0x%" PRIx64 " failed: %s.\n", addr, error.AsCString());
124524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                        result.SetStatus(eReturnStatusFailed);
124624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                        return false;
124724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    }
124824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
124924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                break;
125024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
125124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatDecimal:
125224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                sval64 = Args::StringToSInt64(value_str, INT64_MAX, 0, &success);
125324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                if (!success)
125424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
125524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    result.AppendErrorWithFormat ("'%s' is not a valid signed decimal value.\n", value_str);
125624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    result.SetStatus(eReturnStatusFailed);
125724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    return false;
125824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
125924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                else if (!SIntValueIsValidForSize (sval64, item_byte_size))
126024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
12615f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea                    result.AppendErrorWithFormat ("Value %" PRIi64 " is too large or small to fit in a %lu byte signed integer value.\n", sval64, item_byte_size);
126224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    result.SetStatus(eReturnStatusFailed);
126324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    return false;
126424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
126524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                buffer.PutMaxHex64 (sval64, item_byte_size);
126624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                break;
126724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
126824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatUnsigned:
126924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                uval64 = Args::StringToUInt64(value_str, UINT64_MAX, 0, &success);
127024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                if (!success)
127124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
127224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    result.AppendErrorWithFormat ("'%s' is not a valid unsigned decimal string value.\n", value_str);
127324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    result.SetStatus(eReturnStatusFailed);
127424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    return false;
127524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
127624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                else if (!UIntValueIsValidForSize (uval64, item_byte_size))
127724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
12785f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea                    result.AppendErrorWithFormat ("Value %" PRIu64 " is too large to fit in a %lu byte unsigned integer value.\n", uval64, item_byte_size);
127924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    result.SetStatus(eReturnStatusFailed);
128024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    return false;
128124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
128224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                buffer.PutMaxHex64 (uval64, item_byte_size);
128324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                break;
128424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
128524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            case eFormatOctal:
128624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                uval64 = Args::StringToUInt64(value_str, UINT64_MAX, 8, &success);
128724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                if (!success)
128824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
128924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    result.AppendErrorWithFormat ("'%s' is not a valid octal string value.\n", value_str);
129024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    result.SetStatus(eReturnStatusFailed);
129124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    return false;
129224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
129324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                else if (!UIntValueIsValidForSize (uval64, item_byte_size))
129424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
12955f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea                    result.AppendErrorWithFormat ("Value %" PRIo64 " is too large to fit in a %lu byte unsigned integer value.\n", uval64, item_byte_size);
129624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    result.SetStatus(eReturnStatusFailed);
129724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    return false;
129824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
129924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                buffer.PutMaxHex64 (uval64, item_byte_size);
130024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                break;
130124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            }
130224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
130324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
130424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        if (!buffer.GetString().empty())
130524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
130624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            Error error;
130753d68e749f0715691a95f23e9490d97e484b15daGreg Clayton            if (process->WriteMemory (addr, buffer.GetString().c_str(), buffer.GetString().size(), error) == buffer.GetString().size())
130824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                return true;
130924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            else
131024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            {
13115f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea                result.AppendErrorWithFormat ("Memory write to 0x%" PRIx64 " failed: %s.\n", addr, error.AsCString());
131224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                result.SetStatus(eReturnStatusFailed);
131324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                return false;
131424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            }
131524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
131624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        return true;
131724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
131824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
1319a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton    OptionGroupOptions m_option_group;
1320a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton    OptionGroupFormat m_format_options;
1321a42880a8e464a3e4f93951ae797313e5e38cdcd3Greg Clayton    OptionGroupWriteMemory m_memory_options;
132224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner};
132324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
132424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
132524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
132624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// CommandObjectMemory
132724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//-------------------------------------------------------------------------
132824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
132963094e0bb161580564954dee512955c1c79d3476Greg ClaytonCommandObjectMemory::CommandObjectMemory (CommandInterpreter &interpreter) :
1330238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    CommandObjectMultiword (interpreter,
1331238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                            "memory",
1332c1ad82eb979ee856b86aec6e2acb7bddf75f7c4fCaroline Tice                            "A set of commands for operating on memory.",
133324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                            "memory <subcommand> [<subcommand-options>]")
133424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
1335238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    LoadSubCommand ("read",  CommandObjectSP (new CommandObjectMemoryRead (interpreter)));
1336238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    LoadSubCommand ("write", CommandObjectSP (new CommandObjectMemoryWrite (interpreter)));
133724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
133824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
133924943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerCommandObjectMemory::~CommandObjectMemory ()
134024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
134124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
1342