Skip to content

Instantly share code, notes, and snippets.

@pveierland
Last active July 11, 2021 14:57
Show Gist options
  • Save pveierland/a10a898308f350df39ebe183de4ed320 to your computer and use it in GitHub Desktop.
Save pveierland/a10a898308f350df39ebe183de4ed320 to your computer and use it in GitHub Desktop.
WSL JLinkExe + JLinkGDBServer
#!/usr/bin/env python
import os
import re
import shutil
import subprocess
import sys
windows_working_directory = '/mnt/c/tmp/'
arguments = list(sys.argv)
arguments[0] = '/mnt/c/Program\ Files\ \(x86\)/SEGGER/JLink/JLink.exe'
for i, arg in enumerate(arguments):
if arg == '-CommanderScript':
commander_script = arguments[i+1]
commander_script_basename = os.path.basename(commander_script)
arguments[i+1] = commander_script_basename
target_commander_script = os.path.join(windows_working_directory, commander_script_basename)
shutil.copyfile(commander_script, target_commander_script)
commander_script_contents = open(target_commander_script).read()
binary_file = re.search('loadfile\s+(\S+)[,\s]+', commander_script_contents).group(1)
binary_file_basename = os.path.basename(binary_file)
target_binary_file = os.path.join(windows_working_directory, binary_file_basename)
shutil.copyfile(binary_file, target_binary_file)
commander_script_updated = re.sub(
'loadfile\s+\S+[,\s]+',
'loadfile {} '.format(binary_file_basename),
commander_script_contents,
flags = re.M)
with open(target_commander_script, 'w') as f:
f.write(commander_script_updated)
sys.exit(subprocess.call(' '.join(arguments), shell=True, cwd=windows_working_directory))
#!/usr/bin/env python
import os
import re
import shutil
import subprocess
import sys
windows_working_directory = '/mnt/c/tmp/'
arguments = list(sys.argv)
arguments[0] = '/mnt/c/Program\ Files\ \(x86\)/SEGGER/JLink/JLinkGDBServerCL.exe'
for i, arg in enumerate(arguments):
if arg == '-CommanderScript':
commander_script = arguments[i+1]
commander_script_basename = os.path.basename(commander_script)
arguments[i+1] = commander_script_basename
target_commander_script = os.path.join(windows_working_directory, commander_script_basename)
shutil.copyfile(commander_script, target_commander_script)
commander_script_contents = open(target_commander_script).read()
binary_file = re.search('loadfile\s+(\S+)[,\s]+', commander_script_contents).group(1)
binary_file_basename = os.path.basename(binary_file)
target_binary_file = os.path.join(windows_working_directory, binary_file_basename)
shutil.copyfile(binary_file, target_binary_file)
commander_script_updated = re.sub(
'loadfile\s+\S+[,\s]+',
'loadfile {} '.format(binary_file_basename),
commander_script_contents,
flags = re.M)
with open(target_commander_script, 'w') as f:
f.write(commander_script_updated)
sys.exit(subprocess.call(' '.join(arguments), shell=True, cwd=windows_working_directory))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment