summaryrefslogtreecommitdiff
path: root/src/memmaps.cpp
blob: 352ed45fd644d947e2f1cfcc026aa6b829ff902e (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
#include <config.h>

#include <glibtop/procmap.h>
#include <glibtop/mountlist.h>
#include <sys/stat.h>
#include <glib/gi18n.h>

#include <string>
#include <map>
#include <sstream>
#include <iomanip>
#include <stdexcept>

using std::string;


#include "procman.h"
#include "memmaps.h"
#include "proctable.h"
#include "util.h"


/* be careful with this enum, you could break the column names */
enum
{
	MMAP_COL_FILENAME,
	MMAP_COL_VMSTART,
	MMAP_COL_VMEND,
	MMAP_COL_VMSZ,
	MMAP_COL_FLAGS,
	MMAP_COL_VMOFFSET,
	MMAP_COL_PRIVATE_CLEAN,
	MMAP_COL_PRIVATE_DIRTY,
	MMAP_COL_SHARED_CLEAN,
	MMAP_COL_SHARED_DIRTY,
	MMAP_COL_DEVICE,
	MMAP_COL_INODE,
	MMAP_COL_MAX
};


namespace
{
  class OffsetFormater
  {
    string format;

  public:

    void set(const glibtop_map_entry &last_map)
    {
      this->format = (last_map.end <= G_MAXUINT32) ? "%08" G_GINT64_MODIFIER "x" : "%016" G_GINT64_MODIFIER "x";
    }

    string operator()(guint64 v) const
    {
      char buffer[17];
      g_snprintf(buffer, sizeof buffer, this->format.c_str(), v);
      return buffer;
    }
  };




#if 0

  struct ColumnState
  {
    unsigned visible;
    unsigned id;
    unsigned width;

    int pack() const
    {
      unsigned p = 0;
      p |= (this->visible & 0x0001) << 24;
      p |= (this->id      & 0x00ff) << 16;
      p |= (this->width   & 0xffff);
      return p;
    }

    void unpack(int i)
    {
      this->visible = 0x0001 & (i >> 24);
      this->id      = 0x00ff & (i >> 16);
      this->width   = 0xffff & i;
    }
  };




#endif



  class InodeDevices
  {
    typedef std::map<guint16, string> Map;
    Map devices;

  public:

    void update()
    {
      this->devices.clear();

      glibtop_mountlist list;
      glibtop_mountentry *entries = glibtop_get_mountlist(&list, 1);

      for (unsigned i = 0; i != list.number; ++i) {
	struct stat buf;

	if (stat(entries[i].devname, &buf) != -1)
	  this->devices[buf.st_rdev] = entries[i].devname;
      }

      g_free(entries);
    }

    string get(guint64 dev64)
    {
      if (dev64 == 0)
	return "";

      guint16 dev = dev64 & 0xffff;

      if (dev != dev64)
	g_warning("weird device %" G_GINT64_MODIFIER "x", dev64);

      Map::iterator it(this->devices.find(dev));

      if (it != this->devices.end())
	return it->second;

      guint8 major, minor;
      major = dev >> 8;
      minor = dev;

      std::ostringstream out;
      out << std::hex
	  << std::setfill('0')
	  << std::setw(2) << unsigned(major)
	  << ':'
	  << std::setw(2) << unsigned(minor);

      this->devices[dev] = out.str();
      return out.str();
    }
  };


  class MemMapsData
  {
  public:
    guint timer;
    GtkWidget *tree;
    GSettings *settings;
    ProcInfo *info;
    OffsetFormater format;
    mutable InodeDevices devices;
    const char * const schema;

    MemMapsData(GtkWidget *a_tree, GSettings *a_settings)
      : tree(a_tree),
	settings(a_settings),
	schema("memmapstree")
    {
      procman_get_tree_state(this->settings, this->tree, this->schema);
    }

    ~MemMapsData()
    {
      procman_save_tree_state(this->settings, this->tree, this->schema);
    }
  };
}


struct glibtop_map_entry_cmp
{
  bool operator()(const glibtop_map_entry &a, const guint64 start) const
  {
    return a.start < start;
  }

  bool operator()(const guint64 &start, const glibtop_map_entry &a) const
  {
    return not (*this)(a, start);
  }

};


static void
update_row(GtkTreeModel *model, GtkTreeIter &row, const MemMapsData &mm, const glibtop_map_entry *memmaps)
{
	guint64 size;
	string filename, device;
	string vmstart, vmend, vmoffset;
	char flags[5] = "----";

	size = memmaps->end - memmaps->start;

	if(memmaps->perm & GLIBTOP_MAP_PERM_READ)    flags [0] = 'r';
	if(memmaps->perm & GLIBTOP_MAP_PERM_WRITE)   flags [1] = 'w';
	if(memmaps->perm & GLIBTOP_MAP_PERM_EXECUTE) flags [2] = 'x';
	if(memmaps->perm & GLIBTOP_MAP_PERM_SHARED)  flags [3] = 's';
	if(memmaps->perm & GLIBTOP_MAP_PERM_PRIVATE) flags [3] = 'p';

	if (memmaps->flags & (1 << GLIBTOP_MAP_ENTRY_FILENAME))
	  filename = memmaps->filename;

	vmstart  = mm.format(memmaps->start);
	vmend    = mm.format(memmaps->end);
	vmoffset = mm.format(memmaps->offset);
	device   = mm.devices.get(memmaps->device);

	gtk_list_store_set (GTK_LIST_STORE (model), &row,
			    MMAP_COL_FILENAME, filename.c_str(),
			    MMAP_COL_VMSTART, vmstart.c_str(),
			    MMAP_COL_VMEND, vmend.c_str(),
			    MMAP_COL_VMSZ, size,
			    MMAP_COL_FLAGS, flags,
			    MMAP_COL_VMOFFSET, vmoffset.c_str(),
			    MMAP_COL_PRIVATE_CLEAN, memmaps->private_clean,
			    MMAP_COL_PRIVATE_DIRTY, memmaps->private_dirty,
			    MMAP_COL_SHARED_CLEAN, memmaps->shared_clean,
			    MMAP_COL_SHARED_DIRTY, memmaps->shared_dirty,
			    MMAP_COL_DEVICE, device.c_str(),
			    MMAP_COL_INODE, memmaps->inode,
			    -1);
}




static void
update_memmaps_dialog (MemMapsData *mmdata)
{
	GtkTreeModel *model;
	glibtop_map_entry *memmaps;
	glibtop_proc_map procmap;

	memmaps = glibtop_get_proc_map (&procmap, mmdata->info->pid);
	/* process has disappeared */
	if(!memmaps or procmap.number == 0) return;

	mmdata->format.set(memmaps[procmap.number - 1]);

	model = gtk_tree_view_get_model (GTK_TREE_VIEW (mmdata->tree));

	GtkTreeIter iter;

	typedef std::map<guint64, GtkTreeIter> IterCache;
	IterCache iter_cache;

	/*
	  removes the old maps and
	   also fills a cache of start -> iter in order to speed
	   up add
	*/

	if (gtk_tree_model_get_iter_first(model, &iter)) {
	  while (true) {
	    char *vmstart = 0;
	    guint64 start;
	    gtk_tree_model_get(model, &iter,
			       MMAP_COL_VMSTART, &vmstart,
			       -1);

	    try {
		    std::istringstream(vmstart) >> std::hex >> start;
	    } catch (std::logic_error &e) {
		    g_warning("Could not parse %s", vmstart);
		    start = 0;
	    }

	    g_free(vmstart);

	    bool found = std::binary_search(memmaps, memmaps + procmap.number,
					    start, glibtop_map_entry_cmp());

	    if (found) {
	      iter_cache[start] = iter;
	      if (!gtk_tree_model_iter_next(model, &iter))
		break;
	    } else {
	      if (!gtk_list_store_remove(GTK_LIST_STORE(model), &iter))
		break;
	    }
	  }
	}

	mmdata->devices.update();

	/*
	  add the new maps
	*/

	for (guint i = 0; i != procmap.number; i++) {
	  GtkTreeIter iter;
	  IterCache::iterator it(iter_cache.find(memmaps[i].start));

	  if (it != iter_cache.end())
	    iter = it->second;
	  else
	    gtk_list_store_prepend(GTK_LIST_STORE(model), &iter);

	  update_row(model, iter, *mmdata, &memmaps[i]);
	}

	g_free (memmaps);
}



static void
dialog_response (GtkDialog * dialog, gint response_id, gpointer data)
{
	MemMapsData * const mmdata = static_cast<MemMapsData*>(data);

	g_source_remove (mmdata->timer);

	delete mmdata;
	gtk_widget_destroy (GTK_WIDGET (dialog));
}


static MemMapsData*
create_memmapsdata (ProcData *procdata)
{
	GtkWidget *tree;
	GtkListStore *model;
	guint i;

	const gchar * const titles[] = {
		N_("Filename"),
		// xgettext: virtual memory start
		N_("VM Start"),
		// xgettext: virtual memory end
		N_("VM End"),
		// xgettext: virtual memory syze
		N_("VM Size"),
		N_("Flags"),
		// xgettext: virtual memory offset
		N_("VM Offset"),
		// xgettext: memory that has not been modified since
		// it has been allocated
		N_("Private clean"),
		// xgettext: memory that has been modified since it
		// has been allocated
		N_("Private dirty"),
		// xgettext: shared memory that has not been modified
		// since it has been allocated
		N_("Shared clean"),
		// xgettext: shared memory that has been modified
		// since it has been allocated
		N_("Shared dirty"),
		N_("Device"),
		N_("Inode")
	};

	model = gtk_list_store_new (MMAP_COL_MAX,
				    G_TYPE_STRING, /* MMAP_COL_FILENAME  */
				    G_TYPE_STRING, /* MMAP_COL_VMSTART	 */
				    G_TYPE_STRING, /* MMAP_COL_VMEND	 */
				    G_TYPE_UINT64, /* MMAP_COL_VMSZ	 */
				    G_TYPE_STRING, /* MMAP_COL_FLAGS	 */
				    G_TYPE_STRING, /* MMAP_COL_VMOFFSET  */
				    G_TYPE_UINT64, /* MMAP_COL_PRIVATE_CLEAN */
				    G_TYPE_UINT64, /* MMAP_COL_PRIVATE_DIRTY */
				    G_TYPE_UINT64, /* MMAP_COL_SHARED_CLEAN */
				    G_TYPE_UINT64, /* MMAP_COL_SHARED_DIRTY */
				    G_TYPE_STRING, /* MMAP_COL_DEVICE	 */
				    G_TYPE_UINT64 /* MMAP_COL_INODE	 */
				    );

	tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
	gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (tree), TRUE);
	g_object_unref (G_OBJECT (model));

	for (i = 0; i < MMAP_COL_MAX; i++) {
		GtkCellRenderer *cell;
		GtkTreeViewColumn *col;

		cell = gtk_cell_renderer_text_new();
		col = gtk_tree_view_column_new();
		gtk_tree_view_column_pack_start(col, cell, TRUE);
		gtk_tree_view_column_set_title(col, _(titles[i]));
		gtk_tree_view_column_set_resizable(col, TRUE);
		gtk_tree_view_column_set_sort_column_id(col, i);
		gtk_tree_view_column_set_reorderable(col, TRUE);
		gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);

		switch (i) {
		case MMAP_COL_PRIVATE_CLEAN:
		case MMAP_COL_PRIVATE_DIRTY:
		case MMAP_COL_SHARED_CLEAN:
		case MMAP_COL_SHARED_DIRTY:
		case MMAP_COL_VMSZ:
		  gtk_tree_view_column_set_cell_data_func(col, cell,
							  &procman::size_cell_data_func,
							  GUINT_TO_POINTER(i),
							  NULL);

		  g_object_set(cell, "xalign", 1.0f, NULL);
		  break;

		default:
		  gtk_tree_view_column_set_attributes(col, cell, "text", i, NULL);
		  break;
		}


		switch (i) {
		case MMAP_COL_VMSTART:
		case MMAP_COL_VMEND:
		case MMAP_COL_FLAGS:
		case MMAP_COL_VMOFFSET:
		case MMAP_COL_DEVICE:
			g_object_set(cell, "family", "monospace", NULL);
			break;
		}
	}

	return new MemMapsData(tree, procdata->settings);
}


