File: PlatformProcess.cpp

package info (click to toggle)
edb-debugger 1.3.0-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,124 kB
  • sloc: cpp: 46,241; xml: 4,998; ansic: 3,088; sh: 52; asm: 33; makefile: 5
file content (1208 lines) | stat: -rw-r--r-- 30,189 bytes parent folder | download | duplicates (4)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
/*
Copyright (C) 2015 - 2015 Evan Teran
                          evan.teran@gmail.com

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
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
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, see <http://www.gnu.org/licenses/>.
*/

#ifndef _LARGEFILE64_SOURCE
#define _LARGEFILE64_SOURCE
#endif

#include "PlatformProcess.h"
#include "ByteShiftArray.h"
#include "DebuggerCore.h"
#include "IBreakpoint.h"
#include "MemoryRegions.h"
#include "Module.h"
#include "PlatformCommon.h"
#include "PlatformRegion.h"
#include "PlatformThread.h"
#include "edb.h"
#include "libELF/elf_binary.h"
#include "libELF/elf_model.h"
#include "linker.h"
#include "util/Container.h"

#include <QByteArray>
#include <QDateTime>
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QTextStream>

#include <boost/functional/hash.hpp>
#include <fstream>

#include <elf.h>
#include <linux/limits.h>
#include <pwd.h>
#include <sys/mman.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <unistd.h>

