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.
|
|
|
|
|
|
2022-05-14 18:21:44 +03:00
|
|
|
import os
|
2019-10-24 16:56:28 +03:00
|
|
|
import re
|
2019-04-19 19:56:16 +03:00
|
|
|
import sys
|
|
|
|
|
import time
|
|
|
|
|
from hashlib import sha1
|
|
|
|
|
from io import BytesIO
|
|
|
|
|
|
2022-05-31 17:07:56 +03:00
|
|
|
from platformio.cli import PlatformioCLI
|
2022-06-17 23:05:20 +03:00
|
|
|
from platformio.compat import is_bytes
|
2021-03-17 17:42:11 +02:00
|
|
|
from platformio.debug.exception import DebugInvalidOptionsError
|
2022-05-31 17:20:56 +03:00
|
|
|
from platformio.run.cli import cli as cmd_run
|
|
|
|
|
from platformio.run.cli import print_processing_header
|
2022-05-10 20:21:49 +03:00
|
|
|
from platformio.test.helpers import list_test_names
|
2022-04-26 15:09:51 +03:00
|
|
|
from platformio.test.result import TestSuite
|
|
|
|
|
from platformio.test.runners.base import TestRunnerOptions
|
|
|
|
|
from platformio.test.runners.factory import TestRunnerFactory
|
2019-04-19 19:56:16 +03:00
|
|
|
|
|
|
|
|
|
2019-11-06 22:30:58 +02:00
|
|
|
class GDBMIConsoleStream(BytesIO): # pylint: disable=too-few-public-methods
|
2019-04-19 19:56:16 +03:00
|
|
|
STDOUT = sys.stdout
|
|
|
|
|
|
|
|
|
|
def write(self, text):
|
2019-11-06 22:30:58 +02:00
|
|
|
self.STDOUT.write(escape_gdbmi_stream("~", text))
|
2019-04-19 19:56:16 +03:00
|
|
|
self.STDOUT.flush()
|
|
|
|
|
|
|
|
|
|
|
2019-11-06 22:30:58 +02:00
|
|
|
def is_gdbmi_mode():
|
|
|
|
|
return "--interpreter" in " ".join(PlatformioCLI.leftover_args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def escape_gdbmi_stream(prefix, stream):
|
|
|
|
|
bytes_stream = False
|
|
|
|
|
if is_bytes(stream):
|
|
|
|
|
bytes_stream = True
|
|
|
|
|
stream = stream.decode()
|
|
|
|
|
|
|
|
|
|
if not stream:
|
|
|
|
|
return b"" if bytes_stream else ""
|
|
|
|
|
|
|
|
|
|
ends_nl = stream.endswith("\n")
|
|
|
|
|
stream = re.sub(r"\\+", "\\\\\\\\", stream)
|
|
|
|
|
stream = stream.replace('"', '\\"')
|
|
|
|
|
stream = stream.replace("\n", "\\n")
|
|
|
|
|
stream = '%s"%s"' % (prefix, stream)
|
|
|
|
|
if ends_nl:
|
|
|
|
|
stream += "\n"
|
|
|
|
|
|
|
|
|
|
return stream.encode() if bytes_stream else stream
|
2019-04-19 19:56:16 +03:00
|
|
|
|
|
|
|
|
|
2019-05-10 15:45:52 +03:00
|
|
|
def get_default_debug_env(config):
|
|
|
|
|
default_envs = config.default_envs()
|
2019-06-02 14:11:31 +03:00
|
|
|
all_envs = config.envs()
|
|
|
|
|
for env in default_envs:
|
|
|
|
|
if config.get("env:" + env, "build_type") == "debug":
|
|
|
|
|
return env
|
|
|
|
|
for env in all_envs:
|
|
|
|
|
if config.get("env:" + env, "build_type") == "debug":
|
|
|
|
|
return env
|
|
|
|
|
return default_envs[0] if default_envs else all_envs[0]
|
2019-04-19 19:56:16 +03:00
|
|
|
|
|
|
|
|
|
2021-03-19 20:25:30 +02:00
|
|
|
def predebug_project(
|
|
|
|
|
ctx, project_dir, project_config, env_name, preload, verbose
|
|
|
|
|
): # pylint: disable=too-many-arguments
|
|
|
|
|
debug_testname = project_config.get("env:" + env_name, "debug_test")
|
|
|
|
|
if debug_testname:
|
2022-05-10 20:21:49 +03:00
|
|
|
test_names = list_test_names(project_config)
|
2021-03-19 20:25:30 +02:00
|
|
|
if debug_testname not in test_names:
|
|
|
|
|
raise DebugInvalidOptionsError(
|
|
|
|
|
"Unknown test name `%s`. Valid names are `%s`"
|
|
|
|
|
% (debug_testname, ", ".join(test_names))
|
|
|
|
|
)
|
|
|
|
|
print_processing_header(env_name, project_config, verbose)
|
2022-04-21 18:11:49 +03:00
|
|
|
test_runner = TestRunnerFactory.new(
|
|
|
|
|
TestSuite(env_name, debug_testname),
|
|
|
|
|
project_config,
|
|
|
|
|
TestRunnerOptions(
|
2023-04-17 13:15:00 +03:00
|
|
|
verbose=3 if verbose else 0,
|
2021-03-19 20:25:30 +02:00
|
|
|
without_building=False,
|
2022-04-21 18:11:49 +03:00
|
|
|
without_debugging=False,
|
|
|
|
|
without_uploading=not preload,
|
2021-03-19 20:25:30 +02:00
|
|
|
without_testing=True,
|
|
|
|
|
),
|
|
|
|
|
)
|
2022-04-21 18:11:49 +03:00
|
|
|
test_runner.start(ctx)
|
2021-03-19 20:25:30 +02:00
|
|
|
else:
|
|
|
|
|
ctx.invoke(
|
|
|
|
|
cmd_run,
|
|
|
|
|
project_dir=project_dir,
|
2021-04-11 22:21:01 +03:00
|
|
|
project_conf=project_config.path,
|
2021-03-19 20:25:30 +02:00
|
|
|
environment=[env_name],
|
|
|
|
|
target=["__debug"] + (["upload"] if preload else []),
|
|
|
|
|
verbose=verbose,
|
|
|
|
|
)
|
|
|
|
|
|
2019-06-03 19:20:10 +03:00
|
|
|
if preload:
|
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
|
|
|
2019-04-19 19:56:16 +03:00
|
|
|
def has_debug_symbols(prog_path):
|
2022-05-14 18:21:44 +03:00
|
|
|
if not os.path.isfile(prog_path):
|
2019-04-19 19:56:16 +03:00
|
|
|
return False
|
|
|
|
|
matched = {
|
|
|
|
|
b".debug_info": False,
|
|
|
|
|
b".debug_abbrev": False,
|
|
|
|
|
b" -Og": False,
|
|
|
|
|
b" -g": False,
|
2021-03-19 13:10:29 +02:00
|
|
|
# b"__PLATFORMIO_BUILD_DEBUG__": False,
|
2019-04-19 19:56:16 +03:00
|
|
|
}
|
|
|
|
|
with open(prog_path, "rb") as fp:
|
|
|
|
|
last_data = b""
|
|
|
|
|
while True:
|
|
|
|
|
data = fp.read(1024)
|
|
|
|
|
if not data:
|
|
|
|
|
break
|
|
|
|
|
for pattern, found in matched.items():
|
|
|
|
|
if found:
|
|
|
|
|
continue
|
|
|
|
|
if pattern in last_data + data:
|
|
|
|
|
matched[pattern] = True
|
|
|
|
|
last_data = data
|
|
|
|
|
return all(matched.values())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_prog_obsolete(prog_path):
|
|
|
|
|
prog_hash_path = prog_path + ".sha1"
|
2022-05-14 18:21:44 +03:00
|
|
|
if not os.path.isfile(prog_path):
|
2019-04-19 19:56:16 +03:00
|
|
|
return True
|
|
|
|
|
shasum = sha1()
|
|
|
|
|
with open(prog_path, "rb") as fp:
|
|
|
|
|
while True:
|
|
|
|
|
data = fp.read(1024)
|
|
|
|
|
if not data:
|
|
|
|
|
break
|
|
|
|
|
shasum.update(data)
|
|
|
|
|
new_digest = shasum.hexdigest()
|
2020-03-05 23:52:13 +02:00
|
|
|
old_digest = None
|
2022-05-14 18:21:44 +03:00
|
|
|
if os.path.isfile(prog_hash_path):
|
2021-08-28 13:10:07 +03:00
|
|
|
with open(prog_hash_path, encoding="utf8") as fp:
|
2020-03-05 23:52:13 +02:00
|
|
|
old_digest = fp.read()
|
2019-04-19 19:56:16 +03:00
|
|
|
if new_digest == old_digest:
|
|
|
|
|
return False
|
2021-08-28 13:10:07 +03:00
|
|
|
with open(prog_hash_path, mode="w", encoding="utf8") as fp:
|
2020-03-05 23:52:13 +02:00
|
|
|
fp.write(new_digest)
|
2019-04-19 19:56:16 +03:00
|
|
|
return True
|