| 
									
										
										
										
											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-statements | 
					
						
							|  |  |  | # pylint: disable=too-many-locals, too-many-branches | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							| 
									
										
										
										
											2019-07-04 17:47:26 +03:00
										 |  |  | import signal | 
					
						
							| 
									
										
										
										
											2019-09-27 14:13:53 +03:00
										 |  |  | from os.path import isfile | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | import click | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-27 14:13:53 +03:00
										 |  |  | from platformio import app, exception, fs, proc, util | 
					
						
							| 
									
										
										
										
											2019-10-23 16:05:27 +03:00
										 |  |  | from platformio.commands.debug import helpers | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  | from platformio.managers.core import inject_contrib_pysite | 
					
						
							| 
									
										
										
										
											2019-05-10 15:45:52 +03:00
										 |  |  | from platformio.project.config import ProjectConfig | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  | from platformio.project.helpers import is_platformio_project, load_project_ide_data | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @click.command( | 
					
						
							|  |  |  |     "debug", | 
					
						
							|  |  |  |     context_settings=dict(ignore_unknown_options=True), | 
					
						
							|  |  |  |     short_help="PIO 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 | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  | def cli(ctx, project_dir, project_conf, environment, verbose, interface, __unprocessed): | 
					
						
							| 
									
										
										
										
											2019-09-27 14:13:53 +03:00
										 |  |  |     app.set_session_var("custom_project_conf", project_conf) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-27 14:57:05 +03:00
										 |  |  |     # use env variables from Eclipse or CLion | 
					
						
							|  |  |  |     for sysenv in ("CWD", "PWD", "PLATFORMIO_PROJECT_DIR"): | 
					
						
							|  |  |  |         if is_platformio_project(project_dir): | 
					
						
							|  |  |  |             break | 
					
						
							|  |  |  |         if os.getenv(sysenv): | 
					
						
							|  |  |  |             project_dir = os.getenv(sysenv) | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 19:44:37 +03:00
										 |  |  |     with fs.cd(project_dir): | 
					
						
							| 
									
										
										
										
											2019-09-27 14:13:53 +03:00
										 |  |  |         config = ProjectConfig.get_instance(project_conf) | 
					
						
							| 
									
										
										
										
											2019-05-10 15:45:52 +03:00
										 |  |  |         config.validate(envs=[environment] if environment else None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         env_name = environment or helpers.get_default_debug_env(config) | 
					
						
							|  |  |  |         env_options = config.items(env=env_name, as_dict=True) | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  |         if not set(env_options.keys()) >= set(["platform", "board"]): | 
					
						
							|  |  |  |             raise exception.ProjectEnvsNotAvailable() | 
					
						
							|  |  |  |         debug_options = helpers.validate_debug_options(ctx, env_options) | 
					
						
							|  |  |  |         assert debug_options | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if not interface: | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         return helpers.predebug_project(ctx, project_dir, env_name, False, verbose) | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-03 19:20:10 +03:00
										 |  |  |     configuration = load_project_ide_data(project_dir, env_name) | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  |     if not configuration: | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         raise exception.DebugInvalidOptions("Could not load debug configuration") | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if "--version" in __unprocessed: | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         result = proc.exec_command([configuration["gdb_path"], "--version"]) | 
					
						
							|  |  |  |         if result["returncode"] == 0: | 
					
						
							|  |  |  |             return click.echo(result["out"]) | 
					
						
							|  |  |  |         raise exception.PlatformioException("\n".join([result["out"], result["err"]])) | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 20:30:06 +03:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2019-08-12 19:44:37 +03:00
										 |  |  |         fs.ensure_udev_rules() | 
					
						
							| 
									
										
										
										
											2019-06-11 20:30:06 +03:00
										 |  |  |     except exception.InvalidUdevRules as e: | 
					
						
							| 
									
										
										
										
											2019-11-06 22:30:58 +02:00
										 |  |  |         click.echo( | 
					
						
							|  |  |  |             helpers.escape_gdbmi_stream("~", str(e) + "\n") | 
					
						
							|  |  |  |             if helpers.is_gdbmi_mode() | 
					
						
							|  |  |  |             else str(e) + "\n", | 
					
						
							|  |  |  |             nl=False, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2019-06-11 20:30:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |     debug_options["load_cmds"] = helpers.configure_esp32_load_cmds( | 
					
						
							|  |  |  |         debug_options, configuration | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     rebuild_prog = False | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |     preload = debug_options["load_cmds"] == ["preload"] | 
					
						
							|  |  |  |     load_mode = debug_options["load_mode"] | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  |     if load_mode == "always": | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         rebuild_prog = preload or not helpers.has_debug_symbols( | 
					
						
							|  |  |  |             configuration["prog_path"] | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  |     elif load_mode == "modified": | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         rebuild_prog = helpers.is_prog_obsolete( | 
					
						
							|  |  |  |             configuration["prog_path"] | 
					
						
							|  |  |  |         ) or not helpers.has_debug_symbols(configuration["prog_path"]) | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         rebuild_prog = not isfile(configuration["prog_path"]) | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         debug_options["load_cmds"] = [] | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if rebuild_prog: | 
					
						
							| 
									
										
										
										
											2019-11-06 22:30:58 +02:00
										 |  |  |         if helpers.is_gdbmi_mode(): | 
					
						
							|  |  |  |             click.echo( | 
					
						
							|  |  |  |                 helpers.escape_gdbmi_stream( | 
					
						
							|  |  |  |                     "~", "Preparing firmware for debugging...\n" | 
					
						
							|  |  |  |                 ), | 
					
						
							|  |  |  |                 nl=False, | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             stream = helpers.GDBMIConsoleStream() | 
					
						
							|  |  |  |             with util.capture_std_streams(stream): | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |                 helpers.predebug_project(ctx, project_dir, env_name, preload, verbose) | 
					
						
							| 
									
										
										
										
											2019-11-06 22:30:58 +02:00
										 |  |  |             stream.close() | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  |         else: | 
					
						
							|  |  |  |             click.echo("Preparing firmware for debugging...") | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |             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": | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |             helpers.is_prog_obsolete(configuration["prog_path"]) | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |     if not isfile(configuration["prog_path"]): | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  |         raise exception.DebugInvalidOptions("Program/firmware is missed") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # run debugging client | 
					
						
							|  |  |  |     inject_contrib_pysite() | 
					
						
							| 
									
										
										
										
											2019-10-18 15:41:52 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # pylint: disable=import-outside-toplevel | 
					
						
							| 
									
										
										
										
											2019-10-23 16:05:27 +03:00
										 |  |  |     from platformio.commands.debug.client import GDBClient, reactor | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     client = GDBClient(project_dir, __unprocessed, debug_options, env_options) | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |     client.spawn(configuration["gdb_path"], configuration["prog_path"]) | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 17:47:26 +03:00
										 |  |  |     signal.signal(signal.SIGINT, lambda *args, **kwargs: None) | 
					
						
							| 
									
										
										
										
											2019-04-19 19:56:16 +03:00
										 |  |  |     reactor.run() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return True |