namespace DebuggerCorePlugin {
namespace {

// Used as size of ptrace word
constexpr size_t WordSize = sizeof(long);

/**
 * @brief set_ok
 * @param value
 */
bool set_ok(long value) {
	return (value != -1) || (errno == 0);
}

/**
 * @brief split_max
 * @param str
 * @param maxparts
 * @return
 */
QStringList split_max(const QString &str, int maxparts) {
	int prev_idx = 0;
	int idx      = 0;
	QStringList items;
	for (const QChar &c : str) {
		if (c == ' ') {
			if (prev_idx < idx) {
				if (items.size() < maxparts - 1)
					items << str.mid(prev_idx, idx - prev_idx);
				else {
					items << str.right(str.size() - prev_idx);
					break;
				}
			}
			prev_idx = idx + 1;
		}
		++idx;
	}

	if (prev_idx < str.size() && items.size() < maxparts) {
		items << str.right(str.size() - prev_idx);
	}

	return items;
}

/**
 * parses the data from a line of a memory map file
 *
 * @brief process_map_line
 * @param line
 * @return
 */
std::shared_ptr<IRegion> process_map_line(const QString &line) {

	edb::address_t start;
	edb::address_t end;
	edb::address_t base;
	IRegion::permissions_t permissions;
	QString name;

	const QStringList items = split_max(line, 6);
	if (items.size() >= 3) {
		bool ok;
		const QStringList bounds = items[0].split("-");
		if (bounds.size() == 2) {
			start = edb::address_t::fromHexString(bounds[0], &ok);
			if (ok) {
				end = edb::address_t::fromHexString(bounds[1], &ok);
				if (ok) {
					base = edb::address_t::fromHexString(items[2], &ok);
					if (ok) {
						const QString perms = items[1];
						permissions         = 0;
						if (perms[0] == 'r') permissions |= PROT_READ;
						if (perms[1] == 'w') permissions |= PROT_WRITE;
						if (perms[2] == 'x') permissions |= PROT_EXEC;

						if (items.size() >= 6) {
							name = items[5];
						}

						return std::make_shared<PlatformRegion>(start, end, base, name, permissions);
					}
				}
			}
		}
	}
	return nullptr;
}

/**
 * @brief get_loaded_modules
 * @param process
 * @return
 */
template <class Addr>
QList<Module> get_loaded_modules(const IProcess *process) {

	QList<Module> ret;

	edb::linux_struct::r_debug<Addr> dynamic_info;
	if (process) {
		if (const edb::address_t debug_pointer = process->debugPointer()) {
			if (process->readBytes(debug_pointer, &dynamic_info, sizeof(dynamic_info))) {
				if (dynamic_info.r_map) {

					auto link_address = edb::address_t::fromZeroExtended(dynamic_info.r_map);

					while (link_address) {

						edb::linux_struct::link_map<Addr> map;
						if (process->readBytes(link_address, &map, sizeof(map))) {
							char path[PATH_MAX];
							if (!process->readBytes(edb::address_t::fromZeroExtended(map.l_name), &path, sizeof(path))) {
								path[0] = '\0';
							}

							if (map.l_addr) {
								Module module;
								module.name        = path;
								module.baseAddress = map.l_addr;
								ret.push_back(module);
							}

							link_address = edb::address_t::fromZeroExtended(map.l_next);
						} else {
							break;
						}
					}
				}
			}
		}
	}

	// fallback
	if (ret.isEmpty()) {
		const QList<std::shared_ptr<IRegion>> r = edb::v1::memory_regions().regions();
		QSet<QString> found_modules;

		for (const std::shared_ptr<IRegion> &region : r) {

			// we assume that modules will be listed by absolute path
			if (region->name().startsWith("/")) {
				if (!util::contains(found_modules, region->name())) {
					Module module;
					module.name        = region->name();
					module.baseAddress = region->start();
					found_modules.insert(region->name());
					ret.push_back(module);
				}
			}
		}
	}

	return ret;
}

/**
 * seeks memory file to given address, taking possible negativity of the
 * address into account
 *
 * @brief seek_addr
 * @param file
 * @param address
 */
void seek_addr(QFile &file, edb::address_t address) {
	if (address <= UINT64_MAX / 2) {
		file.seek(address);
	} else {
		const int fd = file.handle();
		// Seek in two parts to avoid specifying negative offset: off64_t is a signed type
		const off64_t halfAddressTruncated = address >> 1;
		lseek64(fd, halfAddressTruncated, SEEK_SET);
		const off64_t secondHalfAddress = address - halfAddressTruncated;
		lseek64(fd, secondHalfAddress, SEEK_CUR);
	}
}

}

/**
 * @brief PlatformProcess::PlatformProcess
 * @param core
 * @param pid
 */
PlatformProcess::PlatformProcess(DebuggerCore *core, edb::pid_t pid)
	: core_(core), pid_(pid) {

	if (!core_->procMemReadBroken_) {
		auto memory_file = std::make_shared<QFile>(QString("/proc/%1/mem").arg(pid_));

		QIODevice::OpenMode flags = QIODevice::ReadOnly | QIODevice::Unbuffered;
		if (!core_->procMemWriteBroken_) {
			flags |= QIODevice::WriteOnly;
		}

		if (memory_file->open(flags)) {
			readOnlyMemFile_ = memory_file;
			if (!core_->procMemWriteBroken_) {
				readWriteMemFile_ = memory_file;
			}
		}
	}
}

/**
 * reads <len> bytes into <buf> starting at <address>
 *
 * @brief PlatformProcess::readBytes
 * @param address
 * @param buf
 * @param len
 * @return
 */
std::size_t PlatformProcess::readBytes(edb::address_t address, void *buf, std::size_t len) const {

	// NOTE(eteran): returns the number of bytes read <N>
	// NOTE(eteran): if the read is short, only the first <N> bytes are defined

	quint64 read = 0;

	Q_ASSERT(buf);
	Q_ASSERT(core_->process_.get() == this);

	auto ptr = reinterpret_cast<char *>(buf);

	if (len != 0) {

		// small reads take the fast path
		if (len == 1) {

			auto it = core_->breakpoints_.find(address);
			if (it != core_->breakpoints_.end()) {
				*ptr = (*it)->originalBytes()[0];
				return 1;
			}

			if (readOnlyMemFile_) {
				seek_addr(*readOnlyMemFile_, address);
				read = readOnlyMemFile_->read(ptr, 1);
				if (read == 1) {
					return 1;
				}
				return 0;
			} else {
				bool ok;
				uint8_t x = ptraceReadByte(address, &ok);
				if (ok) {
					*ptr = x;
					return 1;
				}
				return 0;
			}
		}

		if (readOnlyMemFile_) {
			seek_addr(*readOnlyMemFile_, address);
			read = readOnlyMemFile_->read(ptr, len);
			if (read == 0 || read == quint64(-1)) {
				return 0;
			}
		} else {
			for (std::size_t index = 0; index < len; ++index) {

				// read a byte, if we failed, we are done
				bool ok;
				const uint8_t x = ptraceReadByte(address + index, &ok);
				if (!ok) {
					break;
				}

				// store it
				reinterpret_cast<char *>(buf)[index] = x;

				++read;
			}
		}

		// replace any breakpoints
		Q_FOREACH (const std::shared_ptr<IBreakpoint> &bp, core_->breakpoints_) {
			auto bpBytes                = bp->originalBytes();
			const edb::address_t bpAddr = bp->address();
			// show the original bytes in the buffer..
			for (size_t i = 0; i < bp->size(); ++i) {
				if (bpAddr + i >= address && bpAddr + i < address + read) {
					ptr[bpAddr + i - address] = bpBytes[i];
				}
			}
		}
	}

	return read;
}

/**
 * same as writeBytes, except that it also records the original data that was
 * found at the address being written to.
 *
 * @brief PlatformProcess::patchBytes
 * @param address
 * @param buf
 * @param len
 * @return
 */
std::size_t PlatformProcess::patchBytes(edb::address_t address, const void *buf, size_t len) {

	// NOTE(eteran): Unlike the read_bytes, write_bytes functions, this will
	//               not apply the write if we could not properly backup <len>
	//               bytes as requested.
	// NOTE(eteran): On the off chance that we can READ <len> bytes, but can't
	//               WRITE <len> bytes, we will return the number of bytes
	//               written, but record <len> bytes of patch data.

	Q_ASSERT(buf);
	Q_ASSERT(core_->process_.get() == this);

	Patch patch;
	patch.address = address;
	patch.origBytes.resize(len);
	patch.newBytes = QByteArray(static_cast<const char *>(buf), len);

	size_t read_ret = readBytes(address, patch.origBytes.data(), len);
	if (read_ret != len) {
		return 0;
	}

	patches_.insert(address, patch);

	return writeBytes(address, buf, len);
}

/**
 * writes <len> bytes from <buf> starting at <address>
 *
 * @brief PlatformProcess::writeBytes
 * @param address
 * @param buf
 * @param len
 * @return
 */
std::size_t PlatformProcess::writeBytes(edb::address_t address, const void *buf, std::size_t len) {
	quint64 written = 0;

	Q_ASSERT(buf);
	Q_ASSERT(core_->process_.get() == this);

	if (len != 0) {
		if (readWriteMemFile_) {
			seek_addr(*readWriteMemFile_, address);
			written = readWriteMemFile_->write(reinterpret_cast<const char *>(buf), len);
			if (written == 0 || written == quint64(-1)) {
				return 0;
			}
		} else {
			// TODO write whole words at a time using ptrace_poke.
			for (std::size_t byteIndex = 0; byteIndex < len; ++byteIndex) {
				bool ok = false;
				ptraceWriteByte(address + byteIndex, *(reinterpret_cast<const char *>(buf) + byteIndex), &ok);
				if (!ok) return written;
				++written;
			}
		}
	}

	return written;
}

/**
 * reads <count> pages from the process starting at <address>
 *
 * @brief PlatformProcess::readPages
 * @param address - must be page aligned.
 * @param buf - sizeof(buf) must be >= count * core_->page_size()
 * @param count - number of pages
 * @return
 */
std::size_t PlatformProcess::readPages(edb::address_t address, void *buf, std::size_t count) const {
	Q_ASSERT(buf);
	Q_ASSERT(core_->process_.get() == this);
	return readBytes(address, buf, count * core_->pageSize()) / core_->pageSize();
}

/**
 * @brief PlatformProcess::startTime
 * @return
 */
QDateTime PlatformProcess::startTime() const {
	QFileInfo info(QString("/proc/%1/stat").arg(pid_));
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
	return info.birthTime();
#else
	return info.created();
#endif
}

/**
 * @brief PlatformProcess::arguments
 * @return
 */
QList<QByteArray> PlatformProcess::arguments() const {
	QList<QByteArray> ret;

	if (pid_ != 0) {
		const QString command_line_file(QString("/proc/%1/cmdline").arg(pid_));
		QFile file(command_line_file);

		if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
			QTextStream in(&file);

			QByteArray s;
			QChar ch;

			while (in.status() == QTextStream::Ok) {
				in >> ch;
				if (ch.isNull()) {
					if (!s.isEmpty()) {
						ret << s;
					}
					s.clear();
				} else {
					s += ch;
				}
			}

			if (!s.isEmpty()) {
				ret << s;
			}
		}
	}
	return ret;
}