static gboolean
memmaps_timer (gpointer data)
{
	MemMapsData * const mmdata = static_cast<MemMapsData*>(data);
	GtkTreeModel *model;

	model = gtk_tree_view_get_model (GTK_TREE_VIEW (mmdata->tree));
	g_assert(model);

	update_memmaps_dialog (mmdata);

	return TRUE;
}


static void
create_single_memmaps_dialog (GtkTreeModel *model, GtkTreePath *path,
			      GtkTreeIter *iter, gpointer data)
{
	ProcData * const procdata = static_cast<ProcData*>(data);
	MemMapsData *mmdata;
	GtkWidget *memmapsdialog;
	GtkWidget *dialog_vbox;
	GtkWidget *label;
	GtkWidget *scrolled;
	ProcInfo *info;

	gtk_tree_model_get (model, iter, COL_POINTER, &info, -1);

	if (!info)
		return;

	mmdata = create_memmapsdata (procdata);
	mmdata->info = info;

	memmapsdialog = gtk_dialog_new_with_buttons (_("Memory Maps"), GTK_WINDOW (procdata->app),
						     GTK_DIALOG_DESTROY_WITH_PARENT,
                                                     GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
                                                     NULL);
	gtk_window_set_resizable(GTK_WINDOW(memmapsdialog), TRUE);
	gtk_window_set_default_size(GTK_WINDOW(memmapsdialog), 575, 400);
	gtk_container_set_border_width(GTK_CONTAINER(memmapsdialog), 5);

	dialog_vbox = gtk_dialog_get_content_area (GTK_DIALOG(memmapsdialog));
	gtk_container_set_border_width (GTK_CONTAINER (dialog_vbox), 5);

	label = procman_make_label_for_mmaps_or_ofiles (
		_("_Memory maps for process \"%s\" (PID %u):"),
		info->name,
		info->pid);

	gtk_box_pack_start (GTK_BOX (dialog_vbox), label, FALSE, TRUE, 0);


	scrolled = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
					GTK_POLICY_AUTOMATIC,
					GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled),
					     GTK_SHADOW_IN);

	gtk_container_add (GTK_CONTAINER (scrolled), mmdata->tree);
	gtk_label_set_mnemonic_widget (GTK_LABEL (label), mmdata->tree);

	gtk_box_pack_start (GTK_BOX (dialog_vbox), scrolled, TRUE, TRUE, 0);

	g_signal_connect(G_OBJECT(memmapsdialog), "response",
			 G_CALLBACK(dialog_response), mmdata);

	gtk_widget_show_all (memmapsdialog);

	mmdata->timer = g_timeout_add_seconds (5, memmaps_timer, mmdata);

	update_memmaps_dialog (mmdata);
}


void
create_memmaps_dialog (ProcData *procdata)
{
	/* TODO: do we really want to open multiple dialogs ? */
	gtk_tree_selection_selected_foreach (procdata->selection, create_single_memmaps_dialog,
					     procdata);
}