-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcortex_debug.py
More file actions
116 lines (108 loc) · 4.56 KB
/
Copy pathcortex_debug.py
File metadata and controls
116 lines (108 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import json
import os
import re
import sys
Import("env")
openocd_server_ready_regex = "Listening on port \\d+ for gdb connection"
openocd_extra_server_args = [ '-s', 'nop' ]
openocd_init_cmds = [ "monitor debugwire enable" ]
print(env.BoardConfig().get('debug',{}).get('tools').get('pyavrocd', ""))
def exe_file_name(path):
if path.endswith(".exe") or path.endswith(".EXE"):
return path
if sys.platform.startswith("win"):
return path + ".exe"
return path
def deb_tool():
expl = env.GetProjectOption("debug_tool", default=None)
if expl:
return expl
debug = env.BoardConfig().get('debug',{})
tools = debug.get('tools',None)
if not tools:
return None
elif len(tools) == 1:
return list(tools.keys())[0]
else:
for x in tools:
if tools[x].get("default", False):
return x
return list(tools.keys())[0]
def gen_entry(environment, executable, toolconfig, tc_dir, svdpath,
gdb_path, serverpath, serverargs):
return {
"name": "Cortex-Debug (" + environment + ")",
"type": "cortex-debug",
"servertype": "openocd",
"cwd": "${workspaceRoot}",
"request": "launch",
"executable": executable,
"configFiles": [
"nix"
],
"preLaunchCommands": openocd_init_cmds,
"gdbPath": exe_file_name(gdb_path),
"objdumpPath": exe_file_name(os.path.join(tc_dir,"avr-objdump")),
"serverpath": exe_file_name(serverpath),
"overrideGDBServerStartedRegex": openocd_server_ready_regex,
"svdFile": svdpath,
"runToEntryPoint": "main",
"serverArgs": serverargs + openocd_extra_server_args,
"projectEnvName": environment,
"preLaunchTask": {
"type": "PlatformIO",
"task": "Pre-Debug (" + environment + ")"
}
}
def refresh_json(*args, **kwargs):
jsonfile = os.path.join(env.get('PROJECT_DIR',''), ".vscode", "launch.json")
if os.path.exists(jsonfile):
origjson = {}
with open(jsonfile, "r", encoding="utf-8") as f:
origjson = json.loads(re.sub("//.*","",f.read(),flags=re.MULTILINE))
environment = env.get("PIOENV","")
package_dir = env.get("PROJECT_PACKAGES_DIR", "")
executable = origjson['configurations'][-1].get('executable', '')
toolchainbin_dir = origjson['configurations'][-1].get('toolchainBinDir', '')
svdpath = origjson['configurations'][-1].get('svdPath', '')
tool = deb_tool()
toolconfig = env.BoardConfig().get('debug',{}).get('tools').get(tool, "")
gdb_path = os.path.join(package_dir, toolconfig.get('server', {}).get('package', ""),
"avr-gdb")
server_path = os.path.join(package_dir, toolconfig.get('server', {}).get('package', ""),
toolconfig.get('server', {}).get('executable', ""))
server_args = toolconfig.get('server', {}).get('arguments', [])
new_entry = gen_entry(environment, executable, toolconfig,
toolchainbin_dir, svdpath, gdb_path,
server_path, server_args)
if tool != 'pyavrocd': # sorry, works only with pyavrocd
if origjson['configurations'][0]['type'] == 'cortex-debug':
del origjson['configurations'][0]
new_entry = True
print("No debug tool or incompatible tool specified")
elif origjson['configurations'][0]['type'] != 'cortex-debug':
origjson['configurations'].insert(0, new_entry)
elif origjson['configurations'][0] != new_entry:
origjson['configurations'][0] = new_entry
else:
new_entry = None
if new_entry:
with open(jsonfile, "w", encoding="utf-8") as f:
print("""// AUTOMATICALLY GENERATED FILE. PLEASE DO NOT MODIFY IT MANUALLY
//
// PlatformIO Debugging Solution
//
// Documentation: https://docs.platformio.org/en/latest/plus/debugging.html
// Configuration: https://docs.platformio.org/en/latest/projectconf/sections/env/options/debug/index.html
//
// Updated by Cortex-Debug generation. Choose your launch.json entry wisely""", file=f)
print(json.dumps(origjson, indent=4), file=f)
env.AddCustomTarget(
name="refresh_json",
dependencies=None,
actions=[
refresh_json
],
title="Refresh launch.json",
description="Generate Cortex-Debug launch.json entry"
)