linux logger.c移植问题

本文介绍如何将Andriod的logcat工具移植到Linux环境,详细讲解了针对不同内核版本(3.x与4.x)的修改步骤,包括调整头文件包含、函数参数与结构体字段等,以实现logcat的完美打印控制。

        andriod的logcat工具很好用,可以将其移植到linux上,方便程序调试。logcat在linux上分为两部分:logcat工具和内核驱动logger。logger驱动在3.x内核中位于/drivers/staging/andriod中,如果使用内核版本是3.x,直接编译即可。如果内核版本使用4.x,会编译报错,主要是4.x内核的部分结构体进行了调整,所以需要修改logger.c。本想直接使用4.x内核中的logger.c,不过很遗憾,从4.0开始logger被移除了staging,所以只能修改3.x中的logger.c。我使用的logger.c是3.14.12中的,其它版本下的应该大同小异吧。主要修改点如下:

  1. 包含头文件:#include <uio.h>   
  2. 修改函数logger_aio_write:

         入参改为struct kiocb *iocb和struct iov_iter *iter;  //根据4.x内核file_operations中write_iter原型定义修改

         iocb->ki_nbytes改为iter->count  //4.0内核无ki_nbytes字段,替换字段为iter->count

         nr_segsiov移到iter结构中,前面加iter->即可

    3. file_operations中异步接口改为write_iter

经过上面三处修改应该就能编译了,生成ko文件insmod试试吧,体验logcat的完美打印控制。

def generate(self): try: # 创建TestIP所需要的文件夹 chkFlg = self.__initTestIP() if not chkFlg: return ezautil.setResultMsg(False, self.logger.getObservedMsg()) self.logger.info(MSG.MSG_IA0270001.format(EZACONST.GENIPTESTPROGRAM)) platformResPath = Path( r"C:\temp\EzA10ClientSetup\TestIPLib\1.0.19.0\hsi-ate-testips\hsi-ate-testip-demoStatic\platform\93k8\res") # 静态ip从源路径复制指定文件到目标路径 copyFlg = a93k8util.copy_specific_directories(platformResPath, self.testIP.outputFolder, self.logger) if not copyFlg: return ezautil.setResultMsg(False, self.logger.getObservedMsg()) outPutJsonInfoList = [] output_jsonInfo = smt8.OutputJsonInfo('ALL', 'ALL', 'ALL') a93k8util.collect_files_relative_paths(self.testIP.outputFolder,'timing',output_jsonInfo.timing_files) a93k8util.collect_files_relative_paths(self.testIP.outputFolder, 'vector', output_jsonInfo.vector_files) a93k8util.collect_files_relative_paths(self.testIP.outputFolder, 'flow', output_jsonInfo.subflow_files) a93k8util.collect_files_relative_paths(self.testIP.outputFolder, 'testtable', output_jsonInfo.testtable_files) ods_files = [file for file in output_jsonInfo.testtable_files if Path(file).suffix == ".ods"] json_files = [file for file in output_jsonInfo.testtable_files if Path(file).suffix == ".json"] output_jsonInfo.testtable_files = ods_files output_jsonInfo.testtable_jsons = json_files outPutJsonInfoList.append(output_jsonInfo) jsonInfOutPutPath = Path(self.testIP.outputTempFolder).joinpath('testipOutput.json') a93k8util.write_out_put_json(self.testIP.name, outPutJsonInfoList, self.testIP.outputFolder, jsonInfOutPutPath) # 开始创建TestProgram和Test Instance Plan # chkFlg = self.__genTestProgramAndInstPlan() # if self.unConvertedPat: # self.logger.warning(MSG.MSG_WA0270017.format(",".join(self.unConvertedPat))) if chkFlg: self.logger.info(MSG.MSG_IA0290002.format(EZACONST.GENIPTESTPROGRAM)) else: self.logger.info(MSG.MSG_IA0290003.format(EZACONST.GENIPTESTPROGRAM)) # 返回最终结果 return ezautil.setResultMsg(chkFlg, self.logger.getObservedMsg()) except Exception: self.logger.error("genIPTestProgram发生异常,异常信息:" + traceback.format_exc()) return ezautil.setResultMsg(False, self.logger.getObservedMsg()) platformResPath = Path( r"C:\temp\EzA10ClientSetup\TestIPLib\1.0.19.0\hsi-ate-testips\hsi-ate-testip-demoStatic\platform\93k8\res")这段代码可以优化吗
09-16
def execute_flash(self): """执行刷机""" target_key = self.get_target_key() try: cmd = [ self.flash_exe, # 刷机工具exe f"--{target_key}_dfu_path", version_file # 版本包路径 ] self.add_log("开始刷机...") self.logger.info(f"开始刷机...") self.add_log(f"设备: {device}, 设备对象: {self.target_var.get()}") self.logger.info(f"设备: {device}, 设备对象: {self.target_var.get()}") # 隐藏控制台窗口 if os.name == "nt": startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW startupinfo.wShowWindow = subprocess.SW_HIDE else: startupinfo = None # 执行刷机命令 self.flash_process = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, encoding=self.exe_encoding, errors='ignore', startupinfo=startupinfo ) # 构建命令参数的匹配模式,将命令隐藏 cmd_pattern = re.compile(r'^args:Namespace.*', flags=re.MULTILINE) flag = "" # 标记是否成功 # 读取输出 while True: output = self.flash_process.stdout.readline() if output == '' and self.flash_process.poll() is not None: break if output: # 过滤掉命令参数 filtered_output = cmd_pattern.sub('****' * 20, output) # 用****替换敏感参数 self.add_log(filtered_output.strip()) self.logger.info(filtered_output.strip()) if f"{target_key} upload_result:Upload OK!" in output: flag = "success" elif f"{target_key} upload_result:Upload FAIL" in output: flag = "fail" elif "upload NG 09 01 time out" in output: flag = "error" # 检查进程是否已结束 if self.flash_process.poll() is not None: flag = "fail" break if flag == "time out" and self.flash_process.poll() is None: self.flash_process.terminate() self.add_log("刷机流程已自动终止") self.logger.info("刷机流程已自动终止") return_code = self.flash_process.poll() self.add_log(f"{self.target_var.get()}刷机结果:{flag}") self.logger.info(f"{self.target_var.get()}刷机结果:{flag}") # 检查刷机结果 self.check_flash_result(return_code, flag) except Exception as e: error_msg = f"刷机过程出错: {str(e)}" self.logger.error(error_msg) self.add_log(error_msg) messagebox.showerror("错误", error_msg) finally: self.cleanup_after_flash() 增加超时机制,对output每隔一秒进行判断,如果60s后依然无输出,flag=time out,不支持使用select检查是否有可读数据
最新发布
10-21
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值