-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProgram.cs
366 lines (349 loc) · 15.6 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using System.Text;
using ScriptableFramework;
using DotnetStoryScript;
using DotnetStoryScript.DslExpression;
using System.Linq;
namespace BatchCommand
{
class MainClass
{
static void Main(string[] args)
{
bool interactiveComputing = false;
var scpFile = string.Empty;
var vargs = BatchScript.NewCalculatorValueList();
string scpTxt = string.Empty;
GlobalVariables.Instance.IsDevelopment = true;
GlobalVariables.Instance.IsDebug = true;
GlobalVariables.Instance.IsDevice = false;
LogSystem.OnOutput = (GameLogType type, string msg) => {
Console.WriteLine(msg);
};
BatchScript.Init();
BatchScript.Register("compiledbgscp", "compiledbgscp(scpFile,struFile,apiFile) api", new ExpressionFactoryHelper<CompileDbgScpExp>());
BatchScript.Register("uploaddbgscp", "dumpdbgscp() api", new ExpressionFactoryHelper<UploadDbgScpExp>());
BatchScript.Register("savedbgscp", "savedbgscp(dataFile) api", new ExpressionFactoryHelper<SaveDbgScpExp>());
BatchScript.Register("loaddbgscp", "loaddbgscp(dataFile) api", new ExpressionFactoryHelper<LoadDbgScpExp>());
BatchScript.Register("testdbgscp", "testdbgscp() api", new ExpressionFactoryHelper<TestDbgScpExp>());
BatchScript.Register("addrs2lines", "addrs2lines(textBase,textSeg,dbg,file,outfile) api", new ExpressionFactoryHelper<Addrs2LinesExp>());
if (args.Length == 0) {
scpFile = "main.dsl";
if (File.Exists(scpFile)) {
vargs.Clear();
vargs.Add(scpFile);
}
else {
PrintHelp();
scpFile = string.Empty;
interactiveComputing = true;
}
}
else {
bool isScpPart = false;
for (int i = 0; i < args.Length; ++i) {
if (0 == string.Compare(args[i], "-e", true)) {
if (i < args.Length - 1) {
string arg = args[i + 1];
if (!arg.StartsWith("-")) {
scpTxt = arg;
++i;
}
}
}
else if (0 == string.Compare(args[i], "-i", true)) {
interactiveComputing = true;
}
else if (0 == string.Compare(args[i], "-h", true)) {
PrintHelp();
return;
}
else if (!isScpPart && args[i][0] == '-') {
Console.WriteLine("unknown command option ! {0}", args[i]);
}
else {
if (string.IsNullOrEmpty(scpFile)) {
scpFile = args[i];
isScpPart = true;
if (!File.Exists(scpFile)) {
Console.WriteLine("file path not found ! {0}", scpFile);
}
}
vargs.Add(args[i]);
}
}
}
int exitCode = 0;
try {
var r = BoxedValue.NullObject;
if (interactiveComputing) {
InteractiveComputing();
}
else if (!string.IsNullOrEmpty(scpTxt)) {
r = BatchScript.EvalAndRun(scpTxt);
}
else if (!string.IsNullOrEmpty(scpFile)) {
Stopwatch sw = Stopwatch.StartNew();
r = BatchScript.Run(scpFile, vargs);
sw.Stop();
long us = sw.ElapsedTicks*1000000 / Stopwatch.Frequency;
if (BatchScript.TimeStatisticOn)
Console.WriteLine("consume time: {0}us", us);
}
if (!r.IsNullObject) {
exitCode = r.GetInt();
}
Console.Out.Flush();
} catch (Exception ex) {
BatchScript.Log("exception:{0}\n{1}", ex.Message, ex.StackTrace);
exitCode = -1;
}
BatchScript.RecycleCalculatorValueList(vargs);
Environment.Exit(exitCode);
}
private static void PrintHelp()
{
Console.WriteLine("[usage]BatchCommand [-h] [-i] [-e script_string] [dsl_file arg1 arg2 ...]");
Console.WriteLine(" [-h] show this help");
Console.WriteLine(" [-i] interactive computing mode");
Console.WriteLine(" [-e script_string] run script_string");
Console.WriteLine(" dsl_file source dsl file");
Console.WriteLine(" arg1 arg2 ... arguments to dsl file");
}
private static void InteractiveComputing()
{
Console.WriteLine("Enter exit or quit to exit...");
for (; ; ) {
Console.Write(">");
var line = Console.ReadLine();
if (null != line) {
if (line == "exit" || line == "quit")
break;
if (line == "help" || line.StartsWith("help ")) {
string filter = line.Substring(4).Trim();
foreach (var pair in BatchScript.ApiDocs) {
if (pair.Key.Contains(filter) || pair.Value.Contains(filter)) {
Console.WriteLine("[{0}]:{1}", pair.Key, pair.Value);
}
}
}
else {
var r = BatchScript.EvalAndRun(line);
Console.Write("result:");
Console.WriteLine(r.ToString());
}
}
}
}
}
internal sealed class CompileDbgScpExp : SimpleExpressionBase
{
protected override BoxedValue OnCalc(IList<BoxedValue> operands)
{
string scpFile = "hook.txt";
string struFile = "struct.txt";
string apiFile = "api.txt";
if (operands.Count > 0) {
scpFile = operands[0].AsString;
}
if (operands.Count > 1) {
struFile = operands[1].AsString;
}
if (operands.Count > 2) {
apiFile = operands[2].AsString;
}
if (!string.IsNullOrEmpty(apiFile) && !string.IsNullOrEmpty(struFile) && !string.IsNullOrEmpty(scpFile)) {
string txt = File.ReadAllText(apiFile);
string err = CppDebugScript.DebugScriptCompiler.Instance.LoadApiDefine(txt);
if (string.IsNullOrEmpty(err)) {
LogSystem.Warn("Load API from {0} finished.", apiFile);
}
else {
LogSystem.Warn("Load API from {0} failed:{1}", apiFile, err);
}
txt = File.ReadAllText(struFile);
err = CppDebugScript.DebugScriptCompiler.Instance.LoadStructDefine(txt);
if (string.IsNullOrEmpty(err)) {
LogSystem.Warn("Load Struct from {0} finished.", struFile);
}
else {
LogSystem.Warn("Load Struct from {0} failed:{1}", struFile, err);
}
txt = File.ReadAllText(scpFile);
if (CppDebugScript.DebugScriptCompiler.Instance.Compile(txt, out err)) {
LogSystem.Warn("Compile {0} finished.", scpFile);
var sb = new StringBuilder();
try {
CppDebugScript.DebugScriptCompiler.Instance.DumpAsm(sb);
}
catch (Exception e) {
sb.AppendLine();
sb.AppendLine();
sb.AppendLine("========[Exception]========");
sb.AppendLine(e.ToString());
}
LogSystem.Warn("[ASM]:\n{0}", sb.ToString());
}
else {
LogSystem.Warn("Compile DebugScript from {0} failed:{1}", scpFile, err);
}
}
return BoxedValue.NullObject;
}
}
internal sealed class UploadDbgScpExp : SimpleExpressionBase
{
protected override BoxedValue OnCalc(IList<BoxedValue> operands)
{
CppDebugScript.DebugScriptCompiler.Instance.UploadToCppVM();
return BoxedValue.NullObject;
}
}
internal sealed class SaveDbgScpExp : SimpleExpressionBase
{
protected override BoxedValue OnCalc(IList<BoxedValue> operands)
{
string file = "bytecode.dat";
if (operands.Count > 0) {
file = operands[0].AsString;
}
CppDebugScript.DebugScriptCompiler.Instance.SaveByteCode(file);
return BoxedValue.NullObject;
}
}
internal sealed class LoadDbgScpExp : SimpleExpressionBase
{
protected override BoxedValue OnCalc(IList<BoxedValue> operands)
{
string file = "bytecode.dat";
if (operands.Count > 0) {
file = operands[0].AsString;
}
CppDebugScript.DebugScriptCompiler.Instance.LoadByteCode(file);
return BoxedValue.NullObject;
}
}
internal sealed class TestDbgScpExp : SimpleExpressionBase
{
protected override BoxedValue OnCalc(IList<BoxedValue> operands)
{
int a = CppDebugScript.CppDbgScpInterface.Test1Export(123, 123.456, "test1");
LogSystem.Warn("retval a:{0}", a);
int b = CppDebugScript.CppDbgScpInterface.Test2Export(1234, 1234.456, "test2");
LogSystem.Warn("retval b:{0}", b);
CppDebugScript.CppDbgScpInterface.Test3Export(12345, 12345.456, "test3");
CppDebugScript.CppDbgScpInterface.Test4Export(123456, 123456.456, "test4");
Console.WriteLine("--------");
int ma = CppDebugScript.CppDbgScpInterface.TestMacro1Export(123, 123.456, "testmacro1");
LogSystem.Warn("retval ma:{0}", ma);
int mb = CppDebugScript.CppDbgScpInterface.TestMacro2Export(1234, 1234.456, "testmacro2");
LogSystem.Warn("retval mb:{0}", mb);
CppDebugScript.CppDbgScpInterface.TestMacro3Export(12345, 12345.456, "testmacro3");
CppDebugScript.CppDbgScpInterface.TestMacro4Export(123456, 123456.456, "testmacro4");
return BoxedValue.NullObject;
}
}
internal sealed class Addrs2LinesExp : SimpleExpressionBase
{
protected override BoxedValue OnCalc(IList<BoxedValue> operands)
{
ulong textBase = 0x72a4d6a000;
ulong textSeg = 0x55a000;
string dbg = "E:\\AndroidPlayerDebug\\rel\\Symbols\\arm64-v8a\\libunity.dbg.so";
string file = "C:\\sdk_full\\platform-tools\\temp.txt";
string ofile = file;
if (operands.Count > 0) {
textBase = operands[0].GetULong();
}
if (operands.Count > 1) {
textSeg = operands[1].GetULong();
}
if (operands.Count > 2) {
dbg = operands[2].GetString();
}
if (operands.Count > 3) {
file = operands[3].GetString();
ofile = file;
}
if(operands.Count > 4) {
ofile = operands[4].GetString();
}
int ct = 0;
if (File.Exists(file)) {
var addrHashSet = new HashSet<ulong>();
var dict = new Dictionary<int, ulong>();
var lines = File.ReadAllLines(file);
for (int ix = 0; ix < lines.Length; ++ix) {
string line = lines[ix];
var fs = line.Split(s_WhiteSpaces, StringSplitOptions.RemoveEmptyEntries);
string strAddr = string.Empty;
string so = string.Empty;
if (fs.Length > 10 && fs[10].Contains("libunity.so") && fs[8].StartsWith("0x")) {
strAddr = fs[8];
so = fs[10];
}
else if(fs.Length>3 && fs[3].Contains("libunity.so") && fs[1].StartsWith("0x")) {
strAddr = fs[1];
so = fs[3];
}
if(!string.IsNullOrEmpty(strAddr) && !string.IsNullOrEmpty(so)) {
int si = line.IndexOf(so);
lines[ix] = line.Substring(0, si + so.Length);
if (ulong.TryParse(strAddr.Substring(2), System.Globalization.NumberStyles.HexNumber, null, out var addr)) {
ulong offset = addr - textBase + textSeg;
addrHashSet.Add(offset);
dict.Add(ix, offset);
}
}
}
var addrs = addrHashSet.ToArray();
var symDict = new Dictionary<ulong, string>();
var coption = new ProcessStartOption();
for (int ix = 0; ix < addrs.Length; ix += c_BatchCount) {
var inputs = new List<string>();
for (int i = ix; i < ix + c_BatchCount && i < addrs.Length; ++i) {
inputs.Add(addrs[i].ToString("x"));
}
if (OperatingSystem.IsWindows()) {
var output = new StringBuilder();
var error = new StringBuilder();
int cr = ProcessHelper.RunProcess("addr2line", " -f -C -e " + dbg, coption, 5000, null, null, inputs, output, error, false, false, Encoding.UTF8);
if (cr == 0) {
string results = output.ToString();
var syms = results.Split(s_Seps, StringSplitOptions.RemoveEmptyEntries);
for(int i = 0; i < syms.Length - 1; i += 2) {
ulong offset = addrs[ix + i / 2];
string sym = syms[i] + " " + syms[i + 1];
symDict.TryAdd(offset, sym);
}
ct += inputs.Count;
var pos = Console.GetCursorPosition();
Console.Write(ct + "/" + addrs.Length);
Console.SetCursorPosition(pos.Left, pos.Top);
}
else {
Console.WriteLine("run addr2line failed, exit code:{0}", cr);
}
}
}
for (int ix = 0; ix < lines.Length; ++ix) {
string line = lines[ix];
if(dict.TryGetValue(ix, out var offset) && symDict.TryGetValue(offset, out var sym)) {
lines[ix] = line + " " + sym;
}
}
File.WriteAllLines(ofile, lines);
}
else {
Console.WriteLine("Can't find '{0}' !", file);
}
return ct;
}
private static char[] s_WhiteSpaces = new char[] { ' ', '\t' };
private static char[] s_Seps = new char[] { '\r', '\n' };
private const int c_BatchCount = 500;
}
}