1. 程式人生 > 實用技巧 >VScode+docker建立一個Pin的開發環境

VScode+docker建立一個Pin的開發環境

VScode+docker建立一個Pin的開發環境

由於我一直用Mac,但是一般用Pin都是在Linux下和Windows下,Windows下自然是很方便,直接VS開啟,開發時不要太舒服。但是開發Linux時,我一直都是採用docker+掛載資料夾的方式進行同步開發,沒補全,還得每次輸指令,怪麻煩的。

工具

  • vscode
  • docker
  • 一個含有Pin的docker映象
  • vscode Remote-Containers
  • vscode c/c++外掛

流程

  1. 下載vscode外掛,準備映象開容器
    我這裡用pwndocker掛載資料夾的方式

    docker run -d \
        --rm \
        -h ctf \
        --name ctf \
        -v /CTF-0v0:/ctf/work \
        -p 23946:23946 \
        --cap-add=SYS_PTRACE \
        skysider/pwndocker
    
  2. 用外掛 Attach to Running Container 選中你的容器

  3. 然後選擇工作區即可,我這裡選擇Pin自帶的MyPinTool資料夾作為工作區,等一會vscode自己進行初始化,就可以看到工作區檔案夾了。

    初始是沒有.vscode那個資料夾,我這邊因為已經弄過了,上圖只是參考。

  4. 我這邊把MyPinTool拷貝出來一個副本叫做MyPinTool64,一個作為32位開發工作區一個作為64位開發工作區

  5. 接下來就是配置.vscode的配置檔案了,這裡主要配置兩個檔案,第一個是c_cpp_properties.json第二個是task.json檔案。

    • 第一個c_cpp_properties主要是設定includePath和Define,Pin的標頭檔案還是挺多的,我是觀察在linux下make生成Pintool的時候,會有-I 包含標頭檔案,Define也是觀察命令列下的make得到。
      配置檔案供參考x86_64

      {
          "configurations": [
              {
                  "name": "Linux",
                  "includePath": [
                      "${workspaceFolder}/**",
                      "../../include/pin",
                      "../../include/pin/gen",
                      "../InstLib",
                      "../../../extras/xed-intel64/include/xed",
                      "../../../extras/components/include",
                      "../../../extras/stlport/include",
                      "../../../extras",
                      "../../../extras/libstdc++/include",
                      "../../../extras/crt/include",
                      "../../../extras/crt",
                      "../../../extras/crt/include/arch-x86_64",
                      "../../../extras/crt/include/kernel/uapi",
                      "../../../extras/crt/include/kernel/uapi/asm-x86"
                  ],
                  "defines": [
                      "TARGET_IA32E", 
                      "HOST_IA32E", 
                      "TARGET_LINUX", 
                      "__PIN__=1", 
                      "PIN_CRT=1"
                  ],
                  "macFrameworkPath": [],
                  "compilerPath": "/usr/bin/g++",
                  "cStandard": "${default}",
                  "cppStandard": "${default}",
                  "intelliSenseMode": "${default}"
              }
          ],
          "version": 4
      }
      

      x86

      {
          "configurations": [
              {
                  "name": "Linux",
                  "includePath": [
                      "${workspaceFolder}/**",
                      "../../include/pin",
                      "../../include/pin/gen",
                      "../InstLib",
                      "../../../extras/xed-ia32/include/xed",
                      "../../../extras/components/include",
                      "../../../extras/stlport/include",
                      "../../../extras",
                      "../../../extras/libstdc++/include",
                      "../../../extras/crt/include",
                      "../../../extras/crt",
                      "../../../extras/crt/include/arch-x86",
                      "../../../extras/crt/include/kernel/uapi",
                      "../../../extras/crt/include/kernel/uapi/asm-x86"
                  ],
                  "defines": [
                      "TARGET_IA32", 
                      "HOST_IA32", 
                      "TARGET_LINUX", 
                      "__PIN__=1", 
                      "PIN_CRT=1"
                  ],
                  "macFrameworkPath": [],
                  "compilerPath": "/usr/bin/g++",
                  "cStandard": "${default}",
                  "cppStandard": "${default}",
                  "intelliSenseMode": "${default}"
              }
          ],
          "version": 4
      }
      
    • 第二個是task.json,我這裡配置了兩個任務,一個是編譯,一個是插樁執行,這裡沒有配置launch檔案的原因是覺得多此一舉了,沒有必要弄除錯那些,Pin本來就是算是在除錯原程式,關於Pintool的除錯,打log工具不用打log除錯?(滑稽)這裡主要是插樁執行地方,開始嘗試了vscode三種設定引數的方式去選擇執行的程式,但是我覺得還不如直接改配置來的快速,也就沒有配置引數傳入方式。
      檔案供參考x86_64

      {
          // See https://go.microsoft.com/fwlink/?LinkId=733558
          // for the documentation about the tasks.json format
          "version": "2.0.0",
          "tasks": [
              {
                  "label": "make",
                  "type": "shell",
                  "command": "make obj-intel64/MyPinTool.so TARGET=intel64 ",
                  "problemMatcher": [],
                  "group": {
                      "kind": "build",
                      "isDefault": true
                  }
              },
              {
                  "label": "run",
                  "type": "shell",
                  "command": "../../../pin -t obj-intel64/MyPinTool.so -- ./xxxx",
                  "problemMatcher": [],
                  "group": {
                      "kind": "build",
                      "isDefault": true
                  }
              }
          ]
      }
      

      x86

      {
          // See https://go.microsoft.com/fwlink/?LinkId=733558
          // for the documentation about the tasks.json format
          "version": "2.0.0",
          "tasks": [
              {
                  "label": "make",
                  "type": "shell",
                  "command": "make obj-ia32/MyPinTool.so TARGET=ia32 ",
                  "problemMatcher": [],
                  "group": {
                      "kind": "build",
                      "isDefault": true
                  }
              },
              {
                  "label": "run",
                  "type": "shell",
                  "command": "../../../pin -t obj-intel64/MyPinTool.so -- ./flow",
                  "problemMatcher": [],
                  "group": {
                      "kind": "build",
                      "isDefault": true
                  }
              }
          ]
      }
      
    • 然後就可以快樂的運行了commend + shift + B快樂執行

  6. 配置的感覺挺簡陋的,不過有程式碼補全和糾錯,還能一鍵執行,可以滿足大多數要求了

後記

這是第一次用vscode,配置的略簡陋。