golang远程调试 vscode+dlv

作者:matrix 发布时间:2023-12-31 分类:Golang

远程环境可能会有远程调试需求,比如白名单访问限制等情况

要让本地环境调试远程环境数据,本地代码和远程环境执行代码必须保持一致

安装dlv

远程服务器环境安装 dlv

$ go  install github.com/go-delve/delve/cmd/dlv@latest

查看已安装dlv版本

$ dlv version
Delve Debugger
Version: 1.21.0
Build: $Id: fec0d226b2c2cce1567d5f59169660cf61dc1efe 

启动dlv服务

方法a. 监听已启动进程

$ dlv attach 28122  --listen=:8669 --headless --api-version=2 --log

28122 为已启动进程id
8669 为dlv开启的监听端口

方法b. 通过dlv直接启动指定bin文件

$ dlv  exec   --listen=:8669 --headless --api-version=2 --log  ./main-hhtjim

8669 为dlv开启的监听端口
main-hhtjim 为打包的bin文件

方法c. dlv直接启动并且监听

$ dlv debug --listen=:8669 --headless --api-version=2 --log

8669 为dlv开启的监听端口

本地vscode启动debug

本地配置 .vscode/launch.json

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Remote",
      "type": "go",
      "request": "attach",
      "mode": "remote",
      "remotePath": "/home/work/demo-go/", //项目远程根路径
      "port": 8669, //监听端口
      "host": "www.hhtjim.com", //远程主机/IP
      "cwd": "${workspaceFolder}",//vscode本地工作目录
      "trace": "verbose" //输出详情
     }
  ]
}

销毁dlv监听

调试环境不使用之后切记销毁,dlv目前无身份校验!

django在docker环境中断点调试

作者:matrix 发布时间:2022-01-07 分类:Python

之前是本地Python环境安装Django项目直接运行和断点,现在尝试切换到vscode

图片5398-django在docker环境中断点调试

vscode插件

https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers

https://marketplace.visualstudio.com/items?itemName=ms-python.python

Dockerfile配置

用于创建docker镜像

项目根目录中创建Dockerfile文件

# syntax=docker/dockerfile:1
FROM python:3.10

#设置容器中环境变量
ENV RUN_ENV='develop'
WORKDIR /data

COPY requirements.txt .
RUN pip install -i https://pypi.douban.com/simple/ -r requirements.txt

# 方式2.安装依赖
# RUN pip install -i https://pypi.douban.com/simple/ django pytz pymysql mysqlclient requests numpy python-dateutil matplotlib pandas_datareader scipy sklearn

根目录中需要有requirements.txt

docker-compose配置

用于开启容器,挂载开发目录以及端口

项目根目录中创建docker-compose.debug.yml文件

version: '3.4'

services:
  pythonsamplevscodedjangotutorial:
    image: django-dev:1.1
    build:
      context: .
      dockerfile: ./Dockerfile
    volumes:
      - ./:/data
    # command: ["sh", "-c", "pip install -i https://pypi.douban.com/simple/ debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678  /data/manage.py runserver 0.0.0.0:8000 --nothreading --noreload"]
    command: ["sh", "-c", "python /tmp/debugpy --listen 0.0.0.0:5678  /data/manage.py runserver 0.0.0.0:8000 --nothreading --noreload "]
    ports:
      - 8000:8000
      - 5678:5678

说明:

volumes表示 映射本地路径./-->容器路径/data

5678为断点监听端口
8000为项目访问端口

配置debug

配置vscode DEBUG

项目根目录中编辑或创建.vscode/launch.json

{
  "configurations": [
    {
       "name": "Python: Remote Attach",
       "type": "python",
       "request": "attach",
       "port": 5678,
       "host": "localhost",
       "pathMappings": [
           {
               "localRoot": "${workspaceFolder}",
               "remoteRoot": "/data"
           }
       ]
   }
  ]
}

启动容器

$ docker-compose -f ./docker-compose.debug.yml up

添加断点后,浏览器访问127.0.0.1:8000即可调试

参考:

https://code.visualstudio.com/docs/containers/docker-compose#_python

https://docs.docker.com/compose/compose-file/compose-file-v3/#build

https://www.youtube.com/watch?v=x7lZAmMVo2M