Files
platformio-core/platformio/commands/debug.py

166 lines
5.8 KiB
Python
Raw Normal View History

2019-04-19 19:56:16 +03:00
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=too-many-arguments, too-many-locals
# pylint: disable=too-many-branches, too-many-statements
2019-04-19 19:56:16 +03:00
import asyncio
2019-04-19 19:56:16 +03:00
import os
import subprocess
2019-04-19 19:56:16 +03:00
import click
2020-08-22 17:48:49 +03:00
from platformio import app, exception, fs, proc
from platformio.commands.platform import platform_install as cmd_platform_install
2021-03-19 00:21:44 +02:00
from platformio.compat import IS_WINDOWS
from platformio.debug import helpers
from platformio.debug.config.factory import DebugConfigFactory
from platformio.debug.exception import DebugInvalidOptionsError
from platformio.debug.process.gdb import GDBClientProcess
from platformio.platform.exception import UnknownPlatform
from platformio.platform.factory import PlatformFactory
2019-05-10 15:45:52 +03:00
from platformio.project.config import ProjectConfig
2019-11-28 16:15:54 +02:00
from platformio.project.exception import ProjectEnvsNotAvailableError
from platformio.project.helpers import is_platformio_project
@click.command(
"debug",
context_settings=dict(ignore_unknown_options=True),
short_help="Unified debugger",
)
@click.option(
"-d",
"--project-dir",
default=os.getcwd,
type=click.Path(
exists=True, file_okay=False, dir_okay=True, writable=True, resolve_path=True
),
)
@click.option(
"-c",
"--project-conf",
type=click.Path(
exists=True, file_okay=True, dir_okay=False, readable=True, resolve_path=True
),
)
2019-04-19 19:56:16 +03:00
@click.option("--environment", "-e", metavar="<environment>")
@click.option("--verbose", "-v", is_flag=True)
@click.option("--interface", type=click.Choice(["gdb"]))
@click.argument("__unprocessed", nargs=-1, type=click.UNPROCESSED)
@click.pass_context
def cli(ctx, project_dir, project_conf, environment, verbose, interface, __unprocessed):
app.set_session_var("custom_project_conf", project_conf)
# use env variables from Eclipse or CLion
for name in ("CWD", "PWD", "PLATFORMIO_PROJECT_DIR"):
if is_platformio_project(project_dir):
break
if os.getenv(name):
project_dir = os.getenv(name)
2019-04-19 19:56:16 +03:00
with fs.cd(project_dir):
project_config = ProjectConfig.get_instance(project_conf)
project_config.validate(envs=[environment] if environment else None)
env_name = environment or helpers.get_default_debug_env(project_config)
2019-04-19 19:56:16 +03:00
if not interface:
return helpers.predebug_project(ctx, project_dir, env_name, False, verbose)
2019-04-19 19:56:16 +03:00
env_options = project_config.items(env=env_name, as_dict=True)
if not set(env_options.keys()) >= set(["platform", "board"]):
raise ProjectEnvsNotAvailableError()
try:
platform = PlatformFactory.new(env_options["platform"])
except UnknownPlatform:
ctx.invoke(
cmd_platform_install,
platforms=[env_options["platform"]],
skip_default_package=True,
)
platform = PlatformFactory.new(env_options["platform"])
debug_config = DebugConfigFactory.new(platform, project_config, env_name)
2019-04-19 19:56:16 +03:00
if "--version" in __unprocessed:
return subprocess.run(
[debug_config.client_executable_path, "--version"], check=True
)
2019-04-19 19:56:16 +03:00
try:
fs.ensure_udev_rules()
except exception.InvalidUdevRules as e:
click.echo(
helpers.escape_gdbmi_stream("~", str(e) + "\n")
if helpers.is_gdbmi_mode()
else str(e) + "\n",
nl=False,
)
2019-04-19 19:56:16 +03:00
rebuild_prog = False
preload = debug_config.load_cmds == ["preload"]
load_mode = debug_config.load_mode
2019-04-19 19:56:16 +03:00
if load_mode == "always":
rebuild_prog = preload or not helpers.has_debug_symbols(
debug_config.program_path
)
2019-04-19 19:56:16 +03:00
elif load_mode == "modified":
rebuild_prog = helpers.is_prog_obsolete(
debug_config.program_path
) or not helpers.has_debug_symbols(debug_config.program_path)
if not (debug_config.program_path and os.path.isfile(debug_config.program_path)):
rebuild_prog = True
2019-04-19 19:56:16 +03:00
if preload or (not rebuild_prog and load_mode != "always"):
# don't load firmware through debug server
debug_config.load_cmds = []
2019-04-19 19:56:16 +03:00
if rebuild_prog:
if helpers.is_gdbmi_mode():
click.echo(
helpers.escape_gdbmi_stream(
"~", "Preparing firmware for debugging...\n"
),
nl=False,
)
stream = helpers.GDBMIConsoleStream()
2020-08-22 17:48:49 +03:00
with proc.capture_std_streams(stream):
helpers.predebug_project(ctx, project_dir, env_name, preload, verbose)
stream.close()
2019-04-19 19:56:16 +03:00
else:
click.echo("Preparing firmware for debugging...")
helpers.predebug_project(ctx, project_dir, env_name, preload, verbose)
2019-04-19 19:56:16 +03:00
# save SHA sum of newly created prog
if load_mode == "modified":
helpers.is_prog_obsolete(debug_config.program_path)
2019-04-19 19:56:16 +03:00
if not os.path.isfile(debug_config.program_path):
2019-11-28 16:15:54 +02:00
raise DebugInvalidOptionsError("Program/firmware is missed")
2019-04-19 19:56:16 +03:00
2021-03-19 00:21:44 +02:00
loop = asyncio.ProactorEventLoop() if IS_WINDOWS else asyncio.get_event_loop()
asyncio.set_event_loop(loop)
client = GDBClientProcess(project_dir, debug_config)
coro = client.run(__unprocessed)
loop.run_until_complete(coro)
del client
2021-03-19 00:21:44 +02:00
if IS_WINDOWS:
# an issue with asyncio executor and STIDIN, it cannot be closed gracefully
2021-03-17 21:08:06 +02:00
proc.force_exit()
loop.close()
2019-04-19 19:56:16 +03:00
return True