Alexander Pyhalov
2014-12-13 7b0dcec86c5455046c26524cf1a36c2e9df55fd9
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
This patch adds Python debugger support.  It may be contributed upstream at
some point, but the suitability (or lack thereof) has not yet been determined.
 
--- Python-3.4.2/Makefile.pre.in.~3~    2014-09-22 14:16:07.846210795 -0700
+++ Python-3.4.2/Makefile.pre.in    2014-09-22 14:16:07.961725537 -0700
@@ -471,7 +471,7 @@
 
 # Default target
 all:        build_all
-build_all:    $(BUILDPYTHON) oldsharedmods sharedmods gdbhooks Modules/_testembed python-config
+build_all:    $(BUILDPYTHON) oldsharedmods sharedmods gdbhooks Modules/_testembed python-config build-py_db
 
 # Compile a binary with gcc profile guided optimization.
 profile-opt:
@@ -673,6 +673,15 @@
     @mv config.c Modules
     @echo "The Makefile was updated, you may need to re-run make."
 
+SHLIB_FLAGS = -shared -fpic
+
+libpython$(LDVERSION)_db.so.1.0:        $(srcdir)/py_db/libpython34_db.c
+    $(CC) -o $@ $(CFLAGS) $(PY_CPPFLAGS) $(CPPFLAGS) $(SHLIB_FLAGS) $<
+
+build-py_db:   libpython$(LDVERSION)_db.so.1.0
+
+install-py_db: libpython$(LDVERSION)_db.so.1.0
+    $(INSTALL_SHARED) libpython$(LDVERSION)_db.so.1.0 $(DESTDIR)$(LIBDIR)
 
 Modules/Setup: $(srcdir)/Modules/Setup.dist
     @if test -f Modules/Setup; then \
@@ -1042,7 +1051,7 @@
         $(TESTRUNNER) $(QUICKTESTOPTS)
 
 
-install: @FRAMEWORKINSTALLFIRST@ commoninstall bininstall maninstall @FRAMEWORKINSTALLLAST@
+install: @FRAMEWORKINSTALLFIRST@ commoninstall bininstall maninstall install-py_db @FRAMEWORKINSTALLLAST@
     if test "x$(ENSUREPIP)" != "xno"  ; then \
         case $(ENSUREPIP) in \
             upgrade) ensurepip="--upgrade" ;; \
