Implement pahole-like 'ptype /o' option
[external/binutils.git] / gdb / target-memory.c
index 64c16e1..7f048de 100644 (file)
@@ -1,14 +1,13 @@
 /* Parts of target interface that deal with accessing memory and memory-like
    objects.
 
-   Copyright (C) 2006
-   Free Software Foundation, Inc.
+   Copyright (C) 2006-2017 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
 #include "vec.h"
 #include "target.h"
 #include "memory-map.h"
 
-#include "gdb_assert.h"
-
-#include <stdio.h>
-#include <sys/time.h>
+#include "gdb_sys_time.h"
+#include <algorithm>
 
 static int
 compare_block_starting_address (const void *a, const void *b)
 {
-  const struct memory_write_request *a_req = a;
-  const struct memory_write_request *b_req = b;
+  const struct memory_write_request *a_req
+    = (const struct memory_write_request *) a;
+  const struct memory_write_request *b_req
+    = (const struct memory_write_request *) b;
 
   if (a_req->begin < b_req->begin)
     return -1;
@@ -75,11 +72,11 @@ claim_memory (VEC(memory_write_request_s) *blocks,
       if (end != 0 && end <= r->begin)
        continue;
 
-      claimed_begin = max (begin, r->begin);
+      claimed_begin = std::max (begin, r->begin);
       if (end == 0)
        claimed_end = r->end;
       else
-       claimed_end = min (end, r->end);
+       claimed_end = std::min (end, r->end);
 
       if (claimed_begin == r->begin && claimed_end == r->end)
        VEC_safe_push (memory_write_request_s, *result, r);
@@ -87,10 +84,11 @@ claim_memory (VEC(memory_write_request_s) *blocks,
        {
          struct memory_write_request *n =
            VEC_safe_push (memory_write_request_s, *result, NULL);
-         memset (n, 0, sizeof (struct memory_write_request));
+
+         *n = *r;
          n->begin = claimed_begin;
          n->end = claimed_end;
-         n->data = r->data + (claimed_begin - r->begin);
+         n->data += claimed_begin - r->begin;
        }
     }
 }
@@ -119,8 +117,8 @@ split_regular_and_flash_blocks (VEC(memory_write_request_s) *blocks,
   while (1)
     {
       VEC(memory_write_request_s) **r;
-      region = lookup_mem_region (cur_address);
 
+      region = lookup_mem_region (cur_address);
       r = region->attrib.mode == MEM_FLASH ? flash_blocks : regular_blocks;
       cur_address = region->hi;
       claim_memory (blocks, r, region->lo, region->hi);
@@ -140,14 +138,18 @@ block_boundaries (CORE_ADDR address, CORE_ADDR *begin, CORE_ADDR *end)
 {
   struct mem_region *region;
   unsigned blocksize;
+  CORE_ADDR offset_in_region;
 
   region = lookup_mem_region (address);
   gdb_assert (region->attrib.mode == MEM_FLASH);
   blocksize = region->attrib.blocksize;
+
+  offset_in_region = address - region->lo;
+
   if (begin)
-    *begin = address / blocksize * blocksize;
+    *begin = region->lo + offset_in_region / blocksize * blocksize;
   if (end)
-    *end = (address + blocksize - 1) / blocksize * blocksize;
+    *end = region->lo + (offset_in_region + blocksize - 1) / blocksize * blocksize;
 }
 
 /* Given the list of memory requests to be WRITTEN, this function
@@ -167,7 +169,7 @@ blocks_to_erase (VEC(memory_write_request_s) *written)
       CORE_ADDR begin, end;
 
       block_boundaries (ptr->begin, &begin, 0);
-      block_boundaries (ptr->end, 0, &end);
+      block_boundaries (ptr->end - 1, 0, &end);
 
       if (!VEC_empty (memory_write_request_s, result)
          && VEC_last (memory_write_request_s, result)->end >= begin)
@@ -178,6 +180,7 @@ blocks_to_erase (VEC(memory_write_request_s) *written)
        {
          struct memory_write_request *n =
            VEC_safe_push (memory_write_request_s, result, NULL);
+
          memset (n, 0, sizeof (struct memory_write_request));
          n->begin = begin;
          n->end = end;
@@ -258,6 +261,7 @@ compute_garbled_blocks (VEC(memory_write_request_s) *erased_blocks,
            {
              struct memory_write_request *n =
                VEC_safe_push (memory_write_request_s, result, NULL);
+
              memset (n, 0, sizeof (struct memory_write_request));
              n->begin = erased.begin;
              n->end = written->begin;
@@ -291,7 +295,7 @@ compute_garbled_blocks (VEC(memory_write_request_s) *erased_blocks,
 static void
 cleanup_request_data (void *p)
 {
-  VEC(memory_write_request_s) **v = p;
+  VEC(memory_write_request_s) **v = (VEC(memory_write_request_s) **) p;
   struct memory_write_request *r;
   int i;
 
@@ -302,7 +306,8 @@ cleanup_request_data (void *p)
 static void
 cleanup_write_requests_vector (void *p)
 {
-  VEC(memory_write_request_s) **v = p;
+  VEC(memory_write_request_s) **v = (VEC(memory_write_request_s) **) p;
+
   VEC_free (memory_write_request_s, *v);
 }
 
@@ -337,7 +342,7 @@ target_write_memory_blocks (VEC(memory_write_request_s) *requests,
         sizeof (struct memory_write_request), compare_block_starting_address);
 
   /* Split blocks into list of regular memory blocks,
-     and list of flash memory blocks. */
+     and list of flash memory blocks.  */
   make_cleanup (cleanup_write_requests_vector, &regular);
   make_cleanup (cleanup_write_requests_vector, &flash);
   split_regular_and_flash_blocks (blocks, &regular, &flash);
@@ -368,7 +373,7 @@ target_write_memory_blocks (VEC(memory_write_request_s) *requests,
          for (i = 0; VEC_iterate (memory_write_request_s, garbled, i, r); ++i)
            {
              gdb_assert (r->data == NULL);
-             r->data = xmalloc (r->end - r->begin);
+             r->data = (gdb_byte *) xmalloc (r->end - r->begin);
              err = target_read_memory (r->begin, r->data, r->end - r->begin);
              if (err != 0)
                goto out;
@@ -378,7 +383,8 @@ target_write_memory_blocks (VEC(memory_write_request_s) *requests,
 
          qsort (VEC_address (memory_write_request_s, flash),
                 VEC_length (memory_write_request_s, flash),
-                sizeof (struct memory_write_request), compare_block_starting_address);
+                sizeof (struct memory_write_request),
+                compare_block_starting_address);
        }
     }
 
@@ -396,7 +402,7 @@ target_write_memory_blocks (VEC(memory_write_request_s) *requests,
     {
       LONGEST len;
 
-      len = target_write_with_progress (&current_target,
+      len = target_write_with_progress (current_target.beneath,
                                        TARGET_OBJECT_MEMORY, NULL,
                                        r->data, r->begin, r->end - r->begin,
                                        progress_cb, r->baton);
@@ -421,7 +427,8 @@ target_write_memory_blocks (VEC(memory_write_request_s) *requests,
 
          len = target_write_with_progress (&current_target,
                                            TARGET_OBJECT_FLASH, NULL,
-                                           r->data, r->begin, r->end - r->begin,
+                                           r->data, r->begin,
+                                           r->end - r->begin,
                                            progress_cb, r->baton);
          if (len < (LONGEST) (r->end - r->begin))
            error (_("Error writing data to flash"));