/**
 * @brief PlatformProcess::currentWorkingDirectory
 * @return
 */
QString PlatformProcess::currentWorkingDirectory() const {
	return edb::v1::symlink_target(QString("/proc/%1/cwd").arg(pid_));
}

/**
 * @brief PlatformProcess::executable
 * @return
 */
QString PlatformProcess::executable() const {
	return edb::v1::symlink_target(QString("/proc/%1/exe").arg(pid_));
}

/**
 * @brief PlatformProcess::pid
 * @return
 */
edb::pid_t PlatformProcess::pid() const {
	return pid_;
}

/**
 * @brief PlatformProcess::parent
 * @return
 */
std::shared_ptr<IProcess> PlatformProcess::parent() const {

	struct user_stat user_stat;
	int n = get_user_stat(pid_, &user_stat);
	if (n >= 4) {
		return std::make_shared<PlatformProcess>(core_, user_stat.ppid);
	}

	return nullptr;
}

/**
 * @brief PlatformProcess::codeAddress
 * @return
 */
edb::address_t PlatformProcess::codeAddress() const {
	struct user_stat user_stat;
	int n = get_user_stat(pid_, &user_stat);
	if (n >= 26) {
		return user_stat.startcode;
	}
	return 0;
}

/**
 * @brief PlatformProcess::dataAddress
 * @return
 */