--- /dev/null
+++ Python-3.4.0/py_db/libpython34_db.c
@@ -0,0 +1,654 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <gelf.h>
+
+#include <Python.h>
+#include <frameobject.h>
+
+#include "libpython34_db.h"
+#if defined(_LP64)
+#include "libpython34_db_32.h"
+#endif /* _LP64 */
+
+/*
+ * Because MDB always runs the debugger in the same datamodel as the target,
+ * only functions that are used by the procfs part of this interface (or shared
+ * between the two) are written as 64->32 aware.
+ */
+typedef struct pydb_arch_ops {
+    ssize_t    (*strobj_readdata)(pydb_agent_t *, uintptr_t, unsigned char *,
+        size_t);
+    int    (*frameinfo)(pydb_agent_t *, uintptr_t, char *,
+        size_t, char *, size_t, int *);
+} pydb_arch_ops_t;
+
+struct pydb_agent {
+    struct ps_prochandle *pdb_ph;
+    int pdb_vers;
+    int pdb_is_64bit;
+    int pdb_datamodel;
+    const pydb_arch_ops_t *pdb_ops;
+};
+
+typedef uintptr_t (*pdi_next_cb_t)(pydb_iter_t *);
+
+struct pydb_iter {
+    struct ps_prochandle *pdi_ph;
+    uintptr_t pdi_current;
+    pdi_next_cb_t pdi_nextf;
+};
+
+#define    LIBPYTHON    "libpython3.4.so"
+
+#define    MIN(x, y)    (((x) < (y)) ? (x) : (y))
+
+/* Generic interface to helper functions */
+static ssize_t pydb_strobj_readdata(pydb_agent_t *py, uintptr_t addr,
+    unsigned char *buf, size_t buf_len);
+static int pydb_getlno(pydb_agent_t *py, uintptr_t lnotab_addr, int firstline,
+    int lastinst);
+static int pydb_frameinfo(pydb_agent_t *py, uintptr_t addr, char *funcnm,
+    size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno);
+
+/* datamodel specific implementation of helper functions */
+static ssize_t pydb_strobj_readdata_native(pydb_agent_t *py, uintptr_t addr,
+    unsigned char *buf, size_t buf_len);
+static int pydb_frameinfo_native(pydb_agent_t *py, uintptr_t addr, char *funcnm,
+    size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno);
+
+#if defined (_LP64)
+static ssize_t pydb_strobj_readdata_32(pydb_agent_t *py, uintptr_t addr,
+    unsigned char *buf, size_t buf_len);
+static int pydb_frameinfo_32(pydb_agent_t *py, uintptr_t addr, char *funcnm,
+    size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno);
+#endif /* _LP64 */
+
+static ssize_t pydb_strobj_readstr(pydb_agent_t *py, uintptr_t addr, char *buf,
+    size_t len);
+
+/* Iterator function next routines.  Plugable, configured by iterator init */
+static uintptr_t pydb_frame_iter_next(pydb_iter_t *iter);
+static uintptr_t pydb_interp_iter_next(pydb_iter_t *iter);
+static uintptr_t pydb_thread_iter_next(pydb_iter_t *iter);
+
+static const char *strbasename(const char *s);
+
+static const pydb_arch_ops_t arch_ops_native = {
+    .frameinfo = pydb_frameinfo_native,
+    .strobj_readdata = pydb_strobj_readdata_native,
+};
+
+#if defined (_LP64)
+static const pydb_arch_ops_t arch_ops_32 = {
+    .frameinfo = pydb_frameinfo_32,
+    .strobj_readdata = pydb_strobj_readdata_32,
+};
+#endif /* _LP64 */
+
+static const char *
+strbasename(const char *s)
+{
+    const char *p = strrchr(s, '/');
+
+    if (p == NULL)
+        return (s);
+
+    return (++p);
+}
+
+/* Agent creation / destruction routines */
+
+pydb_agent_t *
+pydb_agent_create(struct ps_prochandle *P, int vers)
+{
+    pydb_agent_t *py;
+    int datamodel;
+
+    if (vers != PYDB_VERSION) {
+        errno = ENOTSUP;
+        return (NULL);
+    }
+
+    if (ps_pdmodel(P, &datamodel) != PS_OK) {
+        return (NULL);
+    }
+
+    py = (pydb_agent_t *)malloc(sizeof (pydb_agent_t));
+    if (py == NULL) {
+        return (NULL);
+    }
+
+    py->pdb_ph = P;
+    py->pdb_vers = vers;
+    py->pdb_datamodel = datamodel;
+    py->pdb_is_64bit = 0;
+    py->pdb_ops = &arch_ops_native;
+
+#if defined (_LP64)
+    py->pdb_is_64bit = (datamodel == PR_MODEL_LP64);
+    if (!py->pdb_is_64bit) {
+        py->pdb_ops = &arch_ops_32;
+    }
+#endif /* _LP64 */
+
+    return (py);
+}
+
+void
+pydb_agent_destroy(pydb_agent_t *py)
+{
+    if (py == NULL) {
+        return;
+    }
+
+    free(py);
+}
+
+/* Helper functions */
+static int
+pydb_getlno(pydb_agent_t *py, uintptr_t lnotab_addr, int firstline,
+    int lastinst)
+{
+    unsigned char lnotab[4096];
+    ssize_t lnotab_len;
+    int addr, line;
+    int i;
+
+    lnotab_len = pydb_strobj_readdata(py, lnotab_addr, lnotab,
+        sizeof (lnotab));
+    if (lnotab_len < 0) {
+        return (-1);
+    }
+
+    /*
+     * Python's line number algorithm is arcane. See here for details:
+     * http://svn.python.org/projects/python/trunk/Objects/lnotab_notes.txt
+     */
+
+    line = firstline;
+    for (addr = i = 0; i < lnotab_len; i += 2) {
+        if (addr + lnotab[i] > lastinst) {
+            break;
+        }
+        addr += lnotab[i];
+        line += lnotab[i + 1];
+    }
+
+    return (line);
+}
+
+static ssize_t
+pydb_strobj_readdata(pydb_agent_t *py, uintptr_t addr, unsigned char *buf,
+    size_t buf_len)
+{
+    return (py->pdb_ops->strobj_readdata(py, addr, buf, buf_len));
+}
+
+static ssize_t
+pydb_strobj_readdata_native(pydb_agent_t *py, uintptr_t addr,
+    unsigned char *buf, size_t buf_len)
+{
+    PyBytesObject sobj;
+    ssize_t obj_sz;
+    ssize_t read_sz;
+    psaddr_t straddr;
+
+    /*
+     * PyBytesObject are variable size.  The size of the PyBytesObject
+     * struct is fixed, and known at compile time; however, the size of the
+     * associated buffer is variable.  The char[1] element at the end of the
+     * structure contains the string, and the ob_size of the PyBytesObject
+     * indicates how much extra space was allocated to contain the string
+     * buffer at the object's tail.  Read in the fixed size portion of the
+     * object first, and then read the contents of the data buffer into the
+     * buffer passed by the caller.
+     */
+
+    if (ps_pread(py->pdb_ph, addr, &sobj, sizeof (PyBytesObject))
+        != PS_OK) {
+        return (-1);
+    }
+
+    obj_sz = (ssize_t)PyString_GET_SIZE(&sobj);
+
+    read_sz = MIN(obj_sz, (ssize_t)buf_len);
+    straddr = (psaddr_t)(addr + offsetof(PyBytesObject, ob_sval));
+
+    if (ps_pread(py->pdb_ph, straddr, buf, (size_t)read_sz) != PS_OK) {
+        return (-1);
+    }
+
+    return (read_sz);
+}
+
+#if defined(_LP64)
+static ssize_t
+pydb_strobj_readdata_32(pydb_agent_t *py, uintptr_t addr,
+    unsigned char *buf, size_t buf_len)
+{
+    PyBytesObject32 sobj;
+    ssize_t obj_sz;
+    ssize_t read_sz;
+    psaddr_t straddr;
+
+    /*
+     * PyBytesObject are variable size.  The size of the PyBytesObject
+     * struct is fixed, and known at compile time; however, the size of the
+     * associated buffer is variable.  The char[1] element at the end of the
+     * structure contains the string, and the ob_size of the PyBytesObject
+     * indicates how much extra space was allocated to contain the string
+     * buffer at the object's tail.  Read in the fixed size portion of the
+     * object first, and then read the contents of the data buffer into the
+     * buffer passed by the caller.
+     */
+
+    if (ps_pread(py->pdb_ph, addr, &sobj, sizeof (PyBytesObject32))
+        != PS_OK) {
+        return (-1);
+    }
+
+    obj_sz = (ssize_t)PyString_GET_SIZE32(&sobj);
+
+    read_sz = MIN(obj_sz, (ssize_t)buf_len);
+    straddr = (psaddr_t)(addr + offsetof(PyBytesObject32, ob_sval));
+
+    if (ps_pread(py->pdb_ph, straddr, buf, (size_t)read_sz) != PS_OK) {
+        return (-1);
+    }
+
+    return (read_sz);
+}
+#endif /* _LP64 */
+
+/*
+ * Most Python PyBytesObject contain strings, as one would expect.  However,
+ * due to some sleazy hackery in parts of the Python code, some string objects
+ * are used as buffers for binary data.  In the general case,
+ * pydb_strobj_readstr() should be used to read strings out of string objects.
+ * It wraps pydb_strobj_readdata(), which should be used by callers only when
+ * trying to retrieve binary data.  (This routine does some string cleanup).
+ */
+static ssize_t
+pydb_strobj_readstr(pydb_agent_t *py, uintptr_t addr, char *buf,
+    size_t buf_len)
+{
+    ssize_t read_sz;
+
+    read_sz = pydb_strobj_readdata(py, addr, (unsigned char *)buf, buf_len);
+
+    if (read_sz >= 0) {
+        if (read_sz >= buf_len) {
+            read_sz = buf_len - 1;
+        }
+
+        buf[read_sz] = '\0';
+    }
+
+    return (read_sz);
+}
+
+
+static int
+pydb_frameinfo(pydb_agent_t *py, uintptr_t addr, char *funcnm,
+    size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno)
+{
+    return (py->pdb_ops->frameinfo(py, addr, funcnm, funcnm_sz,
+        filenm, filenm_sz, lineno));
+}
+
+static int
+pydb_frameinfo_native(pydb_agent_t *py, uintptr_t addr, char *funcnm,
+    size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno)
+{
+    PyFrameObject fo;
+    PyCodeObject co;
+    ssize_t rc;
+
+    if (ps_pread(py->pdb_ph, addr, &fo, sizeof (PyFrameObject))
+        != PS_OK) {
+        return (-1);
+    }
+
+    if (ps_pread(py->pdb_ph, (uintptr_t)fo.f_code, &co,
+        sizeof (PyCodeObject)) != PS_OK) {
+        return (-1);
+    }
+
+    rc = pydb_strobj_readstr(py, (uintptr_t)co.co_name, funcnm, funcnm_sz);
+    if (rc < 0) {
+        return (-1);
+    }
+
+    rc = pydb_strobj_readstr(py, (uintptr_t)co.co_filename, filenm,
+        filenm_sz);
+    if (rc < 0) {
+        return (-1);
+    }
+
+    *lineno = pydb_getlno(py, (uintptr_t)co.co_lnotab, co.co_firstlineno,
+        fo.f_lasti);
+    if (*lineno < 0) {
+        return (-1);
+    }
+
+    return (0);
+}
+
+#if defined (_LP64)
+static int
+pydb_frameinfo_32(pydb_agent_t *py, uintptr_t addr, char *funcnm,
+    size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno)
+{
+    PyFrameObject32 fo;
+    PyCodeObject32 co;
+    ssize_t rc;
+
+    if (ps_pread(py->pdb_ph, addr, &fo, sizeof (PyFrameObject32))
+        != PS_OK) {
+        return (-1);
+    }
+
+    if (ps_pread(py->pdb_ph, (uintptr_t)fo.f_code, &co,
+        sizeof (PyCodeObject32)) != PS_OK) {
+        return (-1);
+    }
+
+    rc = pydb_strobj_readstr(py, (uintptr_t)co.co_name, funcnm, funcnm_sz);
+    if (rc < 0) {
+        return (-1);
+    }
+
+    rc = pydb_strobj_readstr(py, (uintptr_t)co.co_filename, filenm,
+        filenm_sz);
+    if (rc < 0) {
+        return (-1);
+    }
+
+    *lineno = pydb_getlno(py, (uintptr_t)co.co_lnotab, co.co_firstlineno,
+        fo.f_lasti);
+    if (*lineno < 0) {
+        return (-1);
+    }
+
+    return (0);
+}
+
+#endif /* _LP64 */
+
+/* Functions that are part of the library's interface */
+
+/*
+ * Given the address of a PyFrameObject, and a buffer of a known size,
+ * fill the buffer with a description of the frame.
+ */
+int
+pydb_get_frameinfo(pydb_agent_t *py, uintptr_t frame_addr, char *fbuf,
+    size_t bufsz, int verbose)
+{
+    char funcname[1024];
+    char filename[1024];
+    char *fn;
+    int lineno;
+    int length = (py->pdb_is_64bit ? 16 : 8);
+    int rc;
+
+    rc = pydb_frameinfo(py, frame_addr, funcname, sizeof (funcname),
+        filename, sizeof (filename), &lineno);
+    if (rc < 0) {
+        return (-1);
+    }
+
+    if (!verbose) {
+        fn = (char *)strbasename(filename);
+    } else {
+        fn = filename;
+    }
+
+    (void) snprintf(fbuf, bufsz, "%0.*lx %s:%d %s()\n", length,
+        frame_addr, fn, lineno, funcname);
+
+    return (0);
+}
+
+/*
+ * Return a description about a PyFrameObject, if the object is
+ * actually a PyFrameObject.  In this case, the pc argument is checked
+ * to make sure that it came from a function that takes a PyFrameObject
+ * as its first (argv[0]) argument.
+ */
+int
+pydb_pc_frameinfo(pydb_agent_t *py, uintptr_t pc, uintptr_t frame_addr,
+    char *fbuf, size_t bufsz)
+{
+    char funcname[1024];
+    char filename[1024];
+    int lineno;
+    int rc;
+    ps_sym_t psym;
+
+    /*
+     * If PC doesn't match PyEval_EvalFrameEx in either libpython
+     * or the executable, don't decode it.
+     */
+    if (ps_pglobal_sym(py->pdb_ph, LIBPYTHON, "PyEval_EvalFrameEx", &psym)
+        != PS_OK) {
+        return (-1);
+    }
+
+    /* If symbol found, ensure that PC falls within PyEval_EvalFrameEx. */
+    if (pc < psym.st_value || pc > psym.st_value + psym.st_size) {
+        return (-1);
+    }
+
+    rc = pydb_frameinfo(py, frame_addr, funcname, sizeof (funcname),
+        filename, sizeof (filename), &lineno);
+    if (rc < 0) {
+        return (-1);
+    }
+
+    (void) snprintf(fbuf, bufsz, "[ %s:%d (%s) ]\n", filename, lineno,
+        funcname);
+
+    return (0);
+}
+
+/*
+ * Walks the list of PyInterpreterState objects.  If caller doesn't
+ * supply address of list, this method will look it up.
+ */
+pydb_iter_t *
+pydb_interp_iter_init(pydb_agent_t *py, uintptr_t addr)
+{
+    pydb_iter_t *itr;
+    uintptr_t i_addr;
+    int rc;
+
+    if (addr == 0) {
+        rc = ps_pglobal_lookup(py->pdb_ph, LIBPYTHON, "interp_head",
+            (psaddr_t *)&addr);
+        if (rc != PS_OK) {
+            return (NULL);
+        }
+    }
+
+    if (ps_pread(py->pdb_ph, (uintptr_t)addr, &i_addr, sizeof (uintptr_t))
+        != PS_OK) {
+        return (NULL);
+    }
+
+    itr = malloc(sizeof (pydb_iter_t));
+    if (itr == NULL) {
+        return (NULL);
+    }
+
+    itr->pdi_ph = py->pdb_ph;
+    itr->pdi_current = i_addr;
+    itr->pdi_nextf = pydb_interp_iter_next;
+
+    return (itr);
+}
+
+static uintptr_t
+pydb_interp_iter_next(pydb_iter_t *iter)
+{
+    PyInterpreterState st;
+    uintptr_t cur;
+
+    cur = iter->pdi_current;
+
+    if (cur == 0) {
+        return (cur);
+    }
+
+    if (ps_pread(iter->pdi_ph, cur, &st, sizeof (PyInterpreterState))
+        != PS_OK) {
+        iter->pdi_current = 0;
+        return (0);
+    }
+
+    iter->pdi_current = (uintptr_t)st.next;
+
+    return (cur);
+}
+
+/*
+ * Walk a list of Python PyFrameObjects.  The addr argument must be
+ * the address of a valid PyThreadState object.
+ */
+pydb_iter_t *
+pydb_frame_iter_init(pydb_agent_t *py, uintptr_t addr)
+{
+    pydb_iter_t *itr;
+    PyThreadState ts;
+
+    if (ps_pread(py->pdb_ph, (uintptr_t)addr, &ts, sizeof (PyThreadState))
+        != PS_OK) {
+        return (NULL);
+    }
+
+    itr = malloc(sizeof (pydb_iter_t));
+    if (itr == NULL) {
+        return (NULL);
+    }
+
+    itr->pdi_ph = py->pdb_ph;
+    itr->pdi_current = (uintptr_t)ts.frame;
+    itr->pdi_nextf = pydb_frame_iter_next;
+
+    return (itr);
+}
+
+static uintptr_t
+pydb_frame_iter_next(pydb_iter_t *iter)
+{
+    PyFrameObject fo;
+    uintptr_t cur;
+
+    cur = iter->pdi_current;
+
+    if (cur == 0) {
+        return (cur);
+    }
+
+    if (ps_pread(iter->pdi_ph, cur, &fo, sizeof (PyFrameObject))
+        != PS_OK) {
+        iter->pdi_current = 0;
+        return (0);
+    }
+
+    iter->pdi_current = (uintptr_t)fo.f_back;
+
+    return (cur);
+}
+
+/*
+ * Walk a list of Python PyThreadState objects.  The addr argument must be
+ * the address of a valid PyInterpreterState object.
+ */
+pydb_iter_t *
+pydb_thread_iter_init(pydb_agent_t *py, uintptr_t addr)
+{
+    pydb_iter_t *itr;
+    PyInterpreterState is;
+
+    if (ps_pread(py->pdb_ph, (uintptr_t)addr, &is,
+        sizeof (PyInterpreterState)) != PS_OK) {
+        return (NULL);
+    }
+
+    itr = malloc(sizeof (pydb_iter_t));
+    if (itr == NULL) {
+        return (NULL);
+    }
+
+    itr->pdi_ph = py->pdb_ph;
+    itr->pdi_current = (uintptr_t)is.tstate_head;
+    itr->pdi_nextf = pydb_thread_iter_next;
+
+    return (itr);
+}
+
+static uintptr_t
+pydb_thread_iter_next(pydb_iter_t *iter)
+{
+    PyThreadState ts;
+    uintptr_t cur;
+
+    cur = iter->pdi_current;
+
+    if (cur == 0) {
+        return (cur);
+    }
+
+    if (ps_pread(iter->pdi_ph, cur, &ts, sizeof (PyThreadState)) != PS_OK) {
+        iter->pdi_current = 0;
+        return (0);
+    }
+
+    iter->pdi_current = (uintptr_t)ts.next;
+
+    return (cur);
+}
+
+
+uintptr_t
+pydb_iter_next(pydb_iter_t *iter)
+{
+    return (iter->pdi_nextf(iter));
+}
+
+void
+pydb_iter_fini(pydb_iter_t *iter)
+{
+    if (iter == NULL) {
+        return;
+    }
+
+    free(iter);
+}
--- /dev/null
+++ Python-3.4.0/py_db/libpython34_db.h
@@ -0,0 +1,73 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ */
+
+#ifndef    _LIBPYTHON34_DB_H
+#define    _LIBPYTHON34_DB_H
+
+#include <proc_service.h>
+
+#ifdef    __cplusplus
+extern "C" {
+#endif
+
+/* Agent is opaque to library's consumers.  */
+typedef struct pydb_agent pydb_agent_t;
+
+/*
+ * Library's debug version is 1.  Changes to interface should increase this
+ * number.
+ */
+#define    PYDB_VERSION    1
+
+/* Agent creation/destruction routines */
+extern    pydb_agent_t    *pydb_agent_create(struct ps_prochandle *P, int vers);
+extern    void        pydb_agent_destroy(pydb_agent_t *py);
+
+/* Used by callers that know they are looking at a PyFrameObject */
+extern    int    pydb_get_frameinfo(pydb_agent_t *py, uintptr_t frame_addr,
+    char *fbuf, size_t bufsz, int verbose);
+
+/*
+ * Used by callers that don't know if they're looking at PyFrameObject.
+ * Checks PC for traceable functions.
+ */
+extern    int    pydb_pc_frameinfo(pydb_agent_t *py, uintptr_t pc,
+    uintptr_t frame_addr, char *fbuf, size_t bufsz);
+
+/* Iterator functions */
+typedef struct pydb_iter pydb_iter_t;
+
+extern    pydb_iter_t    *pydb_frame_iter_init(pydb_agent_t *py, uintptr_t addr);
+extern    pydb_iter_t    *pydb_interp_iter_init(pydb_agent_t *py,
+    uintptr_t addr);
+extern    pydb_iter_t    *pydb_thread_iter_init(pydb_agent_t *py,
+    uintptr_t addr);
+extern    void        pydb_iter_fini(pydb_iter_t *iter);
+extern    uintptr_t    pydb_iter_next(pydb_iter_t *iter);
+
+#ifdef    __cplusplus
+}
+#endif
+
+#endif    /* _LIBPYTHON34_DB_H */
--- /dev/null
+++ Python-3.4.0/py_db/libpython34_db_32.h
@@ -0,0 +1,121 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
+ */
+
+#ifndef    _LIBPYTHON34_DB_32_H
+#define    _LIBPYTHON34_DB_32_H
+
+#ifdef    __cplusplus
+extern "C" {
+#endif
+
+#include <sys/types.h>
+
+/*
+ * Define 32-bit Python data structures for use by the 64-bit debugger.  This
+ * is so that a 64-bit debugger may properly examine a 32-bit process.
+ *
+ * In many cases, the debug library is only concerned with a few fields in the
+ * Python structure.  In that case, the other ancillary fields are elided.
+ */
+
+typedef uint32_t uintptr32_t;
+typedef int32_t Py_ssize32_t;
+
+typedef struct _is32 {
+    uintptr32_t    next;
+    uintptr32_t    tstate_head;
+} PyInterpreterState32;
+
+typedef struct _ts32 {
+    uintptr32_t    next;
+    uintptr32_t    interp;
+    uintptr32_t    frame;
+} PyThreadState32;
+
+#define    PyObject_HEAD32            \
+    Py_ssize32_t    ob_refcnt;    \
+    uintptr32_t    ob_type;
+
+#define    PyObject_VAR_HEAD32        \
+    PyObject_HEAD32            \
+    Py_ssize32_t    ob_size;
+
+typedef struct {
+    PyObject_HEAD32
+} PyObject32;
+
+typedef struct {
+    PyObject_VAR_HEAD32
+} PyVarObject32;
+
+typedef struct {
+    PyObject_VAR_HEAD32
+    int32_t        ob_shash;
+    int        ob_sstate;
+    char        ob_sval[1];
+} PyBytesObject32;
+
+#define    Py_SIZE32(ob)            (((PyVarObject32*)(ob))->ob_size)
+#define    PyString_GET_SIZE32(op)        Py_SIZE32(op)
+#define    PyString_AS_STRING32(op)    (((PyBytesObject32 *)(op))->ob_sval)
+
+typedef struct {
+    PyObject_VAR_HEAD32
+    uintptr32_t    f_back;
+    uintptr32_t    f_code;
+    uintptr32_t    f_builtins;
+    uintptr32_t    f_globals;
+    uintptr32_t    f_locals;
+    uintptr32_t    f_valuestack;
+    uintptr32_t    f_stacktop;
+    uintptr32_t    f_trace;
+    uintptr32_t    f_exc_typpe, f_exc_value, f_exc_traceback;
+    uintptr32_t    f_tstate;
+    int        f_lasti;
+    int        f_lineno;
+} PyFrameObject32;
+
+typedef struct {
+    PyObject_HEAD32
+    int        co_argcount;
+    int        co_nlocals;
+    int        co_stacksize;
+    int        co_flags;
+    uintptr32_t    co_code;
+    uintptr32_t    co_consts;
+    uintptr32_t    co_names;
+    uintptr32_t    co_varnames;
+    uintptr32_t    co_freevars;
+    uintptr32_t    co_cellvars;
+    uintptr32_t    co_filename;
+    uintptr32_t    co_name;
+    int        co_firstlineno;
+    uintptr32_t    co_lnotab;
+} PyCodeObject32;
+
+#ifdef    __cplusplus
+}
+#endif
+
+#endif    /* _LIBPYTHON34_DB_32_H */
--- /dev/null
+++ Python-3.4.0/py_db/mapfile-vers
@@ -0,0 +1,39 @@
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License (the "License").
+# You may not use this file except in compliance with the License.
+#
+# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+# or http://www.opensolaris.org/os/licensing.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+# If applicable, add the following below this CDDL HEADER, with the
+# fields enclosed by brackets "[]" replaced with your own identifying
+# information: Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+
+#
+# Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+#
+
+SUNWprivate_1.1 {
+    global:
+    pydb_agent_create;
+    pydb_agent_destroy;
+    pydb_frame_iter_init;
+    pydb_get_frameinfo;
+    pydb_pc_frameinfo;
+    pydb_interp_iter_init;
+    pydb_thread_iter_init;
+    pydb_iter_fini;
+    pydb_iter_next;
+    local:
+    *;
+};