Welcome to mirror list, hosted at ThFree Co, Russian Federation.

debug.c « libgloss - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d6fb89a5e08f82de345dd683736cafc951a12bba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
/*
 * Copyright (c) 1995, 1996 Cygnus Support
 *
 * The authors hereby grant permission to use, copy, modify, distribute,
 * and license this software and its documentation for any purpose, provided
 * that existing copyright notices are retained in all copies and that this
 * notice is included verbatim in any distributions. No written agreement,
 * license, or royalty fee is required for any of the authorized uses.
 * Modifications to this software may be copyrighted by their authors
 * and need not follow the licensing terms described here, provided that
 * the new terms are clearly indicated on the first page of each file where
 * they apply.
 */

/*
 *   A debug packet whose contents are <data> looks like:
 *
 *        $ <data> # CSUM1 CSUM2
 *
 *        <data> must be ASCII alphanumeric and cannot include characters
 *        '$' or '#'.  If <data> starts with two characters followed by
 *        ':', then the existing stubs interpret this as a sequence number.
 *
 *       CSUM1 and CSUM2 are ascii hex representation of an 8-bit 
 *        checksum of <data>, the most significant nibble is sent first.
 *        the hex digits 0-9,a-f are used.
 *
 *   We respond with:
 *
 *        +       - if CSUM is correct and ready for next packet
 *        -       - if CSUM is incorrect
 *
 *   <data> is as follows:
 *   Most values are encoded in ascii hex digits.
 */

#include "debug.h"
#include <signal.h>

/*
 * buffers that hold the packets while they're being constructed.
 */
char packet_in_buf[BUFMAX];
char packet_out_buf[BUFMAX];
int packet_index;

/*
 * indicate to caller of mem2hex or hex2mem that there has been an error. 
 * 0 means ok, 1 means error
 */
volatile int mem_err = 0;

/*
 * 1 means print debugging messages from the target, 0 means be quiet. This is
 * changed by gdb_debug().
 */
int remote_debug = 0;

/*
 * indicate whether the debug vectors ahave been initialized
 * 0 means not yet, 1 means yep, it's ready.
 */
int initialized = 0;

/*
 * These variables are instantialted in the GDB stub code.
 */

/* this is a list of signal to exception mappings. */
extern struct trap_info hard_trap_info[];

/* this is a memory fault exception handler, used by mem2hex & hex2mem */
extern void set_mem_fault_trap();

/*
 * print debugging messages. This uses print, rather than one of the
 * stdio routines, cause if there are stack or memory problems, the
 * stdio routines don't work.
 *	params are the debug level, and the string to print
 *	it doesn't return anything.
 */
void
debuglog(int level, char *msg)
{
  char *p;
  unsigned char buf[BUFMAX];
  char newmsg[BUFMAX];
  int i;

  if (level > remote_debug)
    return;

  if ((level <0) || (level > 100)) {
    print ("ERROR: debug print level out of range");
    return;
  }

  /* convert some characters so it'll look right in the log */
  p = newmsg;
  for (i = 0 ; msg[i] != '\0'; i++) {
    if (i > BUFMAX)
      print ("\r\nERROR: Debug message too long\r\n");
    switch (msg[i]) {
    case '\n':                                  /* newlines */
      *p++ = '\\';
      *p++ = 'n';
      continue;
    case '\r':                                  /* carriage returns */
      *p++ = '\\';
      *p++ = 'r';
      continue;
    case '\033':                                /* escape */
      *p++ = '\\';
      *p++ = 'e';
      continue;
    case '\t':                                  /* tab */
      *p++ = '\\';
      *p++ = 't';
      continue;
    case '\b':                                  /* backspace */
      *p++ = '\\';
      *p++ = 'b';
      continue;
    default:                                    /* no change */
      *p++ = msg[i];
    }

    if (msg[i] < 26) {                          /* modify control characters */
      *p++ = '^';
      *p++ = msg[i] + 'A';
      continue;
    }
    if (msg[i] >= 127) {			/* modify control characters */
      *p++ = '!';
      *p++ = msg[i] + 'A';
      continue;
    }
  }
  *p = '\0';                                    /* terminate the string */
  print (newmsg);
  print ("\r\n");
}

/*
 * convert an ascii hex digit to a number.
 *	param is hex digit.
 *	returns a decimal digit.
 */
int
hex2digit (int digit)
{  
  if (digit == 0)
    return 0;

  if (digit >= '0' && digit <= '9')
    return digit - '0';
  if (digit >= 'a' && digit <= 'f')
    return digit - 'a' + 10;
  if (digit >= 'A' && digit <= 'F')
    return digit - 'A' + 10;
  
  /* shouldn't ever get this far */
  return ERROR;
}

/*
 * convert number NIB to a hex digit.
 *	param is a decimal digit.
 *	returns a hex digit.
 */
char
digit2hex(int digit)
{
  if (digit < 10)
    return '0' + digit;
  else
    return 'a' + digit - 10;
}

/* 
 * Convert the memory pointed to by mem into hex, placing result in buf.
 * Return a pointer to the last char put in buf (null), in case of mem fault,
 * return 0.
 * If MAY_FAULT is non-zero, then we will handle memory faults by returning
 * a 0, else treat a fault like any other fault in the stub.
 */
unsigned char *
mem2hex(unsigned char *mem, unsigned char *buf, int count, int may_fault)
{
  unsigned char ch;

  DEBUG (1, "In mem2hex");

  set_mem_fault_trap(MAY_FAULT);

  while (count-- > 0) {
    ch = *mem++;
    if (mem_err) {
      DEBUG (1, "memory fault in mem2hex");
      return 0;
    }
    *buf++ = digit2hex(ch >> 4);
    *buf++ = digit2hex(ch & 0xf);
  }

  *buf = 0;

  set_mem_fault_trap(OK);

  return buf;
}

/*
 * Convert the hex array pointed to by buf into binary to be placed in mem
 * return a pointer to the character AFTER the last byte written
 */
unsigned char *
hex2mem(unsigned char *buf, unsigned char *mem, int count, int may_fault)
{
  int i;
  unsigned char ch;

  DEBUG (1, "In hex2mem");

  set_mem_fault_trap(may_fault);

  for (i=0; i<count; i++) {
    ch = hex2digit(*buf++) << 4;
    ch |= hex2digit(*buf++);
    *mem++ = ch;
    if (mem_err)
      return 0;
  }

  set_mem_fault_trap(0);

  return mem;
}

/*
 * while we find nice hex chars, build an int.
 *	param is a pointer to the string.
 *	returns the int in the param field, and the number of chars processed.
 */
int
hex2int (char **ptr, int *intValue)
{
  int numChars = 0;
  int hexValue;

  *intValue = 0;

  while (**ptr)
    {
      hexValue = hex2digit(**ptr);
      if (hexValue < 0)
        break;

      *intValue = (*intValue << 4) | hexValue;
      numChars ++;
      (*ptr)++;
    }
  return (numChars);
}

/*
 * Scan for the sequence $<data>#<checksum>
 */
void
getpacket(unsigned char *buffer)
{
  unsigned char checksum;
  unsigned char xmitcsum;
  int i;
  int count;
  unsigned char ch;

  do {
    /* wait around for the start character, ignore all other characters */
    while ((ch = (inbyte() & 0x7f)) != '$') ;
    
    checksum = 0;
    xmitcsum = -1;
    
    count = 0;
    
    /* now, read until a # or end of buffer is found */
    while (count < BUFMAX) {
      ch = inbyte() & 0x7f;
      if (ch == '#')
	break;
      checksum = checksum + ch;
      buffer[count] = ch;
      count = count + 1;
    }
    
    if (count >= BUFMAX)
      continue;
    
    buffer[count] = 0;
    
    if (ch == '#') {
      xmitcsum = hex2digit(inbyte() & 0x7f) << 4;
      xmitcsum |= hex2digit(inbyte() & 0x7f);
#if 1
      /* Humans shouldn't have to figure out checksums to type to it. */
      outbyte ('+');
      return;
#endif
      if (checksum != xmitcsum)
	outbyte('-');	/* failed checksum */
      else {
	outbyte('+'); /* successful transfer */
	/* if a sequence char is present, reply the sequence ID */
	if (buffer[2] == ':') {
	  outbyte(buffer[0]);
	  outbyte(buffer[1]);
	  /* remove sequence chars from buffer */
	  count = strlen(buffer);
	  for (i=3; i <= count; i++)
	    buffer[i-3] = buffer[i];
	}
      }
    }
  }
  while (checksum != xmitcsum);
}

