aosp15实现SurfaceFlinger的dump输出带上Layer详细信息踩坑笔记

背景:

针对上一篇文章
aosp15上SurfaceFlinger的dump部分新特性-无Layer信息输出如何解决?
给大家布置了一个小作业,那就是需要实现dumpsys SurfaceFlinger相关输出中可以携带上所有的Layer的详细信息需求,今天来带大家详细实现一下这个需求。

实战踩坑过程

本来想参考以前版本方式,直接使用mDrawingState.traverse的相关方法来进行遍历每一个Layer然后输出相关的信息。

std::string compositionLayers;
 mDrawingState.traverseInReverseZOrder([&](Layer* layer) {
 		//输出相关的layer信息
                   auto* compositionState = layer->getCompositionState();
                   android::base::StringAppendF(&compositionLayers, "* Layer %p (%s)\n", layer,
                                                 layer->getDebugName() ? layer->getDebugName()
                                                                     : "<unknown>");
                compositionState->dump(compositionLayers);
     });

但是明显发现这个mDrawingState.traverse方法压根就不会进入对应的循环,一行代码都不会执行,所以自然没有任何输出。

其实从SurfaceFlinger.cpp的相关代码也可以看出相关的痕迹,要调用 mDrawingState.traverse相关方法一般都会有一个判断值mLayerLifecycleManagerEnabled:

if (!mLayerLifecycleManagerEnabled) {
        StringAppendF(&result, "Composition layers\n");
        mDrawingState.traverseInZOrder([&](Layer* layer) {
            auto* compositionState = layer->getCompositionState();
            if (!compositionState || !compositionState->isVisible) return;
            android::base::StringAppendF(&result, "* Layer %p (%s)\n", layer,
                                         layer->getDebugName() ? layer->getDebugName()
                                                               : "<unknown>");
            compositionState->dump(result);
        });

        StringAppendF(&result, "Offscreen Layers\n");
        for (Layer* offscreenLayer : mOffscreenLayers) {
            offscreenLayer->traverse(LayerVector::StateSet::Drawing,
                                     [&](Layer* layer) { layer->dumpOffscreenDebugInfo(result); });
        }
    }

但是在aosp15版本上这个mLayerLifecycleManagerEnabled值一直都是true,所以根本不会进入执行这块的mDrawingState.traverse,也就是说在aosp15版本的SurfaceFlinger已经弃用了这个mDrawingState.traverse。

正确实战实现

代码实现:
实现要采用2种方式
方式1–采用dumpsys SurfaceFlinger --带上参数,主要是可以单独dump layer数据
代码实现如下:
在这里插入图片描述这个dumpsysAllLayer的实现代码如下:
在这里插入图片描述

方式2采用setprop方式然后控制prop可以整体进行dump
在总的dump方法最后,加入如下代码
在这里插入图片描述这样就可以实现通过prop值控制是否在正常dumpsys SurfaceFlinger输出中带上所有Layer的信息。

使用方式:
方式1

adb shell dumpsys SurfaceFlinger --all-layer