edb::address_t PlatformProcess::dataAddress() const {
	struct user_stat user_stat;
	int n = get_user_stat(pid_, &user_stat);
	if (n >= 27) {
		return user_stat.endcode + 1; // endcode == startdata ?
	}
	return 0;
}

/**
 * @brief PlatformProcess::regions
 * @return
 */
QList<std::shared_ptr<IRegion>> PlatformProcess::regions() const {

	static QList<std::shared_ptr<IRegion>> regions;
	const QString map_file(QString("/proc/%1/maps").arg(pid_));

	// hash the region file to see if it changed or not
	{
		static size_t totalHash = 0;

		std::ifstream mf(map_file.toStdString());
		size_t newHash = 0;
		std::string line;

		while (std::getline(mf, line)) {
			boost::hash_combine(newHash, line);
		}

		if (totalHash == newHash) {
			return regions;
		}

		totalHash = newHash;
		regions.clear();
	}

	// it changed, so let's process it
	QFile file(map_file);
	if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {

		QTextStream in(&file);
		QString line = in.readLine();

		while (!line.isNull()) {
			if (std::shared_ptr<IRegion> region = process_map_line(line)) {
				regions.push_back(region);
			}
			line = in.readLine();
		}
	}

	return regions;
}

/**
 * @brief PlatformProcess::ptraceReadByte
 * @param address
 * @param ok
 * @return
 */
uint8_t PlatformProcess::ptraceReadByte(edb::address_t address, bool *ok) const {
	// TODO(eteran): assert that we are paused

	Q_ASSERT(ok);
	Q_ASSERT(core_->process_.get() == this);

	*ok = false;

	// if this spot is unreadable, then just return 0xff, otherwise
	// continue as normal.

	// core_->page_size() - 1 will always be 0xf* because pagesizes
	// are always 0x10*, so the masking works
	// range of nBytesToNextPage is [1..n] where n=pagesize, and we have to adjust
	// if nByteToNextPage < wordsize
	const size_t nBytesToNextPage = core_->pageSize() - (address & (core_->pageSize() - 1));

	// Avoid crossing page boundary, since next page may be unreadable
	const size_t addressShift = nBytesToNextPage < WordSize ? WordSize - nBytesToNextPage : 0;
	address -= addressShift;

	const long value = ptracePeek(address, ok);

	if (*ok) {
		uint8_t result;
		// We aren't interested in `value` as in number, it's just a buffer, so no endianness magic.
		// Just have to compensate for `addressShift` when reading it.
		std::memcpy(&result, reinterpret_cast<const char *>(&value) + addressShift, sizeof(result));
		return result;
	}

	return 0xff;
}

/**
 * writes a single byte at a given address via ptrace API.
 *
 * @brief PlatformProcess::ptraceWriteByte
 * @param address
 * @param value
 * @param ok
 */
