1#!/usr/bin/env python3
2#
3# Copyright (C) 2021 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import filecmp
18import os
19from pathlib import Path
20import shutil
21import tempfile
22
23from binary_cache_builder import BinaryCacheBuilder
24from simpleperf_utils import ReadElf, remove, ToolFinder
25from . test_utils import TestBase, TestHelper
26
27
28class TestBinaryCacheBuilder(TestBase):
29    def test_copy_binaries_from_symfs_dirs(self):
30        readelf = ReadElf(TestHelper.ndk_path)
31        strip = ToolFinder.find_tool_path('strip', arch='arm')
32        self.assertIsNotNone(strip)
33        symfs_dir = os.path.join(self.test_dir, 'symfs_dir')
34        remove(symfs_dir)
35        os.mkdir(symfs_dir)
36        filename = 'simpleperf_runtest_two_functions_arm'
37        origin_file = TestHelper.testdata_path(filename)
38        source_file = os.path.join(symfs_dir, filename)
39        target_file = os.path.join('binary_cache', filename)
40        expected_build_id = readelf.get_build_id(origin_file)
41        binary_cache_builder = BinaryCacheBuilder(TestHelper.ndk_path, False)
42        binary_cache_builder.binaries['simpleperf_runtest_two_functions_arm'] = expected_build_id
43
44        # Copy binary if target file doesn't exist.
45        remove(target_file)
46        self.run_cmd([strip, '--strip-all', '-o', source_file, origin_file])
47        binary_cache_builder.copy_binaries_from_symfs_dirs([symfs_dir])
48        self.assertTrue(filecmp.cmp(target_file, source_file))
49
50        # Copy binary if target file doesn't have .symtab and source file has .symtab.
51        self.run_cmd([strip, '--strip-debug', '-o', source_file, origin_file])
52        binary_cache_builder.copy_binaries_from_symfs_dirs([symfs_dir])
53        self.assertTrue(filecmp.cmp(target_file, source_file))
54
55        # Copy binary if target file doesn't have .debug_line and source_files has .debug_line.
56        shutil.copy(origin_file, source_file)
57        binary_cache_builder.copy_binaries_from_symfs_dirs([symfs_dir])
58        self.assertTrue(filecmp.cmp(target_file, source_file))
59
60    def test_copy_elf_without_build_id_from_symfs_dir(self):
61        binary_cache_builder = BinaryCacheBuilder(TestHelper.ndk_path, False)
62        binary_cache_builder.binaries['elf'] = ''
63        symfs_dir = TestHelper.testdata_path('data/symfs_without_build_id')
64        source_file = os.path.join(symfs_dir, 'elf')
65        target_file = os.path.join('binary_cache', 'elf')
66        binary_cache_builder.copy_binaries_from_symfs_dirs([symfs_dir])
67        self.assertTrue(filecmp.cmp(target_file, source_file))
68        binary_cache_builder.pull_binaries_from_device()
69        self.assertTrue(filecmp.cmp(target_file, source_file))
70
71    def test_prefer_binary_with_debug_info(self):
72        binary_cache_builder = BinaryCacheBuilder(TestHelper.ndk_path, False)
73        binary_cache_builder.collect_used_binaries(
74            TestHelper.testdata_path('runtest_two_functions_arm64_perf.data'))
75
76        # Create a symfs_dir, which contains elf file with and without debug info.
77        with tempfile.TemporaryDirectory() as tmp_dir:
78            shutil.copy(
79                TestHelper.testdata_path(
80                    'simpleperf_runtest_two_functions_arm64_without_debug_info'),
81                Path(tmp_dir) / 'simpleperf_runtest_two_functions_arm64')
82
83            debug_dir = Path(tmp_dir) / 'debug'
84            debug_dir.mkdir()
85            shutil.copy(TestHelper.testdata_path(
86                'simpleperf_runtest_two_functions_arm64'), debug_dir)
87            # Check if the elf file with debug info is chosen.
88            binary_cache_builder.copy_binaries_from_symfs_dirs([tmp_dir])
89            elf_path = (Path(binary_cache_builder.binary_cache_dir) / 'data' /
90                        'local' / 'tmp' / 'simpleperf_runtest_two_functions_arm64')
91            self.assertTrue(elf_path.is_file())
92            self.assertIn('.debug_info', binary_cache_builder.readelf.get_sections(elf_path))
93
94    def test_create_build_id_list(self):
95        symfs_dir = TestHelper.testdata_dir
96        binary_cache_builder = BinaryCacheBuilder(TestHelper.ndk_path, False)
97        binary_cache_builder.collect_used_binaries(
98            TestHelper.testdata_path('runtest_two_functions_arm64_perf.data'))
99        binary_cache_builder.copy_binaries_from_symfs_dirs([symfs_dir])
100        elf_path = (Path(binary_cache_builder.binary_cache_dir) / 'data' /
101                    'local' / 'tmp' / 'simpleperf_runtest_two_functions_arm64')
102        self.assertTrue(elf_path.is_file())
103
104        binary_cache_builder.create_build_id_list()
105        build_id_list_path = Path(binary_cache_builder.binary_cache_dir) / 'build_id_list'
106        self.assertTrue(build_id_list_path.is_file())
107        with open(build_id_list_path, 'r') as fh:
108            self.assertIn('simpleperf_runtest_two_functions_arm64', fh.read())
109