File: tombstone_test.cpp

package info (click to toggle)
android-platform-system-core 1%3A10.0.0%2Br36-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 123,760 kB
  • sloc: cpp: 197,034; ansic: 18,211; asm: 3,606; sh: 3,180; python: 2,671; java: 693; xml: 266; makefile: 237
file content (825 lines) | stat: -rw-r--r-- 34,308 bytes parent folder | download
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
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <sys/mman.h>
#include <time.h>

#include <memory>
#include <string>

#include <android-base/file.h>
#include <android-base/properties.h>
#include <gtest/gtest.h>

#include "libdebuggerd/utility.h"

#include "UnwinderMock.h"
#include "host_signal_fixup.h"
#include "log_fake.h"

#include "tombstone.cpp"

class TombstoneTest : public ::testing::Test {
 protected:
  virtual void SetUp() {
    unwinder_mock_.reset(new UnwinderMock());

    char tmp_file[256];
    const char data_template[] = "/data/local/tmp/debuggerd_memory_testXXXXXX";
    memcpy(tmp_file, data_template, sizeof(data_template));
    int tombstone_fd = mkstemp(tmp_file);
    if (tombstone_fd == -1) {
      const char tmp_template[] = "/tmp/debuggerd_memory_testXXXXXX";
      memcpy(tmp_file, tmp_template, sizeof(tmp_template));
      tombstone_fd = mkstemp(tmp_file);
      if (tombstone_fd == -1) {
        abort();
      }
    }
    if (unlink(tmp_file) == -1) {
      abort();
    }

    log_.tfd = tombstone_fd;
    amfd_data_.clear();
    log_.amfd_data = &amfd_data_;
    log_.crashed_tid = 12;
    log_.current_tid = 12;
    log_.should_retrieve_logcat = false;

    resetLogs();
  }

  virtual void TearDown() {
    if (log_.tfd >= 0) {
      close(log_.tfd);
    }
  }

  std::unique_ptr<UnwinderMock> unwinder_mock_;

  log_t log_;
  std::string amfd_data_;
};