void PlatformProcess::ptraceWriteByte(edb::address_t address, uint8_t value, bool *ok) {
	// TODO(eteran): assert that we are paused
	// NOTE(eteran): assumes the this will not trample any breakpoints, must
	// be handled in calling code!

	Q_ASSERT(ok);
	Q_ASSERT(core_->process_.get() == this);

	*ok = false;

	// core_->page_size() - 1 will always be 0xf* because pagesizes
	// are always 0x10*, so the masking works
	// range of nBytesToNextPage is [1..n] where n=pagesize, and we have to adjust
	// if nBytesToNextPage < wordsize
	const size_t nBytesToNextPage = core_->pageSize() - (address & (core_->pageSize() - 1));

	// Avoid crossing page boundary, since next page may be inaccessible
	const size_t addressShift = nBytesToNextPage < WordSize ? WordSize - nBytesToNextPage : 0;
	address -= addressShift;

	long word = ptracePeek(address, ok);
	if (!*ok) {
		return;
	}

	// We aren't interested in `value` as in number, it's just a buffer, so no endianness magic.
	// Just have to compensate for `addressShift` when writing it.
	std::memcpy(reinterpret_cast<char *>(&word) + addressShift, &value, sizeof(value));

	*ok = ptracePoke(address, word);
}

/**
 * @brief PlatformProcess::ptracePeek
 * @param address
 * @param ok
 * @return
 */
long PlatformProcess::ptracePeek(edb::address_t address, bool *ok) const {

	// NOTE(eteran): this will fail on newer versions of linux if called from a
	//               different thread than the one which attached to process

	Q_ASSERT(ok);
	Q_ASSERT(core_->process_.get() == this);

	if (EDB_IS_32_BIT && address > 0xffffffffULL) {
		// 32 bit ptrace can't handle such long addresses
		*ok = false;
		return 0;
	}

	errno = 0;
	// NOTE: on some Linux systems ptrace prototype has ellipsis instead of third and fourth arguments
	// Thus we can't just pass address as is on IA32 systems: it'd put 64 bit integer on stack and cause UB
	auto nativeAddress = reinterpret_cast<const void *>(address.toUint());
	const long v       = ptrace(PTRACE_PEEKTEXT, pid_, nativeAddress, 0);
	*ok                = set_ok(v);
	return v;
}

/**
 * @brief PlatformProcess::ptracePoke
 * @param address
 * @param value
 * @return
 */
bool PlatformProcess::ptracePoke(edb::address_t address, long value) {

	Q_ASSERT(core_->process_.get() == this);

	if (EDB_IS_32_BIT && address > 0xffffffffULL) {
		// 32 bit ptrace can't handle such long addresses
		return false;
	}

	// NOTE: on some Linux systems ptrace prototype has ellipsis instead of third and fourth arguments
	// Thus we can't just pass address as is on IA32 systems: it'd put 64 bit integer on stack and cause UB
	auto nativeAddress = reinterpret_cast<const void *>(address.toUint());
	return ptrace(PTRACE_POKETEXT, pid_, nativeAddress, value) != -1;
}

/**
 * @brief PlatformProcess::threads
 * @return
 */
QList<std::shared_ptr<IThread>> PlatformProcess::threads() const {

	Q_ASSERT(core_->process_.get() == this);

	QList<std::shared_ptr<IThread>> threadList;
	threadList.reserve(core_->threads_.size());
	std::copy(core_->threads_.begin(), core_->threads_.end(), std::back_inserter(threadList));
	return threadList;
}

/**
 * @brief PlatformProcess::currentThread
 * @return
 */
std::shared_ptr<IThread> PlatformProcess::currentThread() const {

	Q_ASSERT(core_->process_.get() == this);

	auto it = core_->threads_.find(core_->activeThread_);
	if (it != core_->threads_.end()) {
		return it.value();
	}
	return nullptr;
}

/**
 * @brief PlatformProcess::setCurrentThread
 * @param thread
 */
void PlatformProcess::setCurrentThread(IThread &thread) {
	core_->activeThread_ = static_cast<PlatformThread *>(&thread)->tid();
	edb::v1::update_ui();
}

/**
 * @brief PlatformProcess::uid
 * @return
 */
edb::uid_t PlatformProcess::uid() const {

	const QFileInfo info(QString("/proc/%1").arg(pid_));
	return info.ownerId();
}

/**
 * @brief PlatformProcess::user
 * @return
 */
QString PlatformProcess::user() const {
	if (const struct passwd *const pwd = ::getpwuid(uid())) {
		return pwd->pw_name;
	}

	return QString();
}

