使用Devtools工具调试前端页面
Web组件支持使用DevTools工具调试前端页面。DevTools是一个 Web前端开发调试工具,提供了电脑上调试移动设备前端页面的能力。开发者通过 setWebDebuggingAccess() 接口开启Web组件前端页面调试能力,利用DevTools工具可以在电脑上调试移动设备上的前端网页,设备需为4.1.0及以上版本。
使用DevTools工具,可以执行以下步骤:
- 在应用代码中开启Web调试开关,具体如下:
// xxx.ets
import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
aboutToAppear() {
// 配置Web开启调试模式
webview.WebviewController.setWebDebuggingAccess(true);
}
build() {
Column() {
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
- 开启调试功能需要在DevEco Studio应用工程hap模块的module.json5文件中增加如下权限,添加方法请参考 在配置文件中声明权限 。
"requestPermissions":[
{
"name" : "ohos.permission.INTERNET"
}
]
- 将设备连接上电脑,在电脑端配置端口映射,配置方法如下:
//查找 devtools 远程调试所需的 domain socket 名称,该名称与进程号有关,重启调试应用后,需要重复此步骤,以完成端口转发
cat /proc/net/unix | grep devtools
// 添加映射 [pid] 替换成实际的进程id
hdc fport tcp:9222 localabstract:webview_devtools_remote_[pid]
// 查看映射
hdc fport ls
示例:
hdc shell
cat /proc/net/unix | grep devtools
//显示 webview_devtools_remote_3458
exit
hdc fport tcp:9222 localabstract:webview_devtools_remote_3458
hdc fport ls
- 在电脑端Chrome浏览器地址栏中输入chrome://inspect/#devices,页面识别到设备后,就可以开始页面调试。调试效果如下:
图1 页面调试效果图
- 多应用调试请在调试地址内Devices中的configure添加多个端口号以同时调试多个应用:
图2 添加端口号效果图
- windows便捷脚本,请复制以下信息建立bat文件,开启调试应用后执行:
@echo off
setlocal enabledelayedexpansion
:: Initialize port number and PID list
set PORT=9222
set PID_LIST=
:: Get the list of all forwarded ports and PIDs
for /f "tokens=2,5 delims=:_" %%a in ('hdc fport ls') do (
if %%a gtr !PORT! (
set PORT=%%a
)
for /f "tokens=1 delims= " %%c in ("%%b") do (
set PID_LIST=!PID_LIST! %%c
)
)
:: Increment port number for next application
set temp_PORT=!PORT!
set /a temp_PORT+=1
set PORT=!temp_PORT!
:: Get the domain socket name of devtools
for /f "tokens=*" %%a in ('hdc shell "cat /proc/net/unix | grep devtools"') do (
set SOCKET_NAME=%%a
:: Extract process ID
for /f "delims=_ tokens=4" %%b in ("!SOCKET_NAME!") do set PID=%%b
:: Check if PID already has a mapping
echo !PID_LIST! | findstr /C:" !PID! " >nul
if errorlevel 1 (
:: Add mapping
hdc fport tcp:!PORT! localabstract:webview_devtools_remote_!PID!
if errorlevel 1 (
echo Error: Failed to add mapping.
pause
exit /b
)
:: Add PID to list and increment port number for next application
set PID_LIST=!PID_LIST! !PID!
set temp_PORT=!PORT!
set /a temp_PORT+=1
set PORT=!temp_PORT!
)
)
:: If no process ID was found, prompt the user to open debugging in their application code and provide the documentation link
if "!SOCKET_NAME!"=="" (
echo No process ID was found. Please open debugging in your application code using the corresponding interface. You can find the relevant documentation at this link: [https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/web/web-debugging-with-devtools.md]
pause
exit /b
)
:: Check mapping
hdc fport ls
echo.
echo Script executed successfully. Press any key to exit...
pause >nul
:: Try to open the page in Edge
start msedge chrome://inspect/#devices.com
:: If Edge is not available, then open the page in Chrome
if errorlevel 1 (
start chrome chrome://inspect/#devices.com
)
endlocal
- mac/linux便捷脚本,请复制以下信息建立sh文件,请自行注意chmod以及格式转换,开启调试应用后执行:
#!/bin/bash
# Initial port number
INITIAL_PORT=9222
# Get the current port number, use initial port number if not set previously
CURRENT_PORT=${PORT:-$INITIAL_PORT}
# Get the list of all PIDs that match the condition
PID_LIST=$(hdc shell cat /proc/net/unix | grep webview_devtools_remote_ | awk -F '_' '{print $NF}')
if [ -z "$PID_LIST" ]; then
echo "Failed to retrieve PID from the device"
exit 1
fi
# Increment the port number
PORT=$CURRENT_PORT
# Forward ports for each application one by one
for PID in $PID_LIST; do
# Increment the port number
PORT=$((PORT + 1))
# Execute the hdc fport command
hdc fport tcp:$PORT localabstract:webview_devtools_remote_$PID
# Check if the command executed successfully
if [ $? -ne 0 ]; then
echo "Failed to execute hdc fport command"
exit 1
fi
done
# List all forwarded ports
hdc fport ls
使用crashpad收集Web组件崩溃信息
Web组件支持使用crashpad记录进程崩溃信息。crashpad是chromium内核提供的进程崩溃信息处理工具,在应用使用Web组件导致的进程崩溃出现后(包括应用主进程与Web渲染进程),crashpad会在应用主进程沙箱目录写入minidump文件。该文件为二进制格式,后缀为dmp,其记录了进程崩溃的原因、线程信息、寄存器信息等,应用可以使用该文件分析Web组件相关进程崩溃问题。
使用步骤如下:
- 在应用使用Web组件导致的进程崩溃出现后,会在应用主进程沙箱目录下产生对应的dmp文件,对应的沙箱路径如下:
/data/storage/el2/log/crashpad
-
应用可以访问该路径拿到目录下的dmp文件,然后进行解析,具体步骤如下:
- 通过minidump_stackwalk工具解析dmp文件,可以得到上述dmp文件对应的进程崩溃信息(崩溃的原因、线程信息、寄存器信息等),示例如下(Linux环境):
./minidump_stackwalk b678e0b5-894b-4794-9ab3-fb5d6dda06a3.dmp > parsed_stacktrace.txt
minidump_stackwalk由breakpad项目源码编译得到,编译方法见项目仓库:breakpad仓库地址。
- 查看解析后的文件,以下示例列出部分内容:
Crash reason: SIGSEGV /SEGV_MAPERR 表示导致进程crash的信号,此处示例为段错误
Crash address: 0x0
Process uptime: 12 seconds
Thread 0 (crashed) 表示Thraed 0发生crash
0 libweb_engine.so + 0x2e0b340 0层调用栈,0x2e0b340为so偏移地址,可用来反编译解析crash源码(依赖unstripped so)
x0 = 0x00000006a5719ff8 x1 = 0x000000019a5a28c0
x2 = 0x0000000000020441 x3 = 0x00000000000001b6
x4 = 0x0000000000000018 x5 = 0x0000000000008065
x6 = 0x0000000000008065 x7 = 0x63ff686067666d60
x8 = 0x0000000000000000 x9 = 0x5f129cf9e7bf008c
x10 = 0x0000000000000001 x11 = 0x0000000000000000
x12 = 0x000000069bfcc6d8 x13 = 0x0000000009a1746e
x14 = 0x0000000000000000 x15 = 0x0000000000000000
x16 = 0x0000000690df4850 x17 = 0x000000010c0d47f8
x18 = 0x0000000000000000 x19 = 0x0000005eea827db8
x20 = 0x0000005eea827c38 x21 = 0x00000006a56b1000
x22 = 0x00000006a8b85020 x23 = 0x00000020002103c0
x24 = 0x00000006a56b8a70 x25 = 0x0000000000000000
x26 = 0x00000006a8b84e00 x27 = 0x0000000000000001
x28 = 0x0000000000000000 fp = 0x0000005eea827c10
lr = 0x000000069fa4b33c sp = 0x0000005eea827c10
pc = 0x000000069fa4b340
Found by: given as instruction pointer in context
1 libweb_engine.so + 0x2e0b338
fp = 0x0000005eea827d80 lr = 0x000000069fa48d44
sp = 0x0000005eea827c20 pc = 0x000000069fa4b33c
Found by: previous frame's frame pointer
2 libweb_engine.so + 0x2e08d40
fp = 0x0000005eea827e50 lr = 0x00000006a385cef8
sp = 0x0000005eea827d90 pc = 0x000000069fa48d44
Found by: previous frame's frame pointer
3 libweb_engine.so + 0x6c1cef4
fp = 0x0000005eea828260 lr = 0x00000006a0f11298
sp = 0x0000005eea827e60 pc = 0x00000006a385cef8
......
- 使用llvm工具链解析crash源码位置,示例如下(Linux环境):
./llvm-addr2line -Cfpie libweb_engine.so 0x2e0b340
llvm-addr2line工具链位于sdk中。
鸿蒙全栈开发全新学习指南
有很多小伙伴不知道学习哪些鸿蒙开发技术?不知道需要重点掌握哪些鸿蒙应用开发知识点?而且学习时频繁踩坑,最终浪费大量时间。所以要有一份实用的鸿蒙(HarmonyOS NEXT)学习路线与学习文档用来跟着学习是非常有必要的。
针对一些列因素,整理了一套纯血版鸿蒙(HarmonyOS Next)全栈开发技术的学习路线,包含了鸿蒙开发必掌握的核心知识要点,内容有(ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、WebGL、元服务、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、OpenHarmony驱动开发、系统定制移植等等)鸿蒙(HarmonyOS NEXT)技术知识点。
本路线共分为四个阶段:
第一阶段:鸿蒙初中级开发必备技能
第二阶段:鸿蒙南北双向高工技能基础:gitee.com/MNxiaona/733GH
第三阶段:应用开发中高级就业技术
第四阶段:全网首发-工业级南向设备开发就业技术:gitee.com/MNxiaona/733GH
《鸿蒙 (Harmony OS)开发学习手册》(共计892页)
如何快速入门?
1.基本概念
2.构建第一个ArkTS应用
3.……
开发基础知识:gitee.com/MNxiaona/733GH
1.应用基础知识
2.配置文件
3.应用数据管理
4.应用安全管理
5.应用隐私保护
6.三方应用调用管控机制
7.资源分类与访问
8.学习ArkTS语言
9.……
基于ArkTS 开发
1.Ability开发
2.UI开发
3.公共事件与通知
4.窗口管理
5.媒体
6.安全
7.网络与链接
8.电话服务
9.数据管理
10.后台任务(Background Task)管理
11.设备管理
12.设备使用信息统计
13.DFX
14.国际化开发
15.折叠屏系列
16.……
鸿蒙开发面试真题(含参考答案):gitee.com/MNxiaona/733GH
鸿蒙入门教学视频:
美团APP实战开发教学:gitee.com/MNxiaona/733GH
写在最后
- 如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
- 点赞,转发,有你们的 『点赞和评论』,才是我创造的动力。
- 关注小编,同时可以期待后续文章ing🚀,不定期分享原创知识。
- 想要获取更多完整鸿蒙最新学习资源,请移步前往小编:
gitee.com/MNxiaona/733GH