欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何使用Swift來(lái)實(shí)現(xiàn)一個(gè)命令行工具的方法

 更新時(shí)間:2020年05月15日 14:30:57   作者:超越楊超越  
這篇文章主要介紹了如何使用Swift來(lái)實(shí)現(xiàn)一個(gè)命令行工具,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

本文即簡(jiǎn)單介紹了如何在Swift中開(kāi)發(fā)命令行工具,以及與Shell命令的交互。水文一篇,不喜勿噴。

主要是使用該工具來(lái)解析微信的性能監(jiān)控組件Matrix的OOM Log。

基本模塊

這里,僅簡(jiǎn)單介紹了常見(jiàn)的基本模塊。

Process

Process類可以用來(lái)打開(kāi)另外一個(gè)子進(jìn)程,并監(jiān)控其運(yùn)行情況。

  1. launchPath:指定了執(zhí)行路徑。如可以設(shè)置為 /usr/bin/env ,這個(gè)命令可以用于打印本機(jī)上所有的環(huán)境變量;也可以用于執(zhí)行shell命令,如果你接了參數(shù)的話。本文的Demo就用它來(lái)執(zhí)行輸入的命令。
  2. arguments:參數(shù),以數(shù)組形式傳遞即可。
  3. launch:調(diào)用launch函數(shù)即可啟動(dòng)process,用于執(zhí)行命令。
  4. waitUntilExit:一般執(zhí)行Shell命令,需要等待命令返回。
  5. terminationStatus:當(dāng)前process的結(jié)束狀態(tài),正常為0.
  6. standardOutput:standardOutput對(duì)應(yīng)于終端的標(biāo)準(zhǔn)輸出。standardError則是錯(cuò)誤輸出。

Pipe

Pipe這個(gè)類就是操作系統(tǒng)的管道,在這里用來(lái)接受子進(jìn)程的輸出。這里,可以用于將process的輸出傳遞至管道指定的地方,如一個(gè)output變量,或者文件也可以。

  • fileHandleForReading:pipe從哪里讀取內(nèi)容?
  • fileHandleForWriting:pipe將內(nèi)容寫(xiě)到哪里?

CommandLine

用于獲取腳本參數(shù)而已。

print(CommandLine.argc) // 2
print(CommandLine.arguments) // ["./test.swift", "hello"]

封裝Shell命令

僅執(zhí)行Shell命令

這里提供了兩種調(diào)用Shell命令的封裝函數(shù),個(gè)人更傾向于第二種,直接將Shell命令及參數(shù)封裝成一個(gè)字符串傳入即可。

@discardableResult
func runShell(_ command: String) -> Int32 {
 let task = Process()
 task.launchPath = "/bin/bash"
 task.arguments = ["-c", command]
 task.launch()
 task.waitUntilExit()
 return task.terminationStatus
}

@discardableResult
func runShellWithArgs(_ args: String...) -> Int32 {
 let task = Process()
 task.launchPath = "/usr/bin/env"
 task.arguments = args
 task.launch()
 task.waitUntilExit()
 return task.terminationStatus
}

使用如下:

runShell("pwd")
runShell("ls -l")

runShellWithArgs("pwd")
runShellWithArgs("ls", "-l")

需要Shell命令的輸出內(nèi)容

這里就需要使用到Pipe了。

@discardableResult
func runShellAndOutput(_ command: String) -> (Int32, String?) {
 let task = Process()
 task.launchPath = "/bin/bash"
 task.arguments = ["-c", command]
 
 let pipe = Pipe()
 task.standardOutput = pipe
 task.standardError = pipe
 
 task.launch()
 
 let data = pipe.fileHandleForReading.readDataToEndOfFile()
 let output = String(data: data, encoding: .utf8)
 
 task.waitUntilExit()
 
 return (task.terminationStatus, output)
}

@discardableResult
func runShellWithArgsAndOutput(_ args: String...) -> (Int32, String?) {
 let task = Process()

 task.launchPath = "/usr/bin/env"
 task.arguments = args
 
 let pipe = Pipe()
 task.standardOutput = pipe
 task.standardError = pipe
 
 task.launch()
 
 let data = pipe.fileHandleForReading.readDataToEndOfFile()
 let output = String(data: data, encoding: .utf8)
 
 task.waitUntilExit()
 
 return (task.terminationStatus, output)
}

使用如下:

let (ret1, output1) = runShellAndOutput("ls -l")
if let output11 = output1 {
 print(output11)
}