/*
 * Send the packet in buffer.
 */
void
putpacket(unsigned char *buffer)
{
  unsigned char checksum;
  int count;
  unsigned char ch;

  /*  $<packet info>#<checksum>. */
  do {
    outbyte('$');
    checksum = 0;
    count = 0;
    
    while (ch = buffer[count]) {
      if (! outbyte(ch))
	return;
      checksum += ch;
      count += 1;
    }
    
    outbyte('#');
    outbyte(digit2hex(checksum >> 4));
    outbyte(digit2hex(checksum & 0xf));
    
  }
  while ((inbyte() & 0x7f) != '+');
}

/*
 *
 */
void
gdb_event_loop(int sigval, unsigned long *registers)
{
  int addr;
  int length;
  unsigned char *ptr;
  ptr = packet_out_buf;

  DEBUG (1, "In gdb_event_loop");

  while (1) {
    packet_out_buf[0] = 0;
    
    getpacket(packet_in_buf);      
    ptr = &packet_in_buf[1];

    switch (packet_in_buf[0]) {
    case '?':		/* get the last known signal */
      gdb_last_signal(sigval);
      break;
      
    case 'd':		/* toggle debug messages from the stub */
      gdb_toggle();
      break;
      
    case 'g':		/* return the value of the CPU registers */
      target_read_registers(registers);
      break;
      
    case 'G':	   /* set the value of the CPU registers - return OK */
      target_write_registers(registers);
      break;
      
    case 'm':	  /* mAA..AA,LLLL  Read LLLL bytes at address AA..AA */
      /* Try to read %x,%x.  */
      if (hex2int((char **)&ptr, &addr)
	  && *ptr++ == ','
	  && hex2int((char **)&ptr, &length)) {
	gdb_read_memory(addr, length);
      } else {
	make_return_packet(1);
      }
      break;
      
    case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
      /* Try to read '%x,%x:'.  */
      if (hex2int((char **)&ptr, &addr)
	  && *ptr++ == ','
	  && hex2int((char **)&ptr, &length)
	  && *ptr++ == ':') {
	gdb_write_memory (addr, length, ptr);
      } else {
	make_return_packet(2);
      }
      break;
      
    case 'c':    /* cAA..AA    Continue at address AA..AA(optional) */
      /* try to read optional parameter, pc unchanged if no parm */
      if (hex2int((char **)&ptr, &addr)) {
	write_pc(registers, addr);
      }
      
      /*
       * we need to flush the instruction cache here, as we may have
       * deposited a breakpoint, and the icache probably has no way of
       * knowing that a data ref to some location may have changed
       * something that is in the instruction cache. 
       */
      
      flush_i_cache();
      /* by returning, we pick up execution where we left off */
      return;

      /* kill the program */
    case 'k' :
      gdb_kill();
      break;
    case 'r':		/* Reset */
      target_reset();
      break;
    }			/* switch */
    
    /* reply to the request */
    putpacket(packet_out_buf);
  }
  DEBUG (1, "Leaving handle_exception()");
}

/* Convert the hardware trap type code to a unix signal number. */

int
computeSignal(int tt)
{
  struct trap_info *ht;

  for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
    if (ht->tt == tt)
      return ht->signo;

  return SIGHUP;		/* default for things we don't know about */
}

/*
 * Set up exception handlers for tracing and breakpoints
 */