TEST_F(TombstoneTest, single_map) {
#if defined(__LP64__)
  unwinder_mock_->MockAddMap(0x123456789abcd000UL, 0x123456789abdf000UL, 0, 0, "", 0);
#else
  unwinder_mock_->MockAddMap(0x1234000, 0x1235000, 0, 0, "", 0);
#endif

  dump_all_maps(&log_, unwinder_mock_.get(), 0);

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  const char* expected_dump = \
"\nmemory map (1 entry):\n"
#if defined(__LP64__)
"    12345678'9abcd000-12345678'9abdefff ---         0     12000\n";
#else
"    01234000-01234fff ---         0      1000\n";
#endif
  ASSERT_STREQ(expected_dump, tombstone_contents.c_str());

  ASSERT_STREQ("", amfd_data_.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(TombstoneTest, single_map_elf_build_id) {
  uint64_t build_id_offset;
#if defined(__LP64__)
  build_id_offset = 0x123456789abcd000UL;
  unwinder_mock_->MockAddMap(build_id_offset, 0x123456789abdf000UL, 0, PROT_READ,
                             "/system/lib/libfake.so", 0);
#else
  build_id_offset = 0x1234000;
  unwinder_mock_->MockAddMap(0x1234000, 0x1235000, 0, PROT_READ, "/system/lib/libfake.so", 0);
#endif

  unwinder_mock_->MockSetBuildID(
      build_id_offset,
      std::string{static_cast<char>(0xab), static_cast<char>(0xcd), static_cast<char>(0xef),
                  static_cast<char>(0x12), static_cast<char>(0x34), static_cast<char>(0x56),
                  static_cast<char>(0x78), static_cast<char>(0x90), static_cast<char>(0xab),
                  static_cast<char>(0xcd), static_cast<char>(0xef), static_cast<char>(0x12),
                  static_cast<char>(0x34), static_cast<char>(0x56), static_cast<char>(0x78),
                  static_cast<char>(0x90)});
  dump_all_maps(&log_, unwinder_mock_.get(), 0);

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  const char* expected_dump = \
"\nmemory map (1 entry):\n"
#if defined(__LP64__)
"    12345678'9abcd000-12345678'9abdefff r--         0     12000  /system/lib/libfake.so (BuildId: abcdef1234567890abcdef1234567890)\n";
#else
"    01234000-01234fff r--         0      1000  /system/lib/libfake.so (BuildId: abcdef1234567890abcdef1234567890)\n";
#endif
  ASSERT_STREQ(expected_dump, tombstone_contents.c_str());

  ASSERT_STREQ("", amfd_data_.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(TombstoneTest, multiple_maps) {
  unwinder_mock_->MockAddMap(0xa234000, 0xa235000, 0, 0, "", 0);
  unwinder_mock_->MockAddMap(0xa334000, 0xa335000, 0xf000, PROT_READ, "", 0);
  unwinder_mock_->MockAddMap(0xa434000, 0xa435000, 0x1000, PROT_WRITE, "", 0xd000);
  unwinder_mock_->MockAddMap(0xa534000, 0xa535000, 0x3000, PROT_EXEC, "", 0x2000);
  unwinder_mock_->MockAddMap(0xa634000, 0xa635000, 0, PROT_READ | PROT_WRITE | PROT_EXEC,
                             "/system/lib/fake.so", 0);

  dump_all_maps(&log_, unwinder_mock_.get(), 0);

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  const char* expected_dump =
      "\nmemory map (5 entries):\n"
#if defined(__LP64__)
      "    00000000'0a234000-00000000'0a234fff ---         0      1000\n"
      "    00000000'0a334000-00000000'0a334fff r--      f000      1000\n"
      "    00000000'0a434000-00000000'0a434fff -w-      1000      1000  (load bias 0xd000)\n"
      "    00000000'0a534000-00000000'0a534fff --x      3000      1000  (load bias 0x2000)\n"
      "    00000000'0a634000-00000000'0a634fff rwx         0      1000  /system/lib/fake.so\n";
#else
      "    0a234000-0a234fff ---         0      1000\n"
      "    0a334000-0a334fff r--      f000      1000\n"
      "    0a434000-0a434fff -w-      1000      1000  (load bias 0xd000)\n"
      "    0a534000-0a534fff --x      3000      1000  (load bias 0x2000)\n"
      "    0a634000-0a634fff rwx         0      1000  /system/lib/fake.so\n";
#endif
  ASSERT_STREQ(expected_dump, tombstone_contents.c_str());

  ASSERT_STREQ("", amfd_data_.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(TombstoneTest, multiple_maps_fault_address_before) {
  unwinder_mock_->MockAddMap(0xa434000, 0xa435000, 0x1000, PROT_WRITE, "", 0xd000);
  unwinder_mock_->MockAddMap(0xa534000, 0xa535000, 0x3000, PROT_EXEC, "", 0x2000);
  unwinder_mock_->MockAddMap(0xa634000, 0xa635000, 0, PROT_READ | PROT_WRITE | PROT_EXEC,
                             "/system/lib/fake.so", 0);

  dump_all_maps(&log_, unwinder_mock_.get(), 0x1000);

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  const char* expected_dump =
      "\nmemory map (3 entries):\n"
#if defined(__LP64__)
      "--->Fault address falls at 00000000'00001000 before any mapped regions\n"
      "    00000000'0a434000-00000000'0a434fff -w-      1000      1000  (load bias 0xd000)\n"
      "    00000000'0a534000-00000000'0a534fff --x      3000      1000  (load bias 0x2000)\n"
      "    00000000'0a634000-00000000'0a634fff rwx         0      1000  /system/lib/fake.so\n";
#else
      "--->Fault address falls at 00001000 before any mapped regions\n"
      "    0a434000-0a434fff -w-      1000      1000  (load bias 0xd000)\n"
      "    0a534000-0a534fff --x      3000      1000  (load bias 0x2000)\n"
      "    0a634000-0a634fff rwx         0      1000  /system/lib/fake.so\n";
#endif
  ASSERT_STREQ(expected_dump, tombstone_contents.c_str());

  ASSERT_STREQ("", amfd_data_.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(TombstoneTest, multiple_maps_fault_address_between) {
  unwinder_mock_->MockAddMap(0xa434000, 0xa435000, 0x1000, PROT_WRITE, "", 0xd000);
  unwinder_mock_->MockAddMap(0xa534000, 0xa535000, 0x3000, PROT_EXEC, "", 0x2000);
  unwinder_mock_->MockAddMap(0xa634000, 0xa635000, 0, PROT_READ | PROT_WRITE | PROT_EXEC,
                             "/system/lib/fake.so", 0);

  dump_all_maps(&log_, unwinder_mock_.get(), 0xa533000);

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  const char* expected_dump =
      "\nmemory map (3 entries): (fault address prefixed with --->)\n"
#if defined(__LP64__)
      "    00000000'0a434000-00000000'0a434fff -w-      1000      1000  (load bias 0xd000)\n"
      "--->Fault address falls at 00000000'0a533000 between mapped regions\n"
      "    00000000'0a534000-00000000'0a534fff --x      3000      1000  (load bias 0x2000)\n"
      "    00000000'0a634000-00000000'0a634fff rwx         0      1000  /system/lib/fake.so\n";
#else
      "    0a434000-0a434fff -w-      1000      1000  (load bias 0xd000)\n"
      "--->Fault address falls at 0a533000 between mapped regions\n"
      "    0a534000-0a534fff --x      3000      1000  (load bias 0x2000)\n"
      "    0a634000-0a634fff rwx         0      1000  /system/lib/fake.so\n";
#endif
  ASSERT_STREQ(expected_dump, tombstone_contents.c_str());

  ASSERT_STREQ("", amfd_data_.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(TombstoneTest, multiple_maps_fault_address_in_map) {
  unwinder_mock_->MockAddMap(0xa434000, 0xa435000, 0x1000, PROT_WRITE, "", 0xd000);
  unwinder_mock_->MockAddMap(0xa534000, 0xa535000, 0x3000, PROT_EXEC, "", 0x2000);
  unwinder_mock_->MockAddMap(0xa634000, 0xa635000, 0, PROT_READ | PROT_WRITE | PROT_EXEC,
                             "/system/lib/fake.so", 0);

  dump_all_maps(&log_, unwinder_mock_.get(), 0xa534040);

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  const char* expected_dump =
      "\nmemory map (3 entries): (fault address prefixed with --->)\n"
#if defined(__LP64__)
      "    00000000'0a434000-00000000'0a434fff -w-      1000      1000  (load bias 0xd000)\n"
      "--->00000000'0a534000-00000000'0a534fff --x      3000      1000  (load bias 0x2000)\n"
      "    00000000'0a634000-00000000'0a634fff rwx         0      1000  /system/lib/fake.so\n";
#else
      "    0a434000-0a434fff -w-      1000      1000  (load bias 0xd000)\n"
      "--->0a534000-0a534fff --x      3000      1000  (load bias 0x2000)\n"
      "    0a634000-0a634fff rwx         0      1000  /system/lib/fake.so\n";
#endif
  ASSERT_STREQ(expected_dump, tombstone_contents.c_str());

  ASSERT_STREQ("", amfd_data_.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(TombstoneTest, multiple_maps_fault_address_after) {
  unwinder_mock_->MockAddMap(0xa434000, 0xa435000, 0x1000, PROT_WRITE, "", 0xd000);
  unwinder_mock_->MockAddMap(0xa534000, 0xa535000, 0x3000, PROT_EXEC, "", 0x2000);
  unwinder_mock_->MockAddMap(0xa634000, 0xa635000, 0, PROT_READ | PROT_WRITE | PROT_EXEC,
                             "/system/lib/fake.so", 0);

#if defined(__LP64__)
  uint64_t addr = 0x12345a534040UL;
#else
  uint64_t addr = 0xf534040UL;
#endif
  dump_all_maps(&log_, unwinder_mock_.get(), addr);

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  const char* expected_dump =
      "\nmemory map (3 entries): (fault address prefixed with --->)\n"
#if defined(__LP64__)
      "    00000000'0a434000-00000000'0a434fff -w-      1000      1000  (load bias 0xd000)\n"
      "    00000000'0a534000-00000000'0a534fff --x      3000      1000  (load bias 0x2000)\n"
      "    00000000'0a634000-00000000'0a634fff rwx         0      1000  /system/lib/fake.so\n"
      "--->Fault address falls at 00001234'5a534040 after any mapped regions\n";
#else
      "    0a434000-0a434fff -w-      1000      1000  (load bias 0xd000)\n"
      "    0a534000-0a534fff --x      3000      1000  (load bias 0x2000)\n"
      "    0a634000-0a634fff rwx         0      1000  /system/lib/fake.so\n"
      "--->Fault address falls at 0f534040 after any mapped regions\n";
#endif
  ASSERT_STREQ(expected_dump, tombstone_contents.c_str());

  ASSERT_STREQ("", amfd_data_.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(TombstoneTest, dump_log_file_error) {
  log_.should_retrieve_logcat = true;
  dump_log_file(&log_, 123, "/fake/filename", 10);

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  ASSERT_STREQ("", tombstone_contents.c_str());

  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("6 DEBUG Unable to open /fake/filename: Permission denied\n\n",
               getFakeLogPrint().c_str());

  ASSERT_STREQ("", amfd_data_.c_str());
}

TEST_F(TombstoneTest, dump_header_info) {
  dump_header_info(&log_);

  std::string expected = android::base::StringPrintf(
      "Build fingerprint: '%s'\nRevision: '%s'\n",
      android::base::GetProperty("ro.build.fingerprint", "unknown").c_str(),
      android::base::GetProperty("ro.revision", "unknown").c_str());
  expected += android::base::StringPrintf("ABI: '%s'\n", ABI_STRING);
  ASSERT_STREQ(expected.c_str(), amfd_data_.c_str());
}

TEST_F(TombstoneTest, dump_thread_info_uid) {
  dump_thread_info(&log_, ThreadInfo{.uid = 1,
                                     .pid = 2,
                                     .tid = 3,
                                     .thread_name = "some_thread",
                                     .process_name = "some_process"});
  std::string expected = "pid: 2, tid: 3, name: some_thread  >>> some_process <<<\nuid: 1\n";
  ASSERT_STREQ(expected.c_str(), amfd_data_.c_str());
}

TEST_F(TombstoneTest, dump_timestamp) {
  setenv("TZ", "UTC", 1);
  tzset();
  dump_timestamp(&log_, 0);
  ASSERT_STREQ("Timestamp: 1970-01-01 00:00:00+0000\n", amfd_data_.c_str());
}

class MemoryPattern : public unwindstack::Memory {
 public:
  MemoryPattern() = default;
  virtual ~MemoryPattern() = default;

  size_t Read(uint64_t, void* dst, size_t size) override {
    uint8_t* data = reinterpret_cast<uint8_t*>(dst);
    for (size_t i = 0; i < size; i++) {
      data[i] = (i % 0xff);
    }
    return size;
  }
};

TEST_F(TombstoneTest, dump_stack_single_frame) {
  std::vector<unwindstack::FrameData> frames;
  unwindstack::Maps maps;
  MemoryPattern memory;

  frames.push_back(
      unwindstack::FrameData{.num = 0, .rel_pc = 0x1000, .pc = 0x301000, .sp = 0x2000});
  dump_stack(&log_, frames, &maps, &memory);

  std::string contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &contents));

  std::string expected =
#if defined(__LP64__)
      "         0000000000001f80  0706050403020100\n"
      "         0000000000001f88  0f0e0d0c0b0a0908\n"
      "         0000000000001f90  1716151413121110\n"
      "         0000000000001f98  1f1e1d1c1b1a1918\n"
      "         0000000000001fa0  2726252423222120\n"
      "         0000000000001fa8  2f2e2d2c2b2a2928\n"
      "         0000000000001fb0  3736353433323130\n"
      "         0000000000001fb8  3f3e3d3c3b3a3938\n"
      "         0000000000001fc0  4746454443424140\n"
      "         0000000000001fc8  4f4e4d4c4b4a4948\n"
      "         0000000000001fd0  5756555453525150\n"
      "         0000000000001fd8  5f5e5d5c5b5a5958\n"
      "         0000000000001fe0  6766656463626160\n"
      "         0000000000001fe8  6f6e6d6c6b6a6968\n"
      "         0000000000001ff0  7776757473727170\n"
      "         0000000000001ff8  7f7e7d7c7b7a7978\n"
      "    #00  0000000000002000  0706050403020100\n"
      "         0000000000002008  0f0e0d0c0b0a0908\n"
      "         0000000000002010  1716151413121110\n"
      "         0000000000002018  1f1e1d1c1b1a1918\n"
      "         0000000000002020  2726252423222120\n"
      "         0000000000002028  2f2e2d2c2b2a2928\n"
      "         0000000000002030  3736353433323130\n"
      "         0000000000002038  3f3e3d3c3b3a3938\n"
      "         0000000000002040  4746454443424140\n"
      "         0000000000002048  4f4e4d4c4b4a4948\n"
      "         0000000000002050  5756555453525150\n"
      "         0000000000002058  5f5e5d5c5b5a5958\n"
      "         0000000000002060  6766656463626160\n"
      "         0000000000002068  6f6e6d6c6b6a6968\n"
      "         0000000000002070  7776757473727170\n"
      "         0000000000002078  7f7e7d7c7b7a7978\n";
#else
      "         00001fc0  03020100\n"
      "         00001fc4  07060504\n"
      "         00001fc8  0b0a0908\n"
      "         00001fcc  0f0e0d0c\n"
      "         00001fd0  13121110\n"
      "         00001fd4  17161514\n"
      "         00001fd8  1b1a1918\n"
      "         00001fdc  1f1e1d1c\n"
      "         00001fe0  23222120\n"
      "         00001fe4  27262524\n"
      "         00001fe8  2b2a2928\n"
      "         00001fec  2f2e2d2c\n"
      "         00001ff0  33323130\n"
      "         00001ff4  37363534\n"
      "         00001ff8  3b3a3938\n"
      "         00001ffc  3f3e3d3c\n"
      "    #00  00002000  03020100\n"
      "         00002004  07060504\n"
      "         00002008  0b0a0908\n"
      "         0000200c  0f0e0d0c\n"
      "         00002010  13121110\n"
      "         00002014  17161514\n"
      "         00002018  1b1a1918\n"
      "         0000201c  1f1e1d1c\n"
      "         00002020  23222120\n"
      "         00002024  27262524\n"
      "         00002028  2b2a2928\n"
      "         0000202c  2f2e2d2c\n"
      "         00002030  33323130\n"
      "         00002034  37363534\n"
      "         00002038  3b3a3938\n"
      "         0000203c  3f3e3d3c\n";
#endif
  EXPECT_EQ(expected, contents);
}

TEST_F(TombstoneTest, dump_stack_multiple_frames_same_sp) {
  std::vector<unwindstack::FrameData> frames;
  unwindstack::Maps maps;
  MemoryPattern memory;

  frames.push_back(
      unwindstack::FrameData{.num = 0, .rel_pc = 0x1000, .pc = 0x301000, .sp = 0x2000});
  frames.push_back(
      unwindstack::FrameData{.num = 0, .rel_pc = 0x1400, .pc = 0x301400, .sp = 0x2000});
  dump_stack(&log_, frames, &maps, &memory);

  std::string contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &contents));

  std::string expected =
#if defined(__LP64__)
      "         0000000000001f80  0706050403020100\n"
      "         0000000000001f88  0f0e0d0c0b0a0908\n"
      "         0000000000001f90  1716151413121110\n"
      "         0000000000001f98  1f1e1d1c1b1a1918\n"
      "         0000000000001fa0  2726252423222120\n"
      "         0000000000001fa8  2f2e2d2c2b2a2928\n"
      "         0000000000001fb0  3736353433323130\n"
      "         0000000000001fb8  3f3e3d3c3b3a3938\n"
      "         0000000000001fc0  4746454443424140\n"
      "         0000000000001fc8  4f4e4d4c4b4a4948\n"
      "         0000000000001fd0  5756555453525150\n"
      "         0000000000001fd8  5f5e5d5c5b5a5958\n"
      "         0000000000001fe0  6766656463626160\n"
      "         0000000000001fe8  6f6e6d6c6b6a6968\n"
      "         0000000000001ff0  7776757473727170\n"
      "         0000000000001ff8  7f7e7d7c7b7a7978\n"
      "    #00  0000000000002000  0706050403020100\n"
      "         ................  ................\n"
      "    #01  0000000000002000  0706050403020100\n"
      "         0000000000002008  0f0e0d0c0b0a0908\n"
      "         0000000000002010  1716151413121110\n"
      "         0000000000002018  1f1e1d1c1b1a1918\n"
      "         0000000000002020  2726252423222120\n"
      "         0000000000002028  2f2e2d2c2b2a2928\n"
      "         0000000000002030  3736353433323130\n"
      "         0000000000002038  3f3e3d3c3b3a3938\n"
      "         0000000000002040  4746454443424140\n"
      "         0000000000002048  4f4e4d4c4b4a4948\n"
      "         0000000000002050  5756555453525150\n"
      "         0000000000002058  5f5e5d5c5b5a5958\n"
      "         0000000000002060  6766656463626160\n"
      "         0000000000002068  6f6e6d6c6b6a6968\n"
      "         0000000000002070  7776757473727170\n"
      "         0000000000002078  7f7e7d7c7b7a7978\n";
#else
      "         00001fc0  03020100\n"
      "         00001fc4  07060504\n"
      "         00001fc8  0b0a0908\n"
      "         00001fcc  0f0e0d0c\n"
      "         00001fd0  13121110\n"
      "         00001fd4  17161514\n"
      "         00001fd8  1b1a1918\n"
      "         00001fdc  1f1e1d1c\n"
      "         00001fe0  23222120\n"
      "         00001fe4  27262524\n"
      "         00001fe8  2b2a2928\n"
      "         00001fec  2f2e2d2c\n"
      "         00001ff0  33323130\n"
      "         00001ff4  37363534\n"
      "         00001ff8  3b3a3938\n"
      "         00001ffc  3f3e3d3c\n"
      "    #00  00002000  03020100\n"
      "         ........  ........\n"
      "    #01  00002000  03020100\n"
      "         00002004  07060504\n"
      "         00002008  0b0a0908\n"
      "         0000200c  0f0e0d0c\n"
      "         00002010  13121110\n"
      "         00002014  17161514\n"
      "         00002018  1b1a1918\n"
      "         0000201c  1f1e1d1c\n"
      "         00002020  23222120\n"
      "         00002024  27262524\n"
      "         00002028  2b2a2928\n"
      "         0000202c  2f2e2d2c\n"
      "         00002030  33323130\n"
      "         00002034  37363534\n"
      "         00002038  3b3a3938\n"
      "         0000203c  3f3e3d3c\n";
#endif
  EXPECT_EQ(expected, contents);
}

TEST_F(TombstoneTest, dump_stack_multiple_frames) {
  std::vector<unwindstack::FrameData> frames;
  unwindstack::Maps maps;
  MemoryPattern memory;

  frames.push_back(
      unwindstack::FrameData{.num = 0, .rel_pc = 0x1000, .pc = 0x301000, .sp = 0x2000});
  frames.push_back(
      unwindstack::FrameData{.num = 0, .rel_pc = 0x1400, .pc = 0x301400, .sp = 0x2010});
  frames.push_back(
      unwindstack::FrameData{.num = 0, .rel_pc = 0x1400, .pc = 0x301400, .sp = 0x2100});
  dump_stack(&log_, frames, &maps, &memory);

  std::string contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &contents));

  std::string expected =
#if defined(__LP64__)
      "         0000000000001f80  0706050403020100\n"
      "         0000000000001f88  0f0e0d0c0b0a0908\n"
      "         0000000000001f90  1716151413121110\n"
      "         0000000000001f98  1f1e1d1c1b1a1918\n"
      "         0000000000001fa0  2726252423222120\n"
      "         0000000000001fa8  2f2e2d2c2b2a2928\n"
      "         0000000000001fb0  3736353433323130\n"
      "         0000000000001fb8  3f3e3d3c3b3a3938\n"
      "         0000000000001fc0  4746454443424140\n"
      "         0000000000001fc8  4f4e4d4c4b4a4948\n"
      "         0000000000001fd0  5756555453525150\n"
      "         0000000000001fd8  5f5e5d5c5b5a5958\n"
      "         0000000000001fe0  6766656463626160\n"
      "         0000000000001fe8  6f6e6d6c6b6a6968\n"
      "         0000000000001ff0  7776757473727170\n"
      "         0000000000001ff8  7f7e7d7c7b7a7978\n"
      "    #00  0000000000002000  0706050403020100\n"
      "         0000000000002008  0f0e0d0c0b0a0908\n"
      "    #01  0000000000002010  0706050403020100\n"
      "         0000000000002018  0f0e0d0c0b0a0908\n"
      "         0000000000002020  1716151413121110\n"
      "         0000000000002028  1f1e1d1c1b1a1918\n"
      "         0000000000002030  2726252423222120\n"
      "         0000000000002038  2f2e2d2c2b2a2928\n"
      "         0000000000002040  3736353433323130\n"
      "         0000000000002048  3f3e3d3c3b3a3938\n"
      "         0000000000002050  4746454443424140\n"
      "         0000000000002058  4f4e4d4c4b4a4948\n"
      "         0000000000002060  5756555453525150\n"
      "         0000000000002068  5f5e5d5c5b5a5958\n"
      "         0000000000002070  6766656463626160\n"
      "         0000000000002078  6f6e6d6c6b6a6968\n"
      "         0000000000002080  7776757473727170\n"
      "         0000000000002088  7f7e7d7c7b7a7978\n"
      "         ................  ................\n"
      "    #02  0000000000002100  0706050403020100\n"
      "         0000000000002108  0f0e0d0c0b0a0908\n"
      "         0000000000002110  1716151413121110\n"
      "         0000000000002118  1f1e1d1c1b1a1918\n"
      "         0000000000002120  2726252423222120\n"
      "         0000000000002128  2f2e2d2c2b2a2928\n"
      "         0000000000002130  3736353433323130\n"
      "         0000000000002138  3f3e3d3c3b3a3938\n"
      "         0000000000002140  4746454443424140\n"
      "         0000000000002148  4f4e4d4c4b4a4948\n"
      "         0000000000002150  5756555453525150\n"
      "         0000000000002158  5f5e5d5c5b5a5958\n"
      "         0000000000002160  6766656463626160\n"
      "         0000000000002168  6f6e6d6c6b6a6968\n"
      "         0000000000002170  7776757473727170\n"
      "         0000000000002178  7f7e7d7c7b7a7978\n";
#else
      "         00001fc0  03020100\n"
      "         00001fc4  07060504\n"
      "         00001fc8  0b0a0908\n"
      "         00001fcc  0f0e0d0c\n"
      "         00001fd0  13121110\n"
      "         00001fd4  17161514\n"
      "         00001fd8  1b1a1918\n"
      "         00001fdc  1f1e1d1c\n"
      "         00001fe0  23222120\n"
      "         00001fe4  27262524\n"
      "         00001fe8  2b2a2928\n"
      "         00001fec  2f2e2d2c\n"
      "         00001ff0  33323130\n"
      "         00001ff4  37363534\n"
      "         00001ff8  3b3a3938\n"
      "         00001ffc  3f3e3d3c\n"
      "    #00  00002000  03020100\n"
      "         00002004  07060504\n"
      "         00002008  0b0a0908\n"
      "         0000200c  0f0e0d0c\n"
      "    #01  00002010  03020100\n"
      "         00002014  07060504\n"
      "         00002018  0b0a0908\n"
      "         0000201c  0f0e0d0c\n"
      "         00002020  13121110\n"
      "         00002024  17161514\n"
      "         00002028  1b1a1918\n"
      "         0000202c  1f1e1d1c\n"
      "         00002030  23222120\n"
      "         00002034  27262524\n"
      "         00002038  2b2a2928\n"
      "         0000203c  2f2e2d2c\n"
      "         00002040  33323130\n"
      "         00002044  37363534\n"
      "         00002048  3b3a3938\n"
      "         0000204c  3f3e3d3c\n"
      "         ........  ........\n"
      "    #02  00002100  03020100\n"
      "         00002104  07060504\n"
      "         00002108  0b0a0908\n"
      "         0000210c  0f0e0d0c\n"
      "         00002110  13121110\n"
      "         00002114  17161514\n"
      "         00002118  1b1a1918\n"
      "         0000211c  1f1e1d1c\n"
      "         00002120  23222120\n"
      "         00002124  27262524\n"
      "         00002128  2b2a2928\n"
      "         0000212c  2f2e2d2c\n"
      "         00002130  33323130\n"
      "         00002134  37363534\n"
      "         00002138  3b3a3938\n"
      "         0000213c  3f3e3d3c\n";
#endif
  EXPECT_EQ(expected, contents);
}

TEST_F(TombstoneTest, dump_stack_multiple_frames_disjoint_frames) {
  std::vector<unwindstack::FrameData> frames;
  unwindstack::Maps maps;
  MemoryPattern memory;

  frames.push_back(
      unwindstack::FrameData{.num = 0, .rel_pc = 0x1000, .pc = 0x301000, .sp = 0x2000});
  frames.push_back(
      unwindstack::FrameData{.num = 0, .rel_pc = 0x1400, .pc = 0x301400, .sp = 0x2010});
  frames.push_back(
      unwindstack::FrameData{.num = 0, .rel_pc = 0x1400, .pc = 0x301400, .sp = 0x1000});
  frames.push_back(
      unwindstack::FrameData{.num = 0, .rel_pc = 0x1400, .pc = 0x301400, .sp = 0x1030});
  dump_stack(&log_, frames, &maps, &memory);

  std::string contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &contents));

  std::string expected =
#if defined(__LP64__)
      "         0000000000001f80  0706050403020100\n"
      "         0000000000001f88  0f0e0d0c0b0a0908\n"
      "         0000000000001f90  1716151413121110\n"
      "         0000000000001f98  1f1e1d1c1b1a1918\n"
      "         0000000000001fa0  2726252423222120\n"
      "         0000000000001fa8  2f2e2d2c2b2a2928\n"
      "         0000000000001fb0  3736353433323130\n"
      "         0000000000001fb8  3f3e3d3c3b3a3938\n"
      "         0000000000001fc0  4746454443424140\n"
      "         0000000000001fc8  4f4e4d4c4b4a4948\n"
      "         0000000000001fd0  5756555453525150\n"
      "         0000000000001fd8  5f5e5d5c5b5a5958\n"
      "         0000000000001fe0  6766656463626160\n"
      "         0000000000001fe8  6f6e6d6c6b6a6968\n"
      "         0000000000001ff0  7776757473727170\n"
      "         0000000000001ff8  7f7e7d7c7b7a7978\n"
      "    #00  0000000000002000  0706050403020100\n"
      "         0000000000002008  0f0e0d0c0b0a0908\n"
      "    #01  0000000000002010  0706050403020100\n"
      "         0000000000002018  0f0e0d0c0b0a0908\n"
      "         0000000000002020  1716151413121110\n"
      "         0000000000002028  1f1e1d1c1b1a1918\n"
      "         0000000000002030  2726252423222120\n"
      "         0000000000002038  2f2e2d2c2b2a2928\n"
      "         0000000000002040  3736353433323130\n"
      "         0000000000002048  3f3e3d3c3b3a3938\n"
      "         0000000000002050  4746454443424140\n"
      "         0000000000002058  4f4e4d4c4b4a4948\n"
      "         0000000000002060  5756555453525150\n"
      "         0000000000002068  5f5e5d5c5b5a5958\n"
      "         0000000000002070  6766656463626160\n"
      "         0000000000002078  6f6e6d6c6b6a6968\n"
      "         0000000000002080  7776757473727170\n"
      "         0000000000002088  7f7e7d7c7b7a7978\n"
      "         ................  ................\n"
      "    #02  0000000000001000  0706050403020100\n"
      "         0000000000001008  0f0e0d0c0b0a0908\n"
      "         0000000000001010  1716151413121110\n"
      "         0000000000001018  1f1e1d1c1b1a1918\n"
      "         0000000000001020  2726252423222120\n"
      "         0000000000001028  2f2e2d2c2b2a2928\n"
      "    #03  0000000000001030  0706050403020100\n"
      "         0000000000001038  0f0e0d0c0b0a0908\n"
      "         0000000000001040  1716151413121110\n"
      "         0000000000001048  1f1e1d1c1b1a1918\n"
      "         0000000000001050  2726252423222120\n"
      "         0000000000001058  2f2e2d2c2b2a2928\n"
      "         0000000000001060  3736353433323130\n"
      "         0000000000001068  3f3e3d3c3b3a3938\n"
      "         0000000000001070  4746454443424140\n"
      "         0000000000001078  4f4e4d4c4b4a4948\n"
      "         0000000000001080  5756555453525150\n"
      "         0000000000001088  5f5e5d5c5b5a5958\n"
      "         0000000000001090  6766656463626160\n"
      "         0000000000001098  6f6e6d6c6b6a6968\n"
      "         00000000000010a0  7776757473727170\n"
      "         00000000000010a8  7f7e7d7c7b7a7978\n";
#else
      "         00001fc0  03020100\n"
      "         00001fc4  07060504\n"
      "         00001fc8  0b0a0908\n"
      "         00001fcc  0f0e0d0c\n"
      "         00001fd0  13121110\n"
      "         00001fd4  17161514\n"
      "         00001fd8  1b1a1918\n"
      "         00001fdc  1f1e1d1c\n"
      "         00001fe0  23222120\n"
      "         00001fe4  27262524\n"
      "         00001fe8  2b2a2928\n"
      "         00001fec  2f2e2d2c\n"
      "         00001ff0  33323130\n"
      "         00001ff4  37363534\n"
      "         00001ff8  3b3a3938\n"
      "         00001ffc  3f3e3d3c\n"
      "    #00  00002000  03020100\n"
      "         00002004  07060504\n"
      "         00002008  0b0a0908\n"
      "         0000200c  0f0e0d0c\n"
      "    #01  00002010  03020100\n"
      "         00002014  07060504\n"
      "         00002018  0b0a0908\n"
      "         0000201c  0f0e0d0c\n"
      "         00002020  13121110\n"
      "         00002024  17161514\n"
      "         00002028  1b1a1918\n"
      "         0000202c  1f1e1d1c\n"
      "         00002030  23222120\n"
      "         00002034  27262524\n"
      "         00002038  2b2a2928\n"
      "         0000203c  2f2e2d2c\n"
      "         00002040  33323130\n"
      "         00002044  37363534\n"
      "         00002048  3b3a3938\n"
      "         0000204c  3f3e3d3c\n"
      "         ........  ........\n"
      "    #02  00001000  03020100\n"
      "         00001004  07060504\n"
      "         00001008  0b0a0908\n"
      "         0000100c  0f0e0d0c\n"
      "         00001010  13121110\n"
      "         00001014  17161514\n"
      "         00001018  1b1a1918\n"
      "         0000101c  1f1e1d1c\n"
      "         00001020  23222120\n"
      "         00001024  27262524\n"
      "         00001028  2b2a2928\n"
      "         0000102c  2f2e2d2c\n"
      "    #03  00001030  03020100\n"
      "         00001034  07060504\n"
      "         00001038  0b0a0908\n"
      "         0000103c  0f0e0d0c\n"
      "         00001040  13121110\n"
      "         00001044  17161514\n"
      "         00001048  1b1a1918\n"
      "         0000104c  1f1e1d1c\n"
      "         00001050  23222120\n"
      "         00001054  27262524\n"
      "         00001058  2b2a2928\n"
      "         0000105c  2f2e2d2c\n"
      "         00001060  33323130\n"
      "         00001064  37363534\n"
      "         00001068  3b3a3938\n"
      "         0000106c  3f3e3d3c\n";
#endif
  EXPECT_EQ(expected, contents);
}