Xcode.cpp revision c50fc4b658c46ef43b1add131391ecdd1f9bd752
1//
2//  Xcode.cpp
3//  PerfTestDriver
4//
5//  Created by Enrico Granata on 3/6/13.
6//  Copyright (c) 2013 Apple Inc. All rights reserved.
7//
8
9#include "Xcode.h"
10#include <string>
11
12using namespace std;
13using namespace lldb::perf;
14
15void
16Xcode::FetchVariable (SBValue value, uint32_t expand, bool verbose)
17{
18	auto name = value.GetName();
19	auto num_value = value.GetValueAsUnsigned(0);
20	auto summary = value.GetSummary();
21	auto in_scope = value.IsInScope();
22	auto has_children = value.MightHaveChildren();
23	auto type_1 = value.GetType();
24	auto type_2 = value.GetType();
25	auto type_name_1 = value.GetTypeName();
26	auto type_3 = value.GetType();
27	auto type_name_2 = value.GetTypeName();
28	if (verbose)
29		printf("%s %s = %llu %s\n",value.GetTypeName(),value.GetName(),num_value,summary);
30	if (expand > 0)
31	{
32		auto count = value.GetNumChildren();
33		for (int i = 0; i < count; i++)
34		{
35			SBValue child(value.GetChildAtIndex(i,value.IsDynamic() ? lldb::eDynamicCanRunTarget : lldb::eNoDynamicValues, true));
36			FetchVariable (child,expand-1,verbose);
37		}
38	}
39}
40
41void
42Xcode::FetchModules (SBTarget target, bool verbose)
43{
44	auto count = target.GetNumModules();
45	for (int i = 0; i < count; i++)
46	{
47		SBModule module(target.GetModuleAtIndex(i));
48		auto fspec = module.GetFileSpec();
49		std::string path(1024,0);
50		fspec.GetPath(&path[0],1024);
51		auto uuid = module.GetUUIDBytes();
52		if (verbose)
53		{
54			printf("%s %s\n",path.c_str(),module.GetUUIDString());
55		}
56	}
57}
58
59void
60Xcode::FetchVariables (SBFrame frame, uint32_t expand, bool verbose)
61{
62	auto values = frame.GetVariables (true,true,true,false, eDynamicCanRunTarget);
63	auto count = values.GetSize();
64	for (int i = 0; i < count; i++)
65	{
66		SBValue value(values.GetValueAtIndex(i));
67		FetchVariable (value,expand,verbose);
68	}
69}
70
71void
72Xcode::FetchFrames(SBProcess process, bool variables, bool verbose)
73{
74	auto pCount = process.GetNumThreads();
75	for (int p = 0; p < pCount; p++)
76	{
77		SBThread thread(process.GetThreadAtIndex(p));
78		auto tCount = thread.GetNumFrames ();
79		if (verbose)
80			printf("%s %d %d {%d}\n",thread.GetQueueName(),tCount,thread.GetStopReason(),eStopReasonBreakpoint);
81		for (int t = 0; t < tCount; t++)
82		{
83			SBFrame frame(thread.GetFrameAtIndex(t));
84			auto fp = frame.GetFP();
85			SBThread thread_dup = frame.GetThread();
86			SBFileSpec filespec(process.GetTarget().GetExecutable());
87			std::string path(1024,0);
88			filespec.GetPath(&path[0],1024);
89			auto state = process.GetState();
90			auto pCount_dup = process.GetNumThreads();
91			auto byte_size = process.GetAddressByteSize();
92			auto pc = frame.GetPC();
93			SBSymbolContext context(frame.GetSymbolContext(0x0000006e));
94			SBModule module(context.GetModule());
95			SBLineEntry entry(context.GetLineEntry());
96			SBFileSpec entry_filespec(process.GetTarget().GetExecutable());
97			std::string entry_path(1024,0);
98			entry_filespec.GetPath(&entry_path[0],1024);
99			auto line_1 = entry.GetLine();
100			auto line_2 = entry.GetLine();
101			auto fname = frame.GetFunctionName();
102			if (verbose)
103				printf("%llu %s %d %d %llu %s %d %s\n",fp,path.c_str(),state,byte_size,pc,entry_path.c_str(),line_1,fname);
104			if (variables)
105				FetchVariables (frame, 0, verbose);
106		}
107	}
108}
109
110void
111Xcode::RunExpression (SBFrame frame, const char* expression, bool po, bool verbose)
112{
113	SBValue value (frame.EvaluateExpression (expression, eDynamicCanRunTarget));
114	FetchVariable (value,0,verbose);
115	if (po)
116	{
117		auto descr = value.GetObjectDescription();
118		if (descr)
119			printf("%s\n",descr);
120	}
121}
122
123void
124Xcode::Next (SBThread thread)
125{
126	thread.StepOver();
127}
128
129void
130Xcode::Continue (SBProcess process)
131{
132	process.Continue();
133}
134
135void
136Xcode::RunCommand (SBDebugger debugger, const char* cmd, bool verbose)
137{
138	SBCommandReturnObject sb_ret;
139	auto interpreter = debugger.GetCommandInterpreter();
140	interpreter.HandleCommand(cmd,sb_ret);
141	if (verbose)
142		printf("%s\n%s\n",sb_ret.GetOutput(false),sb_ret.GetError(false));
143}
144
145SBThread
146Xcode::GetThreadWithStopReason (SBProcess process, StopReason reason)
147{
148	auto threads_count = process.GetNumThreads();
149	for (auto thread_num = 0; thread_num < threads_count; thread_num++)
150	{
151		SBThread thread(process.GetThreadAtIndex(thread_num));
152		if (thread.GetStopReason() == reason)
153		{
154			return thread;
155		}
156	}
157	return SBThread();
158}
159
160SBBreakpoint
161Xcode::CreateFileLineBreakpoint (SBTarget target, const char* file, uint32_t line)
162{
163    return target.BreakpointCreateByLocation(file, line);
164}
165