void
set_debug_traps()
{
  struct trap_info *ht;

  DEBUG (1, "Entering set_debug_traps()");

  if (hard_trap_info->tt == 0) {
    print ("ERROR: ARG#$@%^&*!! no hard trap info!!\r\n");
  }

  for (ht = hard_trap_info; ht->tt && ht->signo; ht++) {
    exception_handler(ht->tt, (unsigned long)default_trap_hook);
  }

  /* In case GDB is started before us, ack any packets (presumably
     "$?#xx") sitting there.  */

  outbyte ('+');
  initialized = 1;

  DEBUG (1, "Leaving set_debug_traps()");
}

/*
 * make a return packet.
 *	param is the value to return.
 *		0 = OK, any other value is converted to a two digit hex number.
 *	returns a string or "OK" or "ENN", where NN is the error number. Each N
 *		is an ASCII encoded hex digit.
 */
char *
make_return_packet(int val)
{
  if (val == 0) {
     packet_out_buf[0] = 'O';
     packet_out_buf[1] = 'K';
     packet_out_buf[2] = 0;  
  } else {
    packet_out_buf[0] = 'E';
    packet_out_buf[1] = digit2hex((val >> 4) & 0xf);
    packet_out_buf[2] = digit2hex(val & 0xf);
    packet_out_buf[3] = 0;
  }
  return(packet_out_buf);
}

/*
 * g - read registers.
 *	no params.
 *	returns a vector of words, size is NUM_REGS.
 */
char *
gdb_read_registers()
{
}

/*
 * G - write registers.
 *	param is a vector of words, size is NUM_REGS.
 *	returns an OK or an error number.
 */
char *
gdb_write_registers(char *regs)
{
}

/*
 * m - read memory.
 *	params are the address to start the read at and the number of
 *		bytes to read.  
 *	returns a vector of nbytes or an error number.
 *	Can be fewer bytes than requested if able to read only part of the
 *	data. 
 */
char *
gdb_read_memory(long addr, int nbytes)
{
  if (mem2hex((char *)addr, packet_out_buf, nbytes, MAY_FAULT))
    return(packet_out_buf);
  else {
    return(make_return_packet(3));
  }
}

/*
 * M write memory
 *	params are the address to start writing to, the number of
 *		bytes to write, and the new values of the bytes.
 *	returns an OK or an error number.
 */
char *
gdb_write_memory(long addr, int nbytes, char *mem)
{
 if (hex2mem(mem, (char *)addr, nbytes, MAY_FAULT))
    return(make_return_packet(OK));
  else {
    return(make_return_packet(3));
  }
}

/*
 * c - continue at address.
 *	param is the address to start at, and an optional signal. If
 *		sig is zero, then ignore it.
 *	returns an OK or an error number.
 */
char *
gdb_continue(int sig, long addr)
{
}

/*
 * s - step instruction(s)
 *	param is the address to start at, and an optional signal. If
 *		sig is zero, then ignore it.
 *	returns an OK or an error number.
 */
char *
gdb_step(int sig, long addr)
{
}

/*
 * k - kill program.
 *	no params.
 *	returns an OK or an error number.
 */
char *
gdb_kill()
{
  /* generically, we can't do anything for this command */
  return(make_return_packet(OK));
}

/*
 * ? - last signal.
 *	no params.
 *	returns the last signal number.
 */
char *
gdb_last_signal(int val)
{
  DEBUG (1, "Entering gdb_last_signal()");

  packet_out_buf[0] = 'S';
  packet_out_buf[1] = digit2hex(val >> 4);
  packet_out_buf[2] = digit2hex(val & 0xf);
  packet_out_buf[3] = 0;

  DEBUG (1, "Leaving gdb_last_signal()");
  return (packet_out_buf);
}

/*
 * b - change baud rate.
 *	param is the new baudrate
 *	returns the baud rate.
 */
char *
gdb_baudrate(int baud)
{
  /* generically, we can't do anything for this command */
  return(make_return_packet(OK));
}

/*
 * T - dump state.
 *	no params.
 *	returns the signal number, the registers, the thread ID, and
 *		possible extensions in a vector that looks like:
 *			TAAn...:r...;n...:r...;n...:r...; where:
 *                       AA = signal number
 *                       n... = register number (hex)
 *                       r... = register contents
 *                       n... = `thread'
 *                       r... = thread process ID.  This is a hex integer.
 *                       n... = other string not starting with valid hex digit.
 *                              gdb should ignore this n,r pair and go on to
 *				the next. This way we can extend the protocol.
 */
