forked from sergev/ejtagproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy-skeleton.c
1239 lines (1029 loc) · 36.9 KB
/
proxy-skeleton.c
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2002 Chris Liechti and Steve Underwood
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Implementation of a dummy `skeleton' target for the GDB proxy server.
Exported Data:
skeletone_target - target descriptor of the `skeleton' target
Imported Data:
None
Static Data:
skeleton_XXXX - static data representing status and
parameters of the target
Global Functions:
None
Static Functions:
skeleton_XXXX - methods comprising the `skeleton' target.
A description is in file gdbproxy.h
skeleton_ - local finctions
skeleton_command
$Id: target_skeleton.c,v 1.5 2005/08/21 06:51:56 coppice Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <stdarg.h>
#include <getopt.h>
#include "gdbproxy.h"
/* Note: we are using prefix 'skeleton' for static stuff in
order to simplify debugging of the target code itself */
/* TODO: Put the correct values for the real target in these macros */
#define RP_SKELETON_MIN_ADDRESS 0x0U
#define RP_SKELETON_MAX_ADDRESS 0xFFFFU
#define RP_SKELETON_REG_DATATYPE uint16_t
#define RP_SKELETON_REG_BYTES (16*sizeof(uint16_t))
#define RP_SKELETON_NUM_REGS 16
#define RP_SKELETON_REGNUM_PC 0 /* Program counter reg. number */
#define RP_SKELETON_REGNUM_SP 1 /* Stack pointer reg. number */
#define RP_SKELETON_REGNUM_FP 4 /* Frame pointer reg. number */
#define RP_SKELETON_MAX_BREAKPOINTS 10
/* Some example states a real target might support. */
#define RP_SKELETON_TARGET_STATE_RUNNING 0
#define RP_SKELETON_TARGET_STATE_STOPPED 1
#define RP_SKELETON_TARGET_STATE_SINGLE_STEP_COMPLETE 2
#define RP_SKELETON_TARGET_STATE_BREAKPOINT_HIT 3
/*
* Target methods, static
*/
static void skeleton_help(const char *prog_name);
static int skeleton_open(int argc,
char * const argv[],
const char *prog_name,
log_func log_fn);
static void skeleton_close(void);
static int skeleton_connect(char *status_string,
size_t status_string_size,
int *can_restart);
static int skeleton_disconnect(void);
static void skeleton_kill(void);
static int skeleton_restart(void);
static void skeleton_stop(void);
static int skeleton_set_gen_thread(rp_thread_ref *thread);
static int skeleton_set_ctrl_thread(rp_thread_ref *thread);
static int skeleton_is_thread_alive(rp_thread_ref *thread, int *alive);
static int skeleton_read_registers(uint8_t *data_buf,
uint8_t *avail_buf,
size_t buf_size,
size_t *read_size);
static int skeleton_write_registers(uint8_t *data_buf, size_t write_size);
static int skeleton_read_single_register(unsigned int reg_no,
uint8_t *data_buf,
uint8_t *avail_buf,
size_t buf_size,
size_t *read_size);
static int skeleton_write_single_register(unsigned int reg_no,
uint8_t *data_buf,
size_t write_size);
static int skeleton_read_mem(uint64_t addr,
uint8_t *data_buf,
size_t req_size,
size_t *actual_size);
static int skeleton_write_mem(uint64_t addr,
uint8_t *data_buf,
size_t req_sise);
static int skeleton_resume_from_current(int step, int sig);
static int skeleton_resume_from_addr(int step,
int sig,
uint64_t addr);
static int skeleton_go_waiting(int sig);
static int skeleton_wait_partial(int first,
char *status_string,
size_t status_string_len,
out_func out,
int *implemented,
int *more);
static int skeleton_wait(char *status_string,
size_t status_string_len,
out_func out,
int *implemented);
static int skeleton_process_query(unsigned int *mask,
rp_thread_ref *arg,
rp_thread_info *info);
static int skeleton_list_query(int first,
rp_thread_ref *arg,
rp_thread_ref *result,
size_t max_num,
size_t *num,
int *done);
static int skeleton_current_thread_query(rp_thread_ref *thread);
static int skeleton_offsets_query(uint64_t *text,
uint64_t *data,
uint64_t *bss);
static int skeleton_crc_query(uint64_t addr,
size_t len,
uint32_t *val);
static int skeleton_raw_query(char *in_buf,
char *out_buf,
size_t out_buf_size);
static int skeleton_remcmd(char *in_buf, out_func of, data_func df);
static int skeleton_add_break(int type, uint64_t addr, unsigned int len);
static int skeleton_remove_break(int type, uint64_t addr, unsigned int len);
//Table of remote commands following
static int skeleton_rcmd_help(int argc, char *argv[], out_func of, data_func df); //proto for table entry
static uint32_t crc32(uint8_t *buf, size_t len, uint32_t crc);
#define RCMD(name, hlp) {#name, skeleton_rcmd_##name, hlp} //table entry generation
//Table entry definition
typedef struct
{
const char *name; // command name
int (*function)(int, char**, out_func, data_func); // function to call
const char *help; // one line of help text
} RCMD_TABLE;
/*
* Global target descriptor
*/
rp_target skeleton_target =
{
NULL, /* next */
"skeleton",
"skeleton target to demonstrate the GDB proxy server",
skeleton_help,
skeleton_open,
skeleton_close,
skeleton_connect,
skeleton_disconnect,
skeleton_kill,
skeleton_restart,
skeleton_stop,
skeleton_set_gen_thread,
skeleton_set_ctrl_thread,
skeleton_is_thread_alive,
skeleton_read_registers,
skeleton_write_registers,
skeleton_read_single_register,
skeleton_write_single_register,
skeleton_read_mem,
skeleton_write_mem,
skeleton_resume_from_current,
skeleton_resume_from_addr,
skeleton_go_waiting,
skeleton_wait_partial,
skeleton_wait,
skeleton_process_query,
skeleton_list_query,
skeleton_current_thread_query,
skeleton_offsets_query,
skeleton_crc_query,
skeleton_raw_query,
skeleton_remcmd,
skeleton_add_break,
skeleton_remove_break
};
struct skeleton_status_s
{
/* Start up parameters, set by skeleton_open */
log_func log;
int is_open;
/* Tell wait_xxx method the notion of whether or not
previously called resume is supported */
int target_running;
int target_interrupted;
RP_SKELETON_REG_DATATYPE registers[RP_SKELETON_NUM_REGS];
uint64_t breakpoints[RP_SKELETON_MAX_BREAKPOINTS];
};
static struct skeleton_status_s skeleton_status =
{
NULL,
FALSE,
FALSE,
FALSE,
{0},
{0}
};
/* Local functions */
static char *skeleton_out_treg(char *in, unsigned int reg_no);
static int refresh_registers(void);
/* Target method */
#ifdef NDEBUG
#define DEBUG_OUT(...)
#else
static void DEBUG_OUT(const char *string,...)
{
va_list args;
va_start (args, string);
fprintf (stderr, "debug: ");
vfprintf (stderr, string, args);
fprintf (stderr, "\n");
va_end (args);
}
#endif
static void skeleton_help(const char *prog_name)
{
printf("This is the skeleton target for the GDB proxy server. Usage:\n\n");
printf(" %s [options] %s [skeleton-options] [port]\n",
prog_name,
skeleton_target.name);
printf("\nOptions:\n\n");
printf(" --debug run %s in debug mode\n", prog_name);
printf(" --help `%s --help %s' prints this message\n",
prog_name,
skeleton_target.name);
printf(" --port=PORT use the specified TCP port\n");
printf("\nskeleton-options:\n\n");
printf("\n");
return;
}
/* Target method */
static int skeleton_open(int argc,
char * const argv[],
const char *prog_name,
log_func log_fn)
{
/* TODO: This example assumes a target attached to a parallel port, as many
JTAG controlled devices are.*/
#if WIN32
const char *port = "1";
#elif defined(__linux__)
const char *port = "/dev/parport0";
#else
const char *port ="/dev/ppi0";
#endif
/* Option descriptors */
static struct option long_options[] =
{
/* Options setting flag */
{NULL, 0, 0, 0}
};
assert(!skeleton_status.is_open);
assert(prog_name != NULL);
assert(log_fn != NULL);
/* Set log */
skeleton_status.log = log_fn;
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_open()",
skeleton_target.name);
/* Process options */
for (;;)
{
int c;
int option_index;
c = getopt_long(argc, argv, "+", long_options, &option_index);
if (c == EOF)
break;
switch (c)
{
case 0:
/* Long option which just sets a flag */
break;
default:
skeleton_status.log(RP_VAL_LOGLEVEL_NOTICE,
"%s: Use `%s --help %s' to see a complete list of options",
skeleton_target.name,
prog_name,
skeleton_target.name);
return RP_VAL_TARGETRET_ERR;
}
}
if (optind == (argc - 1))
{
port = argv[optind];
}
else if (optind != argc)
{
/* Bad number of arguments */
skeleton_status.log(RP_VAL_LOGLEVEL_ERR,
"%s: bad number of arguments",
skeleton_target.name);
skeleton_target.help(prog_name);
return RP_VAL_TARGETRET_ERR;
}
if (!skeleton_status.is_open)
{
/* TODO: initialise the target interface */
}
/* TODO: Perform any initial target configuration. Perhaps check if the
target is actually there. */
/* Set up initial default values */
skeleton_status.target_running = FALSE;
skeleton_status.target_interrupted = FALSE;
memset (skeleton_status.registers, 0, sizeof(skeleton_status.registers));
memset (skeleton_status.breakpoints, 0, sizeof(skeleton_status.breakpoints));
skeleton_status.is_open = TRUE;
return RP_VAL_TARGETRET_OK;
}
/* Target method */
static void skeleton_close(void)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_close()",
skeleton_target.name);
assert(skeleton_status.is_open);
/* TODO: Tidy up things and shut down. */
skeleton_status.is_open = FALSE;
}
/* Target method */
static int skeleton_connect(char *status_string,
size_t status_string_len,
int *can_restart)
{
char *cp;
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_connect()",
skeleton_target.name);
assert(skeleton_status.is_open);
assert(status_string != NULL);
assert(status_string_len >= 34);
assert(can_restart != NULL);
*can_restart = TRUE;
/* Fill out the the status string */
sprintf(status_string, "T%02d", RP_SIGNAL_ABORTED);
if (refresh_registers())
return RP_VAL_TARGETRET_ERR;
cp = skeleton_out_treg(&status_string[3], RP_SKELETON_REGNUM_PC);
cp = skeleton_out_treg(cp, RP_SKELETON_REGNUM_FP);
return (cp != NULL) ? RP_VAL_TARGETRET_OK : RP_VAL_TARGETRET_ERR;
}
/* Target method */
static int skeleton_disconnect(void)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_disconnect()",
skeleton_target.name);
return RP_VAL_TARGETRET_NOSUPP;
}
/* Target method */
static void skeleton_kill(void)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_kill()",
skeleton_target.name);
/* TODO: Kill the target debug session. */
}
static int skeleton_restart(void)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_restart()",
skeleton_target.name);
/* Just stop it. The actual restart will be done
when connect is called again */
skeleton_stop();
return RP_VAL_TARGETRET_OK;
}
/* Target method */
static void skeleton_stop(void)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_stop()",
skeleton_target.name);
assert(skeleton_status.is_open);
/* TODO: Steop (i,e, break) the target program. */
skeleton_status.target_interrupted = TRUE;
}
static int skeleton_set_gen_thread(rp_thread_ref *thread)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_set_gen_thread()",
skeleton_target.name);
return RP_VAL_TARGETRET_NOSUPP;
}
/* Target method */
static int skeleton_set_ctrl_thread(rp_thread_ref *thread)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_set_ctrl_thread()",
skeleton_target.name);
return RP_VAL_TARGETRET_NOSUPP;
}
/* Target method */
static int skeleton_is_thread_alive(rp_thread_ref *thread, int *alive)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_is_thread_alive()",
skeleton_target.name);
return RP_VAL_TARGETRET_OK;
}
/* Target method */
static int skeleton_read_registers(uint8_t *data_buf,
uint8_t *avail_buf,
size_t buf_size,
size_t *read_size)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_read_registers()",
skeleton_target.name);
assert(skeleton_status.is_open);
assert(data_buf != NULL);
assert(avail_buf != NULL);
assert(buf_size >= RP_SKELETON_REG_BYTES);
assert(read_size != NULL);
if (refresh_registers())
return RP_VAL_TARGETRET_ERR;
memcpy(data_buf, skeleton_status.registers, RP_SKELETON_REG_BYTES);
memset(avail_buf, 1, RP_SKELETON_REG_BYTES);
*read_size = RP_SKELETON_REG_BYTES;
return RP_VAL_TARGETRET_OK;
}
/* Target method */
static int skeleton_write_registers(uint8_t *buf, size_t write_size)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_write_registers()",
skeleton_target.name);
assert(skeleton_status.is_open);
assert(buf != NULL);
assert(write_size > 0);
assert(write_size <= RP_SKELETON_REG_BYTES);
memcpy(skeleton_status.registers, buf, write_size);
/* TODO: Write the registers to the target. */
return RP_VAL_TARGETRET_OK;
}
/* Target method */
static int skeleton_read_single_register(unsigned int reg_no,
uint8_t *data_buf,
uint8_t *avail_buf,
size_t buf_size,
size_t *read_size)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_read_single_register()",
skeleton_target.name);
assert(skeleton_status.is_open);
assert(data_buf != NULL);
assert(avail_buf != NULL);
assert(buf_size >= RP_SKELETON_REG_BYTES);
assert(read_size != NULL);
if (reg_no < 0 || reg_no > RP_SKELETON_NUM_REGS)
return RP_VAL_TARGETRET_ERR;
if (refresh_registers())
return RP_VAL_TARGETRET_ERR;
memcpy(data_buf, &skeleton_status.registers[reg_no], sizeof(skeleton_status.registers[reg_no]));
memset(avail_buf, 1, sizeof(skeleton_status.registers[reg_no]));
*read_size = sizeof(skeleton_status.registers[reg_no]);
return RP_VAL_TARGETRET_OK;
}
/* Target method */
static int skeleton_write_single_register(unsigned int reg_no,
uint8_t *buf,
size_t write_size)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_write_single_register(%d, 0x%X)",
skeleton_target.name,
reg_no,
((RP_SKELETON_REG_DATATYPE *) buf)[0]);
assert(skeleton_status.is_open);
assert(buf != NULL);
assert(write_size == 2);
if (reg_no < 0 || reg_no > RP_SKELETON_NUM_REGS)
return RP_VAL_TARGETRET_ERR;
skeleton_status.registers[reg_no] = ((RP_SKELETON_REG_DATATYPE *) buf)[0];
/* TODO: Write the register to the target. */
return RP_VAL_TARGETRET_OK;
}
/* Target method */
static int skeleton_read_mem(uint64_t addr,
uint8_t *buf,
size_t req_size,
size_t *actual_size)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_read_mem(0x%llX, ptr, %d, ptr)",
skeleton_target.name,
addr,
req_size);
assert(skeleton_status.is_open);
assert(buf != NULL);
assert(req_size > 0);
assert(actual_size != NULL);
if (addr > RP_SKELETON_MAX_ADDRESS)
{
skeleton_status.log(RP_VAL_LOGLEVEL_ERR,
"%s: bad address 0x%llx",
skeleton_target.name,
addr);
return RP_VAL_TARGETRET_ERR;
}
if (addr + req_size > RP_SKELETON_MAX_ADDRESS + 1)
*actual_size = RP_SKELETON_MAX_ADDRESS + 1 - addr;
else
*actual_size = req_size;
/* TODO: Read the required block of memory from the target. */
return RP_VAL_TARGETRET_OK;
}
/* Target method */
static int skeleton_write_mem(uint64_t addr,
uint8_t *buf,
size_t write_size)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_write_mem(0x%llX, ptr, %d)",
skeleton_target.name,
addr,
write_size);
assert(skeleton_status.is_open);
assert(buf != NULL);
/* GDB does zero length writes for some reason. Treat them harmlessly. */
if (write_size == 0)
return RP_VAL_TARGETRET_OK;
if (addr > RP_SKELETON_MAX_ADDRESS)
{
skeleton_status.log(RP_VAL_LOGLEVEL_ERR,
"%s: bad address 0x%llx",
skeleton_target.name,
addr);
return RP_VAL_TARGETRET_ERR;
}
if ((addr + write_size - 1) > RP_SKELETON_MAX_ADDRESS)
{
skeleton_status.log(RP_VAL_LOGLEVEL_ERR,
"%s: bad address/write_size 0x%llx/0x%x",
skeleton_target.name,
addr,
write_size);
return RP_VAL_TARGETRET_ERR;
}
/* TODO: Write to the target. */
return RP_VAL_TARGETRET_OK;
}
/* Target method */
static int skeleton_resume_from_current(int step, int sig)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_resume_from_current(%s, %d)",
skeleton_target.name,
(step) ? "step" : "run",
sig);
assert(skeleton_status.is_open);
if (step)
/* TODO: Single step the target */;
else
/* TODO: Run the target to a breakpoint, or until we stop it. */;
skeleton_status.target_running = TRUE;
skeleton_status.target_interrupted = FALSE;
return RP_VAL_TARGETRET_OK;
}
/* Target method */
static int skeleton_resume_from_addr(int step, int sig, uint64_t addr)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_resume_from_addr(%s, %d, 0x%llX)",
skeleton_target.name,
(step) ? "step" : "run",
sig,
addr);
assert(skeleton_status.is_open);
skeleton_status.registers[RP_SKELETON_REGNUM_PC] = addr;
/* TODO: Update the PC register in the target */
/* TODO: Run the target from the new PC address. */
skeleton_status.target_running = TRUE;
skeleton_status.target_interrupted = FALSE;
return RP_VAL_TARGETRET_OK;
}
/* Target method */
static int skeleton_go_waiting(int sig)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_go_waiting()",
skeleton_target.name);
return RP_VAL_TARGETRET_NOSUPP;
}
/* Target method */
static int skeleton_wait_partial(int first,
char *status_string,
size_t status_string_len,
out_func of,
int *implemented,
int *more)
{
int state;
char *cp;
int sig;
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_wait_partial()",
skeleton_target.name);
assert(skeleton_status.is_open);
assert(status_string != NULL);
assert(status_string_len >= 34);
assert(of != NULL);
assert(implemented != NULL);
assert(more != NULL);
*implemented = TRUE;
if (!skeleton_status.target_running)
return RP_VAL_TARGETRET_NOSUPP;
#ifdef WIN32
sleep((first) ? 500 : 100);
#else
usleep((first) ? 500000 : 100000);
#endif
/* TODO: Test the target state (i.e. running/stopped) without blocking */
/* If the target only supports a blocking form of test return no support,
and the blocking version of this test will be called instead. That is
not so nice, as the system is less interactive using a blocking test. */
state = RP_SKELETON_TARGET_STATE_RUNNING;
if (state == RP_SKELETON_TARGET_STATE_RUNNING)
{
*more = TRUE;
return RP_VAL_TARGETRET_OK;
}
switch (state)
{
case RP_SKELETON_TARGET_STATE_STOPPED:
if (skeleton_status.target_interrupted)
sig = RP_SIGNAL_INTERRUPT;
else
sig = RP_SIGNAL_ABORTED;
break;
case RP_SKELETON_TARGET_STATE_RUNNING:
*more = TRUE;
return RP_VAL_TARGETRET_OK;
case RP_SKELETON_TARGET_STATE_SINGLE_STEP_COMPLETE:
sig = RP_SIGNAL_BREAKPOINT;
break;
case RP_SKELETON_TARGET_STATE_BREAKPOINT_HIT:
sig = RP_SIGNAL_BREAKPOINT;
break;
default:
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: unexpected state %d for the SKELETON",
skeleton_target.name,
state);
sig = RP_SIGNAL_ABORTED;
break;
}
/* Fill out the status string */
sprintf(status_string, "T%02d", sig);
if (refresh_registers())
return RP_VAL_TARGETRET_ERR;
cp = skeleton_out_treg(&status_string[3], RP_SKELETON_REGNUM_PC);
cp = skeleton_out_treg(cp, RP_SKELETON_REGNUM_FP);
*more = FALSE;
return (cp != NULL) ? RP_VAL_TARGETRET_OK : RP_VAL_TARGETRET_ERR;
}
/* Target method */
static int skeleton_wait(char *status_string,
size_t status_string_len,
out_func of,
int *implemented)
{
int state;
char *cp;
int sig;
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_wait()",
skeleton_target.name);
assert(skeleton_status.is_open);
assert(status_string != NULL);
assert(status_string_len >= 34);
assert(of != NULL);
assert(implemented != NULL);
*implemented = TRUE;
if (!skeleton_status.target_running)
return RP_VAL_TARGETRET_NOSUPP;
/* TODO: Wait for the target to stop */
state = RP_SKELETON_TARGET_STATE_STOPPED;
switch (state)
{
case RP_SKELETON_TARGET_STATE_STOPPED:
sig = RP_SIGNAL_ABORTED;
break;
case RP_SKELETON_TARGET_STATE_SINGLE_STEP_COMPLETE:
sig = RP_SIGNAL_BREAKPOINT;
break;
case RP_SKELETON_TARGET_STATE_BREAKPOINT_HIT:
sig = RP_SIGNAL_BREAKPOINT;
break;
default:
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: unexpected state %d for the SKELETON",
skeleton_target.name,
state);
sig = RP_SIGNAL_ABORTED;
break;
}
/* Fill out the status string */
sprintf(status_string, "T%02d", sig);
if (refresh_registers())
return RP_VAL_TARGETRET_ERR;
cp = skeleton_out_treg(&status_string[3], RP_SKELETON_REGNUM_PC);
cp = skeleton_out_treg(cp, RP_SKELETON_REGNUM_FP);
return (cp != NULL) ? RP_VAL_TARGETRET_OK : RP_VAL_TARGETRET_ERR;
}
/* Target method */
static int skeleton_process_query(unsigned int *mask,
rp_thread_ref *arg,
rp_thread_info *info)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_process_query()",
skeleton_target.name);
/* TODO: Does your target support threads? Is so, implement this function.
Otherwise just return no support. */
return RP_VAL_TARGETRET_NOSUPP;
}
/* Target method */
static int skeleton_list_query(int first,
rp_thread_ref *arg,
rp_thread_ref *result,
size_t max_num,
size_t *num,
int *done)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_list_query()",
skeleton_target.name);
/* TODO: Does your target support threads? Is so, implement this function.
Otherwise just return no support. */
return RP_VAL_TARGETRET_NOSUPP;
}
/* Target method */
static int skeleton_current_thread_query(rp_thread_ref *thread)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_current_thread_query()",
skeleton_target.name);
/* TODO: Does your target support threads? Is so, implement this function.
Otherwise just return no support. */
return RP_VAL_TARGETRET_NOSUPP;
}
/* Target method */
static int skeleton_offsets_query(uint64_t *text, uint64_t *data, uint64_t *bss)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_offsets_query()",
skeleton_target.name);
assert(skeleton_status.is_open);
assert(text != NULL);
assert(data != NULL);
assert(bss != NULL);
/* TODO: Is this what *your* target really needs? */
*text = 0;
*data = 0;
*bss = 0;
return RP_VAL_TARGETRET_OK;
}
/* Target method */
static int skeleton_crc_query(uint64_t addr, size_t len, uint32_t *val)
{
uint8_t buf[1] = {0};
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_crc_query()",
skeleton_target.name);
assert(skeleton_status.is_open);
if (addr > RP_SKELETON_MAX_ADDRESS || addr + len > RP_SKELETON_MAX_ADDRESS + 1)
{
skeleton_status.log(RP_VAL_LOGLEVEL_ERR,
"%s: bad address 0x%llx",
skeleton_target.name,
addr);
return RP_VAL_TARGETRET_ERR;
}
/* TODO: Read the target memory,and use the crc32 routine to calculate
the CRC value to be returned. */
/* Note: The CRC can be calculated in chunks. The first call to crc32
should set the current CRC value to all 1's, as this is the priming
value for CRC32. Subsequent calls should set the current CRC to the
value returned by the previous call, until all the data has been
processed. */
*val = crc32(buf, sizeof(buf), 0xFFFFFFFF);
return RP_VAL_TARGETRET_OK;
}
/* Target method */
static int skeleton_raw_query(char *in_buf, char *out_buf, size_t out_buf_size)
{
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_raw_query()",
skeleton_target.name);
return RP_VAL_TARGETRET_NOSUPP;
}
static int tohex(char *s, const char *t)
{
int i;
static char hex[] = "0123456789abcdef";
i = 0;
while (*t)
{
*s++ = hex[(*t >> 4) & 0x0f];
*s++ = hex[*t & 0x0f];
t++;
i++;
}
*s = '\0';
return i;
}
/* command: erase flash */
static int skeleton_rcmd_erase(int argc, char *argv[], out_func of, data_func df)
{
char buf[1000 + 1];
skeleton_status.log(RP_VAL_LOGLEVEL_DEBUG,
"%s: skeleton_rcmd_erase()",
skeleton_target.name);