remote_gdb.cc (12019:65939e37768a) | remote_gdb.cc (12031:46116545e745) |
---|---|
1/* 2 * Copyright 2015 LabWare 3 * Copyright 2014 Google, Inc. 4 * Copyright (c) 2002-2005 The Regents of The University of Michigan 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are --- 133 unchanged lines hidden (view full) --- 142#include "mem/port.hh" 143#include "mem/se_translating_port_proxy.hh" 144#include "sim/full_system.hh" 145#include "sim/system.hh" 146 147using namespace std; 148using namespace TheISA; 149 | 1/* 2 * Copyright 2015 LabWare 3 * Copyright 2014 Google, Inc. 4 * Copyright (c) 2002-2005 The Regents of The University of Michigan 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are --- 133 unchanged lines hidden (view full) --- 142#include "mem/port.hh" 143#include "mem/se_translating_port_proxy.hh" 144#include "sim/full_system.hh" 145#include "sim/system.hh" 146 147using namespace std; 148using namespace TheISA; 149 |
150static const char GDBStart = '$'; 151static const char GDBEnd = '#'; 152static const char GDBGoodP = '+'; 153static const char GDBBadP = '-'; 154 155static const int GDBPacketBufLen = 1024; 156 |
|
150#ifndef NDEBUG 151vector<BaseRemoteGDB *> debuggers; 152 153void 154debugger() 155{ 156 static int current_debugger = -1; 157 if (current_debugger >= 0 && current_debugger < (int)debuggers.size()) { --- 25 unchanged lines hidden (view full) --- 183 : inputEvent(NULL), gdb(g), port(p) 184{ 185 assert(!gdb->listener); 186 gdb->listener = this; 187} 188 189GDBListener::~GDBListener() 190{ | 157#ifndef NDEBUG 158vector<BaseRemoteGDB *> debuggers; 159 160void 161debugger() 162{ 163 static int current_debugger = -1; 164 if (current_debugger >= 0 && current_debugger < (int)debuggers.size()) { --- 25 unchanged lines hidden (view full) --- 190 : inputEvent(NULL), gdb(g), port(p) 191{ 192 assert(!gdb->listener); 193 gdb->listener = this; 194} 195 196GDBListener::~GDBListener() 197{ |
191 if (inputEvent) 192 delete inputEvent; | 198 delete inputEvent; |
193} 194 195string 196GDBListener::name() 197{ 198 return gdb->name() + ".listener"; 199} 200 --- 113 unchanged lines hidden (view full) --- 314 attached = true; 315 DPRINTFN("remote gdb attached\n"); 316} 317 318void 319BaseRemoteGDB::detach() 320{ 321 attached = false; | 199} 200 201string 202GDBListener::name() 203{ 204 return gdb->name() + ".listener"; 205} 206 --- 113 unchanged lines hidden (view full) --- 320 attached = true; 321 DPRINTFN("remote gdb attached\n"); 322} 323 324void 325BaseRemoteGDB::detach() 326{ 327 attached = false; |
328 active = false; 329 clearSingleStep(); |
|
322 close(fd); 323 fd = -1; 324 325 pollQueue.remove(inputEvent); 326 DPRINTFN("remote gdb detached\n"); 327} 328 | 330 close(fd); 331 fd = -1; 332 333 pollQueue.remove(inputEvent); 334 DPRINTFN("remote gdb detached\n"); 335} 336 |
329const char * 330BaseRemoteGDB::gdb_command(char cmd) 331{ 332 switch (cmd) { 333 case GDBSignal: return "KGDB_SIGNAL"; 334 case GDBSetBaud: return "KGDB_SET_BAUD"; 335 case GDBSetBreak: return "KGDB_SET_BREAK"; 336 case GDBCont: return "KGDB_CONT"; 337 case GDBAsyncCont: return "KGDB_ASYNC_CONT"; 338 case GDBDebug: return "KGDB_DEBUG"; 339 case GDBDetach: return "KGDB_DETACH"; 340 case GDBRegR: return "KGDB_REG_R"; 341 case GDBRegW: return "KGDB_REG_W"; 342 case GDBSetThread: return "KGDB_SET_THREAD"; 343 case GDBCycleStep: return "KGDB_CYCLE_STEP"; 344 case GDBSigCycleStep: return "KGDB_SIG_CYCLE_STEP"; 345 case GDBKill: return "KGDB_KILL"; 346 case GDBMemW: return "KGDB_MEM_W"; 347 case GDBMemR: return "KGDB_MEM_R"; 348 case GDBSetReg: return "KGDB_SET_REG"; 349 case GDBReadReg: return "KGDB_READ_REG"; 350 case GDBQueryVar: return "KGDB_QUERY_VAR"; 351 case GDBSetVar: return "KGDB_SET_VAR"; 352 case GDBReset: return "KGDB_RESET"; 353 case GDBStep: return "KGDB_STEP"; 354 case GDBAsyncStep: return "KGDB_ASYNC_STEP"; 355 case GDBThreadAlive: return "KGDB_THREAD_ALIVE"; 356 case GDBTargetExit: return "KGDB_TARGET_EXIT"; 357 case GDBBinaryDload: return "KGDB_BINARY_DLOAD"; 358 case GDBClrHwBkpt: return "KGDB_CLR_HW_BKPT"; 359 case GDBSetHwBkpt: return "KGDB_SET_HW_BKPT"; 360 case GDBStart: return "KGDB_START"; 361 case GDBEnd: return "KGDB_END"; 362 case GDBGoodP: return "KGDB_GOODP"; 363 case GDBBadP: return "KGDB_BADP"; 364 default: return "KGDB_UNKNOWN"; 365 } 366} 367 | |
368///////////////////////// 369// 370// 371 | 337///////////////////////// 338// 339// 340 |
372bool 373BaseRemoteGDB::getbyte(uint8_t &b) | 341uint8_t 342BaseRemoteGDB::getbyte() |
374{ | 343{ |
375 if (::read(fd, &b, sizeof(b)) == sizeof(b)) { 376 return true; 377 } else { 378 warn("Couldn't read data from debugger, detaching."); 379 detach(); 380 return false; 381 } | 344 uint8_t b; 345 if (::read(fd, &b, sizeof(b)) == sizeof(b)) 346 return b; 347 348 throw BadClient("Couldn't read data from debugger."); |
382} 383 | 349} 350 |
384bool | 351void |
385BaseRemoteGDB::putbyte(uint8_t b) 386{ | 352BaseRemoteGDB::putbyte(uint8_t b) 353{ |
387 if (::write(fd, &b, sizeof(b)) == sizeof(b)) { 388 return true; 389 } else { 390 warn("Couldn't write data to the debugger, detaching."); 391 detach(); 392 return false; 393 } | 354 if (::write(fd, &b, sizeof(b)) == sizeof(b)) 355 return; 356 357 throw BadClient("Couldn't write data to the debugger."); |
394} 395 396// Send a packet to gdb | 358} 359 360// Send a packet to gdb |
397ssize_t | 361void |
398BaseRemoteGDB::send(const char *bp) 399{ 400 const char *p; 401 uint8_t csum, c; 402 403 DPRINTF(GDBSend, "send: %s\n", bp); 404 405 do { 406 p = bp; | 362BaseRemoteGDB::send(const char *bp) 363{ 364 const char *p; 365 uint8_t csum, c; 366 367 DPRINTF(GDBSend, "send: %s\n", bp); 368 369 do { 370 p = bp; |
407 //Start sending a packet 408 if (!putbyte(GDBStart)) 409 return -1; 410 //Send the contents, and also keep a check sum. | 371 // Start sending a packet 372 putbyte(GDBStart); 373 // Send the contents, and also keep a check sum. |
411 for (csum = 0; (c = *p); p++) { | 374 for (csum = 0; (c = *p); p++) { |
412 if (!putbyte(c)) 413 return -1; | 375 putbyte(c); |
414 csum += c; 415 } | 376 csum += c; 377 } |
416 if (//Send the ending character. 417 !putbyte(GDBEnd) || 418 //Sent the checksum. 419 !putbyte(i2digit(csum >> 4)) || 420 !putbyte(i2digit(csum))) { 421 return -1; 422 } 423 //Try transmitting over and over again until the other end doesn't 424 //send an error back. 425 if (!getbyte(c)) 426 return -1; | 378 // Send the ending character. 379 putbyte(GDBEnd); 380 // Send the checksum. 381 putbyte(i2digit(csum >> 4)); 382 putbyte(i2digit(csum)); 383 // Try transmitting over and over again until the other end doesn't 384 // send an error back. 385 c = getbyte(); |
427 } while ((c & 0x7f) == GDBBadP); | 386 } while ((c & 0x7f) == GDBBadP); |
428 return 0; | |
429} 430 431// Receive a packet from gdb 432int 433BaseRemoteGDB::recv(char *bp, int maxlen) 434{ 435 char *p; 436 uint8_t c; 437 int csum; 438 int len; 439 440 do { 441 p = bp; 442 csum = len = 0; | 387} 388 389// Receive a packet from gdb 390int 391BaseRemoteGDB::recv(char *bp, int maxlen) 392{ 393 char *p; 394 uint8_t c; 395 int csum; 396 int len; 397 398 do { 399 p = bp; 400 csum = len = 0; |
443 //Find the beginning of a packet 444 do { 445 if (!getbyte(c)) 446 return -1; 447 } while (c != GDBStart); | 401 // Find the beginning of a packet 402 while ((c = getbyte()) != GDBStart); |
448 | 403 |
449 //Read until you find the end of the data in the packet, and keep 450 //track of the check sum. | 404 // Read until you find the end of the data in the packet, and keep 405 // track of the check sum. |
451 while (len < maxlen) { | 406 while (len < maxlen) { |
452 if (!getbyte(c)) 453 return -1; | 407 c = getbyte(); |
454 if (c == GDBEnd) 455 break; 456 c &= 0x7f; 457 csum += c; 458 *p++ = c; 459 len++; 460 } 461 | 408 if (c == GDBEnd) 409 break; 410 c &= 0x7f; 411 csum += c; 412 *p++ = c; 413 len++; 414 } 415 |
462 //Mask the check sum, and terminate the command string. | 416 // Mask the check sum, and terminate the command string. |
463 csum &= 0xff; 464 *p = '\0'; 465 | 417 csum &= 0xff; 418 *p = '\0'; 419 |
466 //If the command was too long, report an error. | 420 // If the command was too long, report an error. |
467 if (len >= maxlen) { | 421 if (len >= maxlen) { |
468 if (!putbyte(GDBBadP)) 469 return -1; | 422 putbyte(GDBBadP); |
470 continue; 471 } 472 | 423 continue; 424 } 425 |
473 //Bring in the checksum. If the check sum matches, csum will be 0. 474 uint8_t csum1, csum2; 475 if (!getbyte(csum1) || !getbyte(csum2)) 476 return -1; 477 csum -= digit2i(csum1) * 16; 478 csum -= digit2i(csum2); | 426 // Bring in the checksum. If the check sum matches, csum will be 0. 427 csum -= digit2i(getbyte()) * 16; 428 csum -= digit2i(getbyte()); |
479 | 429 |
480 //If the check sum was correct | 430 // If the check sum was correct |
481 if (csum == 0) { | 431 if (csum == 0) { |
482 //Report that the packet was received correctly 483 if (!putbyte(GDBGoodP)) 484 return -1; | 432 // Report that the packet was received correctly 433 putbyte(GDBGoodP); |
485 // Sequence present? 486 if (bp[2] == ':') { | 434 // Sequence present? 435 if (bp[2] == ':') { |
487 if (!putbyte(bp[0]) || !putbyte(bp[1])) 488 return -1; | 436 putbyte(bp[0]); 437 putbyte(bp[1]); |
489 len -= 3; 490 memcpy(bp, bp+3, len); 491 } 492 break; 493 } | 438 len -= 3; 439 memcpy(bp, bp+3, len); 440 } 441 break; 442 } |
494 //Otherwise, report that there was a mistake. 495 if (!putbyte(GDBBadP)) 496 return -1; | 443 // Otherwise, report that there was a mistake. 444 putbyte(GDBBadP); |
497 } while (1); 498 | 445 } while (1); 446 |
499 DPRINTF(GDBRecv, "recv: %s: %s\n", gdb_command(*bp), bp); | 447 DPRINTF(GDBRecv, "recv: %s\n", bp); |
500 | 448 |
501 return (len); | 449 return len; |
502} 503 504// Read bytes from kernel address space for debugger. 505bool 506BaseRemoteGDB::read(Addr vaddr, size_t size, char *data) 507{ 508 static Addr lastaddr = 0; 509 static size_t lastsize = 0; --- 117 unchanged lines hidden (view full) --- 627BaseRemoteGDB::HardBreakpoint::process(ThreadContext *tc) 628{ 629 DPRINTF(GDBMisc, "handling hardware breakpoint at %#x\n", pc()); 630 631 if (tc == gdb->context) 632 gdb->trap(SIGTRAP); 633} 634 | 450} 451 452// Read bytes from kernel address space for debugger. 453bool 454BaseRemoteGDB::read(Addr vaddr, size_t size, char *data) 455{ 456 static Addr lastaddr = 0; 457 static size_t lastsize = 0; --- 117 unchanged lines hidden (view full) --- 575BaseRemoteGDB::HardBreakpoint::process(ThreadContext *tc) 576{ 577 DPRINTF(GDBMisc, "handling hardware breakpoint at %#x\n", pc()); 578 579 if (tc == gdb->context) 580 gdb->trap(SIGTRAP); 581} 582 |
635bool | 583void |
636BaseRemoteGDB::insertSoftBreak(Addr addr, size_t len) 637{ 638 if (!checkBpLen(len)) | 584BaseRemoteGDB::insertSoftBreak(Addr addr, size_t len) 585{ 586 if (!checkBpLen(len)) |
639 panic("invalid length\n"); | 587 throw BadClient("Invalid breakpoint length\n"); |
640 641 return insertHardBreak(addr, len); 642} 643 | 588 589 return insertHardBreak(addr, len); 590} 591 |
644bool | 592void |
645BaseRemoteGDB::removeSoftBreak(Addr addr, size_t len) 646{ 647 if (!checkBpLen(len)) | 593BaseRemoteGDB::removeSoftBreak(Addr addr, size_t len) 594{ 595 if (!checkBpLen(len)) |
648 panic("invalid length\n"); | 596 throw BadClient("Invalid breakpoint length.\n"); |
649 650 return removeHardBreak(addr, len); 651} 652 | 597 598 return removeHardBreak(addr, len); 599} 600 |
653bool | 601void |
654BaseRemoteGDB::insertHardBreak(Addr addr, size_t len) 655{ 656 if (!checkBpLen(len)) | 602BaseRemoteGDB::insertHardBreak(Addr addr, size_t len) 603{ 604 if (!checkBpLen(len)) |
657 panic("invalid length\n"); | 605 throw BadClient("Invalid breakpoint length\n"); |
658 | 606 |
659 DPRINTF(GDBMisc, "inserting hardware breakpoint at %#x\n", addr); | 607 DPRINTF(GDBMisc, "Inserting hardware breakpoint at %#x\n", addr); |
660 661 HardBreakpoint *&bkpt = hardBreakMap[addr]; 662 if (bkpt == 0) 663 bkpt = new HardBreakpoint(this, addr); 664 665 bkpt->refcount++; | 608 609 HardBreakpoint *&bkpt = hardBreakMap[addr]; 610 if (bkpt == 0) 611 bkpt = new HardBreakpoint(this, addr); 612 613 bkpt->refcount++; |
666 667 return true; | |
668} 669 | 614} 615 |
670bool | 616void |
671BaseRemoteGDB::removeHardBreak(Addr addr, size_t len) 672{ 673 if (!checkBpLen(len)) | 617BaseRemoteGDB::removeHardBreak(Addr addr, size_t len) 618{ 619 if (!checkBpLen(len)) |
674 panic("invalid length\n"); | 620 throw BadClient("Invalid breakpoint length\n"); |
675 | 621 |
676 DPRINTF(GDBMisc, "removing hardware breakpoint at %#x\n", addr); | 622 DPRINTF(GDBMisc, "Removing hardware breakpoint at %#x\n", addr); |
677 678 break_iter_t i = hardBreakMap.find(addr); 679 if (i == hardBreakMap.end()) | 623 624 break_iter_t i = hardBreakMap.find(addr); 625 if (i == hardBreakMap.end()) |
680 return false; | 626 throw CmdError("E0C"); |
681 682 HardBreakpoint *hbp = (*i).second; 683 if (--hbp->refcount == 0) { 684 delete hbp; 685 hardBreakMap.erase(i); 686 } | 627 628 HardBreakpoint *hbp = (*i).second; 629 if (--hbp->refcount == 0) { 630 delete hbp; 631 hardBreakMap.erase(i); 632 } |
687 688 return true; | |
689} 690 691void 692BaseRemoteGDB::setTempBreakpoint(Addr bkpt) 693{ 694 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", bkpt); 695 insertHardBreak(bkpt, sizeof(TheISA::MachInst)); 696} 697 698void 699BaseRemoteGDB::clearTempBreakpoint(Addr &bkpt) 700{ 701 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", bkpt); 702 removeHardBreak(bkpt, sizeof(TheISA::MachInst)); 703 bkpt = 0; 704} 705 | 633} 634 635void 636BaseRemoteGDB::setTempBreakpoint(Addr bkpt) 637{ 638 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", bkpt); 639 insertHardBreak(bkpt, sizeof(TheISA::MachInst)); 640} 641 642void 643BaseRemoteGDB::clearTempBreakpoint(Addr &bkpt) 644{ 645 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", bkpt); 646 removeHardBreak(bkpt, sizeof(TheISA::MachInst)); 647 bkpt = 0; 648} 649 |
650enum GdbBreakpointType { 651 GdbSoftBp = '0', 652 GdbHardBp = '1', 653 GdbWriteWp = '2', 654 GdbReadWp = '3', 655 GdbAccWp = '4', 656}; 657 |
|
706const char * 707BaseRemoteGDB::break_type(char c) 708{ 709 switch(c) { | 658const char * 659BaseRemoteGDB::break_type(char c) 660{ 661 switch(c) { |
710 case '0': return "software breakpoint"; 711 case '1': return "hardware breakpoint"; 712 case '2': return "write watchpoint"; 713 case '3': return "read watchpoint"; 714 case '4': return "access watchpoint"; | 662 case GdbSoftBp: return "software breakpoint"; 663 case GdbHardBp: return "hardware breakpoint"; 664 case GdbWriteWp: return "write watchpoint"; 665 case GdbReadWp: return "read watchpoint"; 666 case GdbAccWp: return "access watchpoint"; |
715 default: return "unknown breakpoint/watchpoint"; 716 } 717} 718 | 667 default: return "unknown breakpoint/watchpoint"; 668 } 669} 670 |
719// This function does all command processing for interfacing to a 720// remote gdb. Note that the error codes are ignored by gdb at 721// present, but might eventually become meaningful. (XXX) It might 722// makes sense to use POSIX errno values, because that is what the 723// gdb/remote.c functions want to return. | 671std::map<char, GdbCommand> BaseRemoteGDB::command_map = { 672 // last signal 673 { '?', { "KGDB_SIGNAL", &BaseRemoteGDB::cmd_signal } }, 674 // set baud (deprecated) 675 { 'b', { "KGDB_SET_BAUD", &BaseRemoteGDB::cmd_unsupported } }, 676 // set breakpoint (deprecated) 677 { 'B', { "KGDB_SET_BREAK", &BaseRemoteGDB::cmd_unsupported } }, 678 // resume 679 { 'c', { "KGDB_CONT", &BaseRemoteGDB::cmd_cont } }, 680 // continue with signal 681 { 'C', { "KGDB_ASYNC_CONT", &BaseRemoteGDB::cmd_async_cont } }, 682 // toggle debug flags (deprecated) 683 { 'd', { "KGDB_DEBUG", &BaseRemoteGDB::cmd_unsupported } }, 684 // detach remote gdb 685 { 'D', { "KGDB_DETACH", &BaseRemoteGDB::cmd_detach } }, 686 // read general registers 687 { 'g', { "KGDB_REG_R", &BaseRemoteGDB::cmd_reg_r } }, 688 // write general registers 689 { 'G', { "KGDB_REG_W", &BaseRemoteGDB::cmd_reg_w } }, 690 // set thread 691 { 'H', { "KGDB_SET_THREAD", &BaseRemoteGDB::cmd_set_thread } }, 692 // step a single cycle 693 { 'i', { "KGDB_CYCLE_STEP", &BaseRemoteGDB::cmd_unsupported } }, 694 // signal then cycle step 695 { 'I', { "KGDB_SIG_CYCLE_STEP", &BaseRemoteGDB::cmd_unsupported } }, 696 // kill program 697 { 'k', { "KGDB_KILL", &BaseRemoteGDB::cmd_detach } }, 698 // read memory 699 { 'm', { "KGDB_MEM_R", &BaseRemoteGDB::cmd_mem_r } }, 700 // write memory 701 { 'M', { "KGDB_MEM_W", &BaseRemoteGDB::cmd_mem_w } }, 702 // read register 703 { 'p', { "KGDB_READ_REG", &BaseRemoteGDB::cmd_unsupported } }, 704 // write register 705 { 'P', { "KGDB_SET_REG", &BaseRemoteGDB::cmd_unsupported } }, 706 // query variable 707 { 'q', { "KGDB_QUERY_VAR", &BaseRemoteGDB::cmd_query_var } }, 708 // set variable 709 { 'Q', { "KGDB_SET_VAR", &BaseRemoteGDB::cmd_unsupported } }, 710 // reset system (deprecated) 711 { 'r', { "KGDB_RESET", &BaseRemoteGDB::cmd_unsupported } }, 712 // step 713 { 's', { "KGDB_STEP", &BaseRemoteGDB::cmd_step } }, 714 // signal and step 715 { 'S', { "KGDB_ASYNC_STEP", &BaseRemoteGDB::cmd_async_step } }, 716 // find out if the thread is alive 717 { 'T', { "KGDB_THREAD_ALIVE", &BaseRemoteGDB::cmd_unsupported } }, 718 // target exited 719 { 'W', { "KGDB_TARGET_EXIT", &BaseRemoteGDB::cmd_unsupported } }, 720 // write memory 721 { 'X', { "KGDB_BINARY_DLOAD", &BaseRemoteGDB::cmd_unsupported } }, 722 // remove breakpoint or watchpoint 723 { 'z', { "KGDB_CLR_HW_BKPT", &BaseRemoteGDB::cmd_clr_hw_bkpt } }, 724 // insert breakpoint or watchpoint 725 { 'Z', { "KGDB_SET_HW_BKPT", &BaseRemoteGDB::cmd_set_hw_bkpt } }, 726}; 727 728 |
724bool | 729bool |
725BaseRemoteGDB::trap(int type) | 730BaseRemoteGDB::cmd_unsupported(GdbCommand::Context &ctx) |
726{ | 731{ |
727 uint64_t val; 728 size_t datalen, len; 729 char data[GDBPacketBufLen + 1]; 730 size_t bufferSize; 731 const char *p; 732 char command, subcmd; 733 string var; 734 bool ret; | 732 DPRINTF(GDBMisc, "Unsupported command: %s\n", ctx.cmd->name); 733 DDUMP(GDBMisc, ctx.data, ctx.len); 734 throw Unsupported(); 735} |
735 | 736 |
736 if (!attached) 737 return false; | |
738 | 737 |
739 unique_ptr<BaseRemoteGDB::BaseGdbRegCache> regCache(gdbRegs()); | 738bool 739BaseRemoteGDB::cmd_signal(GdbCommand::Context &ctx) 740{ 741 send(csprintf("S%02x", ctx.type).c_str()); 742 return true; 743} |
740 | 744 |
741 bufferSize = regCache->size() * 2 + 256; 742 unique_ptr<char[]> buffer_mem(new char[bufferSize]); 743 char *buffer = buffer_mem.get(); 744 745 DPRINTF(GDBMisc, "trap: PC=%s\n", context->pcState()); 746 | 745bool 746BaseRemoteGDB::cmd_cont(GdbCommand::Context &ctx) 747{ 748 const char *p = ctx.data; 749 if (ctx.len) { 750 Addr newPc = hex2i(&p); 751 context->pcState(newPc); 752 } |
747 clearSingleStep(); | 753 clearSingleStep(); |
754 return false; 755} |
|
748 | 756 |
749 /* 750 * The first entry to this function is normally through 751 * a breakpoint trap in kgdb_connect(), in which case we 752 * must advance past the breakpoint because gdb will not. 753 * 754 * On the first entry here, we expect that gdb is not yet 755 * listening to us, so just enter the interaction loop. 756 * After the debugger is "active" (connected) it will be 757 * waiting for a "signaled" message from us. 758 */ 759 if (!active) { 760 active = true; 761 } else { 762 // Tell remote host that an exception has occurred. 763 snprintf(buffer, bufferSize, "S%02x", type); 764 if (send(buffer) < 0) 765 return true; | 757bool 758BaseRemoteGDB::cmd_async_cont(GdbCommand::Context &ctx) 759{ 760 const char *p = ctx.data; 761 hex2i(&p); 762 if (*p++ == ';') { 763 Addr newPc = hex2i(&p); 764 context->pcState(newPc); |
766 } | 765 } |
766 clearSingleStep(); 767 return false; 768} |
|
767 | 769 |
768 // Stick frame regs into our reg cache. 769 regCache->getRegs(context); | 770bool 771BaseRemoteGDB::cmd_detach(GdbCommand::Context &ctx) 772{ 773 detach(); 774 return false; 775} |
770 | 776 |
771 for (;;) { 772 int recved = recv(data, sizeof(data)); 773 if (recved < 0) 774 return true; 775 datalen = recved; 776 data[sizeof(data) - 1] = 0; // Sentinel 777 command = data[0]; 778 subcmd = 0; 779 p = data + 1; 780 switch (command) { | 777bool 778BaseRemoteGDB::cmd_reg_r(GdbCommand::Context &ctx) 779{ 780 char buf[2 * regCachePtr->size() + 1]; 781 buf[2 * regCachePtr->size()] = '\0'; 782 mem2hex(buf, regCachePtr->data(), regCachePtr->size()); 783 send(buf); 784 return true; 785} |
781 | 786 |
782 case GDBSignal: 783 // if this command came from a running gdb, answer it -- 784 // the other guy has no way of knowing if we're in or out 785 // of this loop when he issues a "remote-signal". 786 snprintf(buffer, bufferSize, 787 "S%02x", type); 788 if (send(buffer) < 0) 789 return true; 790 continue; | 787bool 788BaseRemoteGDB::cmd_reg_w(GdbCommand::Context &ctx) 789{ 790 const char *p = ctx.data; 791 p = hex2mem(regCachePtr->data(), p, regCachePtr->size()); 792 if (p == NULL || *p != '\0') 793 throw CmdError("E01"); |
791 | 794 |
792 case GDBRegR: 793 if (2 * regCache->size() > bufferSize) 794 panic("buffer too small"); | 795 regCachePtr->setRegs(context); 796 send("OK"); |
795 | 797 |
796 mem2hex(buffer, regCache->data(), regCache->size()); 797 if (send(buffer) < 0) 798 return true; 799 continue; | 798 return true; 799} |
800 | 800 |
801 case GDBRegW: 802 p = hex2mem(regCache->data(), p, regCache->size()); 803 if (p == NULL || *p != '\0') { 804 if (send("E01") < 0) 805 return true; 806 } else { 807 regCache->setRegs(context); 808 if (send("OK") < 0) 809 return true; 810 } 811 continue; | 801bool 802BaseRemoteGDB::cmd_set_thread(GdbCommand::Context &ctx) 803{ 804 const char *p = ctx.data + 1; // Ignore the subcommand byte. 805 if (hex2i(&p) != 0) 806 throw CmdError("E01"); 807 send("OK"); 808 return true; 809} |
812 | 810 |
813 case GDBMemR: 814 val = hex2i(&p); 815 if (*p++ != ',') { 816 if (send("E02") < 0) 817 return true; 818 continue; 819 } 820 len = hex2i(&p); 821 if (*p != '\0') { 822 if (send("E03") < 0) 823 return true; 824 continue; 825 } 826 if (len > bufferSize) { 827 if (send("E04") < 0) 828 return true; 829 continue; 830 } 831 if (!acc(val, len)) { 832 if (send("E05") < 0) 833 return true; 834 continue; 835 } | 811bool 812BaseRemoteGDB::cmd_mem_r(GdbCommand::Context &ctx) 813{ 814 const char *p = ctx.data; 815 Addr addr = hex2i(&p); 816 if (*p++ != ',') 817 throw CmdError("E02"); 818 size_t len = hex2i(&p); 819 if (*p != '\0') 820 throw CmdError("E03"); 821 if (!acc(addr, len)) 822 throw CmdError("E05"); |
836 | 823 |
837 if (read(val, (size_t)len, buffer)) { 838 // variable length array would be nice, but C++ doesn't 839 // officially support those... 840 char *temp = new char[2*len+1]; 841 mem2hex(temp, buffer, len); 842 if (send(temp) < 0) { 843 delete [] temp; 844 return true; 845 } 846 delete [] temp; 847 } else { 848 if (send("E05") < 0) 849 return true; 850 } 851 continue; | 824 char buf[len]; 825 if (!read(addr, len, buf)) 826 throw CmdError("E05"); |
852 | 827 |
853 case GDBMemW: 854 val = hex2i(&p); 855 if (*p++ != ',') { 856 if (send("E06") < 0) 857 return true; 858 continue; 859 } 860 len = hex2i(&p); 861 if (*p++ != ':') { 862 if (send("E07") < 0) 863 return true; 864 continue; 865 } 866 if (len > datalen - (p - data)) { 867 if (send("E08") < 0) 868 return true; 869 continue; 870 } 871 p = hex2mem(buffer, p, bufferSize); 872 if (p == NULL) { 873 if (send("E09") < 0) 874 return true; 875 continue; 876 } 877 if (!acc(val, len)) { 878 if (send("E0A") < 0) 879 return true; 880 continue; 881 } 882 if (write(val, (size_t)len, buffer)) { 883 if (send("OK") < 0) 884 return true; 885 } else { 886 if (send("E0B") < 0) 887 return true; 888 } 889 continue; | 828 char temp[2 * len + 1]; 829 temp[2 * len] = '\0'; 830 mem2hex(temp, buf, len); 831 send(temp); 832 return true; 833} |
890 | 834 |
891 case GDBSetThread: 892 subcmd = *p++; 893 val = hex2i(&p); 894 if (val == 0) { 895 if (send("OK") < 0) 896 return true; 897 } else { 898 if (send("E01") < 0) 899 return true; 900 } 901 continue; | 835bool 836BaseRemoteGDB::cmd_mem_w(GdbCommand::Context &ctx) 837{ 838 const char *p = ctx.data; 839 Addr addr = hex2i(&p); 840 if (*p++ != ',') 841 throw CmdError("E06"); 842 size_t len = hex2i(&p); 843 if (*p++ != ':') 844 throw CmdError("E07"); 845 if (len * 2 > ctx.len - (p - ctx.data)) 846 throw CmdError("E08"); 847 char buf[len]; 848 p = (char *)hex2mem(buf, p, len); 849 if (p == NULL) 850 throw CmdError("E09"); 851 if (!acc(addr, len)) 852 throw CmdError("E0A"); 853 if (!write(addr, len, buf)) 854 throw CmdError("E0B"); 855 send("OK"); 856 return true; 857} |
902 | 858 |
903 case GDBDetach: 904 case GDBKill: 905 active = false; 906 clearSingleStep(); 907 detach(); 908 return true; | 859bool 860BaseRemoteGDB::cmd_query_var(GdbCommand::Context &ctx) 861{ 862 if (string(ctx.data, ctx.len - 1) != "C") 863 throw Unsupported(); 864 send("QC0"); 865 return true; 866} |
909 | 867 |
910 case GDBAsyncCont: 911 subcmd = hex2i(&p); 912 if (*p++ == ';') { 913 val = hex2i(&p); 914 context->pcState(val); 915 } 916 clearSingleStep(); 917 return true; | 868bool 869BaseRemoteGDB::cmd_async_step(GdbCommand::Context &ctx) 870{ 871 const char *p = ctx.data; 872 hex2i(&p); // Ignore the subcommand byte. 873 if (*p++ == ';') { 874 Addr newPc = hex2i(&p); 875 context->pcState(newPc); 876 } 877 setSingleStep(); 878 return false; 879} |
918 | 880 |
919 case GDBCont: 920 if (p - data < (ptrdiff_t)datalen) { 921 val = hex2i(&p); 922 context->pcState(val); 923 } 924 clearSingleStep(); 925 return true; | 881bool 882BaseRemoteGDB::cmd_step(GdbCommand::Context &ctx) 883{ 884 if (ctx.len) { 885 const char *p = ctx.data; 886 Addr newPc = hex2i(&p); 887 context->pcState(newPc); 888 } 889 setSingleStep(); 890 return false; 891} |
926 | 892 |
927 case GDBAsyncStep: 928 subcmd = hex2i(&p); 929 if (*p++ == ';') { 930 val = hex2i(&p); 931 context->pcState(val); 932 } 933 setSingleStep(); 934 return true; | 893bool 894BaseRemoteGDB::cmd_clr_hw_bkpt(GdbCommand::Context &ctx) 895{ 896 const char *p = ctx.data; 897 char subcmd = *p++; 898 if (*p++ != ',') 899 throw CmdError("E0D"); 900 Addr addr = hex2i(&p); 901 if (*p++ != ',') 902 throw CmdError("E0D"); 903 size_t len = hex2i(&p); |
935 | 904 |
936 case GDBStep: 937 if (p - data < (ptrdiff_t)datalen) { 938 val = hex2i(&p); 939 context->pcState(val); 940 } 941 setSingleStep(); 942 return true; | 905 DPRINTF(GDBMisc, "clear %s, addr=%#x, len=%d\n", 906 break_type(subcmd), addr, len); |
943 | 907 |
944 case GDBClrHwBkpt: 945 subcmd = *p++; 946 if (*p++ != ',' && send("E0D") < 0) 947 return true; 948 val = hex2i(&p); 949 if (*p++ != ',' && send("E0D") < 0) 950 return true; 951 len = hex2i(&p); | 908 switch (subcmd) { 909 case GdbSoftBp: 910 removeSoftBreak(addr, len); 911 break; 912 case GdbHardBp: 913 removeHardBreak(addr, len); 914 break; 915 case GdbWriteWp: 916 case GdbReadWp: 917 case GdbAccWp: 918 default: // unknown 919 throw Unsupported(); 920 } 921 send("OK"); |
952 | 922 |
953 DPRINTF(GDBMisc, "clear %s, addr=%#x, len=%d\n", 954 break_type(subcmd), val, len); | 923 return true; 924} |
955 | 925 |
956 ret = false; | 926bool 927BaseRemoteGDB::cmd_set_hw_bkpt(GdbCommand::Context &ctx) 928{ 929 const char *p = ctx.data; 930 char subcmd = *p++; 931 if (*p++ != ',') 932 throw CmdError("E0D"); 933 Addr addr = hex2i(&p); 934 if (*p++ != ',') 935 throw CmdError("E0D"); 936 size_t len = hex2i(&p); |
957 | 937 |
958 switch (subcmd) { 959 case '0': // software breakpoint 960 ret = removeSoftBreak(val, len); 961 break; | 938 DPRINTF(GDBMisc, "set %s, addr=%#x, len=%d\n", 939 break_type(subcmd), addr, len); |
962 | 940 |
963 case '1': // hardware breakpoint 964 ret = removeHardBreak(val, len); 965 break; | 941 switch (subcmd) { 942 case GdbSoftBp: 943 insertSoftBreak(addr, len); 944 break; 945 case GdbHardBp: 946 insertHardBreak(addr, len); 947 break; 948 case GdbWriteWp: 949 case GdbReadWp: 950 case GdbAccWp: 951 default: // unknown 952 throw Unsupported(); 953 } 954 send("OK"); |
966 | 955 |
967 case '2': // write watchpoint 968 case '3': // read watchpoint 969 case '4': // access watchpoint 970 default: // unknown 971 if (send("") < 0) 972 return true; 973 break; 974 } | 956 return true; 957} |
975 | 958 |
976 if (send(ret ? "OK" : "E0C") < 0) 977 return true; 978 continue; | |
979 | 959 |
980 case GDBSetHwBkpt: 981 subcmd = *p++; 982 if (*p++ != ',' && send("E0D") < 0) 983 return true; 984 val = hex2i(&p); 985 if (*p++ != ',' && send("E0D") < 0) 986 return true; 987 len = hex2i(&p); | 960// This function does all command processing for interfacing to a 961// remote gdb. Note that the error codes are ignored by gdb at 962// present, but might eventually become meaningful. (XXX) It might 963// makes sense to use POSIX errno values, because that is what the 964// gdb/remote.c functions want to return. 965bool 966BaseRemoteGDB::trap(int type) 967{ |
988 | 968 |
989 DPRINTF(GDBMisc, "set %s, addr=%#x, len=%d\n", 990 break_type(subcmd), val, len); | 969 if (!attached) 970 return false; |
991 | 971 |
992 ret = false; | 972 DPRINTF(GDBMisc, "trap: PC=%s\n", context->pcState()); |
993 | 973 |
994 switch (subcmd) { 995 case '0': // software breakpoint 996 ret = insertSoftBreak(val, len); 997 break; | 974 clearSingleStep(); |
998 | 975 |
999 case '1': // hardware breakpoint 1000 ret = insertHardBreak(val, len); 1001 break; | 976 /* 977 * The first entry to this function is normally through 978 * a breakpoint trap in kgdb_connect(), in which case we 979 * must advance past the breakpoint because gdb will not. 980 * 981 * On the first entry here, we expect that gdb is not yet 982 * listening to us, so just enter the interaction loop. 983 * After the debugger is "active" (connected) it will be 984 * waiting for a "signaled" message from us. 985 */ 986 if (!active) { 987 active = true; 988 } else { 989 // Tell remote host that an exception has occurred. 990 send(csprintf("S%02x", type).c_str()); 991 } |
1002 | 992 |
1003 case '2': // write watchpoint 1004 case '3': // read watchpoint 1005 case '4': // access watchpoint 1006 default: // unknown 1007 if (send("") < 0) 1008 return true; 1009 break; 1010 } | 993 // Stick frame regs into our reg cache. 994 regCachePtr = gdbRegs(); 995 regCachePtr->getRegs(context); |
1011 | 996 |
1012 if (send(ret ? "OK" : "E0C") < 0) 1013 return true; 1014 continue; | 997 char data[GDBPacketBufLen + 1]; 998 GdbCommand::Context cmdCtx; 999 cmdCtx.type = type; 1000 cmdCtx.data = &data[1]; |
1015 | 1001 |
1016 case GDBQueryVar: 1017 var = string(p, datalen - 1); 1018 if (var == "C") { 1019 if (send("QC0") < 0) 1020 return true; 1021 } else { 1022 if (send("") < 0) 1023 return true; 1024 } 1025 continue; | 1002 for (;;) { 1003 try { 1004 size_t datalen = recv(data, sizeof(data)); 1005 if (datalen < 1) 1006 throw BadClient(); |
1026 | 1007 |
1027 case GDBSetBaud: 1028 case GDBSetBreak: 1029 case GDBDebug: 1030 case GDBCycleStep: 1031 case GDBSigCycleStep: 1032 case GDBReadReg: 1033 case GDBSetVar: 1034 case GDBReset: 1035 case GDBThreadAlive: 1036 case GDBTargetExit: 1037 case GDBBinaryDload: 1038 // Unsupported command 1039 DPRINTF(GDBMisc, "Unsupported command: %s\n", 1040 gdb_command(command)); 1041 DDUMP(GDBMisc, (uint8_t *)data, datalen); 1042 if (send("") < 0) 1043 return true; 1044 continue; | 1008 data[datalen] = 0; // Sentinel 1009 cmdCtx.cmd_byte = data[0]; 1010 cmdCtx.len = datalen - 1; |
1045 | 1011 |
1046 default: 1047 // Unknown command. 1048 DPRINTF(GDBMisc, "Unknown command: %c(%#x)\n", 1049 command, command); 1050 if (send("") < 0) 1051 return true; 1052 continue; | 1012 auto cmdIt = command_map.find(cmdCtx.cmd_byte); 1013 if (cmdIt == command_map.end()) { 1014 DPRINTF(GDBMisc, "Unknown command: %c(%#x)\n", 1015 cmdCtx.cmd_byte, cmdCtx.cmd_byte); 1016 throw Unsupported(); 1017 } 1018 cmdCtx.cmd = &(cmdIt->second); |
1053 | 1019 |
1020 if (!(this->*(cmdCtx.cmd->func))(cmdCtx)) 1021 break; |
|
1054 | 1022 |
1023 } catch (BadClient &e) { 1024 if (e.warning) 1025 warn(e.warning); 1026 detach(); 1027 break; 1028 } catch (Unsupported &e) { 1029 send(""); 1030 } catch (CmdError &e) { 1031 send(e.error.c_str()); 1032 } catch (...) { 1033 panic("Unrecognzied GDB exception."); |
|
1055 } 1056 } 1057 1058 return true; 1059} 1060 1061// Convert a hex digit into an integer. 1062// This returns -1 if the argument passed is no valid hex digit. 1063int 1064BaseRemoteGDB::digit2i(char c) 1065{ 1066 if (c >= '0' && c <= '9') 1067 return (c - '0'); 1068 else if (c >= 'a' && c <= 'f') 1069 return (c - 'a' + 10); 1070 else if (c >= 'A' && c <= 'F') | 1034 } 1035 } 1036 1037 return true; 1038} 1039 1040// Convert a hex digit into an integer. 1041// This returns -1 if the argument passed is no valid hex digit. 1042int 1043BaseRemoteGDB::digit2i(char c) 1044{ 1045 if (c >= '0' && c <= '9') 1046 return (c - '0'); 1047 else if (c >= 'a' && c <= 'f') 1048 return (c - 'a' + 10); 1049 else if (c >= 'A' && c <= 'F') |
1071 | |
1072 return (c - 'A' + 10); 1073 else 1074 return (-1); 1075} 1076 1077// Convert the low 4 bits of an integer into an hex digit. 1078char 1079BaseRemoteGDB::i2digit(int n) --- 29 unchanged lines hidden (view full) --- 1109 msb = digit2i(*src++); 1110 if (msb < 0) 1111 return (src - 1); 1112 lsb = digit2i(*src++); 1113 if (lsb < 0) 1114 return (NULL); 1115 *dst++ = (msb << 4) | lsb; 1116 } | 1050 return (c - 'A' + 10); 1051 else 1052 return (-1); 1053} 1054 1055// Convert the low 4 bits of an integer into an hex digit. 1056char 1057BaseRemoteGDB::i2digit(int n) --- 29 unchanged lines hidden (view full) --- 1087 msb = digit2i(*src++); 1088 if (msb < 0) 1089 return (src - 1); 1090 lsb = digit2i(*src++); 1091 if (lsb < 0) 1092 return (NULL); 1093 *dst++ = (msb << 4) | lsb; 1094 } |
1117 return (src); | 1095 return src; |
1118} 1119 1120// Convert an hex string into an integer. 1121// This returns a pointer to the character following the last valid 1122// hex digit. 1123Addr 1124BaseRemoteGDB::hex2i(const char **srcp) 1125{ 1126 const char *src = *srcp; 1127 Addr r = 0; 1128 int nibble; 1129 1130 while ((nibble = digit2i(*src)) >= 0) { 1131 r *= 16; 1132 r += nibble; 1133 src++; 1134 } 1135 *srcp = src; | 1096} 1097 1098// Convert an hex string into an integer. 1099// This returns a pointer to the character following the last valid 1100// hex digit. 1101Addr 1102BaseRemoteGDB::hex2i(const char **srcp) 1103{ 1104 const char *src = *srcp; 1105 Addr r = 0; 1106 int nibble; 1107 1108 while ((nibble = digit2i(*src)) >= 0) { 1109 r *= 16; 1110 r += nibble; 1111 src++; 1112 } 1113 *srcp = src; |
1136 return (r); | 1114 return r; |
1137} 1138 | 1115} 1116 |