/**
 * @brief PlatformProcess::name
 * @return
 */
QString PlatformProcess::name() const {
	struct user_stat user_stat;
	const int n = get_user_stat(pid_, &user_stat);
	if (n >= 2) {
		return user_stat.comm;
	}

	return QString();
}

/**
 * @brief PlatformProcess::loadedModules
 * @return
 */
QList<Module> PlatformProcess::loadedModules() const {
	if (edb::v1::debuggeeIs64Bit()) {
		return get_loaded_modules<Elf64_Addr>(this);
	} else if (edb::v1::debuggeeIs32Bit()) {
		return get_loaded_modules<Elf32_Addr>(this);
	} else {
		return QList<Module>();
	}
}

/**
 * stops *all* threads of a process
 *
 * @brief PlatformProcess::pause
 * @return
 */
Status PlatformProcess::pause() {
	// belive it or not, I belive that this is sufficient for all threads.
	// This is because in the debug event handler, a SIGSTOP is sent
	// to all threads when any event arrives, so no need to explicitly do
	// it here. We just need any thread to stop. So we'll just target the
	// pid_ which will send it to any one of the threads in the process.
	if (::kill(pid_, SIGSTOP) == -1) {
		const char *const strError = strerror(errno);
		qWarning() << "Unable to pause process" << pid_ << ": kill(SIGSTOP) failed:" << strError;
		return Status(strError);
	}

	return Status::Ok;
}

/**
 * resumes ALL threads
 *
 * @brief PlatformProcess::resume
 * @param status
 * @return
 */
Status PlatformProcess::resume(edb::EventStatus status) {

	// NOTE(eteran): OK, this is very tricky. When the user wants to resume
	// while ignoring a signal (DEBUG_CONTINUE), we need to know which thread
	// needs to have the signal ignored, and which need to have their signals
	// passed during the resume

	// TODO: assert that we are paused
	Q_ASSERT(core_->process_.get() == this);

	QString errorMessage;

	if (status != edb::DEBUG_STOP) {

		if (std::shared_ptr<IThread> thread = currentThread()) {
			const auto resumeStatus = thread->resume(status);
			if (!resumeStatus) {
				errorMessage += tr("Failed to resume thread %1: %2\n").arg(thread->tid()).arg(resumeStatus.error());
			}

			// resume the other threads passing the signal they originally reported had
			for (auto &other_thread : threads()) {
				if (util::contains(core_->waitedThreads_, other_thread->tid())) {
					const auto resumeStatus = other_thread->resume();
					if (!resumeStatus) {
						errorMessage += tr("Failed to resume thread %1: %2\n").arg(thread->tid()).arg(resumeStatus.error());
					}
				}
			}
		}
	}

	if (errorMessage.isEmpty()) {
		return Status::Ok;
	}

	qWarning() << errorMessage.toStdString().c_str();
	return Status("\n" + errorMessage);
}

/**
 * steps the currently active thread
 *
 * @brief PlatformProcess::step
 * @param status
 * @return
 */
Status PlatformProcess::step(edb::EventStatus status) {
	// TODO: assert that we are paused
	Q_ASSERT(core_->process_.get() == this);

	if (status != edb::DEBUG_STOP) {
		if (std::shared_ptr<IThread> thread = currentThread()) {
			return thread->step(status);
		}
	}
	return Status::Ok;
}

/**
 * @brief PlatformProcess::isPaused
 * @return true if ALL threads are currently in the debugger's wait list
 */
bool PlatformProcess::isPaused() const {
	for (auto &thread : threads()) {
		if (!thread->isPaused()) {
			return false;
		}
	}

	return true;
}

/**
 * @brief PlatformProcess::patches
 * @return any patches applied to this process
 */
QMap<edb::address_t, Patch> PlatformProcess::patches() const {
	return patches_;
}

/**
 * @brief PlatformProcess::entry_point
 * @return
 */
edb::address_t PlatformProcess::entryPoint() const {

	QFile auxv(QString("/proc/%1/auxv").arg(pid_));
	if (auxv.open(QIODevice::ReadOnly)) {

		if (edb::v1::debuggeeIs64Bit()) {
			elf64_auxv_t entry;
			while (auxv.read(reinterpret_cast<char *>(&entry), sizeof(entry))) {
				if (entry.a_type == AT_ENTRY) {
					return entry.a_un.a_val;
				}
			}
		} else if (edb::v1::debuggeeIs32Bit()) {
			elf32_auxv_t entry;
			while (auxv.read(reinterpret_cast<char *>(&entry), sizeof(entry))) {
				if (entry.a_type == AT_ENTRY) {
					return entry.a_un.a_val;
				}
			}
		}
	}

	return edb::address_t{};
}

/**
 * @brief get_program_headers
 * @param process
 * @param phdr_memaddr
 * @param num_phdr
 * @return
 */
bool get_program_headers(const IProcess *process, edb::address_t *phdr_memaddr, int *num_phdr) {

	*phdr_memaddr = edb::address_t{};
	*num_phdr     = 0;

	QFile auxv(QString("/proc/%1/auxv").arg(process->pid()));
	if (auxv.open(QIODevice::ReadOnly)) {

		if (edb::v1::debuggeeIs64Bit()) {
			elf64_auxv_t entry;
			while (auxv.read(reinterpret_cast<char *>(&entry), sizeof(entry))) {
				switch (entry.a_type) {
				case AT_PHDR:
					*phdr_memaddr = entry.a_un.a_val;
					break;
				case AT_PHNUM:
					*num_phdr = entry.a_un.a_val;
					break;
				}
			}
		} else if (edb::v1::debuggeeIs32Bit()) {
			elf32_auxv_t entry;
			while (auxv.read(reinterpret_cast<char *>(&entry), sizeof(entry))) {
				switch (entry.a_type) {
				case AT_PHDR:
					*phdr_memaddr = entry.a_un.a_val;
					break;
				case AT_PHNUM:
					*num_phdr = entry.a_un.a_val;
					break;
				}
			}
		}
	}

	return (*phdr_memaddr != 0 && *num_phdr != 0);
}

/**
 * @brief get_debug_pointer
 * @param process
 * @param phdr_memaddr
 * @param count
 * @param relocation
 * @return
 */
template <class Model>
edb::address_t get_debug_pointer(const IProcess *process, edb::address_t phdr_memaddr, int count, edb::address_t relocation) {

	using elf_phdr = typename Model::elf_phdr;

	elf_phdr phdr;
	for (int i = 0; i < count; ++i) {
		if (process->readBytes(phdr_memaddr + i * sizeof(elf_phdr), &phdr, sizeof(elf_phdr))) {
			if (phdr.p_type == PT_DYNAMIC) {
				try {

					auto buf = std::make_unique<uint8_t[]>(phdr.p_memsz);

					if (process->readBytes(phdr.p_vaddr + relocation, &buf[0], phdr.p_memsz)) {
						auto dynamic = reinterpret_cast<typename Model::elf_dyn *>(&buf[0]);
						while (dynamic->d_tag != DT_NULL) {
							if (dynamic->d_tag == DT_DEBUG) {
								return dynamic->d_un.d_val;
							}
							++dynamic;
						}
					}
				} catch (const std::bad_alloc &) {
					qDebug() << "[get_debug_pointer] no more memory";
					return 0;
				}
			}
		}
	}

	return 0;
}

/**
 * @brief get_relocation
 * @param process
 * @param phdr_memaddr
 * @param i
 * @return
 */
template <class Model>
edb::address_t get_relocation(const IProcess *process, edb::address_t phdr_memaddr, int i) {

	using elf_phdr = typename Model::elf_phdr;

	elf_phdr phdr;
	if (process->readBytes(phdr_memaddr + i * sizeof(elf_phdr), &phdr, sizeof(elf_phdr))) {
		if (phdr.p_type == PT_PHDR) {
			return phdr_memaddr - phdr.p_vaddr;
		}
	}

	return -1;
}

/**
 * attempts to locate the ELF debug pointer in the target process and returns
 * it, 0 of not found
 *
 * @brief PlatformProcess::debug_pointer
 * @return
 */
