How to use encode_mi_cli method in avocado

Best Python code snippet using avocado_python

gdb.py

Source:gdb.py Github

copy

Full Screen

...83 """84 if not line.endswith('\n'):85 line = "%s\n" % line86 return gdbmi_parser.session().process(line)87def encode_mi_cli(command):88 """89 Encodes a regular (CLI) command into the proper MI form90 :param command: the regular cli command to send91 :type command: str92 :returns: the encoded (escaped) MI command93 :rtype: str94 """95 return '-interpreter-exec console "%s"' % command96def is_stopped_exit(parsed_mi_msg):97 return (hasattr(parsed_mi_msg, 'class_') and98 (parsed_mi_msg.class_ == 'stopped') and99 hasattr(parsed_mi_msg, 'result') and100 hasattr(parsed_mi_msg.result, 'reason') and101 (parsed_mi_msg.result.reason == "exited"))102def is_thread_group_exit(parsed_mi_msg):103 return (hasattr(parsed_mi_msg, 'class_') and104 (parsed_mi_msg.class_ == 'thread-group-exited'))105def is_exit(parsed_mi_msg):106 return (is_stopped_exit(parsed_mi_msg) or107 is_thread_group_exit(parsed_mi_msg))108def is_break_hit(parsed_mi_msg):109 return (hasattr(parsed_mi_msg, 'class_') and110 (parsed_mi_msg.class_ == 'stopped') and111 hasattr(parsed_mi_msg, 'result') and112 hasattr(parsed_mi_msg.result, 'reason') and113 (parsed_mi_msg.result.reason == "breakpoint-hit"))114def is_sigsegv(parsed_mi_msg):115 return (hasattr(parsed_mi_msg, 'class_') and116 (parsed_mi_msg.class_ == 'stopped') and117 hasattr(parsed_mi_msg, 'result') and118 hasattr(parsed_mi_msg.result, 'signal_name') and119 (parsed_mi_msg.result.reason == "SIGSEGV"))120def is_sigabrt_stopped(parsed_mi_msg):121 return (hasattr(parsed_mi_msg, 'class_') and122 (parsed_mi_msg.class_ == 'stopped') and123 hasattr(parsed_mi_msg, 'record_type') and124 (parsed_mi_msg.record_type == 'result') and125 (parsed_mi_msg.result.reason == 'signal-received') and126 (parsed_mi_msg.result.signal_name == 'SIGABRT'))127def is_sigabrt_console(parsed_mi_msg):128 return (hasattr(parsed_mi_msg, 'record_type') and129 (parsed_mi_msg.record_type == 'stream') and130 hasattr(parsed_mi_msg, 'type') and131 (parsed_mi_msg.type == 'console') and132 hasattr(parsed_mi_msg, 'value') and133 parsed_mi_msg.value == 'SIGABRT, Aborted.\n')134def is_sigabrt(parsed_mi_msg):135 return (is_sigabrt_stopped(parsed_mi_msg) or136 is_sigabrt_console(parsed_mi_msg))137def is_fatal_signal(parsed_mi_msg):138 return is_sigsegv(parsed_mi_msg) or is_sigabrt(parsed_mi_msg)139def format_as_hex(char):140 """141 Formats a single ascii character as a lower case hex string142 :param char: a single ascii character143 :type char: str144 :returns: the character formatted as a lower case hex string145 :rtype: str146 """147 return "%2x" % ord(char)148def string_to_hex(text):149 """150 Formats a string of text into an hex representation151 :param text: a multi character string152 :type text: str153 :returns: the string converted to an hex representation154 :rtype: str155 """156 return "".join(map(format_as_hex, text))157class CommandResult:158 """159 A GDB command, its result, and other possible messages160 """161 def __init__(self, command):162 self.command = command163 self.timestamp = time.monotonic()164 self.stream_messages = []165 self.application_output = []166 self.result = None167 def get_application_output(self):168 """169 Return all application output concatenated as a single string170 :returns: application output concatenated171 :rtype: str172 """173 return "".join(self.application_output)174 def get_stream_messages_text(self):175 """176 Return all stream messages text concatenated as a single string177 :returns: stream messages text concatenated178 :rtype: str179 """180 return "".join([m.value for m in self.stream_messages])181 def __repr__(self):182 return "%s at %.9f" % (self.command, self.timestamp)183class GDB:184 """185 Wraps a GDB subprocess for easier manipulation186 """187 REQUIRED_ARGS = ['--interpreter=mi',188 '--quiet']189 DEFAULT_BREAK = 'main'190 def __init__(self, path=None, *extra_args): # pylint: disable=W1113191 if path is None:192 path = find_command("gdb", default="/usr/bin/gdb")193 self.path = path194 args = [self.path]195 args += self.REQUIRED_ARGS196 args += extra_args197 try:198 self.process = subprocess.Popen(args,199 stdin=subprocess.PIPE,200 stdout=subprocess.PIPE,201 stderr=subprocess.PIPE,202 close_fds=True)203 except OSError as details:204 if details.errno == 2:205 exc = OSError("File '%s' not found" % args[0])206 exc.errno = 2207 raise exc208 else:209 raise210 fcntl.fcntl(self.process.stdout.fileno(),211 fcntl.F_SETFL, os.O_NONBLOCK)212 self.read_until_break()213 # If this instance is connected to another target. If so, what214 # tcp port it's connected to215 self.connected_to = None216 # any GDB MI async messages217 self.async_messages = []218 self.commands_history = []219 # whatever comes from the app that is not a GDB MI message220 self.output_messages = []221 self.output_messages_queue = []222 def read_gdb_response(self, timeout=0.01, max_tries=100):223 """224 Read raw responses from GDB225 :param timeout: the amount of time to way between read attempts226 :type timeout: float227 :param max_tries: the maximum number of cycles to try to read until228 a response is obtained229 :type max_tries: int230 :returns: a string containing a raw response from GDB231 :rtype: str232 """233 current_try = 0234 while current_try < max_tries:235 try:236 line = self.process.stdout.readline()237 line = line.strip()238 if line:239 return line240 except IOError:241 current_try += 1242 if current_try >= max_tries:243 raise ValueError("Could not read GDB response")244 else:245 time.sleep(timeout)246 def read_until_break(self, max_lines=100):247 """248 Read lines from GDB until a break condition is reached249 :param max_lines: the maximum number of lines to read250 :type max_lines: int251 :returns: a list of messages read252 :rtype: list of str253 """254 result = []255 while True:256 line = self.read_gdb_response()257 if line in GDB_BREAK_CONDITIONS:258 break259 if len(result) >= max_lines:260 break261 result.append(line)262 return result263 def send_gdb_command(self, command):264 """265 Send a raw command to the GNU debugger input266 :param command: the GDB command, hopefully in MI language267 :type command: str268 :returns: None269 """270 if not command.endswith('\n'):271 command = "%s\n" % command272 self.process.stdin.write(command.encode())273 self.process.stdin.flush()274 def cmd(self, command):275 """276 Sends a command and parses all lines until prompt is received277 :param command: the GDB command, hopefully in MI language278 :type command: str279 :returns: a :class:`CommandResult` instance280 :rtype: :class:`CommandResult`281 """282 cmd = CommandResult(command)283 self.send_gdb_command(command)284 responses = self.read_until_break()285 result_response_received = False286 for line in responses:287 # If the line can not be properly parsed, it is *most likely*288 # generated by the application being run inside the debugger289 try:290 parsed_response = parse_mi(line.decode())291 except Exception: # pylint: disable=W0703292 cmd.application_output.append(line)293 continue294 if (parsed_response.type == 'console' and295 parsed_response.record_type == 'stream'):296 cmd.stream_messages.append(parsed_response)297 elif parsed_response.type == 'result':298 if result_response_received:299 # raise an exception here, because no two result300 # responses should come from a single command AFAIK301 raise Exception("Many result responses to a single cmd")302 result_response_received = True303 cmd.result = parsed_response304 else:305 self.async_messages.append(parsed_response)306 return cmd307 def cli_cmd(self, command):308 """309 Sends a cli command encoded as an MI command310 :param command: a regular GDB cli command311 :type command: str312 :returns: a :class:`CommandResult` instance313 :rtype: :class:`CommandResult`314 """315 cmd = encode_mi_cli(command)316 return self.cmd(cmd)317 def cmd_exists(self, command):318 """319 Checks if a given command exists320 :param command: a GDB MI command, including the dash (-) prefix321 :type command: str322 :returns: either True or False323 :rtype: bool324 """325 gdb_info_command = "-info-gdb-mi-command %s" % command[1:]326 r = self.cmd(gdb_info_command)327 return r.result.result.command.exists == 'true'328 def set_file(self, path):329 """...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful