Repository navigation
Expand file tree
/
Copy pathandroid_utils.cpp
More file actions
889 lines (695 loc) · 23.9 KB
/
Copy pathandroid_utils.cpp
File metadata and controls
889 lines (695 loc) · 23.9 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2018-2026 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "android_utils.h"
#include <ctype.h>
#include <algorithm>
#include "common/formatting.h"
#include "common/threading.h"
#include "core/core.h"
#include "strings/string_utils.h"
namespace Android
{
bool IsHostADB(const char *hostname)
{
return !strncmp(hostname, "adb:", 4);
}
void ExtractDeviceIDAndIndex(const rdcstr &hostname, int &index, rdcstr &deviceID)
{
if(!IsHostADB(hostname.c_str()))
return;
const char *c = hostname.c_str();
c += 4;
index = atoi(c);
c = strchr(c, ':');
if(!c)
{
index = 0;
return;
}
c++;
deviceID = c;
}
rdcstr GetPackageName(const rdcstr &packageAndActivity)
{
if(packageAndActivity.empty())
return "";
int32_t start = 0;
if(packageAndActivity[0] == '/')
start++;
int32_t activitySep = packageAndActivity.find('/', start);
if(activitySep < 0)
return packageAndActivity.substr(start);
return packageAndActivity.substr(start, activitySep - start);
}
rdcstr GetActivityName(const rdcstr &packageAndActivity)
{
if(packageAndActivity.empty())
return "";
int32_t start = 0;
if(packageAndActivity[0] == '/')
start++;
int32_t activitySep = packageAndActivity.find('/', start);
if(activitySep < 0)
return "";
return packageAndActivity.substr(activitySep + 1);
}
ABI GetABI(const rdcstr &abiName)
{
if(abiName == "armeabi-v7a")
return ABI::armeabi_v7a;
else if(abiName == "arm64-v8a")
return ABI::arm64_v8a;
else if(abiName == "x86")
return ABI::x86;
else if(abiName == "x86_64")
return ABI::x86_64;
RDCWARN("Unknown or unsupported ABI %s", abiName.c_str());
return ABI::unknown;
}
rdcstr GetPlainABIName(ABI abi)
{
switch(abi)
{
case ABI::arm64_v8a: return "arm64";
case ABI::armeabi_v7a: return "arm32";
case ABI::x86_64: return "x64";
case ABI::x86: return "x86";
default: break;
}
return "unsupported";
}
rdcarray<ABI> GetSupportedABIs(const rdcstr &deviceID)
{
rdcstr adbAbi = adbExecCommand(deviceID, "shell getprop ro.product.cpu.abi").strStdout.trimmed();
// these returned lists should be such that the first entry is the 'lowest command denominator' -
// typically 32-bit.
switch(GetABI(adbAbi))
{
case ABI::arm64_v8a: return {ABI::armeabi_v7a, ABI::arm64_v8a};
case ABI::armeabi_v7a: return {ABI::armeabi_v7a};
case ABI::x86_64: return {ABI::x86, ABI::x86_64};
case ABI::x86: return {ABI::x86};
default: break;
}
return {};
}
rdcstr GetRenderDocPackageForABI(ABI abi)
{
return RENDERDOC_ANDROID_PACKAGE_BASE "." + GetPlainABIName(abi);
}
rdcstr GetPathForPackage(const rdcstr &deviceID, const rdcstr &packageName)
{
rdcstr pkgPath = adbExecCommand(deviceID, "shell pm path " + packageName).strStdout.trimmed();
// if there are multiple slices, the path will be returned on many lines. Take only the first
// line, assuming all of the apks are in the same directory
if(pkgPath.find("\n") >= 0)
{
rdcarray<rdcstr> lines;
split(pkgPath, lines, '\n');
pkgPath = lines[0].trimmed();
}
if(pkgPath.empty() || pkgPath.find("package:") != 0 || pkgPath.find("base.apk") == -1)
return pkgPath;
pkgPath.erase(0, strlen("package:"));
pkgPath.erase(pkgPath.size() - strlen("base.apk"), ~0U);
return pkgPath;
}
bool IsSupported(rdcstr deviceID)
{
rdcstr api =
Android::adbExecCommand(deviceID, "shell getprop ro.build.version.sdk").strStdout.trimmed();
int apiVersion = atoi(api.c_str());
// SDK 23 == Android 6.0, our minimum spec. Only fail if we did parse an SDK string, in case some
// Android devices don't support the query - we assume they are new enough.
if(apiVersion >= 0 && apiVersion < 23)
{
RDCWARN("Device '%s' is on api version %d which is not supported",
GetFriendlyName(deviceID).c_str(), apiVersion);
return false;
}
return true;
}
rdcstr GetFolderName(const rdcstr &deviceID)
{
rdcstr api =
Android::adbExecCommand(deviceID, "shell getprop ro.build.version.sdk").strStdout.trimmed();
int apiVersion = atoi(api.c_str());
if(apiVersion >= 30)
return "media/";
return "data/";
}
bool SupportsNativeLayers(const rdcstr &deviceID)
{
rdcstr api =
Android::adbExecCommand(deviceID, "shell getprop ro.build.version.sdk").strStdout.trimmed();
int apiVersion = atoi(api.c_str());
// SDK 29 == Android 10.0, the first version that included layering
if(apiVersion >= 29)
return true;
return false;
}
rdcstr DetermineInstalledABI(const rdcstr &deviceID, const rdcstr &packageName)
{
RDCLOG("Checking installed ABI for %s", packageName.c_str());
rdcstr abi;
rdcstr dump = adbExecCommand(deviceID, "shell pm dump " + packageName).strStdout;
if(dump.empty())
RDCERR("Unable to pm dump %s", packageName.c_str());
// Walk through the output and look for primaryCpuAbi
rdcstr prefix = "primaryCpuAbi=";
int offset = dump.find("primaryCpuAbi=");
if(offset >= 0)
{
offset = dump.find('=', offset) + 1;
int newline = dump.find('\n', offset);
if(newline >= 0)
abi = dump.substr(offset, newline - offset).trimmed();
}
if(abi.empty())
RDCERR("Unable to determine installed abi for: %s", packageName.c_str());
return abi;
}
rdcstr GetFriendlyName(const rdcstr &deviceID)
{
// run adb root now, so we hit any disconnection that we're going to before trying to connect.
// If we can't be root, this is cheap, if we're already root, this is cheap, if we can be root
// and this changes us it will block only the first time - and we expect this function to be
// slow-ish.
//
// We do this here so that we sneakily take advantage of the above caching - otherwise we spam adb
// root commands into the log
Android::adbExecCommand(deviceID, "root");
rdcstr manuf =
Android::adbExecCommand(deviceID, "shell getprop ro.product.manufacturer").strStdout.trimmed();
rdcstr model =
Android::adbExecCommand(deviceID, "shell getprop ro.product.model").strStdout.trimmed();
rdcstr combined;
if(manuf.empty() && model.empty())
combined = "";
else if(manuf.empty() && !model.empty())
combined = model;
else if(!manuf.empty() && model.empty())
combined = manuf + " device";
else if(!manuf.empty() && !model.empty())
combined = manuf + " " + model;
return combined;
}
bool HasRootAccess(const rdcstr &deviceID)
{
RDCLOG("Checking for root access on %s", deviceID.c_str());
// Try switching adb to root and check a few indicators for success
// Nothing will fall over if we get a false positive here, it just enables
// additional methods of getting things set up.
Process::ProcessResult result = adbExecCommand(deviceID, "root");
rdcstr whoami = adbExecCommand(deviceID, "shell whoami").strStdout.trimmed();
if(whoami == "root")
return true;
rdcstr checksu =
adbExecCommand(deviceID, "shell test -e /system/xbin/su && echo found").strStdout.trimmed();
if(checksu == "found")
return true;
return false;
}
rdcstr GetFirstMatchingLine(const rdcstr &haystack, const rdcstr &needle)
{
int needleOffset = haystack.find(needle);
if(needleOffset == -1)
return rdcstr();
int nextLine = haystack.find('\n', needleOffset + 1);
return haystack.substr(needleOffset, nextLine == -1 ? ~0U : size_t(nextLine - needleOffset));
}
bool IsDebuggable(const rdcstr &deviceID, const rdcstr &packageName)
{
RDCLOG("Checking that APK is debuggable");
rdcstr info = adbExecCommand(deviceID, "shell dumpsys package " + packageName).strStdout;
rdcstr pkgFlags = GetFirstMatchingLine(info, "pkgFlags=[");
if(pkgFlags == "")
{
RDCERR("Couldn't get pkgFlags from adb");
return false;
}
return pkgFlags.contains("DEBUGGABLE");
}
// on android only when we hit this function we write a marker that isn't a standard log. The
// purpose is to always try and have a unique message in the last N lines so that we can detect if
// we ever lose messages.
void TickDeviceLogcat()
{
#if ENABLED(RDOC_ANDROID)
static uint64_t freq = (uint64_t)Timing::GetTickFrequency();
const uint64_t timeMS = uint64_t(Timing::GetTick() / freq);
static uint64_t prevTimeMS = 0;
// don't spam more than once every 100ms to avoid saturating our log
if(timeMS > prevTimeMS + 100)
{
prevTimeMS = timeMS;
OSUtility::WriteOutput(OSUtility::Output_DebugMon,
StringFormat::Fmt("__rdoc_internal_android_logcat %llu", timeMS).c_str());
}
#endif
}
struct LogLine
{
time_t timestamp = 0;
uint32_t pid = 0;
LogType logtype = LogType::Comment;
rdcstr filename;
uint32_t line_number = 0;
rdcstr message;
bool parse(const rdcstr &line)
{
#define EXPECT_CHAR(c) \
if(idx >= line.length() || line[idx] != c) \
return false; \
idx++;
// Parse out mostly our own log files, but also output that looks like crash callstacks
//
// Example lines:
//
// clang-format off
//
// 0 1 2 3 4 5 6 7 8 9 10
// 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456
// I/renderdoc( 1234): @1234567812345678@ RDOC 001234: [01:02:03] filename.cpp( 123) - Log - Hello
//
// F/libc (11519): Fatal signal 11 (SIGSEGV), code 1, fault addr 0x4 in tid 11618 (FooBar), pid 11519 (blah)
// F/DEBUG (12061): backtrace:
// F/DEBUG (12061): #00 pc 000485ec /system/lib/libc.so (pthread_mutex_lock+1)
// F/DEBUG (12061): #01 pc 00137449 /data/app/org.renderdoc.renderdoccmd.arm32==/lib/arm/libVkLayer_GLES_RenderDoc.so
// F/DEBUG (12061): #02 pc 0013bbf1 /data/app/org.renderdoc.renderdoccmd.arm32==/lib/arm/libVkLayer_GLES_RenderDoc.so
//
// clang-format on
//
size_t idx = 0;
// too short - minimum is 22 for prefix. Could be longer if PID is over 5 digits
// saves on length checks below
if(line.length() <= 20)
return false;
// skip past priority character
idx++;
EXPECT_CHAR('/');
// we assume that the logcat filters have worked, so ignore the logcat tag here. Just check if
// it's ours or not
bool ownLog = !strncmp(&line[idx], "renderdoc", 9);
while(idx < line.length() && line[idx] != '(')
idx++;
size_t tagEnd = idx;
EXPECT_CHAR('(');
// skip spaces
while(idx < line.length() && isspace(line[idx]))
idx++;
// process this PID field - we'll override it with our own if this is one of our logs
pid = 0;
while(idx < line.length() && isdigit(line[idx]))
{
pid *= 10;
pid += int(line[idx] - '0');
idx++;
}
EXPECT_CHAR(')');
EXPECT_CHAR(':');
EXPECT_CHAR(' ');
if(!ownLog)
{
// don't know anything more about the format, this whole thing is the message.
message = line.substr(idx);
// use current host time, it's not accurate but it's close enough.
timestamp = Timing::GetUTCTime();
filename = line.substr(2, tagEnd - 2);
line_number = 0;
logtype = LogType::Comment;
switch(line[0])
{
case 'V': // VERBOSE
case 'D': // DEBUG
logtype = LogType::Debug;
break;
case 'I': // INFO
logtype = LogType::Comment;
break;
case 'W': // WARN
logtype = LogType::Warning;
break;
case 'E': // ERROR
logtype = LogType::Error;
break;
case 'F': // FATAL
logtype = LogType::Fatal;
break;
case 'S': // SILENT
default: logtype = LogType::Comment; break;
}
// remove any padding spaces
while(!filename.empty() && isspace(filename.back()))
filename.pop_back();
// if adb gave us DOS newlines, remove the \r
if(message.back() == '\r')
message.pop_back();
// ignore these libc spam messages
if(message.contains("Access denied finding property"))
return false;
return true;
}
// skip past digits and '@', this field is only so that we don't ever get duplicates in the
// output even if the same message is printed
while(idx < line.length() && (line[idx] == '@' || isalnum(line[idx])))
idx++;
EXPECT_CHAR(' ');
// should be at least 64 more characters
if(idx + 64 > line.length())
return false;
if(strncmp(&line[idx], "RDOC ", 5) != 0)
return false;
idx += 5;
pid = 0;
while(idx < line.length() && isdigit(line[idx]))
{
pid *= 10;
pid += int(line[idx] - '0');
idx++;
}
EXPECT_CHAR(':');
EXPECT_CHAR(' ');
EXPECT_CHAR('[');
// expect HH:MM:SS
if(idx + 8 >= line.length())
return false;
// we only need the time part, so just take it from the epoch
uint32_t h = 0, m = 0, s = 0;
h = int(line[idx + 0] - '0') * 10 + int(line[idx + 1] - '0');
m = int(line[idx + 3] - '0') * 10 + int(line[idx + 4] - '0');
s = int(line[idx + 6] - '0') * 10 + int(line[idx + 7] - '0');
if(line[idx + 2] != ':' || line[idx + 5] != ':')
return false;
timestamp = (h * 60 + m) * 60 + s;
idx += 8;
EXPECT_CHAR(']');
while(idx < line.length() && line[idx] != '(')
{
filename.push_back(line[idx]);
idx++;
}
// strip spaces
filename.trim();
if(filename.empty())
return false;
EXPECT_CHAR('(');
while(idx < line.length() && isspace(line[idx]))
idx++;
line_number = 0;
while(idx < line.length() && isdigit(line[idx]))
{
line_number *= 10;
line_number += int(line[idx] - '0');
idx++;
}
EXPECT_CHAR(')');
EXPECT_CHAR(' ');
EXPECT_CHAR('-');
EXPECT_CHAR(' ');
rdcstr logtype_str;
while(idx < line.length() && line[idx] != '-')
{
logtype_str.push_back(line[idx]);
idx++;
}
logtype_str.trim();
if(logtype_str == "Debug")
logtype = LogType::Debug;
else if(logtype_str == "Log")
logtype = LogType::Comment;
else if(logtype_str == "Warning")
logtype = LogType::Warning;
else if(logtype_str == "Error")
logtype = LogType::Error;
else if(logtype_str == "Fatal")
logtype = LogType::Fatal;
else
return false;
EXPECT_CHAR('-');
EXPECT_CHAR(' ');
if(idx >= line.length())
return false;
message = line.substr(idx);
// if adb gave us DOS newlines, remove the \r
if(message.back() == '\r')
message.pop_back();
#undef EXPECT_CHAR
return true;
}
};
// we need to keep track of logcat threads, so that if we start a new one up on a device before the
// old one has finished, we don't start overlapping and double-printing messages.
static Threading::CriticalSection logcatThreadLock;
static std::map<rdcstr, LogcatThread *> logcatThreads;
LogcatThread *ProcessLogcat(rdcstr deviceID)
{
LogcatThread *ret = NULL;
Threading::ThreadHandle joinThread = 0;
// ensure any previous thread running is really finished
{
SCOPED_LOCK(logcatThreadLock);
// if this thread is running, kill it immediately
if(logcatThreads[deviceID])
{
joinThread = logcatThreads[deviceID]->thread;
{
SCOPED_LOCK(logcatThreads[deviceID]->lock);
logcatThreads[deviceID]->immediateExit = true;
}
}
}
// if we had a thread to join, do so now. It will remove itself from the above map and
// self-delete, but not detach the thread
if(joinThread)
{
Threading::JoinThread(joinThread);
Threading::CloseThread(joinThread);
}
// start a new thread to monitor this device's logcat
ret = new LogcatThread;
{
SCOPED_LOCK(logcatThreadLock);
logcatThreads[deviceID] = ret;
}
ret->deviceID = deviceID;
ret->thread = Threading::CreateThread([ret]() {
bool done = false;
RDCDEBUG("Starting monitoring logcat on %s", ret->deviceID.c_str());
while(!done)
{
// tick the logcat
ret->Tick();
// sleep 400ms, but in small chunks to let us respond to immediateExit more quickly
for(int i = 0; i < 10; i++)
{
Threading::Sleep(40);
time_t now = Timing::GetUTCTime();
{
SCOPED_LOCK(ret->lock);
if(ret->immediateExit || (ret->finishTime && ret->finishTime + 5 < now))
{
done = true;
break;
}
}
}
}
RDCDEBUG("Stopping monitoring logcat on %s", ret->deviceID.c_str());
bool detach = true;
// we need to exit. Take the logcat thread lock first
{
SCOPED_LOCK(logcatThreadLock);
// if our immediateExit flag is set then we shouldn't detach, as the above code will join
// with this thread to be sure we're done
{
SCOPED_LOCK(ret->lock);
detach = !ret->immediateExit;
}
// remove ourselves from the map now, so that as soon as the lock is released the above code
// can safely check for no overlap (we won't tick again so even if the threads overlap the
// processing won't).
logcatThreads[ret->deviceID] = NULL;
}
// if we should detach because no-one is going to join us, do that now
if(detach)
Threading::DetachThread(ret->thread);
ret->thread = 0;
// finally see if we can delete the struct
ret->SelfDelete();
});
return ret;
}
void LogcatThread::Finish()
{
SCOPED_LOCK(lock);
finishTime = Timing::GetUTCTime();
SelfDelete();
}
void LogcatThread::Tick()
{
// adb is extremely unreliable, so although it supposedly contains functionality to filter for
// everything after a certain timestamp, this can actually drop messages. Instead we just always
// grab the last 750 lines and hope that the device doesn't ever peak over 1 line per millisecond
// such that we'd miss something. Note another joy of adb - the line count is applied *before*
// the filter, so if something else spams 1000 lines we won't see our own.
const uint32_t lineBacklog = 750;
// logcat
// -t N // always the last N messages, and (implied -d) stop after doing so
// -v brief // print the 'brief' format
// -s // silence everything as a default
// renderdoc:* // print logcats from our tag.
// libc:* // or from libc (prints crash messages)
// DEBUG:* // or from DEBUG (prints crash messages)
//
// This gives us all messages from renderdoc since the last timestamp.
rdcstr command =
StringFormat::Fmt("logcat -t %u -v brief -s renderdoc:* libc:* DEBUG:*", lineBacklog);
rdcstr logcat = Android::adbExecCommand(deviceID, command, ".", true).strStdout.trimmed();
rdcarray<rdcstr> lines;
split(logcat, lines, '\n');
// remove \n from any lines right now to prevent it breaking further processing
for(rdcstr &line : lines)
if(!line.empty() && line.back() == '\r')
line.pop_back();
// only do any processing if we had a line last time that we know to start from.
if(!lastLogcatLine.empty())
{
int idx = lines.indexOf(lastLogcatLine);
if(idx >= 0)
{
// remove everything up to and including that line
lines.erase(0, idx + 1);
}
else
{
RDCWARN("Couldn't find last line. Potentially missed logcat messages.");
}
for(const rdcstr &line : lines)
{
LogLine logline;
if(logline.parse(line))
{
rdclog_direct(logline.timestamp, logline.pid, logline.logtype, "ADRD",
logline.filename.c_str(), logline.line_number, "%s", logline.message.c_str());
rdclog_flush();
}
}
}
// store the last line (if we have one) to search for and start from next time
if(!lines.empty())
lastLogcatLine = lines.back();
}
};
template <>
rdcstr DoStringise(const Android::ABI &el)
{
BEGIN_ENUM_STRINGISE(Android::ABI);
{
STRINGISE_ENUM_CLASS(unknown);
STRINGISE_ENUM_CLASS(armeabi_v7a);
STRINGISE_ENUM_CLASS(arm64_v8a);
STRINGISE_ENUM_CLASS(x86);
STRINGISE_ENUM_CLASS(x86_64);
}
END_ENUM_STRINGISE();
}
#if ENABLED(ENABLE_UNIT_TESTS)
#undef None
#include "catch/catch.hpp"
TEST_CASE("Test that log line parsing is robust", "[android]")
{
using namespace Android;
SECTION("Empty string")
{
LogLine line;
CHECK(line.parse("") == false);
};
SECTION("Invalid strings")
{
LogLine line1;
CHECK(line1.parse("--------- beginning of main") == false);
LogLine line2;
CHECK(
line2.parse("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam lacus lacus, "
"egestas vitae elementum sit amet, venenatis ac nunc.") == false);
};
SECTION("Valid strings")
{
LogLine line;
const char *valid_text =
R"(I/renderdoc( 1234): @1234567812345678@ RDOC 001234: [01:02:03] filename.cpp( 123) - Warning - Hello)";
CHECK(line.parse(valid_text) == true);
CHECK(line.filename == "filename.cpp");
CHECK(line.line_number == 123);
CHECK((line.logtype == LogType::Warning));
CHECK(line.message == "Hello");
CHECK(line.pid == 1234);
CHECK(line.timestamp == 3723);
LogLine highpid;
const char *highpid_text =
R"(I/renderdoc(12345678): @1234567812345678@ RDOC 12345678: [01:02:03] filename.cpp( 123) - Warning - Hello)";
CHECK(highpid.parse(highpid_text) == true);
CHECK(highpid.filename == "filename.cpp");
CHECK(highpid.line_number == 123);
CHECK((highpid.logtype == LogType::Warning));
CHECK(highpid.message == "Hello");
CHECK(highpid.pid == 12345678);
CHECK(highpid.timestamp == 3723);
LogLine longname;
const char *longname_text =
R"(I/renderdoc( 1234): @1234567812345678@ RDOC 001234: [01:02:03] a_very_long_source_filename.cpp( 123) - Warning - Hello)";
CHECK(longname.parse(longname_text) == true);
CHECK(longname.filename == "a_very_long_source_filename.cpp");
CHECK(longname.line_number == 123);
CHECK((longname.logtype == LogType::Warning));
CHECK(longname.message == "Hello");
CHECK(longname.pid == 1234);
CHECK(longname.timestamp == 3723);
LogLine longlinenum;
const char *longlinenum_text =
R"(I/renderdoc( 1234): @1234567812345678@ RDOC 001234: [01:02:03] filename.cpp(12345678) - Warning - Hello)";
CHECK(longlinenum.parse(longlinenum_text) == true);
CHECK(longlinenum.filename == "filename.cpp");
CHECK(longlinenum.line_number == 12345678);
CHECK((longlinenum.logtype == LogType::Warning));
CHECK(longlinenum.message == "Hello");
CHECK(longlinenum.pid == 1234);
CHECK(longlinenum.timestamp == 3723);
};
SECTION("Invalid strings - truncated")
{
rdcstr truncated =
R"(I/renderdoc( 1234): @1234567812345678@ RDOC 001234: [01:02:03] filename.cpp( 123) - Warning - H)";
LogLine working;
CHECK(working.parse(truncated) == true);
while(!truncated.empty())
{
truncated.pop_back();
LogLine broken;
CHECK(broken.parse(truncated) == false);
}
};
}
#endif