• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright (C) 2024 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import pytest
17import subprocess
18import re
19import time
20import threading
21import sqlite3
22from string import Template
23import os
24
25
26def get_pid_by_process_name(process_name):
27    pid = None
28    cmd = f"hdc shell \"pidof {process_name}\""
29    try:
30        pid = subprocess.check_output(cmd, shell=False, encoding="utf-8", text=True)
31        pid = int(pid.strip().split()[0])
32    except subprocess.CalledProcessError as e:
33        print(f"Command failed: {cmd}\nError: {e}")
34    except Exception as e:
35        print(f"Unexpected error: {e}")
36    return pid
37
38
39def get_file_size(file_path):
40    size = os.path.getsize(file_path)
41    return size
42
43
44def task_nativehook_dwarf():
45    subprocess.check_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/inputfiles/nativehook/config_nativehook_dwarf.txt -o /data/local/tmp/test_nativehook_dwarf.htrace -t 60 -s -k"')
46
47
48def write_str_file(file_path, large_string):
49    lines = large_string.split('\n')
50    with open(file_path, 'w') as file:
51        for line in lines:
52            file.write(line + '\n')
53
54
55#检查进程是否离线
56def check_process_offline():
57    count = 0
58    while (count < 5):
59        pid_profiler = get_pid_by_process_name("hiprofilerd")
60        pid_plugin = get_pid_by_process_name("hiprofiler_plugins")
61        pid_daemon = get_pid_by_process_name("native_daemon")
62        assert (pid_profiler > 0)
63        assert (pid_plugin > 0)
64        assert (pid_daemon > 0)
65        time.sleep(10)
66        count = count + 1
67
68
69def hap_op_func(ability_name, bundle_name):
70    #打开系统设置的应用com.ohos.settings.MainAbility  com.ohos.settings
71    subprocess.check_output(f"hdc shell uitest uiInput keyEvent Home", shell=False,
72                                text=True, encoding="utf-8")
73    time.sleep(1)
74    subprocess.check_output(f"hdc shell aa start -a {ability_name} -b {bundle_name}", shell=False,
75                                text=True, encoding="utf-8")
76    time.sleep(2)
77
78
79def hidumper_op_func(ability_name, bundle_name):
80    pid_text = subprocess.run(f"hdc shell pidof '{bundle_name}'", stdout=subprocess.PIPE, text=True, check=True)
81    pidinfo = pid_text.stdout
82    if pidinfo.strip() != "":
83        subprocess.check_output(f"hdc shell kill " + pidinfo.strip(), shell=False, text=True, encoding="utf-8")
84    #拉起hidumper
85    subprocess.check_output(f"hdc shell hidumper -h", shell=False, text=True, encoding="utf-8")
86
87
88def hidumper_prepare_func(ability_name, bundle_name):
89    pid_text = subprocess.run(f"hdc shell pidof '{bundle_name}'", stdout=subprocess.PIPE, text=True, check=True)
90    pidinfo = pid_text.stdout
91    #进程存在
92    if pidinfo.strip() != "":
93        subprocess.check_output(f"hdc shell hidumper -lc", shell=False, text=True, encoding="utf-8")
94    else:
95        subprocess.check_output(f"hdc shell hidumper -h", shell=False, text=True, encoding="utf-8")
96
97
98def hidumper_op_nostart_func(ability_name, bundle_name):
99    subprocess.check_output(f"hdc shell hidumper -lc", shell=False, text=True, encoding="utf-8")
100    subprocess.check_output(f"hdc shell hidumper -c", shell=False, text=True, encoding="utf-8")
101
102
103def nativehook_dwarf_startup(statistics_int, ability_name, bundle_name, op_func):
104    pid_text = subprocess.run(f"hdc shell pidof '{bundle_name}'", stdout=subprocess.PIPE, text=True, check=True)
105    pidinfo = pid_text.stdout
106    if pidinfo.strip() != "":
107        subprocess.check_output(f"hdc shell kill " + pidinfo.strip(), shell=False, text=True, encoding="utf-8")
108    #删除cppcrash
109    subprocess.check_output(f"hdc shell rm -f /data/log/faultlog/faultlogger/cppcrash-*", shell=False, text=True, encoding="utf-8")
110    #dwarf 统计模式
111    file_content = Template('request_id: 1                       \n'
112                            'session_config {                    \n'
113                            ' buffers {                          \n'
114                            '  pages: 16384                      \n'
115                            ' }                                  \n'
116                            '}                                   \n'
117                            'plugin_configs {                    \n'
118                            ' plugin_name: "nativehook"          \n'
119                            ' sample_interval: 5000              \n'
120                            ' config_data {                      \n'
121                            '  save_file: false                  \n'
122                            '  smb_pages: 16384                  \n'
123                            '  max_stack_depth: 8                \n'
124                            '  process_name: "${s2}"             \n'
125                            '  string_compressed: true           \n'
126                            '  fp_unwind: false                  \n'
127                            '  blocked: false                    \n'
128                            '  callframe_compress: true          \n'
129                            '  record_accurately: true           \n'
130                            '  offline_symbolization: false      \n'
131                            '  statistics_interval: ${s1}        \n'
132                            '  startup_mode: true                \n'
133                            '  js_stack_report: 1                \n'
134                            '  max_js_stack_depth: 2             \n'
135                            ' }                                  \n'
136                            '}                                   \n')
137    vmfile = file_content.safe_substitute(s1=statistics_int, s2=bundle_name)
138    #写入文件
139    write_str_file("./inputfiles/nativehook/config_nativehook_dwarf.txt", vmfile)
140
141    subprocess.check_output(f"hdc file send ./inputfiles/nativehook/config_nativehook_dwarf.txt /data/local/tmp/", shell=False,
142                            text=True, encoding="utf-8")
143    task_thread = threading.Thread(target=task_nativehook_dwarf, args=())
144    task_thread.start()
145    time.sleep(2)
146    check_thread = threading.Thread(target=check_process_offline, args=())
147    check_thread.start()
148    op_func(ability_name, bundle_name)
149    check_thread.join()
150    task_thread.join()
151    subprocess.run(f'hdc file recv /data/local/tmp/test_nativehook_dwarf.htrace ./outputfiles/', shell=False,
152                     text=True, encoding="utf-8")
153    # 检查文件大小
154    file_size = get_file_size(f"./outputfiles/test_nativehook_dwarf.htrace")
155    assert (file_size > 1024)
156    #检查是否存在crash
157    output_text = subprocess.run(f'hdc shell "ls /data/log/faultlog/faultlogger"', stdout=subprocess.PIPE, text=True, check=True)
158    process_info = output_text.stdout
159    lines = process_info.strip().split('\n')
160    check_crash = False
161    for line in lines:
162        if line.find("profiler") != -1 or line.find("native_daemon") != -1:
163            check_crash = True
164            break
165    assert (check_crash == False)
166
167
168def nativehook_dwarf_no_startup(prepare_op_func, statistics_int, ability_name, bundle_name, op_func):
169    prepare_op_func(ability_name, bundle_name)
170    #删除cppcrash
171    subprocess.check_output(f"hdc shell rm -f /data/log/faultlog/faultlogger/cppcrash-*", shell=False, text=True, encoding="utf-8")
172    #dwarf
173    file_content = Template('request_id: 1                       \n'
174                            'session_config {                    \n'
175                            ' buffers {                          \n'
176                            '  pages: 16384                      \n'
177                            ' }                                  \n'
178                            '}                                   \n'
179                            'plugin_configs {                    \n'
180                            ' plugin_name: "nativehook"          \n'
181                            ' sample_interval: 5000              \n'
182                            ' config_data {                      \n'
183                            '  save_file: false                  \n'
184                            '  smb_pages: 16384                  \n'
185                            '  max_stack_depth: 8                \n'
186                            '  process_name: "${s2}"             \n'
187                            '  string_compressed: true           \n'
188                            '  fp_unwind: false                  \n'
189                            '  blocked: false                    \n'
190                            '  callframe_compress: true          \n'
191                            '  record_accurately: true           \n'
192                            '  offline_symbolization: false      \n'
193                            '  statistics_interval: ${s1}        \n'
194                            '  startup_mode: false               \n'
195                            '  js_stack_report: 1                \n'
196                            '  max_js_stack_depth: 2             \n'
197                            ' }                                  \n'
198                            '}                                   \n')
199    vmfile = file_content.safe_substitute(s1=statistics_int, s2=bundle_name)
200    #写入文件
201    write_str_file("./inputfiles/nativehook/config_nativehook_dwarf.txt", vmfile)
202
203    subprocess.check_output(f"hdc file send ./inputfiles/nativehook/config_nativehook_dwarf.txt /data/local/tmp/", shell=False,
204                            text=True, encoding="utf-8")
205    task_thread = threading.Thread(target=task_nativehook_dwarf, args=())
206    task_thread.start()
207    time.sleep(2)
208    check_thread = threading.Thread(target=check_process_offline, args=())
209    check_thread.start()
210    op_func(ability_name, bundle_name)
211    check_thread.join()
212    task_thread.join()
213    subprocess.run(f'hdc file recv /data/local/tmp/test_nativehook_dwarf.htrace ./outputfiles/', shell=False,
214                     text=True, encoding="utf-8")
215    # 检查文件大小
216    file_size = get_file_size(f"./outputfiles/test_nativehook_dwarf.htrace")
217    assert (file_size > 1024)
218    #检查是否存在crash
219    output_text = subprocess.run(f'hdc shell "ls /data/log/faultlog/faultlogger"', stdout=subprocess.PIPE, text=True, check=True)
220    process_info = output_text.stdout
221    lines = process_info.strip().split('\n')
222    check_crash = False
223    for line in lines:
224        if line.find("profiler") != -1 or line.find("native_daemon") != -1:
225            check_crash = True
226            break
227    assert (check_crash == False)
228
229
230def nativehook_dwarf_check_data(statistics_flag):
231    subprocess.check_output(r"./inputfiles/trace_streamer_db.exe ./outputfiles/test_nativehook_dwarf.htrace -e ./outputfiles/test_nativehook_dwarf.db")
232    # 连接数据库文件
233    conn = sqlite3.connect(r'./outputfiles/test_nativehook_dwarf.db')
234    # # 创建游标对象
235    cursor = conn.cursor()
236    # 检查是否存在符号数据
237    cursor.execute('select * from native_hook_frame , data_dict where symbol_id = data_dict.id limit 10')
238    result = cursor.fetchall()
239    row_count = len(result)
240    assert (row_count > 0)
241    column_names = [description[0] for description in cursor.description]
242    for row in result:
243        #检查是否存在符号
244        assert (row[column_names.index('data')] is not None)
245    if statistics_flag:
246        #检查是否存在统计数据
247        cursor.execute('select * from native_hook_statistic limit 10')
248        result = cursor.fetchall()
249        row_count = len(result)
250        assert (row_count > 0)
251        column_names = [description[0] for description in cursor.description]
252        for row in result:
253            #检查是否统计数据
254            assert (row[column_names.index('apply_count')] > 0)
255            assert (row[column_names.index('release_count')] >= 0)
256            assert (row[column_names.index('apply_size')] > 0)
257            assert (row[column_names.index('release_size')] >= 0)
258    else:
259        #非统计模式
260        cursor.execute('select * from native_hook limit 10')
261        result = cursor.fetchall()
262        row_count = len(result)
263        assert (row_count > 0)
264        names = [description[0] for description in cursor.description]
265        for row in result:
266            assert (row[names.index('event_type')] == 'AllocEvent' or row[names.index('event_type')] == 'FreeEvent' or row[names.index('event_type')] == 'MmapEvent')
267            assert (row[names.index('heap_size')] > 0)
268            assert (row[names.index('all_heap_size')] >= 0)
269
270    cursor.close()
271    conn.close()
272    return True
273
274
275class TestHiprofilerMemoryPlugin:
276    @pytest.mark.L0
277    #启动模式 hap 应用
278    def test_nativehook_dwarf(self):
279        #获得32位还是64位
280        sys_bit = subprocess.run(f"hdc shell getconf LONG_BIT", stdout=subprocess.PIPE, text=True, check=True)
281        sysinfo = sys_bit.stdout
282        if sysinfo.strip() == "32":
283            #非统计模式
284            nativehook_dwarf_startup(0, "com.ohos.photos.MainAbility", "com.ohos.photos", hap_op_func)
285            assert nativehook_dwarf_check_data(False)
286        else:
287            #非统计模式
288            nativehook_dwarf_startup(0, "com.ohos.launcher", "com.ohos.launcher", hap_op_func)
289            assert nativehook_dwarf_check_data(False)
290
291    @pytest.mark.L0
292    #启动模式 10S 统计一次 hap应用
293    def test_nativehook_dwarf_statics(self):
294        #获得32位还是64位
295        sys_bit = subprocess.run(f"hdc shell getconf LONG_BIT", stdout=subprocess.PIPE, text=True, check=True)
296        sysinfo = sys_bit.stdout
297        if sysinfo.strip() == "32":
298            #统计模式
299            nativehook_dwarf_startup(10, "com.ohos.photos.MainAbility", "com.ohos.photos", hap_op_func)
300            assert nativehook_dwarf_check_data(True)
301        else:
302            #统计模式
303            nativehook_dwarf_startup(10, "com.ohos.launcher", "com.ohos.launcher", hap_op_func)
304            assert nativehook_dwarf_check_data(True)
305
306    @pytest.mark.L0
307    #启动模式 10S 统计一次 SA 进程 如:hidumper_service
308    def test_nativehook_dwarf_native_statics(self):
309        #统计模式
310        nativehook_dwarf_startup(10, "", "hidumper_service", hidumper_op_func)
311        assert nativehook_dwarf_check_data(True)
312        #非统计模式
313        nativehook_dwarf_startup(0, "", "hidumper_service", hidumper_op_func)
314        assert nativehook_dwarf_check_data(False)
315
316    #非启动模式
317    def test_nativehook_dwarf_native_not_startup(self):
318        nativehook_dwarf_no_startup(hidumper_prepare_func, 10, "", "hidumper_service", hidumper_op_nostart_func)
319        assert nativehook_dwarf_check_data(True)
320        #非统计模式
321        nativehook_dwarf_no_startup(hidumper_prepare_func, 0, "", "hidumper_service", hidumper_op_nostart_func)
322        assert nativehook_dwarf_check_data(False)
323