adb shell dumpsys SurfaceFlinger  --all-layer
+ Layer (Display 0 name="Built-in Screen"#43) uid=1000
  Region TransparentRegion (this=0 count=0)
  Region VisibleRegion (this=0 count=0)
  Region SurfaceDamageRegion (this=0 count=0)
      layerStack=   0, z=        0, pos=(0,0), size=(   0,   0), crop=[  0,   0,  -1,  -1], cornerRadius=0.000000, isProtected=0, isTrustedOverlay=0, isOpaque=0, invalidate=0, dataspace=BT709 sRGB Full range, defaultPixelFormat=, backgroundBlurRadius=0, color=(-1.000,-1.000,-1.000,1.000), flags=0x00000002, tr=[0.00, 0.00][0.00, 0.00]
      parent=none
      zOrderRelativeOf=none
      activeBuffer=[   0x   0:   0,Unknown/None], tr=[0.00, 0.00][0.00, 0.00] queued-frames=0 metadata={9:4bytes}, cornerRadiusCrop=[0.00, 0.00, 0.00, 0.00],  shadowRadius=0.000, 
+ Layer (WindowedMagnification:0:31#3) uid=1000
  Region TransparentRegion (this=0 count=0)
  Region VisibleRegion (this=0 count=0)
  Region SurfaceDamageRegion (this=0 count=0)
      layerStack=   0, z=        0, pos=(0,0), size=(   0,   0), crop=[  0,   0,   0,   0], cornerRadius=0.000000, isProtected=0, isTrustedOverlay=0, isOpaque=0, invalidate=0, dataspace=BT709 sRGB Full range, defaultPixelFormat=, backgroundBlurRadius=0, color=(-1.000,-1.000,-1.000,1.000), flags=0x00000000, tr=[0.00, 0.00][0.00, 0.00]
      parent=Display 0 name="Built-in Screen"#43
      zOrderRelativeOf=none
      activeBuffer=[   0x   0:   0,Unknown/None], tr=[0.00, 0.00][0.00, 0.00] queued-frames=0 metadata={9:4bytes}, cornerRadiusCrop=[0.00, 0.00, 0.00, 0.00],  shadowRadius=0.000, 
+ Layer (HideDisplayCutout:0:14#4) uid=1000
  Region TransparentRegion (this=0 count=0)
  Region VisibleRegion (this=0 count=0)
  Region SurfaceDamageRegion (this=0 count=0)
      layerStack=   0, z=        0, pos=(0,0), size=(   0,   0), crop=[  0,   0,  -1,  -1], cornerRadius=0.000000, isProtected=0, isTrustedOverlay=0, isOpaque=0, invalidate=0, dataspace=BT709 sRGB Full range, defaultPixelFormat=, backgroundBlurRadius=0, color=(-1.000,-1.000,-1.000,1.000), flags=0x00000000, tr=[0.00, 0.00][0.00, 0.00]
      parent=WindowedMagnification:0:31#3
      zOrderRelativeOf=none
      activeBuffer=[   0x   0:   0,Unknown/None], tr=[0.00, 0.00][0.00, 0.00] queued-frames=0 metadata={9:4bytes}, cornerRadiusCrop=[0.00, 0.00, 0.00, 0.00],  shadowRadius=0.000, 
+ Layer (OneHanded:0:14#5) uid=1000
  Region TransparentRegion (this=0 count=0)
  Region VisibleRegion (this=0 count=0)
  Region SurfaceDamageRegion (this=0 count=0)
      layerStack=   0, z=        0, pos=(0,0), size=(   0,   0), crop=[  0,   0,  -1,  -1], cornerRadius=0.000000, isProtected=0, isTrustedOverlay=0, isOpaque=0, invalidate=0, dataspace=BT709 sRGB Full range, defaultPixelFormat=, backgroundBlurRadius=0, color=(-1.000,-1.000,-1.000,1.000), flags=0x00000000, tr=[0.00, 0.00][0.00, 0.00]
      parent=HideDisplayCutout:0:14#4
      zOrderRelativeOf=none
      activeBuffer=[   0x   0:   0,Unknown/None], tr=[0.00, 0.00][0.00, 0.00] queued-frames=0 metadata={9:4bytes}, cornerRadiusCrop=[0.00, 0.00, 0.00, 0.00],  shadowRadius=0.000, 
+ Layer (FullscreenMagnification:0:12#6) uid=1000
  Region TransparentRegion (this=0 count=0)
  Region VisibleRegion (this=0 count=0)
  Region SurfaceDamageRegion (this=0 count=0)
      layerStack=   0, z=        0, pos=(0,0), size=(   0,   0), crop=[  0,   0,  -1,  -1], cornerRadius=0.000000, isProtected=0, isTrustedOverlay=0, isOpaque=0, invalidate=0, dataspace=BT709 sRGB Full range, defaultPixelFormat=, backgroundBlurRadius=0, color=(-1.000,-1.000,-1.000,1.000), flags=0x00000000, tr=[0.00, 0.00][0.00, 0.00]
      parent=OneHanded:0:14#5
      zOrderRelativeOf=none
      activeBuffer=[   0x   0:   0,Unknown/None], tr=[0.00, 0.00][0.00, 0.00] queued-frames=0 metadata={9:4bytes}, cornerRadiusCrop=[0.00, 0.00, 0.00, 0.00],  shadowRadius=0.000, 
+ Layer (Leaf:0:1#7) uid=1000
  Region TransparentRegion (this=0 count=0)
  Region VisibleRegion (this=0 count=0)
  Region SurfaceDamageRegion (this=0 count=0)
      layerStack=   0, z=        0, pos=(0,0), size=(   0,   0), crop=[  0,   0,  -1,  -1], cornerRadius=0.000000, isProtected=0, isTrustedOverlay=0, isOpaque=0, invalidate=0, dataspace=BT709 sRGB Full range, defaultPixelFormat=, backgroundBlurRadius=0, color=(-1.000,-1.000,-1.000,1.000), flags=0x00000000, tr=[0.00, 0.00][0.00, 0.00]
      parent=FullscreenMagnification:0:12#6
      zOrderRelativeOf=none
      activeBuffer=[   0x   0:   0,Unknown/None], tr=[0.00, 0.00][0.00, 0.00] queued-frames=0 metadata={9:4bytes}, cornerRadiusCrop=[0.00, 0.00, 0.00, 0.00],  shadowRadius=0.000, 
+ Layer (WallpaperWindowToken{970337b token=android.os.Binder@2e0df0a}#62) uid=1000
  Region TransparentRegion (this=0 count=0)
  Region VisibleRegion (this=0 count=0)
  Region SurfaceDamageRegion (this=0 count=0)
      layerStack=   0, z=        0, pos=(0,0), size=(   0,   0), crop=[  0,   0,  -1,  -1], cornerRadius=0.000000, isProtected=0, isTrustedOverlay=0, isOpaque=0, invalidate=0, dataspace=BT709 sRGB Full range, defaultPixelFormat=, backgroundBlurRadius=0, color=(-1.000,-1.000,-1.000,1.000), flags=0x00000000, tr=[0.00, 0.00][0.00, 0.00]
      parent=Leaf:0:1#7
      zOrderRelativeOf=none
      activeBuffer=[   0x   0:   0,Unknown/None], tr=[0.00, 0.00][0.00, 0.00] queued-frames=0 metadata={9:4bytes}, cornerRadiusCrop=[0.00, 0.00, 0.00, 0.00],  shadowRadius=0.000, 

方式2
先进行prop的设置,开启dump所有的layers

adb shell setprop debug.sf.dump_all_layers true

然后再执行dumpsys SurfaceFlinger

Window Infos:
  max send vsync id: -1
  max send delay (ns): 0 ns
  unsent messages: 0  #正常情况下的结束地方

+ Layer (Display 0 name="Built-in Screen"#43) uid=1000
  Region TransparentRegion (this=0 count=0)
  Region VisibleRegion (this=0 count=0)
  Region SurfaceDamageRegion (this=0 count=0)
      layerStack=   0, z=        0, pos=(0,0), size=(   0,   0), crop=[  0,   0,  -1,  -1], cornerRadius=0.000000, isProtected=0, isTrustedOverlay=0, isOpaque=0, invalidate=0, dataspace=BT709 sRGB Full range, defaultPixelFormat=, backgroundBlurRadius=0, color=(-1.000,-1.000,-1.000,1.000), flags=0x00000002, tr=[0.00, 0.00][0.00, 0.00]
      parent=none
      zOrderRelativeOf=none
      activeBuffer=[   0x   0:   0,Unknown/None], tr=[0.00, 0.00][0.00, 0.00] queued-frames=0 metadata={9:4bytes}, cornerRadiusCrop=[0.00, 0.00, 0.00, 0.00],  shadowRadius=0.000, 
+ Layer (WindowedMagnification:0:31#3) uid=1000
  Region TransparentRegion (this=0 count=0)
  Region VisibleRegion (this=0 count=0)
  Region SurfaceDamageRegion (this=0 count=0)
      layerStack=   0, z=        0, pos=(0,0), size=(   0,   0), crop=[  0,   0,   0,   0], cornerRadius=0.000000, isProtected=0, isTrustedOverlay=0, isOpaque=0, invalidate=0, dataspace=BT709 sRGB Full range, defaultPixelFormat=, backgroundBlurRadius=0, color=(-1.000,-1.000,-1.000,1.000), flags=0x00000000, tr=[0.00, 0.00][0.00, 0.00]
      parent=Display 0 name="Built-in Screen"#43
      zOrderRelativeOf=none
      activeBuffer=[   0x   0:   0,Unknown/None], tr=[0.00, 0.00][0.00, 0.00] queued-frames=0 metadata={9:4bytes}, cornerRadiusCrop=[0.00, 0.00, 0.00, 0.00],  shadowRadius=0.000, 
+ Layer (HideDisplayCutout:0:14#4) uid=1000
  Region TransparentRegion (this=0 count=0)
  Region VisibleRegion (this=0 count=0)
  Region SurfaceDamageRegion (this=0 count=0)
      layerStack=   0, z=        0, pos=(0,0), size=(   0,   0), crop=[  0,   0,  -1,  -1], cornerRadius=0.000000, isProtected=0, isTrustedOverlay=0, isOpaque=0, invalidate=0, dataspace=BT709 sRGB Full range, defaultPixelFormat=, backgroundBlurRadius=0, color=(-1.000,-1.000,-1.000,1.000), flags=0x00000000, tr=[0.00, 0.00][0.00, 0.00]
      parent=WindowedMagnification:0:31#3
      zOrderRelativeOf=none
      activeBuffer=[   0x   0:   0,Unknown/None], tr=[0.00, 0.00][0.00, 0.00] queued-frames=0 metadata={9:4bytes}, cornerRadiusCrop=[0.00, 0.00, 0.00, 0.00],  shadowRadius=0.000, 
+ Layer (OneHanded:0:14#5) uid=1000
  Region TransparentRegion (this=0 count=0)
  Region VisibleRegion (this=0 count=0)
  Region SurfaceDamageRegion (this=0 count=0)
      layerStack=   0, z=        0, pos=(0,0), size=(   0,   0), crop=[  0,   0,  -1,  -1], cornerRadius=0.000000, isProtected=0, isTrustedOverlay=0, isOpaque=0, invalidate=0, dataspace=BT709 sRGB Full range, defaultPixelFormat=, backgroundBlurRadius=0, color=(-1.000,-1.000,-1.000,1.000), flags=0x00000000, tr=[0.00, 0.00][0.00, 0.00]
      parent=HideDisplayCutout:0:14#4
      zOrderRelativeOf=none
      activeBuffer=[   0x   0:   0,Unknown/None], tr=[0.00, 0.00][0.00, 0.00] queued-frames=0 metadata={9:4bytes}, cornerRadiusCrop=[0.00, 0.00, 0.00, 0.00],  shadowRadius=0.000, 
+ Layer (FullscreenMagnification:0:12#6) uid=1000
  Region TransparentRegion (this=0 count=0)
  Region VisibleRegion (this=0 count=0)
  Region SurfaceDamageRegion (this=0 count=0)
      layerStack=   0, z=        0, pos=(0,0), size=(   0,   0), crop=[  0,   0,  -1,  -1], cornerRadius=0.000000, isProtected=0, isTrustedOverlay=0, isOpaque=0, invalidate=0, dataspace=BT709 sRGB Full range, defaultPixelFormat=, backgroundBlurRadius=0, color=(-1.000,-1.000,-1.000,1.000), flags=0x00000000, tr=[0.00, 0.00][0.00, 0.00]
      parent=OneHanded:0:14#5
      zOrderRelativeOf=none
      activeBuffer=[   0x   0:   0,Unknown/None], tr=[0.00, 0.00][0.00, 0.00] queued-frames=0 metadata={9:4bytes}, cornerRadiusCrop=[0.00, 0.00, 0.00, 0.00],  shadowRadius=0.000, 
+ Layer (Leaf:0:1#7) uid=1000
  Region TransparentRegion (this=0 count=0)
  Region VisibleRegion (this=0 count=0)
  Region SurfaceDamageRegion (this=0 count=0)
      layerStack=   0, z=        0, pos=(0,0), size=(   0,   0), crop=[  0,   0,  -1,  -1], cornerRadius=0.000000, isProtected=0, isTrustedOverlay=0, isOpaque=0, invalidate=0, dataspace=BT709 sRGB Full range, defaultPixelFormat=, backgroundBlurRadius=0, color=(-1.000,-1.000,-1.000,1.000), flags=0x00000000, tr=[0.00, 0.00][0.00, 0.00]
      parent=FullscreenMagnification:0:12#6
      zOrderRelativeOf=none
      activeBuffer=[   0x   0:   0,Unknown/None], tr=[0.00, 0.00][0.00, 0.00] queued-frames=0 metadata={9:4bytes}, cornerRadiusCrop=[0.00, 0.00, 0.00, 0.00],  shadowRadius=0.000, 
+ Layer (WallpaperWindowToken{970337b token=android.os.Binder@2e0df0a}#62) uid=1000
  Region TransparentRegion (this=0 count=0)
  Region VisibleRegion (this=0 count=0)
  Region SurfaceDamageRegion (this=0 count=0)
      layerStack=   0, z=        0, pos=(0,0), size=(   0,   0), crop=[  0,   0,  -1,  -1], cornerRadius=0.000000, isProtected=0, isTrustedOverlay=0, isOpaque=0, invalidate=0, dataspace=BT709 sRGB Full range, defaultPixelFormat=, backgroundBlurRadius=0, color=(-1.000,-1.000,-1.000,1.000), flags=0x00000000, tr=[0.00, 0.00][0.00, 0.00]
      parent=Leaf:0:1#7
      zOrderRelativeOf=none
      activeBuffer=[   0x   0:   0,Unknown/None], tr=[0.00, 0.00][0.00, 0.00] queued-frames=0 metadata={9:4bytes}, cornerRadiusCrop=[0.00, 0.00, 0.00, 0.00],  shadowRadius=0.000, 

默认情况下debug.sf.dump_all_layers是false,所以不会dump
在这里插入图片描述
更多framework实战干货,请关注下面“千里马学框架”

<think>我们需要在SurfaceFlinger进程中执行系统命令(例如通过system()函数调用)。在Android中,这通常需要SELinux权限的配置。 在AOSP中,SELinux策略文件通常位于`device/<manufacturer>/<device>/sepolicy`或`system/sepolicy`目录下。我们需要为SurfaceFlinger域(通常是`surfaceflinger`域)添加执行系统命令的权限。 具体来说,SurfaceFlinger进程需要以下权限: 1. 允许执行shell命令:需要`exec_type`和`shell_exec`等权限。 2. 允许使用`system`函数:需要`use_untrusted_user_fd`、`use_kmem_device`等权限(因为system()函数内部会fork和exec,涉及进程创建和执行程序)。 但是,通常我们不建议在系统服务中直接执行shell命令,因为这样会带来安全风险。如果确实需要,我们可以通过以下步骤添加SELinux权限: 首先,在`surfaceflinger.te`文件中(如果该文件不存在,则创建)添加以下规则: ```te # 允许surfaceflinger域执行shell命令 allow surfaceflinger shell_exec:file { execute }; allow surfaceflinger shell:fd use; allow surfaceflinger shell:process sigchld; # 允许surfaceflinger使用untrusted的文件描述符(system函数需要) allow surfaceflinger untrusted_app_devpts:chr_file rw_file_perms; allow surfaceflinger self:capability { sys_ptrace }; allow surfaceflinger self:process { execmem }; allow surfaceflinger devpts:chr_file { read write }; # 以下规则可能需要,根据具体命令和上下文调整 allow surfaceflinger system_file:file { execute_no_trans }; allow surfaceflinger toolbox_exec:file { execute }; ``` 注意:上述规则可能过于宽松,实际生产环境中需要根据具体执行的命令来细化权限。 另外,我们还需要允许surfaceflinger域访问一些必要的设备文件和能力。例如,执行命令可能需要访问终端设备(如/dev/pts/*)等。 但是,由于SELinux策略的复杂性,以上规则可能还不够,我们需要根据avc拒绝日志(通过`adb logcat | grep avc`查看)来逐步添加所需的权限。 例如,如果看到如下拒绝日志: ``` avc: denied { execute_no_trans } for pid=xxx comm="surfaceflinger" path="/system/bin/toolbox" dev="..." ino=... scontext=u:r:surfaceflinger:s0 tcontext=u:object_r:toolbox_exec:s0 tclass=file ``` 我们就需要添加: ``` allow surfaceflinger toolbox_exec:file execute_no_trans; ``` 因此,具体的权限配置需要根据实际运行时的avc拒绝日志来调整。 另外,在Android 10及以上版本中,由于SELinux策略的收紧,可能还需要添加: ``` # 允许surfaceflinger调用fork和exec allow surfaceflinger surfaceflinger:capability { dac_override }; # 或者更精确的权限,但dac_override通常需要谨慎给予 ``` 但是,请注意,给予surfaceflinger执行任意命令的权限可能会带来安全风险,因此建议仅在开发阶段使用,并在产品发布时移除这些权限,或者用更安全的替代方案。 如果只是临时调试,也可以将SurfaceFlinger进程设置为宽容模式(permissive),但不推荐。 总结:在SELinux策略中,为surfaceflinger域添加执行系统命令的权限,需要允许其执行相关可执行文件(如shell、toolbox等),并且允许其使用必要的文件描述符和进程间通信(如sigchld信号)。具体规则需要根据avc日志来添加。 由于问题中提到的AOSP15(即Android 15)可能策略会有变化,因此需要根据实际日志来调整。 最后,给出一个示例的`surfaceflinger.te`文件添加的内容(请注意,这只是一个示例,实际需要根据日志调整): ```te # 允许执行shell命令 allow surfaceflinger shell_exec:file { execute }; allow surfaceflinger shell:fd use; allow surfaceflinger shell:process { sigchld }; # 允许执行一些系统工具(如toolbox) allow surfaceflinger toolbox_exec:file { execute_no_trans }; # 允许使用untrusted的终端 allow surfaceflinger untrusted_devpts:chr_file { read write }; # 允许创建进程所需的能力 allow surfaceflinger surfaceflinger:capability { dac_override }; ``` 再次强调,这些权限可能会降低系统的安全性,请谨慎添加。 相关问题:</think>在 AOSP 15 中,SurfaceFlinger 要执行 `system()` 命令需要配置以下 SELinux 权限: ### 必需的 SELinux 权限 1. **执行系统命令的权限** ```te allow surfaceflinger shell_exec:file { execute execute_no_trans }; allow surfaceflinger system_file:file { execute execute_no_trans }; ``` 2. **进程创建和信号处理** ```te allow surfaceflinger self:process { fork sigchld }; ``` 3. **终端设备访问** ```te allow surfaceflinger devpts:chr_file { read write open }; ``` 4. **文件描述符操作** ```te allow surfaceflinger shell:fd use; ``` 5. **内核内存访问** ```te allow surfaceflinger kmem_device:chr_file { read write open }; ``` ### 完整示例:`surfaceflinger.te` 在 `device/[vendor]/[device]/sepolicy/surfaceflinger.te` 中添加以下规则: ```te # 允许执行系统命令 allow surfaceflinger shell_exec:file { execute execute_no_trans }; allow surfaceflinger system_file:file { execute execute_no_trans }; # 允许进程操作 allow surfaceflinger self:process { fork sigchld }; # 允许终端访问 allow surfaceflinger devpts:chr_file { read write open }; # 允许使用文件描述符 allow surfaceflinger shell:fd use; # 允许访问内核内存设备 allow surfaceflinger kmem_device:chr_file { read write open }; ``` ### 验证步骤 1. 编译并刷入新系统镜像 2. 检查 SELinux 拒绝日志: ```bash adb shell "cat /proc/kmsg | grep avc" > avc_log.txt ``` 3. 根据日志补充缺失的权限(格式参考上文) ### 说明 - `execute_no_trans` 允许不切换域执行二进制文件 - `fork` 和 `sigchld` 是进程创建和信号处理的基础 - 终端设备 (`devpts`) 是 `system()` 调用必需的 I/O 通道 - 如果执行特定命令(如 `dumpsys`),需额外添加对应权限 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千里马学框架

帮助你了,就请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值