12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698 |
- /*++
- Copyright (c) 2013 Minoca Corp. All Rights Reserved
- Module Name:
- dbgapi.c
- Abstract:
- This module implements support for low level debugger core services.
- Author:
- Evan Green 7-May-2013
- Environment:
- Debug client
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include "dbgrtl.h"
- #include <minoca/debug/spproto.h>
- #include <minoca/lib/im.h>
- #include <minoca/debug/dbgext.h>
- #include "dbgapi.h"
- #include "dbgrprof.h"
- #include "console.h"
- #include "userdbg.h"
- #include "symbols.h"
- #include "dbgrcomm.h"
- #include "dbgsym.h"
- #include <assert.h>
- #include <ctype.h>
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // Define the number of milliseconds to poll and see if the user has requested
- // a break-in.
- //
- #define DEBUG_USER_POLL_MILLISECONDS 200
- //
- // Define the length of the standard x86 function prologue.
- //
- #define X86_FUNCTION_PROLOGUE_LENGTH 3
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- typedef struct _DEBUG_COMPLETE_ACKNOWLEDGE_PACKET {
- DEBUG_PACKET_HEADER Header;
- DEBUG_PACKET_ACKNOWLEDGE Acknowledge;
- } DEBUG_COMPLETE_ACKNOWLEDGE_PACKET, *_DEBUG_COMPLETE_ACKNOWLEDGE_PACKET;
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- BOOL
- DbgpKdContinue (
- VOID
- );
- BOOL
- DbgpKdSetRegisters (
- PREGISTERS_UNION Registers
- );
- BOOL
- DbgpKdGetSpecialRegisters (
- PSPECIAL_REGISTERS_UNION SpecialRegisters
- );
- BOOL
- DbgpKdSetSpecialRegisters (
- PSET_SPECIAL_REGISTERS Command
- );
- BOOL
- DbgpKdSingleStep (
- VOID
- );
- BOOL
- DbgpKdWaitForEvent (
- PDEBUGGER_EVENT Event
- );
- BOOL
- DbgpKdRangeStep (
- PRANGE_STEP RangeStep
- );
- BOOL
- DbgpKdSwitchProcessors (
- ULONG ProcessorNumber
- );
- BOOL
- DbgpKdGetLoadedModuleList (
- PMODULE_LIST_HEADER *ModuleList
- );
- BOOL
- DbgpKdReadWriteMemory (
- BOOL WriteOperation,
- BOOL VirtualMemory,
- ULONGLONG Address,
- PVOID Buffer,
- ULONG BufferSize,
- PULONG BytesCompleted
- );
- BOOL
- DbgpKdReboot (
- DEBUG_REBOOT_TYPE RebootType
- );
- BOOL
- DbgpKdSendPacket (
- PDEBUG_PACKET Packet
- );
- BOOL
- DbgpKdReceivePacket (
- PDEBUG_PACKET Packet,
- ULONG TimeoutMilliseconds,
- PBOOL TimeoutOccurred
- );
- BOOL
- DbgpKdReceivePacketHeader (
- PDEBUG_PACKET_HEADER Packet,
- ULONG TimeoutMilliseconds,
- PBOOL TimeoutOccurred
- );
- INT
- DbgpKdSynchronize (
- VOID
- );
- USHORT
- DbgpKdCalculateChecksum (
- PVOID Data,
- ULONG DataLength
- );
- BOOL
- DbgpKdReceiveBytes (
- PVOID Buffer,
- ULONG BytesToRead
- );
- BOOL
- DbgpKdSendBytes (
- PVOID Buffer,
- ULONG BytesToSend
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- BOOL DbgBreakInDesired;
- BOOL DbgBreakInRequestSent;
- //
- // Define the globals used to transmit and receive kernel debug packets.
- //
- DEBUG_PACKET DbgRxPacket;
- DEBUG_PACKET DbgTxPacket;
- //
- // Define what the function prologue might look like.
- //
- UCHAR DbgX86FunctionPrologue[X86_FUNCTION_PROLOGUE_LENGTH] = {0x55, 0x89, 0xE5};
- //
- // Set this to TRUE to view the bytes going across the wire.
- //
- BOOL DbgKdPrintRawBytes = FALSE;
- BOOL DbgKdPrintMemoryAccesses = FALSE;
- //
- // Set this to TRUE to enable byte escaping for transports that cannot send
- // certain bytes.
- //
- BOOL DbgKdEncodeBytes = FALSE;
- //
- // This boolean gets set to TRUE when a resynchronization byte is found in the
- // data stream between packets.
- //
- BOOL DbgKdConnectionReset;
- //
- // ------------------------------------------------------------------ Functions
- //
- INT
- DbgInitialize (
- PDEBUGGER_CONTEXT Context,
- DEBUG_CONNECTION_TYPE ConnectionType
- )
- /*++
- Routine Description:
- This routine initializes data structures for common debugger API.
- Arguments:
- Context - Supplies a pointer to the application context.
- ConnectionType - Supplies the type of debug connection to set the debugger
- up in.
- Return Value:
- 0 on success.
- Returns an error code on failure.
- --*/
- {
- assert(ConnectionType != DebugConnectionInvalid);
- Context->ConnectionType = ConnectionType;
- if (ConnectionType == DebugConnectionUser) {
- Context->MachineType = DbgGetHostMachineType();
- }
- return 0;
- }
- VOID
- DbgDestroy (
- PDEBUGGER_CONTEXT Context,
- DEBUG_CONNECTION_TYPE ConnectionType
- )
- /*++
- Routine Description:
- This routine destroys data structures for common debugger API.
- Arguments:
- Context - Supplies a pointer to the application context.
- ConnectionType - Supplies the type of debug connection for which the
- debugger was initialized.
- Return Value:
- None.
- --*/
- {
- return;
- }
- INT
- DbgKdConnect (
- PDEBUGGER_CONTEXT Context,
- BOOL RequestBreak,
- PCONNECTION_RESPONSE *ConnectionDetails,
- PULONG BufferSize
- )
- /*++
- Routine Description:
- This routine establishes a link with the target. It is assumed that
- the underlying communication layer has already been established (COM ports
- have been opened and initialized, etc).
- Arguments:
- Context - Supplies a pointer to the application context.
- RequestBreak - Supplies a boolean indicating whether or not the host
- should break in.
- ConnectionDetails - Supplies a pointer where a pointer to details about the
- kernel connection will be returned. The caller is responsible for
- freeing this memory when done.
- BufferSize - Supplies a pointer where the total size of the connection
- details buffer will be returned.
- Return Value:
- 0 on success.
- Non-zero on error.
- --*/
- {
- PCONNECTION_REQUEST ConnectionRequest;
- PCONNECTION_RESPONSE ConnectionResponse;
- PCONNECTION_RESPONSE NewResponse;
- INT Result;
- *BufferSize = 0;
- *ConnectionDetails = NULL;
- assert(Context->ConnectionType == DebugConnectionKernel);
- //
- // Set the connection reset flag so that receive knows to ignore incoming
- // resync bytes.
- //
- DbgKdConnectionReset = TRUE;
- //
- // Synchronize with the target to make sure it is ready and listening.
- //
- Result = DbgpKdSynchronize();
- if (Result != 0) {
- return Result;
- }
- //
- // Fill out the connection request structure, and send the initial packet.
- //
- ConnectionRequest = (PCONNECTION_REQUEST)DbgTxPacket.Payload;
- ConnectionRequest->ProtocolMajorVersion = DEBUG_PROTOCOL_MAJOR_VERSION;
- ConnectionRequest->ProtocolRevision = DEBUG_PROTOCOL_REVISION;
- ConnectionRequest->BreakRequested = RequestBreak;
- DbgTxPacket.Header.Command = DbgConnectionRequest;
- DbgTxPacket.Header.PayloadSize = sizeof(CONNECTION_REQUEST);
- Result = DbgpKdSendPacket(&DbgTxPacket);
- if (Result == FALSE) {
- DbgOut("Unable to send Connection Request packet!\n");
- return EPIPE;
- }
- //
- // Attempt to receive the connection response packet. Get through resync
- // bytes.
- //
- Result = DbgpKdReceivePacket(&DbgRxPacket, 0, NULL);
- if (Result == FALSE) {
- DbgOut("Unable to receive Connection Response packet!\n");
- return EPIPE;
- }
- //
- // The connection is now established, so future resync bytes reset it.
- //
- DbgKdConnectionReset = FALSE;
- ConnectionResponse = (PCONNECTION_RESPONSE)DbgRxPacket.Payload;
- if (DbgRxPacket.Header.Command != DbgConnectionAcknowledge) {
- //
- // Check for a debugger/target version mismatch, and bail out if one
- // occurred.
- //
- if (DbgRxPacket.Header.Command == DbgConnectionWrongVersion) {
- DbgOut("Version mismatch! Debugger version: %d.%02d, Target "
- "version: %d.%02d.\n",
- DEBUG_PROTOCOL_MAJOR_VERSION,
- DEBUG_PROTOCOL_REVISION,
- ConnectionResponse->ProtocolMajorVersion,
- ConnectionResponse->ProtocolRevision);
- //
- // Check if the target rejected the request gracefully.
- //
- } else if (DbgRxPacket.Header.Command == DbgConnectionInvalidRequest) {
- DbgOut("Command rejected by target\n");
- //
- // The target sent something completely unexpected.
- //
- } else {
- DbgOut("Expecting DbgConnectionAcknowledge, got %d\n",
- DbgRxPacket.Header.Command);
- }
- return EIO;
- }
- NewResponse = malloc(DbgRxPacket.Header.PayloadSize);
- if (NewResponse == NULL) {
- return ENOMEM;
- }
- //
- // A connection was successfully established. Copy the connection details
- // to the caller's structure.
- //
- RtlCopyMemory(NewResponse,
- ConnectionResponse,
- DbgRxPacket.Header.PayloadSize);
- *ConnectionDetails = NewResponse;
- *BufferSize = DbgRxPacket.Header.PayloadSize;
- return 0;
- }
- INT
- DbgContinue (
- PDEBUGGER_CONTEXT Context,
- ULONG SignalToDeliver
- )
- /*++
- Routine Description:
- This routine sends the "go" command to the target, signaling to continue
- execution.
- Arguments:
- Context - Supplies a pointer to the application context.
- SignalToDeliver - Supplies the signal number to actually send to the
- application. For kernel debugging, this parameter is ignored.
- Return Value:
- 0 on success.
- Non-zero on failure.
- --*/
- {
- INT Result;
- if (Context->ConnectionType == DebugConnectionKernel) {
- Result = DbgpKdContinue();
- } else if (Context->ConnectionType == DebugConnectionUser) {
- Result = DbgpUserContinue(SignalToDeliver);
- } else {
- DbgOut("Error: Unknown connection type %d.\n", Context->ConnectionType);
- Result = FALSE;
- }
- if (Result != FALSE) {
- Context->TargetFlags |= DEBUGGER_TARGET_RUNNING;
- Result = 0;
- } else {
- Result = EINVAL;
- }
- return Result;
- }
- ULONG
- DbgGetSignalToDeliver (
- PDEBUGGER_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine returns the value for the "signal to deliver" parameters when
- letting the target continue. For user mode processes, breaks into the
- debugger occur because of signal delivery, and the debugger has the choice
- of whether or not to actually deliver a signal.
- Arguments:
- Context - Supplies a pointer to the application context.
- Return Value:
- Returns the signal to deliver for the upcoming target continuation.
- 0 for kernel debugging or if no signal should be delivered to the target.
- This value can just be passed through.
- --*/
- {
- ULONG SignalToDeliver;
- SignalToDeliver = 0;
- if (Context->ConnectionType == DebugConnectionUser) {
- SignalToDeliver = Context->CurrentEvent.SignalParameters.SignalNumber;
- SignalToDeliver = DbgpUserGetSignalToDeliver(SignalToDeliver);
- }
- return SignalToDeliver;
- }
- INT
- DbgSetRegisters (
- PDEBUGGER_CONTEXT Context,
- PREGISTERS_UNION Registers
- )
- /*++
- Routine Description:
- This routine sets the registers of the target.
- Arguments:
- Context - Supplies a pointer to the application context.
- Registers - Supplies a pointer to the registers to set. All register values
- will be written.
- Return Value:
- 0 on success.
- Non-zero on failure.
- --*/
- {
- INT Result;
- if (Context->ConnectionType == DebugConnectionKernel) {
- Result = DbgpKdSetRegisters(Registers);
- } else if (Context->ConnectionType == DebugConnectionUser) {
- Result = DbgpUserSetRegisters(Registers);
- } else {
- DbgOut("Error: Unknown connection type %d.\n", Context->ConnectionType);
- Result = FALSE;
- }
- if (Result != FALSE) {
- Result = 0;
- } else {
- Result = EINVAL;
- }
- return Result;
- }
- INT
- DbgGetSpecialRegisters (
- PDEBUGGER_CONTEXT Context,
- PSPECIAL_REGISTERS_UNION SpecialRegisters
- )
- /*++
- Routine Description:
- This routine gets the registers of the target.
- Arguments:
- Context - Supplies a pointer to the application context.
- SpecialRegisters - Supplies a pointer where the registers will be returned.
- Return Value:
- 0 on success.
- Non-zero on failure.
- --*/
- {
- INT Result;
- if (Context->ConnectionType == DebugConnectionKernel) {
- Result = DbgpKdGetSpecialRegisters(SpecialRegisters);
- } else if (Context->ConnectionType == DebugConnectionUser) {
- DbgOut("Special registers cannot be accessed in user mode.\n");
- Result = FALSE;
- } else {
- DbgOut("Error: Unknown connection type %d.\n", Context->ConnectionType);
- Result = FALSE;
- }
- if (Result != FALSE) {
- Result = 0;
- } else {
- Result = EINVAL;
- }
- return Result;
- }
- INT
- DbgSetSpecialRegisters (
- PDEBUGGER_CONTEXT Context,
- PSET_SPECIAL_REGISTERS Command
- )
- /*++
- Routine Description:
- This routine sets the special registers from the target.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the details of the set special registers
- command.
- Return Value:
- 0 on success.
- Non-zero on failure.
- --*/
- {
- INT Result;
- if (Context->ConnectionType == DebugConnectionKernel) {
- Result = DbgpKdSetSpecialRegisters(Command);
- } else if (Context->ConnectionType == DebugConnectionUser) {
- DbgOut("Special registers cannot be accessed in user mode.\n");
- Result = FALSE;
- } else {
- DbgOut("Error: Unknown connection type %d.\n", Context->ConnectionType);
- Result = FALSE;
- }
- if (Result != FALSE) {
- Result = 0;
- } else {
- Result = EINVAL;
- }
- return Result;
- }
- INT
- DbgSingleStep (
- PDEBUGGER_CONTEXT Context,
- ULONG SignalToDeliver
- )
- /*++
- Routine Description:
- This routine steps the target by one instruction.
- Arguments:
- Context - Supplies a pointer to the application context.
- SignalToDeliver - Supplies the signal number to actually send to the
- application. For kernel debugging, this parameter is ignored.
- Return Value:
- 0 on success.
- Returns an error code on failure.
- --*/
- {
- INT Result;
- if (Context->ConnectionType == DebugConnectionKernel) {
- Result = DbgpKdSingleStep();
- } else if (Context->ConnectionType == DebugConnectionUser) {
- Result = DbgpUserSingleStep(SignalToDeliver);
- } else {
- DbgOut("Error: Unknown connection type %d.\n", Context->ConnectionType);
- Result = FALSE;
- }
- if (Result == FALSE) {
- return EINVAL;
- }
- Context->TargetFlags |= DEBUGGER_TARGET_RUNNING;
- Result = 0;
- return Result;
- }
- INT
- DbgWaitForEvent (
- PDEBUGGER_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine gets an event from the target, such as a break event or other
- exception.
- Arguments:
- Context - Supplies a pointer to the application context.
- Return Value:
- 0 on success.
- Non-zero on failure.
- --*/
- {
- INT Result;
- Context->CurrentEvent.Type = DebuggerEventInvalid;
- if (Context->ConnectionType == DebugConnectionKernel) {
- Result = DbgpKdWaitForEvent(&(Context->CurrentEvent));
- } else if (Context->ConnectionType == DebugConnectionUser) {
- Result = DbgpUserWaitForEvent(&(Context->CurrentEvent));
- } else {
- DbgOut("Error: Unknown connection type %d.\n", Context->ConnectionType);
- Result = FALSE;
- }
- if (Result != FALSE) {
- Result = 0;
- } else {
- Result = EINVAL;
- }
- return Result;
- }
- INT
- DbgRangeStep (
- PDEBUGGER_CONTEXT Context,
- PRANGE_STEP RangeStep,
- ULONG SignalToDeliver
- )
- /*++
- Routine Description:
- This routine continues execution until a range of execution addresses is
- reached.
- Arguments:
- Context - Supplies the application context.
- RangeStep - Supplies a pointer to the range to go to.
- SignalToDeliver - Supplies the signal number to actually send to the
- application. For kernel debugging, this parameter is ignored.
- Return Value:
- 0 on success.
- Returns an error code on failure.
- --*/
- {
- INT Result;
- if (Context->ConnectionType == DebugConnectionKernel) {
- Result = DbgpKdRangeStep(RangeStep);
- } else if (Context->ConnectionType == DebugConnectionUser) {
- Result = DbgpUserRangeStep(RangeStep, SignalToDeliver);
- } else {
- DbgOut("Error: Unknown connection type %d.\n", Context->ConnectionType);
- Result = FALSE;
- }
- if (Result == FALSE) {
- return EINVAL;
- }
- Result = 0;
- Context->TargetFlags |= DEBUGGER_TARGET_RUNNING;
- return Result;
- }
- INT
- DbgSwitchProcessors (
- PDEBUGGER_CONTEXT Context,
- ULONG ProcessorNumber
- )
- /*++
- Routine Description:
- This routine switches the debugger to another processor.
- Arguments:
- Context - Supplies a pointer to the debugger context.
- ProcessorNumber - Supplies the processor number to switch to.
- DebuggeeRunning - Supplies a pointer where a boolean will be returned
- indicating whether or not the target has continued.
- Return Value:
- 0 on success.
- Returns an error code on failure.
- --*/
- {
- INT Result;
- if (Context->ConnectionType == DebugConnectionKernel) {
- Result = DbgpKdSwitchProcessors(ProcessorNumber);
- if (Result == FALSE) {
- Result = EINVAL;
- goto SwitchProcessorsEnd;
- } else {
- Context->TargetFlags |= DEBUGGER_TARGET_RUNNING;
- }
- } else if (Context->ConnectionType == DebugConnectionUser) {
- Result = DbgpUserSwitchThread(ProcessorNumber,
- &(Context->CurrentEvent));
- if (Result == FALSE) {
- Result = EINVAL;
- goto SwitchProcessorsEnd;
- }
- } else {
- DbgOut("Error: Unknown connection type %d.\n", Context->ConnectionType);
- Result = EINVAL;
- goto SwitchProcessorsEnd;
- }
- Result = 0;
- SwitchProcessorsEnd:
- return Result;
- }
- INT
- DbgGetThreadList (
- PDEBUGGER_CONTEXT Context,
- PULONG ThreadCount,
- PULONG *ThreadIds
- )
- /*++
- Routine Description:
- This routine gets the list of active threads in the process (or active
- processors in the machine for kernel mode).
- Arguments:
- Context - Supplies a pointer to the application context.
- ThreadCount - Supplies a pointer where the number of threads will be
- returned on success.
- ThreadIds - Supplies a pointer where an array of thread IDs (or processor
- numbers) will be returned on success. It is the caller's responsibility
- to free this memory.
- Return Value:
- 0 on success.
- Non-zero on failure.
- --*/
- {
- ULONG AllocationSize;
- PULONG CurrentThreadEntry;
- ULONG ProcessorCount;
- PULONG Processors;
- INT Result;
- ULONG ThreadIndex;
- *ThreadCount = 0;
- *ThreadIds = NULL;
- if (Context->ConnectionType == DebugConnectionKernel) {
- ProcessorCount =
- Context->CurrentEvent.BreakNotification.ProcessorOrThreadCount;
- assert(ProcessorCount != 0);
- *ThreadCount = ProcessorCount;
- AllocationSize = sizeof(ULONG) * ProcessorCount;
- Processors = malloc(AllocationSize);
- if (Processors == NULL) {
- DbgOut("Error: Failed to malloc %d for processor list.\n",
- AllocationSize);
- return ENOMEM;
- }
- CurrentThreadEntry = Processors;
- for (ThreadIndex = 0; ThreadIndex < ProcessorCount; ThreadIndex += 1) {
- *CurrentThreadEntry = ThreadIndex;
- }
- *ThreadIds = Processors;
- Result = TRUE;
- } else if (Context->ConnectionType == DebugConnectionUser) {
- Result = DbgpUserGetThreadList(ThreadCount, ThreadIds);
- } else {
- DbgOut("Error: Unknown connection type %d.\n", Context->ConnectionType);
- Result = FALSE;
- }
- if (Result != FALSE) {
- Result = 0;
- } else {
- Result = EINVAL;
- }
- return Result;
- }
- INT
- DbgGetLoadedModuleList (
- PDEBUGGER_CONTEXT Context,
- PMODULE_LIST_HEADER *ModuleList
- )
- /*++
- Routine Description:
- This routine retrieves the list of loaded binaries from the target.
- Arguments:
- Context - Supplies a pointer to the application context.
- ModuleList - Supplies a pointer where a pointer to the loaded module header
- and subsequent array of entries will be returned. It is the caller's
- responsibility to free this allocated memory when finished.
- Return Value:
- 0 on success.
- Non-zero on failure.
- --*/
- {
- INT Result;
- if (Context->ConnectionType == DebugConnectionKernel) {
- Result = DbgpKdGetLoadedModuleList(ModuleList);
- } else if (Context->ConnectionType == DebugConnectionUser) {
- Result = DbgpUserGetLoadedModuleList(ModuleList);
- } else {
- DbgOut("Error: Unknown connection type %d.\n", Context->ConnectionType);
- Result = FALSE;
- }
- if (Result != FALSE) {
- Result = 0;
- } else {
- Result = EINVAL;
- }
- return Result;
- }
- VOID
- DbgRequestBreakIn (
- PDEBUGGER_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine attempts to stop the running target.
- Arguments:
- Context - Supplies a pointer to the application context.
- Return Value:
- None.
- --*/
- {
- if (Context->ConnectionType == DebugConnectionKernel) {
- DbgBreakInRequestSent = FALSE;
- DbgBreakInDesired = TRUE;
- } else if (Context->ConnectionType == DebugConnectionUser) {
- DbgpUserRequestBreakIn();
- } else {
- DbgOut("Error: Unknown connection type %d.\n", Context->ConnectionType);
- }
- return;
- }
- INT
- DbgReadMemory (
- PDEBUGGER_CONTEXT Context,
- BOOL VirtualMemory,
- ULONGLONG Address,
- ULONG BytesToRead,
- PVOID Buffer,
- PULONG BytesRead
- )
- /*++
- Routine Description:
- This routine retrieves the debuggee's memory.
- Arguments:
- Context - Supplies a pointer to the application context.
- VirtualMemory - Supplies a flag indicating whether the read should be
- virtual or physical.
- Address - Supplies the address to read from the target's memory.
- BytesToRead - Supplies the number of bytes to be read.
- Buffer - Supplies a pointer to the buffer where the memory contents will be
- returned.
- BytesRead - Supplies a pointer that receive the number of bytes that were
- actually read from the target.
- Return Value:
- 0 on success.
- Returns an error code on failure.
- --*/
- {
- BOOL Result;
- if (Context->ConnectionType == DebugConnectionKernel) {
- Result = DbgpKdReadWriteMemory(FALSE,
- VirtualMemory,
- Address,
- Buffer,
- BytesToRead,
- BytesRead);
- } else if (Context->ConnectionType == DebugConnectionUser) {
- Result = DbgpUserReadWriteMemory(FALSE,
- VirtualMemory,
- Address,
- Buffer,
- BytesToRead,
- BytesRead);
- } else {
- DbgOut("Error: Unknown connection type %d.\n", Context->ConnectionType);
- Result = FALSE;
- }
- if (Result == FALSE) {
- return EINVAL;
- }
- return 0;
- }
- INT
- DbgWriteMemory (
- PDEBUGGER_CONTEXT Context,
- BOOL VirtualMemory,
- ULONGLONG Address,
- ULONG BytesToWrite,
- PVOID Buffer,
- PULONG BytesWritten
- )
- /*++
- Routine Description:
- This routine writes to the debuggee's memory.
- Arguments:
- Context - Supplies a pointer to the application context.
- VirtualMemory - Supplies a flag indicating whether the read should be
- virtual or physical.
- Address - Supplies the address to write to the target's memory.
- BytesToWrite - Supplies the number of bytes to be written.
- Buffer - Supplies a pointer to the buffer containing the values to write.
- BytesWritten - Supplies a pointer that receives the number of bytes that
- were actually written to the target.
- Return Value:
- 0 if the write was successful.
- Returns an error code on failure.
- --*/
- {
- INT Result;
- if (Context->ConnectionType == DebugConnectionKernel) {
- Result = DbgpKdReadWriteMemory(TRUE,
- VirtualMemory,
- Address,
- Buffer,
- BytesToWrite,
- BytesWritten);
- } else if (Context->ConnectionType == DebugConnectionUser) {
- Result = DbgpUserReadWriteMemory(TRUE,
- VirtualMemory,
- Address,
- Buffer,
- BytesToWrite,
- BytesWritten);
- } else {
- DbgOut("Error: Unknown connection type %d.\n", Context->ConnectionType);
- Result = FALSE;
- }
- if (Result == FALSE) {
- return EINVAL;
- }
- return 0;
- }
- INT
- DbgReboot (
- PDEBUGGER_CONTEXT Context,
- ULONG RebootType
- )
- /*++
- Routine Description:
- This routine attempts to reboot the target machine.
- Arguments:
- Context - Supplies a pointer to the application context.
- RebootType - Supplies the type of reboot to perform. See the
- DEBUG_REBOOT_TYPE enumeration.
- Return Value:
- 0 if the write was successful.
- Returns an error code on failure.
- --*/
- {
- INT Result;
- if (Context->ConnectionType == DebugConnectionKernel) {
- Result = DbgpKdReboot(RebootType);
- if (Result == FALSE) {
- Result = EINVAL;
- }
- Result = 0;
- } else if (Context->ConnectionType == DebugConnectionUser) {
- DbgOut("Reboot is only supported on kernel debug targets.\n");
- Result = ENODEV;
- } else {
- DbgOut("Error: Unknown connection type %d.\n", Context->ConnectionType);
- Result = EINVAL;
- }
- if (Result == 0) {
- Context->TargetFlags |= DEBUGGER_TARGET_RUNNING;
- }
- return Result;
- }
- INT
- DbgGetCallStack (
- PDEBUGGER_CONTEXT Context,
- PREGISTERS_UNION Registers,
- PSTACK_FRAME Frames,
- PULONG FrameCount
- )
- /*++
- Routine Description:
- This routine attempts to unwind the call stack starting at the given
- machine state.
- Arguments:
- Context - Supplies a pointer to the application context.
- Registers - Supplies an optional pointer to the registers on input. On
- output, these registers will be updated with the unwound value. If this
- is NULL, then the current break notification registers will be used.
- Frames - Supplies a pointer where the array of stack frames will be
- returned.
- FrameCount - Supplies the number of frames allocated in the frames
- argument, representing the maximum number of frames to get. On output,
- returns the number of valid frames in the array.
- Return Value:
- 0 on success.
- Returns an error code on failure.
- --*/
- {
- ULONG FrameIndex;
- REGISTERS_UNION LocalRegisters;
- INT SearchIndex;
- INT Status;
- BOOL Unwind;
- if (Registers == NULL) {
- assert(Context->CurrentEvent.Type == DebuggerEventBreak);
- RtlCopyMemory(&LocalRegisters,
- &(Context->CurrentEvent.BreakNotification.Registers),
- sizeof(REGISTERS_UNION));
- Registers = &LocalRegisters;
- }
- Unwind = TRUE;
- FrameIndex = 0;
- while (FrameIndex < *FrameCount) {
- Status = DbgStackUnwind(Context,
- Registers,
- &Unwind,
- &(Frames[FrameIndex]));
- if (Status == EOF) {
- Status = 0;
- break;
- } else if (Status != 0) {
- break;
- }
- //
- // If the stack frame appears to loop back on itself, stop.
- //
- for (SearchIndex = 0; SearchIndex < FrameIndex; SearchIndex += 1) {
- if (Frames[SearchIndex].FramePointer ==
- Frames[FrameIndex].FramePointer) {
- break;
- }
- }
- FrameIndex += 1;
- if (SearchIndex != FrameIndex - 1) {
- DbgOut("Stack frame loops, frame %I64x.\n",
- Frames[FrameIndex].FramePointer);
- break;
- }
- }
- *FrameCount = FrameIndex;
- return Status;
- }
- INT
- DbgStackUnwind (
- PDEBUGGER_CONTEXT Context,
- PREGISTERS_UNION Registers,
- PBOOL Unwind,
- PSTACK_FRAME Frame
- )
- /*++
- Routine Description:
- This routine attempts to unwind the stack by one frame.
- Arguments:
- Context - Supplies a pointer to the application context.
- Registers - Supplies a pointer to the registers on input. On output, these
- registers will be updated with the unwound value.
- Unwind - Supplies a pointer that on input should initially be set to TRUE,
- indicating to use the symbol unwinder if possible. If unwinding is not
- possible, this will be set to FALSE, and should remain FALSE for the
- remainder of the stack frames unwound.
- Frame - Supplies a pointer where the basic frame information for this
- frame will be returned.
- Return Value:
- 0 on success.
- EOF if there are no more stack frames.
- Returns an error code on failure.
- --*/
- {
- ULONGLONG BasePointer;
- ULONG ByteIndex;
- ULONG BytesRead;
- ULONGLONG DebasedPc;
- PDEBUGGER_MODULE Module;
- ULONGLONG Pc;
- ULONG PointerSize;
- ULONGLONG StackPointer;
- INT Status;
- PDEBUG_SYMBOLS Symbols;
- UCHAR WorkingBuffer[24];
- UCHAR X86InstructionContents[X86_FUNCTION_PROLOGUE_LENGTH];
- //
- // First look up the symbols and see if they can unwind the stack.
- //
- Pc = DbgGetPc(Context, Registers);
- if (*Unwind != FALSE) {
- Module = DbgpFindModuleFromAddress(Context, Pc, &DebasedPc);
- if ((Module != NULL) && (Module->Symbols != NULL) &&
- (Module->Symbols->Interface->Unwind != NULL)) {
- Symbols = Module->Symbols;
- assert(Symbols->RegistersContext == NULL);
- Symbols->RegistersContext = Registers;
- Status = Symbols->Interface->Unwind(Symbols, DebasedPc, Frame);
- Symbols->RegistersContext = NULL;
- if (Status != 0) {
- if (Status != ENOENT) {
- DbgOut("Failed to unwind stack at PC 0x%I64x\n", Pc);
- }
- } else {
- //
- // Set the PC to the return address. For architectures like
- // x86 this is a no-op. On ARM, the return address register
- // (r14) is different than the PC (r15).
- //
- DbgSetPc(Context, Registers, Frame->ReturnAddress);
- goto StackUnwindEnd;
- }
- }
- *Unwind = FALSE;
- }
- //
- // Symbols do not exist or were no help. Use traditional frame chaining to
- // unwind the stack.
- //
- PointerSize = DbgGetTargetPointerSize(Context);
- DbgGetStackRegisters(Context, Registers, &StackPointer, &BasePointer);
- switch (Context->MachineType) {
- case MACHINE_TYPE_X86:
- //
- // If the instruction pointer was supplied, check the contents of the
- // instruction against the standard prologue. If they're equal, then
- // set up the first stack frame to more accurately represent the
- // first stack frame that hasn't quite yet been created.
- //
- if (Pc != 0) {
- Status = DbgReadMemory(Context,
- TRUE,
- Pc,
- X86_FUNCTION_PROLOGUE_LENGTH,
- X86InstructionContents,
- &BytesRead);
- if ((Status == 0) &&
- (BytesRead == X86_FUNCTION_PROLOGUE_LENGTH)) {
- for (ByteIndex = 0;
- ByteIndex < X86_FUNCTION_PROLOGUE_LENGTH;
- ByteIndex += 1) {
- if (X86InstructionContents[ByteIndex] !=
- DbgX86FunctionPrologue[ByteIndex]) {
- break;
- }
- }
- //
- // If the for loop made it all the way through without breaking,
- // a function prologue is about to execute. The base pointer is
- // in the stack pointer, and is about to be pushed, so parse the
- // first frame keeping that in mind. The return address was the
- // most recent thing pushed on the stack.
- //
- if (ByteIndex == X86_FUNCTION_PROLOGUE_LENGTH) {
- Frame->FramePointer = StackPointer + PointerSize;
- Frame->ReturnAddress = 0;
- Status = DbgReadMemory(Context,
- TRUE,
- StackPointer,
- PointerSize,
- &(Frame->ReturnAddress),
- &BytesRead);
- if ((Status != 0) || (BytesRead != PointerSize)) {
- if (Status == 0) {
- Status = EINVAL;
- }
- goto StackUnwindEnd;
- }
- }
- }
- }
- //
- // Stop if the base pointer is zero.
- //
- if (BasePointer == 0) {
- Status = EOF;
- goto StackUnwindEnd;
- }
- //
- // Store the base pointer for the current frame.
- //
- Frame->FramePointer = BasePointer;
- //
- // From the base pointer, the next two pointers in memory are the
- // next base pointer and then the return address.
- //
- Status = DbgReadMemory(Context,
- TRUE,
- BasePointer,
- PointerSize * 2,
- WorkingBuffer,
- &BytesRead);
- if ((Status != 0) || (BytesRead != (PointerSize * 2))) {
- if (Status == 0) {
- Status = EINVAL;
- }
- goto StackUnwindEnd;
- }
- BasePointer = 0;
- RtlCopyMemory(&BasePointer, WorkingBuffer, PointerSize);
- Frame->ReturnAddress = 0;
- RtlCopyMemory(&(Frame->ReturnAddress),
- (PUCHAR)WorkingBuffer + PointerSize,
- PointerSize);
- //
- // Update the registers.
- //
- Registers->X86.Eip = Frame->ReturnAddress;
- Registers->X86.Esp = Registers->X86.Ebp;
- Registers->X86.Ebp = BasePointer;
- break;
- case MACHINE_TYPE_ARM:
- //
- // Stop if the base pointer is zero.
- //
- if (BasePointer == 0) {
- Status = EOF;
- goto StackUnwindEnd;
- }
- //
- // The newer AAPCS calling convention sets up the frames where
- // *(fp-4) is next frame pointer, and *fp is the return address.
- // Store the base pointer for the current frame.
- //
- Frame->FramePointer = BasePointer;
- Frame->ReturnAddress = 0;
- //
- // Read in just below the base of the frame to get the return
- // address and next frame address.
- //
- Status = DbgReadMemory(Context,
- TRUE,
- BasePointer - PointerSize,
- PointerSize * 2,
- WorkingBuffer,
- &BytesRead);
- if ((Status != 0) || (BytesRead != (PointerSize * 2))) {
- if (Status == 0) {
- Status = EINVAL;
- }
- goto StackUnwindEnd;
- }
- //
- // Store the return address for this function, then follow the base
- // pointer to get the base pointer for the calling function.
- //
- RtlCopyMemory(&(Frame->ReturnAddress),
- (PUCHAR)WorkingBuffer + PointerSize,
- PointerSize);
- Registers->Arm.R13Sp = BasePointer;
- BasePointer = 0;
- RtlCopyMemory(&BasePointer, WorkingBuffer, PointerSize);
- Registers->Arm.R15Pc = Frame->ReturnAddress;
- if ((Registers->Arm.R15Pc & ARM_THUMB_BIT) != 0) {
- Registers->Arm.R7 = BasePointer;
- Registers->Arm.Cpsr |= PSR_FLAG_THUMB;
- } else {
- Registers->Arm.R11Fp = BasePointer;
- Registers->Arm.Cpsr &= ~PSR_FLAG_THUMB;
- }
- break;
- default:
- assert(FALSE);
- Status = EINVAL;
- }
- StackUnwindEnd:
- return Status;
- }
- INT
- DbgPrintCallStack (
- PDEBUGGER_CONTEXT Context,
- PREGISTERS_UNION Registers,
- BOOL PrintFrameNumbers
- )
- /*++
- Routine Description:
- This routine prints a call stack starting with the given registers.
- Arguments:
- Context - Supplies a pointer to the application context.
- Registers - Supplies an optional pointer to the registers to use when
- unwinding.
- PrintFrameNumbers - Supplies a boolean indicating whether or not frame
- numbers should be printed to the left of every frame.
- Return Value:
- 0 on success.
- Returns an error code on failure.
- --*/
- {
- ULONGLONG CallSite;
- ULONG FrameCount;
- ULONG FrameIndex;
- PSTACK_FRAME Frames;
- REGISTERS_UNION LocalRegisters;
- INT Result;
- Frames = NULL;
- if (Registers == NULL) {
- assert(Context->CurrentEvent.Type == DebuggerEventBreak);
- memcpy(&LocalRegisters,
- &(Context->CurrentEvent.BreakNotification.Registers),
- sizeof(REGISTERS_UNION));
- Registers = &LocalRegisters;
- }
- //
- // Initialize the call site with the current instruction pointer.
- //
- CallSite = DbgGetPc(Context, Registers);
- //
- // Allocate the call stack frames buffer.
- //
- Frames = malloc(sizeof(STACK_FRAME) * MAX_CALL_STACK);
- if (Frames == NULL) {
- DbgOut("Failed to allocate memory for call stack buffer.\n");
- Result = ENOMEM;
- goto PrintCallStackEnd;
- }
- FrameCount = MAX_CALL_STACK;
- Result = DbgGetCallStack(Context, Registers, Frames, &FrameCount);
- if (Result != 0) {
- DbgOut("Error: Failed to get call stack: %s.\n", strerror(Result));
- }
- //
- // Print the column headings.
- //
- if (PrintFrameNumbers != FALSE) {
- DbgOut("No ");
- }
- DbgOut("Frame RetAddr Call Site\n");
- for (FrameIndex = 0; FrameIndex < FrameCount; FrameIndex += 1) {
- if (PrintFrameNumbers != FALSE) {
- DbgOut("%2d ", FrameIndex);
- }
- DbgOut("%08I64x %08I64x ",
- Frames[FrameIndex].FramePointer,
- Frames[FrameIndex].ReturnAddress);
- //
- // Attempt to print the call site location with symbols. If nothing was
- // printed, fall back to printing the raw address.
- //
- DbgPrintAddressSymbol(Context, CallSite);
- DbgOut("\n");
- //
- // The next stack frame's call site is this frame's return address. Set
- // up the variables and loop again.
- //
- CallSite = Frames[FrameIndex].ReturnAddress;
- }
- Result = 0;
- PrintCallStackEnd:
- if (Frames != NULL) {
- free(Frames);
- }
- return Result;
- }
- INT
- DbgGetTargetInformation (
- PDEBUGGER_CONTEXT Context,
- PDEBUG_TARGET_INFORMATION TargetInformation,
- ULONG TargetInformationSize
- )
- /*++
- Routine Description:
- This routine returns information about the machine being debugged.
- Arguments:
- Context - Supplies a pointer to the application context.
- TargetInformation - Supplies a pointer where the target information will
- be returned.
- TargetInformationSize - Supplies the size of the target information buffer.
- This must be the size of a debug target information structure.
- Return Value:
- 0 on success.
- Returns an error code on failure.
- --*/
- {
- if ((TargetInformation == NULL) ||
- (TargetInformationSize != sizeof(DEBUG_TARGET_INFORMATION))) {
- return ENOSPC;
- }
- RtlZeroMemory(TargetInformation, sizeof(DEBUG_TARGET_INFORMATION));
- TargetInformation->MachineType = Context->MachineType;
- return 0;
- }
- ULONG
- DbgGetTargetPointerSize (
- PDEBUGGER_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine returns the size of a pointer on the target machine, in bytes.
- Arguments:
- Context - Supplies a pointer to the application context.
- Return Value:
- The size of a pointer on the target system, in bytes.
- --*/
- {
- ULONG PointerSize;
- switch (Context->MachineType) {
- case MACHINE_TYPE_X86:
- case MACHINE_TYPE_ARM:
- PointerSize = sizeof(ULONG);
- break;
- default:
- PointerSize = 0;
- assert(FALSE);
- break;
- }
- return PointerSize;
- }
- VOID
- DbgGetStackRegisters (
- PDEBUGGER_CONTEXT Context,
- PREGISTERS_UNION Registers,
- PULONGLONG StackPointer,
- PULONGLONG FramePointer
- )
- /*++
- Routine Description:
- This routine returns the stack and/or frame pointer registers from a
- given registers union.
- Arguments:
- Context - Supplies a pointer to the application context.
- Registers - Supplies a pointer to the filled out registers union.
- StackPointer - Supplies an optional pointer where the stack register value
- will be returned.
- FramePointer - Supplies an optional pointer where teh stack frame base
- register value will be returned.
- Return Value:
- None.
- --*/
- {
- ULONGLONG FrameValue;
- ULONGLONG StackValue;
- switch (Context->MachineType) {
- case MACHINE_TYPE_X86:
- StackValue = Registers->X86.Esp;
- FrameValue = Registers->X86.Ebp;
- break;
- case MACHINE_TYPE_ARM:
- StackValue = Registers->Arm.R13Sp;
- if ((Registers->Arm.Cpsr & PSR_FLAG_THUMB) != 0) {
- FrameValue = Registers->Arm.R7;
- } else {
- FrameValue = Registers->Arm.R11Fp;
- }
- break;
- default:
- assert(FALSE);
- StackValue = 0;
- FrameValue = 0;
- break;
- }
- if (StackPointer != NULL) {
- *StackPointer = StackValue;
- }
- if (FramePointer != NULL) {
- *FramePointer = FrameValue;
- }
- return;
- }
- ULONGLONG
- DbgGetPc (
- PDEBUGGER_CONTEXT Context,
- PREGISTERS_UNION Registers
- )
- /*++
- Routine Description:
- This routine returns the value of the program counter (instruction pointer)
- register in the given registers union.
- Arguments:
- Context - Supplies a pointer to the application context.
- Registers - Supplies an optional pointer to the filled out registers union.
- If NULL, then the registers from the current frame will be used.
- Return Value:
- Returns the instruction pointer member from the given registers.
- --*/
- {
- ULONGLONG Value;
- if (Registers == NULL) {
- Registers = &(Context->FrameRegisters);
- }
- switch (Context->MachineType) {
- case MACHINE_TYPE_X86:
- Value = Registers->X86.Eip;
- break;
- case MACHINE_TYPE_ARM:
- Value = Registers->Arm.R15Pc;
- break;
- default:
- assert(FALSE);
- Value = 0;
- break;
- }
- return Value;
- }
- VOID
- DbgSetPc (
- PDEBUGGER_CONTEXT Context,
- PREGISTERS_UNION Registers,
- ULONGLONG Value
- )
- /*++
- Routine Description:
- This routine sets the value of the program counter (instruction pointer)
- register in the given registers union.
- Arguments:
- Context - Supplies a pointer to the application context.
- Registers - Supplies an optional pointer to the filled out registers union.
- If NULL, then the registers from the current frame will be used.
- Value - Supplies the new value to set.
- Return Value:
- None.
- --*/
- {
- if (Registers == NULL) {
- Registers = &(Context->FrameRegisters);
- }
- switch (Context->MachineType) {
- case MACHINE_TYPE_X86:
- Registers->X86.Eip = Value;
- break;
- case MACHINE_TYPE_ARM:
- Registers->Arm.R15Pc = Value;
- break;
- default:
- assert(FALSE);
- break;
- }
- return;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- BOOL
- DbgpKdContinue (
- VOID
- )
- /*++
- Routine Description:
- This routine sends the "go" command to the target, signaling to continue
- execution.
- Arguments:
- None.
- Return Value:
- Returns TRUE if successful, or FALSE if there was an error.
- --*/
- {
- BOOL Result;
- DbgTxPacket.Header.Command = DbgCommandGo;
- DbgTxPacket.Header.PayloadSize = 0;
- Result = DbgpKdSendPacket(&DbgTxPacket);
- if (Result == FALSE) {
- DbgOut("Error sending go command.\n");
- }
- return Result;
- }
- BOOL
- DbgpKdSetRegisters (
- PREGISTERS_UNION Registers
- )
- /*++
- Routine Description:
- This routine sets the registers of the kernel debugging target.
- Arguments:
- Registers - Supplies a pointer to the registers to set. All register values
- will be written.
- Return Value:
- Returns TRUE if successful, or FALSE if there was an error.
- --*/
- {
- BOOL Result;
- DbgTxPacket.Header.Command = DbgCommandSetRegisters;
- DbgTxPacket.Header.PayloadSize = sizeof(REGISTERS_UNION);
- RtlCopyMemory(DbgTxPacket.Payload, Registers, sizeof(REGISTERS_UNION));
- Result = DbgpKdSendPacket(&DbgTxPacket);
- if (Result == FALSE) {
- DbgOut("Error setting registers.\n");
- }
- return Result;
- }
- BOOL
- DbgpKdGetSpecialRegisters (
- PSPECIAL_REGISTERS_UNION SpecialRegisters
- )
- /*++
- Routine Description:
- This routine gets the special registers from the target.
- Arguments:
- SpecialRegisters - Supplies a pointer where the registers will be returned
- on success.
- Return Value:
- Returns TRUE if successful, or FALSE if there was an error.
- --*/
- {
- BOOL Result;
- DbgTxPacket.Header.Command = DbgCommandGetSpecialRegisters;
- DbgTxPacket.Header.PayloadSize = 0;
- Result = DbgpKdSendPacket(&DbgTxPacket);
- if (Result == FALSE) {
- DbgOut("Error setting registers.\n");
- }
- Result = DbgpKdReceivePacket(&DbgRxPacket, 0, NULL);
- if (Result == FALSE) {
- goto KdGetSpecialRegistersEnd;
- }
- if (DbgRxPacket.Header.Command != DbgCommandReturnSpecialRegisters) {
- DbgOut("Error: Got packet %d, expected special registers return.\n",
- DbgRxPacket.Header.Command);
- Result = FALSE;
- goto KdGetSpecialRegistersEnd;
- }
- if (DbgRxPacket.Header.PayloadSize != sizeof(SPECIAL_REGISTERS_UNION)) {
- DbgOut("Error: Unexpected payload size %d. Expected %d.\n",
- DbgRxPacket.Header.PayloadSize,
- sizeof(SPECIAL_REGISTERS_UNION));
- Result = FALSE;
- goto KdGetSpecialRegistersEnd;
- }
- RtlCopyMemory(SpecialRegisters,
- DbgRxPacket.Payload,
- sizeof(SPECIAL_REGISTERS_UNION));
- Result = TRUE;
- KdGetSpecialRegistersEnd:
- return Result;
- }
- BOOL
- DbgpKdSetSpecialRegisters (
- PSET_SPECIAL_REGISTERS Command
- )
- /*++
- Routine Description:
- This routine sets the special registers from the target.
- Arguments:
- Command - Supplies a pointer to the details of the set special registers
- command.
- Return Value:
- Returns TRUE if successful, or FALSE if there was an error.
- --*/
- {
- BOOL Result;
- DbgTxPacket.Header.Command = DbgCommandSetSpecialRegisters;
- DbgTxPacket.Header.PayloadSize = sizeof(SET_SPECIAL_REGISTERS);
- RtlCopyMemory(DbgTxPacket.Payload, Command, sizeof(SET_SPECIAL_REGISTERS));
- Result = DbgpKdSendPacket(&DbgTxPacket);
- if (Result == FALSE) {
- DbgOut("Error: Failed to send set special registers.\n");
- }
- return Result;
- }
- BOOL
- DbgpKdSingleStep (
- VOID
- )
- /*++
- Routine Description:
- This routine steps one instruction in the kernel debugging target.
- Arguments:
- None.
- Return Value:
- Returns TRUE if successful, or FALSE if there was an error.
- --*/
- {
- BOOL Result;
- DbgTxPacket.Header.Command = DbgCommandSingleStep;
- DbgTxPacket.Header.PayloadSize = 0;
- Result = DbgpKdSendPacket(&DbgTxPacket);
- if (Result == FALSE) {
- DbgOut("Error sending single step command.\n");
- }
- return Result;
- }
- BOOL
- DbgpKdWaitForEvent (
- PDEBUGGER_EVENT Event
- )
- /*++
- Routine Description:
- This routine gets an event from the target, such as a break event or other
- exception.
- Arguments:
- Event - Supplies a pointer where the event details will be returned.
- Return Value:
- Returns TRUE if successful, or FALSE if there was an error.
- --*/
- {
- BOOL Result;
- ULONG Retries;
- BOOL Return;
- BOOL TimeoutOccurred;
- Retries = 5;
- Return = FALSE;
- while (Return == FALSE) {
- //
- // Attempt to get a packet from the target.
- //
- while (TRUE) {
- //
- // If the connection was reset, attempt to reinitialize.
- //
- if (DbgKdConnectionReset != FALSE) {
- Event->Type = DebuggerEventShutdown;
- Event->ShutdownNotification.ShutdownType =
- ShutdownTypeSynchronizationLost;
- Result = TRUE;
- goto KdWaitForEventEnd;
- }
- Result = DbgpKdReceivePacket(&DbgRxPacket,
- DEBUG_USER_POLL_MILLISECONDS,
- &TimeoutOccurred);
- //
- // If the packet failed to be received from something other than
- // a timeout, bail out.
- //
- if ((Result == FALSE) && (TimeoutOccurred == FALSE)) {
- if (DbgKdConnectionReset != FALSE) {
- continue;
- }
- DbgOut("Communication Error.\n");
- goto KdWaitForEventEnd;
- }
- //
- // If the packet was successfully received, break out of this loop.
- //
- if (Result != FALSE) {
- break;
- }
- //
- // A packet could not be received due to a timeout. Check to see if
- // a break-in packet should be sent, and continue.
- //
- if ((DbgBreakInDesired != FALSE) &&
- (DbgBreakInRequestSent == FALSE)) {
- DbgTxPacket.Header.Command = DbgBreakRequest;
- DbgTxPacket.Header.PayloadSize = 0;
- Result = DbgpKdSendPacket(&DbgTxPacket);
- if (Result == FALSE) {
- if (DbgKdConnectionReset != FALSE) {
- continue;
- }
- DbgOut("Error: Could not send break request.\n");
- Retries -= 1;
- if (Retries == 0) {
- return FALSE;
- }
- }
- DbgBreakInRequestSent = TRUE;
- }
- }
- Return = TRUE;
- switch (DbgRxPacket.Header.Command) {
- //
- // A breakpoint has been reached.
- //
- case DbgBreakNotification:
- Event->Type = DebuggerEventBreak;
- RtlCopyMemory(&(Event->BreakNotification),
- DbgRxPacket.Payload,
- sizeof(BREAK_NOTIFICATION));
- DbgBreakInDesired = FALSE;
- DbgBreakInRequestSent = FALSE;
- break;
- //
- // A shutdown occurred.
- //
- case DbgShutdownNotification:
- Event->Type = DebuggerEventShutdown;
- RtlCopyMemory(&(Event->ShutdownNotification),
- DbgRxPacket.Payload,
- sizeof(SHUTDOWN_NOTIFICATION));
- break;
- //
- // The profiler sent some data. Because the profiler notification has
- // a variable data array, the event only stores a pointer to a global
- // buffer for the current profiler notification.
- //
- case DbgProfilerNotification:
- Event->Type = DebuggerEventProfiler;
- assert(DbgRxPacket.Header.PayloadSize <= DEBUG_PAYLOAD_SIZE);
- Event->ProfilerNotification =
- (PPROFILER_NOTIFICATION)(DbgRxPacket.Payload);
- break;
- default:
- //
- // The target sent an unknown command.
- //
- DbgOut("Unknown event received: 0x%x\n",
- DbgRxPacket.Header.Command);
- Return = FALSE;
- }
- if (Return != FALSE) {
- break;
- }
- }
- Result = TRUE;
- KdWaitForEventEnd:
- return Result;
- }
- BOOL
- DbgpKdRangeStep (
- PRANGE_STEP RangeStep
- )
- /*++
- Routine Description:
- This routine continues execution until a range of execution addresses is
- reached.
- Arguments:
- RangeStep - Supplies a pointer to the range to go to.
- Return Value:
- Returns TRUE if successful, or FALSE on failure.
- --*/
- {
- BOOL Result;
- DbgTxPacket.Header.Command = DbgCommandRangeStep;
- DbgTxPacket.Header.PayloadSize = sizeof(RANGE_STEP);
- RtlCopyMemory(DbgTxPacket.Payload, RangeStep, sizeof(RANGE_STEP));
- Result = DbgpKdSendPacket(&DbgTxPacket);
- if (Result == FALSE) {
- DbgOut("Error sending single step command.\n");
- }
- return Result;
- }
- BOOL
- DbgpKdSwitchProcessors (
- ULONG ProcessorNumber
- )
- /*++
- Routine Description:
- This routine switches the debugger to another processor.
- Arguments:
- ProcessorNumber - Supplies the processor number to switch to.
- Return Value:
- Returns TRUE if successful, or FALSE if there was no change.
- --*/
- {
- PSWITCH_PROCESSOR_REQUEST Request;
- BOOL Result;
- //
- // Send the switch command.
- //
- DbgTxPacket.Header.Command = DbgCommandSwitchProcessor;
- Request = (PSWITCH_PROCESSOR_REQUEST)(DbgTxPacket.Payload);
- DbgTxPacket.Header.PayloadSize = sizeof(SWITCH_PROCESSOR_REQUEST);
- RtlZeroMemory(Request, sizeof(SWITCH_PROCESSOR_REQUEST));
- Request->ProcessorNumber = ProcessorNumber;
- Result = DbgpKdSendPacket(&DbgTxPacket);
- return Result;
- }
- BOOL
- DbgpKdGetLoadedModuleList (
- PMODULE_LIST_HEADER *ModuleList
- )
- /*++
- Routine Description:
- This routine retrieves the list of loaded binaries from the kernel
- debugging target.
- Arguments:
- ModuleList - Supplies a pointer where a pointer to the loaded module header
- and subsequent array of entries will be returned. It is the caller's
- responsibility to free this allocated memory when finished.
- Return Value:
- Returns TRUE on success, or FALSE on failure.
- --*/
- {
- ULONG AllocationSize;
- PMODULE_LIST_HEADER List;
- ULONG ModuleCount;
- PMODULE_LIST_HEADER ModuleListHeader;
- ULONG Offset;
- PLOADED_MODULE_ENTRY PacketModuleEntry;
- BOOL Result;
- List = NULL;
- //
- // Request the loaded modules list header to determine how many modules
- // are loaded in the target.
- //
- DbgTxPacket.Header.Command = DbgModuleListEntriesRequest;
- DbgTxPacket.Header.PayloadSize = 0;
- Result = DbgpKdSendPacket(&DbgTxPacket);
- if (Result == FALSE) {
- DbgOut("Failed to send list entries request.\n");
- goto KdGetLoadedModuleListEnd;
- }
- Result = DbgpKdReceivePacket(&DbgRxPacket, 0, NULL);
- if ((Result == FALSE) ||
- (DbgRxPacket.Header.Command != DbgModuleListHeader)) {
- DbgOut("Failed to receive list header. Got %d %d\n",
- Result,
- DbgRxPacket.Header.Command);
- Result = FALSE;
- goto KdGetLoadedModuleListEnd;
- }
- ModuleListHeader = (PMODULE_LIST_HEADER)DbgRxPacket.Payload;
- //
- // Allocate space for the header and no entries.
- //
- ModuleCount = ModuleListHeader->ModuleCount;
- AllocationSize = sizeof(MODULE_LIST_HEADER);
- List = malloc(AllocationSize);
- if (List == NULL) {
- DbgOut("Error: Failed to allocate %d bytes for module list.\n",
- AllocationSize);
- Result = FALSE;
- goto KdGetLoadedModuleListEnd;
- }
- RtlZeroMemory(List, AllocationSize);
- RtlCopyMemory(List, ModuleListHeader, sizeof(MODULE_LIST_HEADER));
- Offset = sizeof(MODULE_LIST_HEADER);
- PacketModuleEntry = (PLOADED_MODULE_ENTRY)(DbgRxPacket.Payload);
- //
- // Get all modules. For each module, attempt to match it to an existing
- // loaded module. If none is found, attempt to load the module.
- //
- while (ModuleCount != 0) {
- Result = DbgpKdReceivePacket(&DbgRxPacket, 0, NULL);
- if ((Result == FALSE) ||
- (DbgRxPacket.Header.Command != DbgModuleListEntry) ||
- (PacketModuleEntry->StructureSize < sizeof(LOADED_MODULE_ENTRY))) {
- DbgOut("Failed to get module list. Got %x %x %x\n",
- Result,
- DbgRxPacket.Header.Command,
- PacketModuleEntry->StructureSize);
- Result = FALSE;
- goto KdGetLoadedModuleListEnd;
- }
- AllocationSize += PacketModuleEntry->StructureSize;
- List = realloc(List, AllocationSize);
- if (List == NULL) {
- DbgOut("Error: Failed to realloc %d bytes (for entry of %d "
- "bytes).\n",
- AllocationSize,
- PacketModuleEntry->StructureSize);
- Result = FALSE;
- goto KdGetLoadedModuleListEnd;
- }
- RtlCopyMemory((PUCHAR)List + Offset,
- DbgRxPacket.Payload,
- PacketModuleEntry->StructureSize);
- Offset += PacketModuleEntry->StructureSize;
- ModuleCount -= 1;
- }
- KdGetLoadedModuleListEnd:
- if (Result == FALSE) {
- if (List != NULL) {
- free(List);
- List = NULL;
- }
- }
- *ModuleList = List;
- return Result;
- }
- BOOL
- DbgpKdReadWriteMemory (
- BOOL WriteOperation,
- BOOL VirtualMemory,
- ULONGLONG Address,
- PVOID Buffer,
- ULONG BufferSize,
- PULONG BytesCompleted
- )
- /*++
- Routine Description:
- This routine retrieves or writes to the kernel target's memory.
- Arguments:
- WriteOperation - Supplies a flag indicating whether this is a read
- operation (FALSE) or a write operation (TRUE).
- VirtualMemory - Supplies a flag indicating whether the memory accessed
- should be virtual or physical.
- Address - Supplies the address to read from or write to in the target's
- memory.
- Buffer - Supplies a pointer to the buffer where the memory contents will be
- returned for read operations, or supplies a pointer to the values to
- write to memory on for write operations.
- BufferSize - Supplies the size of the supplied buffer, in bytes.
- BytesCompleted - Supplies a pointer that receive the number of bytes that
- were actually read from or written to the target.
- Return Value:
- Returns TRUE if the operation was successful.
- FALSE if there was an error.
- --*/
- {
- PSTR AccessString;
- ULONG BytesThisRound;
- ULONG MaxSize;
- PBYTE PacketData;
- PMEMORY_REQUEST Request;
- PMEMORY_CONTENTS Response;
- BOOL Result;
- PWRITE_REQUEST_ACKNOWLEDGEMENT WriteAcknowledge;
- *BytesCompleted = 0;
- Result = TRUE;
- if (DbgKdPrintMemoryAccesses != FALSE) {
- AccessString = "Read";
- if (WriteOperation != FALSE) {
- AccessString = "Write";
- }
- printf("%s %d bytes at address %08I64x.\n",
- AccessString,
- BufferSize,
- Address);
- }
- //
- // Currently only virtual reads are supported.
- //
- if (VirtualMemory == FALSE) {
- Result = FALSE;
- goto KdReadWriteMemoryEnd;
- }
- //
- // Calculate the maximum amount of data that can be transferred in one
- // packet. If the request is larger than that, it will need to be broken up.
- //
- if (WriteOperation != FALSE) {
- MaxSize = DEBUG_PACKET_SIZE - sizeof(DEBUG_PACKET_HEADER) -
- sizeof(MEMORY_REQUEST);
- } else {
- MaxSize = DEBUG_PACKET_SIZE - sizeof(DEBUG_PACKET_HEADER) -
- sizeof(MEMORY_CONTENTS);
- }
- Request = (PMEMORY_REQUEST)DbgTxPacket.Payload;
- while (*BytesCompleted < BufferSize) {
- //
- // If the request is too big for one packet, cap it.
- //
- if (BufferSize - *BytesCompleted > MaxSize) {
- BytesThisRound = MaxSize;
- } else {
- BytesThisRound = BufferSize - *BytesCompleted;
- }
- DbgTxPacket.Header.PayloadSize = sizeof(MEMORY_REQUEST);
- if (WriteOperation != FALSE) {
- DbgTxPacket.Header.Command = DbgMemoryWriteVirtual;
- DbgTxPacket.Header.PayloadSize += BytesThisRound;
- memcpy(Request + 1, Buffer + *BytesCompleted, BytesThisRound);
- } else {
- DbgTxPacket.Header.Command = DbgMemoryReadVirtual;
- }
- Request->Address = Address;
- Request->Size = BytesThisRound;
- //
- // Send the request packet and get the response.
- //
- Result = DbgpKdSendPacket(&DbgTxPacket);
- if (Result == FALSE) {
- goto KdReadWriteMemoryEnd;
- }
- Result = DbgpKdReceivePacket(&DbgRxPacket, 0, NULL);
- if (Result == FALSE) {
- goto KdReadWriteMemoryEnd;
- }
- //
- // Verify the packet that was received, and bail out if it was
- // unexpected.
- //
- if (DbgRxPacket.Header.Command == DbgInvalidCommand) {
- DbgOut("Error: Target rejected the memory request!\n");
- Result = FALSE;
- goto KdReadWriteMemoryEnd;
- }
- //
- // Handle the result from a write operation.
- //
- if (WriteOperation != FALSE) {
- if ((DbgRxPacket.Header.Command != DbgMemoryWriteAcknowledgement) ||
- (DbgRxPacket.Header.PayloadSize !=
- sizeof(WRITE_REQUEST_ACKNOWLEDGEMENT))) {
- DbgOut("Error: Received garbage command %d from target!\n",
- DbgRxPacket.Header.Command);
- Result = FALSE;
- goto KdReadWriteMemoryEnd;
- }
- WriteAcknowledge =
- (PWRITE_REQUEST_ACKNOWLEDGEMENT)DbgRxPacket.Payload;
- *BytesCompleted += WriteAcknowledge->BytesWritten;
- BufferSize -= WriteAcknowledge->BytesWritten;
- Address += WriteAcknowledge->BytesWritten;
- //
- // If the target didn't write as much as requested, stop asking.
- //
- if (WriteAcknowledge->BytesWritten != BytesThisRound) {
- break;
- }
- //
- // Handle the result from a read operation.
- //
- } else {
- if ((DbgRxPacket.Header.Command != DbgMemoryContents) ||
- (DbgRxPacket.Header.PayloadSize < sizeof(MEMORY_CONTENTS))) {
- DbgOut("Error: Received garbage command %d from target!\n",
- DbgRxPacket.Header.Command);
- Result = FALSE;
- goto KdReadWriteMemoryEnd;
- }
- //
- // Get the data from the packet and put it in the buffer.
- //
- Response = (PMEMORY_CONTENTS)DbgRxPacket.Payload;
- PacketData = (PBYTE)DbgRxPacket.Payload + sizeof(MEMORY_CONTENTS);
- RtlCopyMemory(Buffer + *BytesCompleted, PacketData, Response->Size);
- *BytesCompleted += Response->Size;
- Address += Response->Size;
- //
- // If the target didn't return as much data as requested, then don't
- // request any more.
- //
- if (Response->Size != BytesThisRound) {
- break;
- }
- }
- }
- KdReadWriteMemoryEnd:
- return Result;
- }
- BOOL
- DbgpKdReboot (
- DEBUG_REBOOT_TYPE RebootType
- )
- /*++
- Routine Description:
- This routine forcefully reboots the target machine.
- Arguments:
- RebootType - Supplies the reboot type.
- Return Value:
- Returns TRUE if successful, or FALSE if there was no change.
- --*/
- {
- PDEBUG_REBOOT_REQUEST Request;
- BOOL Result;
- //
- // Send the reboot command.
- //
- DbgTxPacket.Header.Command = DbgCommandReboot;
- Request = (PDEBUG_REBOOT_REQUEST)(DbgTxPacket.Payload);
- DbgTxPacket.Header.PayloadSize = sizeof(DEBUG_REBOOT_REQUEST);
- RtlZeroMemory(Request, sizeof(DEBUG_REBOOT_REQUEST));
- Request->ResetType = RebootType;
- Result = DbgpKdSendPacket(&DbgTxPacket);
- return Result;
- }
- BOOL
- DbgpKdSendPacket (
- PDEBUG_PACKET Packet
- )
- /*++
- Routine Description:
- This routine sends a packet across the wire to the debugging target when
- connected to a kernel.
- Arguments:
- Packet - Supplies a pointer to the debug packet. The checksum field will be
- calculated in this function.
- Return Value:
- TRUE if successful, FALSE on error.
- --*/
- {
- DEBUG_PACKET_HEADER Acknowledge;
- USHORT Checksum;
- ULONG HeaderSize;
- ULONG Retries;
- BOOL Status;
- BOOL TimeoutOccurred;
- HeaderSize = sizeof(DEBUG_PACKET_HEADER);
- Status = FALSE;
- if (Packet->Header.PayloadSize > DEBUG_PACKET_SIZE - HeaderSize) {
- DbgOut("Error: Oversized packet attempting to be sent!\n");
- return FALSE;
- }
- Packet->Header.Magic = DEBUG_PACKET_MAGIC;
- Packet->Header.PayloadSizeComplement = ~(Packet->Header.PayloadSize);
- Packet->Header.Checksum = 0;
- Checksum = DbgpKdCalculateChecksum(Packet,
- Packet->Header.PayloadSize + HeaderSize);
- Packet->Header.Checksum = Checksum;
- Retries = 10;
- while (Retries > 0) {
- //
- // Send the packet, then attempt to receive the response acknowledge.
- //
- Status = DbgpKdSendBytes(Packet,
- HeaderSize + Packet->Header.PayloadSize);
- if (Status == FALSE) {
- DbgOut("Error: Unable to send packet!\n");
- break;
- }
- Status = DbgpKdReceivePacketHeader(&Acknowledge,
- 5000,
- &TimeoutOccurred);
- if ((Status != FALSE) &&
- (Acknowledge.Command == DbgPacketAcknowledge)) {
- Status = TRUE;
- break;
- }
- //
- // If nonsense seems to have been received, start over.
- //
- Retries -= 1;
- Status = FALSE;
- }
- return Status;
- }
- BOOL
- DbgpKdReceivePacket (
- PDEBUG_PACKET Packet,
- ULONG TimeoutMilliseconds,
- PBOOL TimeoutOccurred
- )
- /*++
- Routine Description:
- This routine receives a packet across the wire from the host when connected
- to a kernel.
- Arguments:
- Packet - Supplies a pointer to the buffer that will receive the debug
- packet.
- TimeoutMilliseconds - Supplies the number of milliseconds to wait for a
- packet before giving up. Once a packet header has been received, the
- function will block until an entire packet is received.
- TimeoutOccurred - Supplies an optional pointer to a boolean indicating that
- the timeout occurred before a header could be received.
- Return Value:
- TRUE if a packet was received.
- FALSE if a communication error occurred.
- --*/
- {
- DEBUG_COMPLETE_ACKNOWLEDGE_PACKET Acknowledge;
- USHORT CalculatedChecksum;
- USHORT Checksum;
- USHORT HeaderChecksum;
- ULONG HeaderSize;
- PSTR LastCharacter;
- ULONG Retries;
- BOOL Status;
- HeaderSize = sizeof(DEBUG_PACKET_HEADER);
- Retries = 10;
- if (TimeoutOccurred != NULL) {
- *TimeoutOccurred = FALSE;
- }
- while (TRUE) {
- Status = DbgpKdReceivePacketHeader(&(Packet->Header),
- TimeoutMilliseconds,
- TimeoutOccurred);
- if (Status == FALSE) {
- break;
- }
- //
- // If the packet has a payload, get that as well.
- //
- if (Packet->Header.PayloadSize != 0) {
- Status = DbgpKdReceiveBytes(&(Packet->Payload),
- Packet->Header.PayloadSize);
- if (Status == FALSE) {
- DbgOut("Error: Unable to receive packet payload.\n");
- goto KdReceivePacketHeaderRetry;
- }
- }
- //
- // Ensure that the packet came across okay. The checksum field is not
- // included in the checksum calculation, so zero it out while
- // calculating.
- //
- HeaderChecksum = Packet->Header.Checksum;
- Packet->Header.Checksum = 0;
- CalculatedChecksum = DbgpKdCalculateChecksum(
- Packet,
- HeaderSize + Packet->Header.PayloadSize);
- Packet->Header.Checksum = HeaderChecksum;
- if (HeaderChecksum != CalculatedChecksum) {
- DbgOut("Error: Checksum mismatch on received packet!\n"
- "Calculated %x Header %x\n",
- CalculatedChecksum,
- HeaderChecksum);
- goto KdReceivePacketHeaderRetry;
- }
- //
- // Ignore spurious acknowledges.
- //
- if (Packet->Header.Command == DbgPacketAcknowledge) {
- DbgOut("Skipping spurious acknowledge.\n");
- continue;
- }
- //
- // The payload is okay. Send the acknowledge and break.
- //
- RtlZeroMemory(&Acknowledge, sizeof(DEBUG_COMPLETE_ACKNOWLEDGE_PACKET));
- Acknowledge.Header.Magic = DEBUG_PACKET_MAGIC;
- Acknowledge.Header.Command = DbgPacketAcknowledge;
- Acknowledge.Header.PayloadSize = sizeof(DEBUG_PACKET_ACKNOWLEDGE);
- Acknowledge.Header.PayloadSizeComplement =
- ~(Acknowledge.Header.PayloadSize);
- Acknowledge.Acknowledge.BreakInRequested = DbgBreakInDesired;
- Checksum = DbgpKdCalculateChecksum(
- &Acknowledge,
- sizeof(DEBUG_COMPLETE_ACKNOWLEDGE_PACKET));
- Acknowledge.Header.Checksum = Checksum;
- Status = DbgpKdSendBytes(&Acknowledge,
- sizeof(DEBUG_COMPLETE_ACKNOWLEDGE_PACKET));
- if (Status == FALSE) {
- goto KdReceivePacketHeaderRetry;
- }
- //
- // Handle certain events inline.
- //
- if (Packet->Header.Command == DbgPrintString) {
- LastCharacter = ((PCHAR)(Packet->Payload)) + DEBUG_PAYLOAD_SIZE - 1;
- //
- // Terminate the last character for safety, and print out the
- // string.
- //
- *LastCharacter = '\0';
- DbgOut("%s", Packet->Payload);
- continue;
- }
- Status = TRUE;
- break;
- KdReceivePacketHeaderRetry:
- if (Retries == 0) {
- Status = FALSE;
- break;
- }
- //
- // Ask the host to resend and loop.
- //
- DbgOut("Asking for Resend, %d retries.\n", Retries);
- Acknowledge.Header.Command = DbgPacketResend;
- Acknowledge.Header.PayloadSize = 0;
- Acknowledge.Header.PayloadSizeComplement =
- ~(Acknowledge.Header.PayloadSize);
- Checksum = DbgpKdCalculateChecksum(&Acknowledge,
- sizeof(DEBUG_PACKET_HEADER));
- Acknowledge.Header.Checksum = Checksum;
- Status = DbgpKdSendBytes(&Acknowledge, sizeof(DEBUG_PACKET_HEADER));
- if (Status == FALSE) {
- break;
- }
- Retries -= 1;
- }
- return Status;
- }
- BOOL
- DbgpKdReceivePacketHeader (
- PDEBUG_PACKET_HEADER Packet,
- ULONG TimeoutMilliseconds,
- PBOOL TimeoutOccurred
- )
- /*++
- Routine Description:
- This routine receives a packet header across the wire from the host when
- connected to a kernel.
- Arguments:
- Packet - Supplies a pointer to the buffer that will receive the debug
- packet header.
- TimeoutMilliseconds - Supplies the number of milliseconds to wait for a
- packet before giving up. Once a packet header has been received, the
- function will block until an entire packet is received.
- TimeoutOccurred - Supplies an optional pointer to a boolean indicating that
- the timeout occurred before a header could be received.
- Return Value:
- TRUE if a packet was received.
- FALSE if a communication error occurred.
- --*/
- {
- ULONG BytesAvailable;
- ULONG HeaderSize;
- UCHAR Magic;
- ULONG Retries;
- BOOL Status;
- ULONG TimeWaited;
- HeaderSize = sizeof(DEBUG_PACKET_HEADER);
- if (TimeoutOccurred != NULL) {
- *TimeoutOccurred = FALSE;
- }
- Retries = 10;
- while (Retries != 0) {
- //
- // If a timeout is specified, ensure that at least a header's worth of
- // data is available. If the timeout expires, return.
- //
- if (TimeoutMilliseconds != 0) {
- TimeWaited = 0;
- while (TRUE) {
- BytesAvailable = CommReceiveBytesReady();
- if (BytesAvailable != 0) {
- break;
- }
- CommStall(15);
- TimeWaited += 15;
- if (TimeWaited >= TimeoutMilliseconds) {
- if (TimeoutOccurred != NULL) {
- *TimeoutOccurred = TRUE;
- }
- return FALSE;
- }
- }
- }
- //
- // Attempt to synchronize on the magic field.
- //
- Magic = 0;
- Status = DbgpKdReceiveBytes(&Magic, 1);
- if (Status == FALSE) {
- Retries -= 1;
- continue;
- }
- if (Magic != DEBUG_PACKET_MAGIC_BYTE1) {
- //
- // Check for a resynchronization byte, indicating the target is
- // new or confused. If the state is already reset, then this is
- // probably during the initial connect, where extra resync bytes
- // from the target are ignored.
- //
- if ((Magic == DEBUG_SYNCHRONIZE_TARGET) &&
- (DbgKdConnectionReset == FALSE)) {
- DbgKdConnectionReset = TRUE;
- Status = FALSE;
- break;
- }
- continue;
- }
- Magic = 0;
- Status = DbgpKdReceiveBytes(&Magic, 1);
- if (Status == FALSE) {
- Retries -= 1;
- continue;
- }
- if (Magic != DEBUG_PACKET_MAGIC_BYTE2) {
- continue;
- }
- //
- // Get the packet header. Sometimes this is all that's required.
- //
- Packet->Magic = DEBUG_PACKET_MAGIC;
- Status = DbgpKdReceiveBytes((PUCHAR)Packet + DEBUG_PACKET_MAGIC_SIZE,
- HeaderSize - DEBUG_PACKET_MAGIC_SIZE);
- if (Status == FALSE) {
- DbgOut("Error: Unable to receive packet header!\n");
- Retries -= 1;
- continue;
- }
- if ((USHORT)~(Packet->PayloadSize) != Packet->PayloadSizeComplement) {
- DbgOut("Resynchronizing due to payload size complement "
- "mismatch.\n");
- Retries -= 1;
- Status = FALSE;
- continue;
- }
- if (Packet->PayloadSize > DEBUG_PACKET_SIZE - HeaderSize) {
- DbgOut("Error: Oversized packet received. Command 0x%x, "
- "PayloadSize 0x%x.\n",
- Packet->Command,
- Packet->PayloadSize);
- Retries -= 1;
- Status = FALSE;
- continue;
- }
- Status = TRUE;
- break;
- }
- return Status;
- }
- INT
- DbgpKdSynchronize (
- VOID
- )
- /*++
- Routine Description:
- This routine synchronizes with the target machine, making sure it is ready
- to receive the connection request.
- Arguments:
- None.
- Return Value:
- 0 on success.
- Non-zero on error.
- --*/
- {
- BOOL Result;
- ULONG Retries;
- ULONG Status;
- UCHAR SynchronizeByte;
- ULONG TimeWaited;
- //
- // Check to see if the target has already sent a sync to the host.
- //
- while (CommReceiveBytesReady() != 0) {
- Result = DbgpKdReceiveBytes(&SynchronizeByte, sizeof(UCHAR));
- if (Result == FALSE) {
- Status = EPIPE;
- goto KdSynchronizeEnd;
- }
- if (SynchronizeByte == DEBUG_SYNCHRONIZE_TARGET) {
- Status = 0;
- goto KdSynchronizeEnd;
- }
- }
- Retries = 10;
- while (Retries > 0) {
- //
- // Send a little query.
- //
- SynchronizeByte = DEBUG_SYNCHRONIZE_HOST;
- Result = DbgpKdSendBytes(&SynchronizeByte, 1);
- if (Result == FALSE) {
- Status = EPIPE;
- continue;
- }
- //
- // Wait for a response.
- //
- TimeWaited = 0;
- while (TimeWaited < 5000) {
- if (CommReceiveBytesReady() != 0) {
- Result = DbgpKdReceiveBytes(&SynchronizeByte, 1);
- if (Result == FALSE) {
- Status = EPIPE;
- break;
- }
- if (SynchronizeByte == DEBUG_SYNCHRONIZE_TARGET) {
- Status = 0;
- goto KdSynchronizeEnd;
- }
- } else {
- CommStall(15);
- TimeWaited += 15;
- }
- }
- Retries -= 1;
- }
- Status = EPIPE;
- KdSynchronizeEnd:
- return Status;
- }
- USHORT
- DbgpKdCalculateChecksum (
- PVOID Data,
- ULONG DataLength
- )
- /*++
- Routine Description:
- This routine computes a checksum over a given length for kernel debug
- transport packets. It can handle both odd and even length data.
- Arguments:
- Data - Supplies a pointer to the data that is to be checksummed.
- DataLength - Supplies the length of the data buffer, in bytes.
- Return Value:
- Returns the checksum.
- --*/
- {
- USHORT Checksum;
- PUSHORT CurrentData;
- ULONG Index;
- ULONG ShortLength;
- Checksum = 0;
- Index = 0;
- //
- // Checksums are calculated by adding up a series of two-byte values.
- // Convert the pointer to a short pointer and divide bytes by 2 to get size
- // in shorts.
- //
- ShortLength = DataLength / 2;
- CurrentData = (PUSHORT)Data;
- while (Index < ShortLength) {
- Checksum += *CurrentData;
- CurrentData += 1;
- Index += 1;
- }
- //
- // If the data was an odd length, then there's one byte left to be added.
- // Add only that byte.
- //
- if ((ShortLength * 2) != DataLength) {
- Checksum += *((PUCHAR)CurrentData);
- }
- return Checksum;
- }
- BOOL
- DbgpKdReceiveBytes (
- PVOID Buffer,
- ULONG BytesToRead
- )
- /*++
- Routine Description:
- This routine receives a number of bytes from the debugger connection.
- Arguments:
- Buffer - Supplies a pointer to the buffer where the data should be returned.
- BytesToRead - Supplies the number of bytes that should be received into the
- buffer.
- Return Value:
- Returns TRUE on success, FALSE on failure.
- --*/
- {
- ULONG ByteIndex;
- PUCHAR Bytes;
- CHAR Character;
- ULONG Count;
- ULONG Index;
- BOOL NextEscaped;
- BOOL Status;
- NextEscaped = FALSE;
- Status = TRUE;
- Bytes = Buffer;
- while (BytesToRead != 0) {
- Status = CommReceive(Bytes, BytesToRead);
- if (Status == FALSE) {
- DbgOut("Failed to receive %d bytes.\n", BytesToRead);
- return Status;
- }
- if (DbgKdPrintRawBytes != FALSE) {
- DbgOut("RX: ");
- for (ByteIndex = 0; ByteIndex < BytesToRead; ByteIndex += 1) {
- DbgOut("%02X ", Bytes[ByteIndex]);
- }
- DbgOut("\nRX: ");
- for (ByteIndex = 0; ByteIndex < BytesToRead; ByteIndex += 1) {
- Character = Bytes[ByteIndex];
- if (!isprint(Character)) {
- Character = '.';
- }
- DbgOut("%02c ", Character);
- }
- DbgOut("\n");
- }
- //
- // If escaping is on, then remove any escape bytes found, and fix up
- // the escaped byte.
- //
- Count = 0;
- if (DbgKdEncodeBytes != FALSE) {
- //
- // If the last byte received was an escape, then unescape this
- // first byte.
- //
- if (NextEscaped != FALSE) {
- NextEscaped = FALSE;
- Bytes[0] -= DEBUG_ESCAPE;
- Bytes += 1;
- BytesToRead -= 1;
- if (BytesToRead == 0) {
- break;
- }
- }
- for (Index = 0; Index < BytesToRead - 1; Index += 1) {
- if (Bytes[Index] == DEBUG_ESCAPE) {
- memmove(&(Bytes[Index]),
- &(Bytes[Index + 1]),
- BytesToRead - Index - 1);
- Count += 1;
- Bytes[Index] -= DEBUG_ESCAPE;
- }
- }
- if (Bytes[Index] == DEBUG_ESCAPE) {
- Count += 1;
- NextEscaped = TRUE;
- }
- }
- Bytes += BytesToRead - Count;
- BytesToRead = Count;
- }
- return Status;
- }
- BOOL
- DbgpKdSendBytes (
- PVOID Buffer,
- ULONG BytesToSend
- )
- /*++
- Routine Description:
- This routine sends a number of bytes through the debugger connection.
- Arguments:
- Buffer - Supplies a pointer to the buffer where the data to be sent resides.
- BytesToSend - Supplies the number of bytes that should be sent.
- Return Value:
- Returns TRUE on success, FALSE on failure.
- --*/
- {
- ULONG ByteIndex;
- PUCHAR Bytes;
- UCHAR EncodedByte[2];
- ULONG SendSize;
- BOOL Status;
- Bytes = Buffer;
- if (DbgKdPrintRawBytes != FALSE) {
- DbgOut("TX: ");
- for (ByteIndex = 0; ByteIndex < BytesToSend; ByteIndex += 1) {
- DbgOut("%02X ", Bytes[ByteIndex]);
- }
- DbgOut("\n");
- }
- Status = TRUE;
- while (BytesToSend != 0) {
- SendSize = 0;
- if (DbgKdEncodeBytes != FALSE) {
- //
- // Gather bytes until one is found that needs escaping.
- //
- while ((SendSize < BytesToSend) &&
- (Bytes[SendSize] != DEBUG_XON) &&
- (Bytes[SendSize] != DEBUG_XOFF) &&
- (Bytes[SendSize] != DEBUG_ESCAPE)) {
- SendSize += 1;
- }
- //
- // If no escaping is needed, just send everything.
- //
- } else {
- SendSize = BytesToSend;
- }
- if (SendSize != 0) {
- Status = CommSend(Bytes, SendSize);
- if (Status == FALSE) {
- DbgOut("Failed to send %d bytes.\n", BytesToSend);
- break;
- }
- }
- Bytes += SendSize;
- BytesToSend -= SendSize;
- if (BytesToSend != 0) {
- EncodedByte[0] = DEBUG_ESCAPE;
- EncodedByte[1] = *Bytes + DEBUG_ESCAPE;
- Status = CommSend(EncodedByte, 2);
- if (Status == FALSE) {
- DbgOut("Failed to send %d bytes.\n", BytesToSend);
- break;
- }
- Bytes += 1;
- BytesToSend -= 1;
- }
- }
- return Status;
- }
|