char *
gdb_dump_state()
{
}

/*
 * D - host requests a detach
 *	no params.
 *	returns either a S, T, W, or X command.
 *	returns an OK or an error number.
 */
char *
gdb_detach()
{
}

/*
 * H - set thread.
 *	params are the command to execute and the thread ID.
 *		cmd = 'c' for thread used in step and continue;
 *		cmd = 'g' for thread used in other operations.
 *		tid = -1 for all threads.
 *		tid = zero, pick a thread,any thread.
 *	returns an OK or an error number.
 */
char *
gdb_set_thread(int cmd, int tid)
{
  /* generically, we can't do anything for this command */
  return(make_return_packet(OK));
}

/*
 * p - read one register.
 *	param is the register number.
 *	returns the register value or ENN.
 */
char *
gdb_read_reg(int reg)
{
  /* generically, we can't do anything for this command */
  return(make_return_packet(OK));
}

/*
 * P - write one register.
 *	params are the register number, and its new value.
 *	returns the register value or ENN.
 */
char *
gdb_write_reg(int reg, long val)
{
  /* generically, we can't do anything for this command */
  
  return(make_return_packet(OK));
}

/*
 * W - process exited.
 *	no params.
 *	returns the exit status.
 */
char *
gdb_exited()
{
  /* generically, we can't do anything for this command */
  return(make_return_packet(OK));
}

/*
 * X - process terminated.
 *	no params.
 *	returns the last signal.
 */
char *
gdb_terminated()
{
}

/*
 * O - hex encoding.
 *	params are a vector of bytes, and the number of bytes to encode.
 *	returns a vector of ASCII encoded hex numbers.
 */
char *
gdb_hex(char *str, int nbytes)
{
}

/*
 * A - tread alive request.
 *	param is the thread ID.
 *	returns an OK or an error number.
 */
char *
gdb_thread_alive(int tid)
{
  /* generically, we can't do anything for this command */
  return(make_return_packet(OK));
}

/*
 * ! - extended protocol.
 *	no params.
 *	returns an OK or an error number.
 */
char *
gdb_extended()
{
  /* generically, we can't do anything for this command */
  return(make_return_packet(OK));
}

/*
 * d - toggle gdb stub diagnostics.
 *	no params.
 *	returns an OK or an error number.
 */
char *
gdb_debug()
{
  if (remote_debug > 0)
    remote_debug = 0;
  else
    remote_debug = 1;

  return(make_return_packet(OK));
}

/*
 * d - toggle gdb stub.
 *	no params.
 *	returns an OK or an error number.
 */
char *
gdb_toggle()
{
  static int level = 0;

  if (remote_debug) {
    level = remote_debug;
    remote_debug = 0;
  } else {
    remote_debug = level;
  }

  return(make_return_packet(OK));
}

/*
 * r - reset target
 *	no params.
 *	returns an OK or an error number.
 */
char *
gdb_reset()
{
  /* generically, we can't do anything for this command */
  return(make_return_packet(OK));
}

/*
 * t - search backwards.
 *	params are the address to start searching from, a pattern to match, and
 *		the mask to use.
 *	FIXME: not entirely sure what this is supposed to return.
 */
char *
gdb_search(long addr, long pat, long mask)
{
  /* generically, we can't do anything for this command */
  return(make_return_packet(OK));
}

/*
 * q - general get query.
 *	param is a string, that's the query to be executed.
 *	FIXME: not entirely sure what this is supposed to return.
 */
char *
gdb_get_query(char *query)
{
  /* generically, we can't do anything for this command */
  return(make_return_packet(OK));
}

/*
 * Q - general set query
 *	param is a string, that's the query to be executed.
 *	FIXME: not entirely sure what this means.
 *	returns an OK or an error number.
 */
char *
gdb_set(char *query)
{
  /* generically, we can't do anything for this command */
  return(make_return_packet(OK));
}