let (ret2, output2) = runShellWithArgsAndOutput("ls", "-l")
if let output22 = output2 {
 print(output2)
}

如何解析Matrix的OOM Log

Matrix的OOM Log格式如下,其實(shí)就是一個(gè)大JSON:

{
 "head": {
  "protocol_ver": 1,
  "phone": "iPhone10,1",
  "os_ver": "13.4",
  "launch_time": 1589361495000,
  "report_time": 1589362109100,
  "app_uuid": ""
 },
 "items": [
  {
   "tag": "iOS_MemStat",
   "info": "",
   "scene": "",
   "name": "Malloc 12.54 MiB",
   "size": 146313216,
   "count": 1,
   "stacks": [
    {
     "caller": "f07199ac8a903127b17f0a906ffb0237@84128",
     "size": 146313216,
     "count": 1,
     "frames": [
      {
       "uuid": "a0a7d67af0f3399a8f006f92716d8e6f",
       "offset": 67308
      },
      {
       "uuid": "a0a7d67af0f3399a8f006f92716d8e6f",
       "offset": 69836
      },
      {
       "uuid": "f07199ac8a903127b17f0a906ffb0237",
       "offset": 84128
      },
      {
       "uuid": "b80198f7beb93e79b25c7a27d68bb489",
       "offset": 14934312
      },
      {
       "uuid": "1a46239df2fc34b695bc9f38869f0c85",
       "offset": 1126304
      },
      {
       "uuid": "1a46239df2fc34b695bc9f38869f0c85",
       "offset": 123584
      },
      {
       "uuid": "1a46239df2fc34b695bc9f38869f0c85",
       "offset": 1135100
      }]
    }
   ]
  }
 ]
}

解析的思路其實(shí)非常簡(jiǎn)單,將JSON轉(zhuǎn)為Model,然后根據(jù)所需,提取對(duì)應(yīng)的信息即可。

uuid是mach-o的唯一標(biāo)識(shí),offset則是符號(hào)相對(duì)于mach-o基地址的偏移量。拿到dSYM文件,使用 atos 命令即可進(jìn)行符號(hào)化。

guard let rawLogModel = MatrixOOMLogParser.parse() else { exit(-1) }
print("______ Start to process Matrix OOM Log ...")

let group = DispatchGroup()

var metaLog = ""

for item in bodyInfo.items {
 guard let stacks = item.stacks else { continue }
 
 group.enter()
 
 DispatchQueue.global().async {
  var log = "______ item ______ name: \(item.name), size: \(item.size), count: \(item.count) \n"
  metaLog += log
  
  for stack in stacks {
   let outputs = stack.frames.map({ (frame: MatrixOOMLogModelFrame) -> String in
    // let uuid = frame.uuid
    let offset = frame.offset
    let instructionAddress = loadAddress + offset
    let (_, output) = runShellAndOutput("xcrun atos -o \(dwarf) -arch arm64 -l 0x1 \(instructionAddress.hexValue)")
    return output ?? ""
   })
   
   log += outputs.joined()
   
   print(log)
  }
  
  group.leave()
 }
}

group.wait()

print("\n\(metaLog)\n")

print("______ Finished processing Matrix OOM Log ...")

MatrixOOMLogParser.parse() 就是將JSON轉(zhuǎn)為Model,這里用的就是Swift里邊的Codable。

這里有一個(gè)需要注意的點(diǎn),Mac CLI沒(méi)有Bundle的概念,只有一個(gè)bin文件。所以對(duì)于原始的JSON文件,只能通過(guò)外部bundle的方式來(lái)添加。通過(guò) New->Target 單獨(dú)建立一個(gè)bundle。需要在 Xcode -> Build Phases -> Copy Files 中添加該bundle名,然后即可通過(guò) Bundle(url: mockDataBundleURL) 來(lái)加載該bundle并獲取其中的log文件了。

因?yàn)閍tos的執(zhí)行時(shí)間較長(zhǎng),所以大量的符號(hào)化操作會(huì)非常耗時(shí)。一般來(lái)說(shuō),這段代碼執(zhí)行六七分鐘左右,可以將一個(gè)Matrix的OOM Log完全符號(hào)化。而符號(hào)化之后的記錄如何分析,就是另外一個(gè)話題了。

參考資料

How do I run an terminal command in a swift script? (e.g. xcodebuild)

到此這篇關(guān)于如何使用Swift來(lái)實(shí)現(xiàn)一個(gè)命令行工具的文章就介紹到這了,更多相關(guān)Swift 命令行內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評(píng)論