int gdbr_write_memory(libgdbr_t *g, ut64 address, const uint8_t *data, ut64 len) { int ret = 0; int command_len, pkt, max_cmd_len = 64; ut64 num_pkts, last, data_sz; char *tmp; if (!g || !data) { return -1; } g->stub_features.pkt_sz = R_MAX (g->stub_features.pkt_sz, 64); data_sz = g->stub_features.pkt_sz / 2; if (data_sz < 1) { return -1; } num_pkts = len / data_sz; last = len % data_sz; if (!(tmp = calloc (max_cmd_len + g->stub_features.pkt_sz, sizeof (char)))) { return -1; } for (pkt = num_pkts - 1; pkt >= 0; pkt--) { if ((command_len = snprintf (tmp, max_cmd_len, "%s%016"PFMT64x ",%"PFMT64x ":", CMD_WRITEMEM, address + (pkt * data_sz), data_sz)) < 0) { goto fail; } pack_hex ((char *) data + (pkt * data_sz), data_sz, (tmp + command_len)); if ((ret = send_msg (g, tmp)) < 0) { goto fail; } if ((ret = read_packet (g)) < 0) { goto fail; } if ((ret = handle_M (g)) < 0) { goto fail; } } if (last) { if ((command_len = snprintf (tmp, max_cmd_len, "%s%016"PFMT64x ",%"PFMT64x ":", CMD_WRITEMEM, address + (num_pkts * data_sz), last)) < 0) { goto fail; } pack_hex ((char *) data + (num_pkts * data_sz), last, (tmp + command_len)); if ((ret = send_msg (g, tmp)) < 0) { goto fail; } if ((ret = read_packet (g)) < 0) { goto fail; } if ((ret = handle_M (g)) < 0) { goto fail; } } free (tmp); return 0; fail: free (tmp); return -1; }
int stub_main(int arch_reason) { int ret; int gdb_signal; gdb_signal = decode_signal(arch_reason); /* Do not announce stop reply if we were detached. */ if (isattached) { send_stop_reply(gdb_signal); } /* We are now attached. */ isattached = 1; do { int cmd; char *p; while (getpkt(&pktbuf[0], sizeof(pktbuf)) < 1); p = &pktbuf[0]; cmd = *p; if ('?' == cmd) { send_stop_reply(gdb_signal); ret = 0; } else if ('c' == cmd) { ret = handle_c(p); } else if ('s' == cmd) { ret = handle_s(p); } else if ('D' == cmd) { ret = handle_D(p); if (ret & CMD_LEAVE) { isattached = 0; } } else if ('g' == cmd) { ret = handle_g(p); } else if ('G' == cmd) { ret = handle_G(p); } else if ('P' == cmd) { ret = handle_P(p); } else if ('m' == cmd) { ret = handle_m(p); } else if ('M' == cmd) { ret = handle_M(p); } else { /* * For any command not supported by the stub, an empty * response ('$#00') should be returned. */ p = ""; ret = CMD_REPLY; } if (ret & CMD_REPLY) { putpkt(p); } } while (!(ret & CMD_LEAVE)); return ret; }
int gdbr_write_memory(libgdbr_t* g, uint64_t address, const uint8_t* data, uint64_t len) { int ret = 0; char command[255] = {}; int command_len = snprintf (command, 255, "%s%016lx,%ld:", CMD_WRITEMEM, address, len); char* tmp = calloc (command_len + (len * 2), sizeof (uint8_t)); if (!tmp) return -1; memcpy (tmp, command, command_len); pack_hex ((char*)data, len, (tmp + command_len)); ret = send_command (g, tmp); free (tmp); if (ret < 0) return ret; if (read_packet (g) > 0) { parse_packet (g, 0); return handle_M (g); } return -1; }