edb::address_t PlatformProcess::debugPointer() const {

	// NOTE(eteran): some of this code is from or inspired by code in
	// gdb/gdbserver/linux-low.c

	edb::address_t phdr_memaddr;
	int num_phdr;

	if (get_program_headers(this, &phdr_memaddr, &num_phdr)) {

		/* Compute relocation: it is expected to be 0 for "regular" executables,
		 * non-zero for PIE ones.  */
		edb::address_t relocation = -1;
		for (int i = 0; relocation == -1 && i < num_phdr; i++) {

			if (edb::v1::debuggeeIs64Bit()) {
				relocation = get_relocation<elf_model<64>>(this, phdr_memaddr, i);
			} else if (edb::v1::debuggeeIs32Bit()) {
				relocation = get_relocation<elf_model<32>>(this, phdr_memaddr, i);
			}
		}

		if (relocation == -1) {
			/* PT_PHDR is optional, but necessary for PIE in general.
			 * Fortunately any real world executables, including PIE
			 * executables, have always PT_PHDR present.  PT_PHDR is not
			 * present in some shared libraries or in fpc (Free Pascal 2.4)
			 * binaries but neither of those have a need for or present
			 * DT_DEBUG anyway (fpc binaries are statically linked).
			 *
			 * Therefore if there exists DT_DEBUG there is always also PT_PHDR.
			 *
			 * GDB could find RELOCATION also from AT_ENTRY - e_entry.  */
			return 0;
		}

		if (edb::v1::debuggeeIs64Bit()) {
			return get_debug_pointer<elf_model<64>>(this, phdr_memaddr, num_phdr, relocation);
		} else if (edb::v1::debuggeeIs32Bit()) {
			return get_debug_pointer<elf_model<32>>(this, phdr_memaddr, num_phdr, relocation);
		}
	}

	return edb::address_t{};
}

/**
 * @brief PlatformProcess::calculateMain
 * @return
 */
edb::address_t PlatformProcess::calculateMain() const {
	if (edb::v1::debuggeeIs64Bit()) {
		ByteShiftArray ba(14);

		edb::address_t entry_point = this->entryPoint();

		for (int i = 0; i < 50; ++i) {
			uint8_t byte;
			if (readBytes(entry_point + i, &byte, sizeof(byte))) {
				ba << byte;

				edb::address_t address = 0;

				if (ba.size() >= 13) {
					// beginning of a call preceeded by a 64-bit mov and followed by a hlt
					if (ba[0] == 0x48 && ba[1] == 0xc7 && ba[7] == 0xe8 && ba[12] == 0xf4) {
						// Seems that this 64-bit mov still has a 32-bit immediate
						address = *reinterpret_cast<const edb::address_t *>(ba.data() + 3) & 0xffffffff;
					}

					// same heuristic except for PIC binaries
					else if (ba.size() >= 14 && ba[0] == 0x48 && ba[1] == 0x8d && ba[2] == 0x3d && ba[7] == 0xFF && ba[8] == 0x15 && ba[13] == 0xf4) {
						// It's signed relative!
						auto rel = *reinterpret_cast<const qint32 *>(ba.data() + 3);
						// ba[0] is entry_point + i - 13. instruction is 7 bytes long.
						address = rel + entry_point + i - 13 + 7;
					}

					if (address) {
						// TODO: make sure that this address resides in an executable region
						qDebug() << "No main symbol found, calculated it to be " << edb::v1::format_pointer(address) << " using heuristic";
						return address;
					}
				}
			} else {
				break;
			}
		}
	} else if (edb::v1::debuggeeIs32Bit()) {
		ByteShiftArray ba(11);

		edb::address_t entry_point = this->entryPoint();

		for (int i = 0; i < 50; ++i) {
			uint8_t byte;
			if (readBytes(entry_point + i, &byte, sizeof(byte))) {
				ba << byte;

				if (ba.size() >= 11) {
					// beginning of a call preceeded by a push and followed by a hlt
					if (ba[0] == 0x68 && ba[5] == 0xe8 && ba[10] == 0xf4) {
						edb::address_t address(0);

						auto to = reinterpret_cast<char *>(&address);
						std::memcpy(to, ba.data() + 1, sizeof(uint32_t));

						// TODO: make sure that this address resides in an executable region
						qDebug() << "No main symbol found, calculated it to be " << edb::v1::format_pointer(address) << " using heuristic";
						return address;
					}
				}
			} else {
				break;
			}
		}
	}

	return 0;
}

/**
 * @brief PlatformProcess::stardardInput
 * @return
 */
QString PlatformProcess::stardardInput() const {
	return input_;
}

/**
 * @brief PlatformProcess::stardardOutput
 * @return
 */
QString PlatformProcess::stardardOutput() const {
	return output_;
}

}