summaryrefslogtreecommitdiffstats
path: root/base/image_util.h
blob: 0fd4a8f632e044135d1501c460eae96419b08e5b (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
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file/namespace contains utility functions for gathering
// information about PE (Portable Executable) headers within
// images (dll's / exe's )

#ifndef BASE_IMAGE_UTIL_H_
#define BASE_IMAGE_UTIL_H_

#include <windows.h>
#include <vector>

#include "base/basictypes.h"

namespace image_util {

// Contains both the PE section name (.text, .reloc etc) and its size.
struct ImageSectionData {
  ImageSectionData (const std::string& section_name, size_t section_size)
      : name (section_name),
        size_in_bytes(section_size) {
  }

  std::string name;
  size_t size_in_bytes;
};

typedef std::vector<ImageSectionData> ImageSectionsData;

// Provides image statistics for modules of a specified process, or for the
// specified process' own executable file. To use, invoke CreateImageMetrics()
// to get an instance for a specified process, then access the information via
// methods.
class ImageMetrics {
 public:
  // Creates an ImageMetrics instance for given process owned by
  // the caller.
  explicit ImageMetrics(HANDLE process);
  ~ImageMetrics();

  // Fills a vector of ImageSectionsData containing name/size info
  // for every section found in the specified dll's PE section table.
  // The DLL must be loaded by the process associated with this ImageMetrics
  // instance.
  bool GetDllImageSectionData(const std::string& loaded_dll_name,
                              ImageSectionsData* section_sizes);

  // Fills a vector if ImageSectionsData containing name/size info
  // for every section found in the executable file of the process
  // associated with this ImageMetrics instance.
  bool GetProcessImageSectionData(ImageSectionsData* section_sizes);

 private:
  // Helper for GetDllImageSectionData and GetProcessImageSectionData
  bool GetImageSectionSizes(char* qualified_path, ImageSectionsData* result);

  HANDLE process_;

  DISALLOW_COPY_AND_ASSIGN(ImageMetrics);
};

}  // namespace image_util

#endif  // BASE_IMAGE_UTIL_H_